Commit 6a1caacb by Daniel Dahan

updated MaterialLayer and Material*Views internal animation logic

parent e59a6f3e
...@@ -439,13 +439,6 @@ public class MaterialButton : UIButton { ...@@ -439,13 +439,6 @@ public class MaterialButton : UIButton {
} }
/** /**
:name: actionForLayer
*/
public override func actionForLayer(layer: CALayer, forKey event: String) -> CAAction? {
return nil // returning nil enables the animations for the layer property that are normally disabled.
}
/**
:name: prepareView :name: prepareView
*/ */
public func prepareView() { public func prepareView() {
......
...@@ -18,26 +18,285 @@ ...@@ -18,26 +18,285 @@
import UIKit import UIKit
@objc(MaterialLayer)
public class MaterialLayer : CAShapeLayer { public class MaterialLayer : CAShapeLayer {
/** /**
:name: visualLayer
*/
public private(set) lazy var visualLayer: CAShapeLayer = CAShapeLayer()
/**
:name: x
*/
public var x: CGFloat {
get {
return frame.origin.x
}
set(value) {
frame.origin.x = value
}
}
/**
:name: y
*/
public var y: CGFloat {
get {
return frame.origin.y
}
set(value) {
frame.origin.y = value
}
}
/**
:name: width
*/
public var width: CGFloat {
get {
return frame.size.width
}
set(value) {
frame.size.width = value
if .None != shape {
frame.size.height = value
}
}
}
/**
:name: height
*/
public var height: CGFloat {
get {
return frame.size.height
}
set(value) {
frame.size.height = value
if .None != shape {
frame.size.width = value
}
}
}
/**
:name: image
*/
public var image: UIImage? {
get {
return nil == visualLayer.contents ? nil : UIImage(CGImage: visualLayer.contents as! CGImageRef)
}
set(value) {
visualLayer.contents = value?.CGImage
}
}
/**
:name: contentsRect
*/
public override var contentsRect: CGRect {
get {
return visualLayer.contentsRect
}
set(value) {
visualLayer.contentsRect = contentsRect
}
}
/**
:name: contentsCenter
*/
public override var contentsCenter: CGRect {
get {
return visualLayer.contentsCenter
}
set(value) {
visualLayer.contentsCenter = contentsCenter
}
}
/**
:name: contentsScale
*/
public override var contentsScale: CGFloat {
get {
return visualLayer.contentsScale
}
set(value) {
visualLayer.contentsScale = contentsScale
}
}
/**
:name: contentsGravity
*/
public override var contentsGravity: String {
get {
return visualLayer.contentsGravity
}
set(value) {
visualLayer.contentsGravity = value
}
}
/**
:name: masksToBounds
*/
public override var masksToBounds: Bool {
get {
return visualLayer.masksToBounds
}
set(value) {
visualLayer.masksToBounds = value
}
}
/**
:name: cornerRadius
*/
public override var cornerRadius: CGFloat {
get {
return visualLayer.cornerRadius
}
set(value) {
visualLayer.cornerRadius = value
if .Circle == shape {
shape = .None
}
}
}
/**
:name: shape
*/
public var shape: MaterialShape {
didSet {
if .None != shape {
if width < height {
width = height
} else {
height = width
}
}
}
}
/**
:name: shadowDepth
*/
public var shadowDepth: MaterialDepth {
didSet {
let value: MaterialDepthType = MaterialDepthToValue(shadowDepth)
shadowOffset = value.offset
shadowOpacity = value.opacity
shadowRadius = value.radius
}
}
/**
:name: init :name: init
*/ */
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
shape = .None
shadowDepth = .None
super.init(coder: aDecoder) super.init(coder: aDecoder)
prepareLayer()
} }
/** /**
:name: init :name: init
*/ */
public override init(layer: AnyObject) { public override init(layer: AnyObject) {
shape = .None
shadowDepth = .None
super.init(layer: layer) super.init(layer: layer)
prepareLayer()
} }
/** /**
:name: init :name: init
*/ */
public override init() { public override init() {
shape = .None
shadowDepth = .None
super.init() super.init()
prepareLayer()
}
/**
:name: init
*/
public convenience init(frame: CGRect) {
self.init()
self.frame = frame
}
public override func layoutSublayers() {
super.layoutSublayers()
visualLayer.frame = bounds
visualLayer.position = CGPointMake(width / 2, height / 2)
prepareShape()
}
/**
: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) {
(delegate as? MaterialAnimationDelegate)?.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!)
})
}
(delegate as? MaterialAnimationDelegate)?.materialAnimationDidStop?(anim, finished: flag)
removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
}
}
/**
:name: prepareLayer
*/
public func prepareLayer() {
// visualLayer
masksToBounds = true
visualLayer.zPosition = -1
addSublayer(visualLayer)
}
//
// :name: prepareShape
//
internal func prepareShape() {
if .Circle == shape {
cornerRadius = width / 2
}
} }
} }
...@@ -154,7 +154,7 @@ public class MaterialTextLayer : CATextLayer { ...@@ -154,7 +154,7 @@ public class MaterialTextLayer : CATextLayer {
:name: stringSize :name: stringSize
*/ */
public func stringSize(constrainedToWidth width: Double) -> CGSize { public func stringSize(constrainedToWidth width: Double) -> CGSize {
if let v:UIFont = internalFont { if let v: UIFont = internalFont {
if 0 < text?.utf16.count { if 0 < text?.utf16.count {
return v.sizeOfString(text!, constrainedToWidth: width) return v.sizeOfString(text!, constrainedToWidth: width)
} }
......
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