Commit f98360e5 by Daniel Dahan

adding MaterialLayer basics

parent e59a6f3e
...@@ -21,6 +21,20 @@ import UIKit ...@@ -21,6 +21,20 @@ import UIKit
@objc(MaterialButton) @objc(MaterialButton)
public class MaterialButton : UIButton { public class MaterialButton : UIButton {
/** /**
:name: layerClass
*/
public override class func layerClass() -> AnyClass {
return MaterialLayer.self
}
/**
:name: materialLayer
*/
public var materialLayer: MaterialLayer {
return layer as! MaterialLayer
}
/**
:name: visualLayer :name: visualLayer
*/ */
public private(set) lazy var visualLayer: CAShapeLayer = CAShapeLayer() public private(set) lazy var visualLayer: CAShapeLayer = CAShapeLayer()
...@@ -33,7 +47,14 @@ public class MaterialButton : UIButton { ...@@ -33,7 +47,14 @@ public class MaterialButton : UIButton {
/** /**
:name: delegate :name: delegate
*/ */
public weak var delegate: MaterialAnimationDelegate? public weak var delegate: MaterialAnimationDelegate? {
get {
return materialLayer.animationDelegate
}
set(value) {
materialLayer.animationDelegate = delegate
}
}
/** /**
:name: pulseScale :name: pulseScale
...@@ -338,43 +359,7 @@ public class MaterialButton : UIButton { ...@@ -338,43 +359,7 @@ public class MaterialButton : UIButton {
:name: animation :name: animation
*/ */
public func animation(animation: CAAnimation) { public func animation(animation: CAAnimation) {
animation.delegate = self materialLayer.animation(animation)
if let a: CABasicAnimation = animation as? CABasicAnimation {
a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
}
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
layer.addAnimation(a, forKey: a.keyPath!)
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil)
} else if let a: CATransition = animation as? CATransition {
layer.addAnimation(a, forKey: kCATransition)
}
}
/**
:name: animationDidStart
*/
public override func animationDidStart(anim: CAAnimation) {
delegate?.materialAnimationDidStart?(anim)
}
/**
:name: animationDidStop
*/
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a: CAPropertyAnimation = anim as? CAPropertyAnimation {
if let b: CABasicAnimation = a as? CABasicAnimation {
MaterialAnimation.animationDisabled({
self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
})
}
delegate?.materialAnimationDidStop?(anim, finished: flag)
layer.removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
}
} }
/** /**
......
...@@ -18,8 +18,14 @@ ...@@ -18,8 +18,14 @@
import UIKit import UIKit
@objc(MaterialLayer)
public class MaterialLayer : CAShapeLayer { public class MaterialLayer : CAShapeLayer {
/** /**
:name: animationDelegate
*/
public var animationDelegate: MaterialAnimationDelegate?
/**
:name: init :name: init
*/ */
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
...@@ -39,5 +45,48 @@ public class MaterialLayer : CAShapeLayer { ...@@ -39,5 +45,48 @@ public class MaterialLayer : CAShapeLayer {
public override init() { public override init() {
super.init() super.init()
} }
/**
:name: animation
*/
public func animation(animation: CAAnimation) {
animation.delegate = self
if let a: CABasicAnimation = animation as? CABasicAnimation {
a.fromValue = (nil == presentationLayer() ? self : presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
}
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
addAnimation(a, forKey: a.keyPath!)
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
addAnimation(a, forKey: nil)
} else if let a: CATransition = animation as? CATransition {
addAnimation(a, forKey: kCATransition)
}
}
/**
:name: animationDidStart
*/
public override func animationDidStart(anim: CAAnimation) {
animationDelegate?.materialAnimationDidStart?(anim)
}
/**
:name: animationDidStop
*/
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a: CAPropertyAnimation = anim as? CAPropertyAnimation {
if let b: CABasicAnimation = a as? CABasicAnimation {
MaterialAnimation.animationDisabled({
self.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
})
}
animationDelegate?.materialAnimationDidStop?(anim, finished: flag)
removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
}
}
} }
...@@ -20,15 +20,36 @@ import UIKit ...@@ -20,15 +20,36 @@ import UIKit
@objc(MaterialView) @objc(MaterialView)
public class MaterialView : UIView { public class MaterialView : UIView {
// /**
// :name: visualLayer :name: layerClass
// */
public private(set) lazy var visualLayer: CAShapeLayer = CAShapeLayer() public override class func layerClass() -> AnyClass {
return MaterialLayer.self
}
/**
:name: materialLayer
*/
public var materialLayer: MaterialLayer {
return layer as! MaterialLayer
}
/** /**
:name: visualLayer :name: visualLayer
*/ */
public weak var delegate: MaterialAnimationDelegate? public private(set) lazy var visualLayer: CAShapeLayer = CAShapeLayer()
/**
:name: delegate
*/
public weak var delegate: MaterialAnimationDelegate? {
get {
return materialLayer.animationDelegate
}
set(value) {
materialLayer.animationDelegate = delegate
}
}
/** /**
:name: image :name: image
...@@ -330,43 +351,7 @@ public class MaterialView : UIView { ...@@ -330,43 +351,7 @@ public class MaterialView : UIView {
:name: animation :name: animation
*/ */
public func animation(animation: CAAnimation) { public func animation(animation: CAAnimation) {
animation.delegate = self materialLayer.animation(animation)
if let a: CABasicAnimation = animation as? CABasicAnimation {
a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
}
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
layer.addAnimation(a, forKey: a.keyPath!)
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil)
} else if let a: CATransition = animation as? CATransition {
layer.addAnimation(a, forKey: kCATransition)
}
}
/**
:name: animationDidStart
*/
public override func animationDidStart(anim: CAAnimation) {
delegate?.materialAnimationDidStart?(anim)
}
/**
:name: animationDidStop
*/
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a: CAPropertyAnimation = anim as? CAPropertyAnimation {
if let b: CABasicAnimation = a as? CABasicAnimation {
MaterialAnimation.animationDisabled({
self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
})
}
delegate?.materialAnimationDidStop?(anim, finished: flag)
layer.removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
}
} }
// //
......
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