Commit 24b2bdfb by Daniel Dahan

updated MaterialAnimation internals and added delegation handling

parent ac8fb0e1
...@@ -18,6 +18,19 @@ ...@@ -18,6 +18,19 @@
import UIKit import UIKit
@objc(MaterialAnimationDelegate)
public protocol MaterialAnimationDelegate {
/**
:name: materialAnimationDidStart
*/
optional func materialAnimationDidStart(animation: CAAnimation)
/**
:name: materialAnimationDidStop
*/
optional func materialAnimationDidStop(animation: CAAnimation, finished flag: Bool)
}
public typealias MaterialAnimationFillModeType = String public typealias MaterialAnimationFillModeType = String
public enum MaterialAnimationFillMode { public enum MaterialAnimationFillMode {
......
...@@ -18,19 +18,6 @@ ...@@ -18,19 +18,6 @@
import UIKit import UIKit
@objc(MaterialButtonDelegate)
public protocol MaterialButtonDelegate {
/**
:name: animationDidStart
*/
optional func animationDidStart(materialButton: MaterialButton, animation: CAAnimation)
/**
:name: animationDidStop
*/
optional func animationDidStop(materialButton: MaterialButton, animation: CAAnimation, finished flag: Bool)
}
@objc(MaterialButton) @objc(MaterialButton)
public class MaterialButton : UIButton { public class MaterialButton : UIButton {
/** /**
...@@ -46,7 +33,7 @@ public class MaterialButton : UIButton { ...@@ -46,7 +33,7 @@ public class MaterialButton : UIButton {
/** /**
:name: delegate :name: delegate
*/ */
public weak var delegate: MaterialButtonDelegate? public weak var delegate: MaterialAnimationDelegate?
/** /**
:name: pulseScale :name: pulseScale
...@@ -343,6 +330,7 @@ public class MaterialButton : UIButton { ...@@ -343,6 +330,7 @@ public class MaterialButton : UIButton {
prepareShape() prepareShape()
visualLayer.frame = bounds visualLayer.frame = bounds
visualLayer.position = CGPointMake(width / 2, height / 2)
visualLayer.cornerRadius = layer.cornerRadius visualLayer.cornerRadius = layer.cornerRadius
} }
...@@ -356,13 +344,10 @@ public class MaterialButton : UIButton { ...@@ -356,13 +344,10 @@ public class MaterialButton : UIButton {
} }
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation { if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
layer.addAnimation(a, forKey: a.keyPath!) layer.addAnimation(a, forKey: a.keyPath!)
if true == filterAnimations(a) {
visualLayer.addAnimation(a, forKey: a.keyPath!)
}
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup { } else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil) layer.addAnimation(a, forKey: nil)
filterAnimations(a) } else if let a: CATransition = animation as? CATransition {
visualLayer.addAnimation(a, forKey: nil) layer.addAnimation(a, forKey: kCATransition)
} }
} }
...@@ -370,7 +355,7 @@ public class MaterialButton : UIButton { ...@@ -370,7 +355,7 @@ public class MaterialButton : UIButton {
:name: animationDidStart :name: animationDidStart
*/ */
public override func animationDidStart(anim: CAAnimation) { public override func animationDidStart(anim: CAAnimation) {
delegate?.animationDidStart?(self, animation: anim) delegate?.materialAnimationDidStart?(anim)
} }
/** /**
...@@ -382,16 +367,14 @@ public class MaterialButton : UIButton { ...@@ -382,16 +367,14 @@ public class MaterialButton : UIButton {
MaterialAnimation.animationDisabled({ MaterialAnimation.animationDisabled({
self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!) self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
}) })
delegate?.materialAnimationDidStop?(anim, finished: flag)
} }
layer.removeAnimationForKey(a.keyPath!) layer.removeAnimationForKey(a.keyPath!)
visualLayer.removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup { } else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! { for x in a.animations! {
animationDidStop(x, finished: true) animationDidStop(x, finished: true)
} }
} }
delegate?.animationDidStop?(self, animation: anim, finished: flag)
} }
// //
......
...@@ -18,19 +18,6 @@ ...@@ -18,19 +18,6 @@
import UIKit import UIKit
@objc(MaterialViewDelegate)
public protocol MaterialViewDelegate {
/**
:name: animationDidStart
*/
optional func animationDidStart(materialView: MaterialView, animation: CAAnimation)
/**
:name: animationDidStop
*/
optional func animationDidStop(materialView: MaterialView, animation: CAAnimation, finished flag: Bool)
}
@objc(MaterialView) @objc(MaterialView)
public class MaterialView : UIView { public class MaterialView : UIView {
// //
...@@ -41,7 +28,7 @@ public class MaterialView : UIView { ...@@ -41,7 +28,7 @@ public class MaterialView : UIView {
/** /**
:name: visualLayer :name: visualLayer
*/ */
public weak var delegate: MaterialViewDelegate? public weak var delegate: MaterialAnimationDelegate?
/** /**
:name: image :name: image
...@@ -335,6 +322,7 @@ public class MaterialView : UIView { ...@@ -335,6 +322,7 @@ public class MaterialView : UIView {
prepareShape() prepareShape()
visualLayer.frame = bounds visualLayer.frame = bounds
visualLayer.position = CGPointMake(width / 2, height / 2)
visualLayer.cornerRadius = layer.cornerRadius visualLayer.cornerRadius = layer.cornerRadius
} }
...@@ -348,13 +336,8 @@ public class MaterialView : UIView { ...@@ -348,13 +336,8 @@ public class MaterialView : UIView {
} }
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation { if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
layer.addAnimation(a, forKey: a.keyPath!) layer.addAnimation(a, forKey: a.keyPath!)
if true == filterAnimations(a) {
visualLayer.addAnimation(a, forKey: a.keyPath!)
}
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup { } else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil) layer.addAnimation(a, forKey: nil)
filterAnimations(a)
visualLayer.addAnimation(a, forKey: nil)
} else if let a: CATransition = animation as? CATransition { } else if let a: CATransition = animation as? CATransition {
layer.addAnimation(a, forKey: kCATransition) layer.addAnimation(a, forKey: kCATransition)
} }
...@@ -364,7 +347,7 @@ public class MaterialView : UIView { ...@@ -364,7 +347,7 @@ public class MaterialView : UIView {
:name: animationDidStart :name: animationDidStart
*/ */
public override func animationDidStart(anim: CAAnimation) { public override func animationDidStart(anim: CAAnimation) {
delegate?.animationDidStart?(self, animation: anim) delegate?.materialAnimationDidStart?(anim)
} }
/** /**
...@@ -376,16 +359,14 @@ public class MaterialView : UIView { ...@@ -376,16 +359,14 @@ public class MaterialView : UIView {
MaterialAnimation.animationDisabled({ MaterialAnimation.animationDisabled({
self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!) self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
}) })
delegate?.materialAnimationDidStop?(anim, finished: flag)
} }
layer.removeAnimationForKey(a.keyPath!) layer.removeAnimationForKey(a.keyPath!)
visualLayer.removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup { } else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! { for x in a.animations! {
animationDidStop(x, finished: true) animationDidStop(x, finished: true)
} }
} }
delegate?.animationDidStop?(self, animation: anim, finished: flag)
} }
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment