@@ -13,12 +13,12 @@ class SwipeHelper {
1313 private let swipeViewSize : CGSize
1414 private let swipeViewCenter : CGPoint
1515 open var options = SwipeOptions ( )
16-
16+
1717 init ( with frame: CGRect ) {
1818 swipeViewSize = frame. size
1919 swipeViewCenter = CGPoint ( x: frame. width / 2 , y: frame. height / 2 )
2020 }
21-
21+
2222 /// Move and animate a view to a desired position
2323 ///
2424 /// - Parameters:
@@ -28,7 +28,7 @@ class SwipeHelper {
2828 func move( _ card: UIView , duration: Double = 0.25 , toPoint: CGPoint ) {
2929 move ( card, duration: duration, toPoint: toPoint, completion: { } )
3030 }
31-
31+
3232 /// Move and animate a view to a desired position
3333 ///
3434 /// - Parameters:
@@ -44,20 +44,21 @@ class SwipeHelper {
4444 completion ( )
4545 } )
4646 }
47+
4748 /// Move and animate a view to a desired position,
4849 /// transforming the view according to the current SwipeOptions
4950 /// Uses dismissAnimationDuration set in SwipeOptions
5051 /// - Parameters:
5152 /// - card: The view to be move
5253 /// - toPoint: Destination of the move action
5354 /// - completion: Callback fireing when the animation is done and the view is moved.
54-
55+
5556 func moveFastAndTransform( _ card: SwipableView , toPoint: CGPoint , completion: @escaping ( ) -> Void ) {
5657 addBorderToCard ( card)
57-
58- let rotation = self . calculateRotationAnimation ( cardCenter: toPoint)
59- let scale = self . calculateScaleAnimation ( cardCenter: toPoint)
60-
58+
59+ let rotation = calculateRotationAnimation ( cardCenter: toPoint)
60+ let scale = calculateScaleAnimation ( cardCenter: toPoint)
61+
6162 card. respondToSwipe ( like: toPoint. x > 0 , opacity: toPoint. equalTo ( swipeViewCenter) ? 0.0 : 1.0 )
6263 UIView . animate ( withDuration: options. dismissAnimationDuration, delay: 0 , options: UIViewAnimationOptions . curveLinear, animations: {
6364 card. center = toPoint
@@ -67,43 +68,43 @@ class SwipeHelper {
6768 completion ( )
6869 } )
6970 }
70-
71+
7172 /// Calculate the magnitude if a throw based on the velocity vector
7273 /// Used when determining if a card has been thrown 'hard enough' to be dismissed.
7374 /// - Parameter velocity: velocity vector of the gesture
7475 /// - Returns: magnitude of the throw
75- func calculateThrowMagnitude( for velocity: CGPoint ) -> Float {
76+ func calculateThrowMagnitude( for velocity: CGPoint ) -> Float {
7677 let velXSq = Float ( velocity. x) * Float( velocity. x)
7778 let velYSq = Float ( velocity. y) * Float( velocity. y)
7879 return sqrtf ( velXSq + velYSq)
7980 }
80-
81+
8182 /// Returns a view to its original visual state in regards to border, rotation and scale.
8283 /// Animates the reset of the view
8384 /// - Parameter card: View to reset
8485 func resetCard( _ card: UIView ) {
8586 let rotation = CATransform3DMakeRotation ( 0 , 0 , 0 , 1 )
8687 let scale = CATransform3DMakeScale ( 1.0 , 1.0 , 1.0 )
87-
88+
8889 let borderAnim = CABasicAnimation ( keyPath: " borderWidth " )
8990 borderAnim. fromValue = card. layer. borderWidth
9091 borderAnim. toValue = 0
9192 borderAnim. duration = 0.1
9293 card. layer. borderWidth = 0
93-
94+
9495 let cornerAnim = CABasicAnimation ( keyPath: " cornerRadius " )
9596 cornerAnim. fromValue = card. layer. cornerRadius
9697 cornerAnim. toValue = 0
9798 cornerAnim. duration = 0.1
9899 card. layer. cornerRadius = 0
99-
100+
100101 let both = CAAnimationGroup ( )
101102 both. duration = 0.1
102103 both. animations = [ cornerAnim, borderAnim]
103104 both. timingFunction = CAMediaTimingFunction ( name: kCAMediaTimingFunctionLinear)
104-
105+
105106 card. layer. add ( both, forKey: " both " )
106-
107+
107108 UIView . animate ( withDuration: 0.1 ) {
108109 card. layer. transform = CATransform3DConcat ( rotation, scale)
109110 }
@@ -118,18 +119,18 @@ class SwipeHelper {
118119 card. layer. transform = CATransform3DConcat ( rotation, scale)
119120 addBorderToCard ( card)
120121 }
121-
122+
122123 func addBorderToCard( _ card: UIView ) {
123124 card. layer. borderWidth = 10
124125 card. layer. borderColor = UIColor . white. cgColor
125126 card. layer. cornerRadius = 10
126127 card. layer. masksToBounds = true
127128 }
128-
129+
129130 private func calculateScaleAnimation( cardCenter: CGPoint ) -> CATransform3D {
130131 let horizontalDistance = calculateHorizontalDistanceFromCenter ( cardCenter)
131132 let verticalDistance = calculateVerticalDistanceFromCenter ( cardCenter)
132-
133+
133134 var scaleFactor = CGFloat ( 0 )
134135 if horizontalDistance >= verticalDistance {
135136 scaleFactor = CGFloat ( 1 - horizontalDistance / 800.0 )
@@ -138,45 +139,45 @@ class SwipeHelper {
138139 }
139140 return CATransform3DMakeScale ( scaleFactor, scaleFactor, 1.0 )
140141 }
141-
142+
142143 private func calculateRotationAnimation( cardCenter: CGPoint ) -> CATransform3D {
143144 let xFromCenter = Double ( cardCenter. x - swipeViewSize. width / 2 )
144- var rads = CGFloat ( xFromCenter/ 10.0 * . pi / 180.0 )
145+ var rads = CGFloat ( xFromCenter / 10.0 * . pi / 180.0 )
145146 if abs ( rads) > 1.4 {
146147 rads = - rads
147148 }
148149 return CATransform3DMakeRotation ( rads, 0 , 0 , 1 )
149150 }
150-
151+
151152 /// Calculates the distance in the horizontal plane from the position of a view to the center of the screen
152153 ///
153154 /// - Parameter cardCenter: A positonal coordinate, preferably the center of a view.
154155 /// - Returns: The horizontal distance from the center of the screen
155156 private func calculateHorizontalDistanceFromCenter( _ cardCenter: CGPoint ) -> Double {
156157 return Double ( abs ( cardCenter. x - swipeViewSize. width / 2 ) )
157158 }
158-
159+
159160 /// Calculates the distance in the vertical plane from the position of a view to the center of the screen
160161 ///
161162 /// - Parameter cardCenter: A positonal coordinate, preferably the center of a view.
162163 /// - Returns: The vertical distance from the center of the screen
163164 private func calculateVerticalDistanceFromCenter( _ cardCenter: CGPoint ) -> Double {
164165 return Double ( abs ( cardCenter. y - swipeViewSize. height / 2 ) )
165166 }
166-
167+
167168 /// Calculate a proper destination for a dismissal of a view based on its position
168169 /// Places the view far to the left if the view is to the left the the center of the screen and vice versa.
169170 /// - Parameter card: View which endpoint to calculate
170171 /// - Returns: Proper destination for the view
171172 func calculateEndpoint( _ card: UIView ) -> CGPoint {
172173 let deltaX = card. center. x - swipeViewSize. width / 2
173174 let deltaY = card. center. y - swipeViewSize. height / 2
174-
175+
175176 let k = deltaY / deltaX
176177 let toX = deltaX < 0 ? - swipeViewSize . height / 2 : swipeViewSize. width + swipeViewSize. height / 2
177178 return CGPoint ( x: toX, y: toX * k)
178179 }
179-
180+
180181 /// Calculate a proper destination for a dismissal of a view based on current velocity
181182 /// Places the view far to the left if the view is currently moving to the left and vice versa.
182183 /// The angle from the center to the proposed destination of the view is based on the angle of the velocity vector
@@ -187,7 +188,7 @@ class SwipeHelper {
187188 let toX = velocity. x < 0 ? - swipeViewSize . height / 2 : swipeViewSize. width + swipeViewSize. height / 2
188189 return CGPoint ( x: toX, y: toX * k)
189190 }
190-
191+
191192 /// Converts a position with coordinates with the origin of the screen as origo to one using the center of the screen as origo.
192193 /// Can be used to convert a origin value to a center value refering to the same positioning of a full screen view.
193194 /// - Parameter center: Position using origin as origo
0 commit comments