-
Notifications
You must be signed in to change notification settings - Fork 2
/
UIView+InterpolatingMotion.swift
157 lines (135 loc) · 4.83 KB
/
UIView+InterpolatingMotion.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// UIView+InterpolatingMotion.swift
// Motion Shadows Demo
//
// Created by Michael Nachbaur on 2019-04-22.
// Copyright © 2019 Michael Nachbaur. All rights reserved.
//
import UIKit
import ObjectiveC
@IBDesignable public extension UIView {
private struct AssociatedKeys {
static var shadowMotionOffset: UInt8 = 0
static var motionOffset: UInt8 = 0
}
@objc @IBInspectable var cornerRadius : CGFloat {
get {
return layer.cornerRadius
}
set(radius) {
layer.cornerRadius = radius
}
}
@objc @IBInspectable var borderWidth : CGFloat {
get {
return layer.borderWidth
}
set(width) {
layer.borderWidth = width
}
}
@objc @IBInspectable var borderColor : UIColor? {
get {
return layer.borderColor as? UIColor? ?? nil
}
set(color) {
layer.borderColor = color?.cgColor
}
}
@objc @IBInspectable var shadowColor : UIColor? {
get {
return layer.shadowColor as? UIColor? ?? nil
}
set(color) {
layer.shadowColor = color?.cgColor
}
}
@objc @IBInspectable var shadowRadius : CGFloat {
get {
return layer.shadowRadius
}
set(radius) {
layer.shadowRadius = radius
}
}
@objc @IBInspectable var shadowOpacity : Float {
get {
return layer.shadowOpacity
}
set(opacity) {
layer.shadowOpacity = opacity
}
}
@objc @IBInspectable var shadowOffset : CGSize {
get {
return layer.shadowOffset
}
set(offset) {
layer.shadowOffset = offset
}
}
@objc @IBInspectable var shadowMotionOffset : CGSize {
get {
guard let value = objc_getAssociatedObject(self, &AssociatedKeys.shadowMotionOffset) as? NSValue else {
return .zero
}
return value.cgSizeValue
}
set(newSize) {
var currentSize = CGSize.zero
let currentValue = objc_getAssociatedObject(self, &AssociatedKeys.shadowMotionOffset) as? NSValue
if currentValue != nil {
currentSize = currentValue!.cgSizeValue
}
if currentSize.equalTo(newSize) {
return
}
objc_setAssociatedObject(self, &AssociatedKeys.shadowMotionOffset, NSValue(cgSize: newSize), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
addInterpolatingMotionEffect(keyPath: "layer.shadowOffset.width", effectType: .tiltAlongHorizontalAxis, range: newSize.width)
addInterpolatingMotionEffect(keyPath: "layer.shadowOffset.height", effectType: .tiltAlongVerticalAxis, range: newSize.height)
}
}
@objc @IBInspectable var motionOffset : CGSize {
get {
guard let value = objc_getAssociatedObject(self, &AssociatedKeys.motionOffset) as? NSValue else {
return .zero
}
return value.cgSizeValue
}
set(newSize) {
var currentSize = CGSize.zero
let currentValue = objc_getAssociatedObject(self, &AssociatedKeys.motionOffset) as? NSValue
if currentValue != nil {
currentSize = currentValue!.cgSizeValue
}
if currentSize.equalTo(newSize) {
return
}
objc_setAssociatedObject(self, &AssociatedKeys.motionOffset, NSValue(cgSize: newSize), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
addInterpolatingMotionEffect(keyPath: "center.x", effectType: .tiltAlongHorizontalAxis, range: newSize.width)
addInterpolatingMotionEffect(keyPath: "center.y", effectType: .tiltAlongVerticalAxis, range: newSize.height)
}
}
private func addInterpolatingMotionEffect(keyPath : String, effectType : UIInterpolatingMotionEffect.EffectType, range value : CGFloat) {
for effect in motionEffects {
if let interpolatingEffect = effect as? UIInterpolatingMotionEffect {
if interpolatingEffect.keyPath == keyPath {
removeMotionEffect(interpolatingEffect)
break
}
}
}
if value == 0 {
return
}
let effect = UIInterpolatingMotionEffect(keyPath: keyPath, type: effectType)
if value < 0 {
effect.minimumRelativeValue = -(abs(value));
effect.maximumRelativeValue = abs(value);
} else {
effect.minimumRelativeValue = abs(value);
effect.maximumRelativeValue = -(abs(value));
}
addMotionEffect(effect)
}
}