Commit b52dc86e by Daniel Dahan

updated MaterialButton to use MaterialLayer

parent 6850033a
...@@ -102,11 +102,23 @@ public class MaterialButton : UIButton { ...@@ -102,11 +102,23 @@ public class MaterialButton : UIButton {
} }
/** /**
:name: masksToBounds
*/
public var masksToBounds: Bool {
get {
return materialLayer.masksToBounds
}
set(value) {
materialLayer.masksToBounds = value
}
}
/**
:name: backgroundColor :name: backgroundColor
*/ */
public override var backgroundColor: UIColor? { public override var backgroundColor: UIColor? {
didSet { didSet {
layer.backgroundColor = backgroundColor?.CGColor materialLayer.backgroundColor = backgroundColor?.CGColor
} }
} }
...@@ -115,10 +127,10 @@ public class MaterialButton : UIButton { ...@@ -115,10 +127,10 @@ public class MaterialButton : UIButton {
*/ */
public var x: CGFloat { public var x: CGFloat {
get { get {
return frame.origin.x return materialLayer.x
} }
set(value) { set(value) {
frame.origin.x = value materialLayer.x = value
} }
} }
...@@ -127,10 +139,10 @@ public class MaterialButton : UIButton { ...@@ -127,10 +139,10 @@ public class MaterialButton : UIButton {
*/ */
public var y: CGFloat { public var y: CGFloat {
get { get {
return frame.origin.y return materialLayer.y
} }
set(value) { set(value) {
frame.origin.y = value materialLayer.y = value
} }
} }
...@@ -139,13 +151,10 @@ public class MaterialButton : UIButton { ...@@ -139,13 +151,10 @@ public class MaterialButton : UIButton {
*/ */
public var width: CGFloat { public var width: CGFloat {
get { get {
return frame.size.width return materialLayer.width
} }
set(value) { set(value) {
frame.size.width = value materialLayer.width = value
if .None != shape {
frame.size.height = value
}
} }
} }
...@@ -154,13 +163,10 @@ public class MaterialButton : UIButton { ...@@ -154,13 +163,10 @@ public class MaterialButton : UIButton {
*/ */
public var height: CGFloat { public var height: CGFloat {
get { get {
return frame.size.height return materialLayer.height
} }
set(value) { set(value) {
frame.size.height = value materialLayer.height = value
if .None != shape {
frame.size.width = value
}
} }
} }
...@@ -210,28 +216,11 @@ public class MaterialButton : UIButton { ...@@ -210,28 +216,11 @@ public class MaterialButton : UIButton {
} }
/** /**
:name: masksToBounds
*/
public var masksToBounds: Bool {
get {
return visualLayer.masksToBounds
}
set(value) {
visualLayer.masksToBounds = value
}
}
/**
:name: cornerRadius :name: cornerRadius
*/ */
public var cornerRadius: MaterialRadius? { public var cornerRadius: MaterialRadius {
didSet { didSet {
if let v: MaterialRadius = cornerRadius { materialLayer.cornerRadius = MaterialRadiusToValue(cornerRadius)
layer.cornerRadius = MaterialRadiusToValue(v)
if .Circle == shape {
shape = .None
}
}
} }
} }
...@@ -242,9 +231,9 @@ public class MaterialButton : UIButton { ...@@ -242,9 +231,9 @@ public class MaterialButton : UIButton {
didSet { didSet {
if .None != shape { if .None != shape {
if width < height { if width < height {
frame.size.width = height width = height
} else { } else {
frame.size.height = width height = width
} }
} }
} }
...@@ -255,7 +244,7 @@ public class MaterialButton : UIButton { ...@@ -255,7 +244,7 @@ public class MaterialButton : UIButton {
*/ */
public var borderWidth: MaterialBorder { public var borderWidth: MaterialBorder {
didSet { didSet {
layer.borderWidth = MaterialBorderToValue(borderWidth) materialLayer.borderWidth = MaterialBorderToValue(borderWidth)
} }
} }
...@@ -264,7 +253,7 @@ public class MaterialButton : UIButton { ...@@ -264,7 +253,7 @@ public class MaterialButton : UIButton {
*/ */
public var borderColor: UIColor? { public var borderColor: UIColor? {
didSet { didSet {
layer.borderColor = borderColor?.CGColor materialLayer.borderColor = borderColor?.CGColor
} }
} }
...@@ -322,6 +311,7 @@ public class MaterialButton : UIButton { ...@@ -322,6 +311,7 @@ public class MaterialButton : UIButton {
shadowDepth = .None shadowDepth = .None
shape = .None shape = .None
contentInsets = .None contentInsets = .None
cornerRadius = .None
super.init(coder: aDecoder) super.init(coder: aDecoder)
} }
...@@ -333,26 +323,26 @@ public class MaterialButton : UIButton { ...@@ -333,26 +323,26 @@ public class MaterialButton : UIButton {
shadowDepth = .None shadowDepth = .None
shape = .None shape = .None
contentInsets = .None contentInsets = .None
cornerRadius = .None
super.init(frame: frame) super.init(frame: frame)
prepareView() prepareView()
} }
/** /**
:name: init :name: layoutSublayersOfLayer
*/ */
public convenience init() { public override func layoutSublayersOfLayer(layer: CALayer) {
self.init(frame: CGRectNull) super.layoutSublayersOfLayer(layer)
if self.layer == layer {
prepareShape()
}
} }
/** /**
:name: layoutSubviews :name: init
*/ */
public override func layoutSubviews() { public convenience init() {
super.layoutSubviews() self.init(frame: CGRectNull)
prepareShape()
visualLayer.frame = bounds
visualLayer.position = CGPointMake(width / 2, height / 2)
visualLayer.cornerRadius = layer.cornerRadius
} }
/** /**
...@@ -424,24 +414,13 @@ public class MaterialButton : UIButton { ...@@ -424,24 +414,13 @@ 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() {
// visualLayer
visualLayer.zPosition = -1
layer.addSublayer(visualLayer)
// pulseLayer // pulseLayer
pulseLayer.hidden = true pulseLayer.hidden = true
pulseLayer.zPosition = 1 pulseLayer.zPosition = 1
visualLayer.addSublayer(pulseLayer) materialLayer.visualLayer.addSublayer(pulseLayer)
} }
// //
...@@ -449,7 +428,7 @@ public class MaterialButton : UIButton { ...@@ -449,7 +428,7 @@ public class MaterialButton : UIButton {
// //
internal func prepareShape() { internal func prepareShape() {
if .Circle == shape { if .Circle == shape {
layer.cornerRadius = width / 2 materialLayer.cornerRadius = width / 2
} }
} }
......
...@@ -289,6 +289,9 @@ public class MaterialLayer : CAShapeLayer { ...@@ -289,6 +289,9 @@ public class MaterialLayer : CAShapeLayer {
} }
animationDelegate?.materialAnimationDidStop?(anim, finished: flag) animationDelegate?.materialAnimationDidStop?(anim, finished: flag)
removeAnimationForKey(a.keyPath!) removeAnimationForKey(a.keyPath!)
if "cornerRadius" == a.keyPath {
visualLayer.addAnimation(a, forKey: "cornerRadius")
}
} 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)
...@@ -301,9 +304,9 @@ public class MaterialLayer : CAShapeLayer { ...@@ -301,9 +304,9 @@ public class MaterialLayer : CAShapeLayer {
*/ */
public func prepareLayer() { public func prepareLayer() {
// visualLayer // visualLayer
masksToBounds = true // masksToBounds = true
visualLayer.zPosition = -1 // visualLayer.zPosition = -1
addSublayer(visualLayer) // addSublayer(visualLayer)
} }
// //
......
...@@ -210,10 +210,10 @@ public class MaterialView : UIView { ...@@ -210,10 +210,10 @@ public class MaterialView : UIView {
*/ */
public var shadowRadius: CGFloat { public var shadowRadius: CGFloat {
get { get {
return materialLayer.shadowRadius return layer.shadowRadius
} }
set(value) { set(value) {
materialLayer.shadowRadius = value layer.shadowRadius = value
} }
} }
...@@ -338,13 +338,6 @@ public class MaterialView : UIView { ...@@ -338,13 +338,6 @@ public class MaterialView : UIView {
} }
/** /**
: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: animation :name: animation
*/ */
public func animation(animation: CAAnimation) { public func animation(animation: CAAnimation) {
...@@ -375,7 +368,7 @@ public class MaterialView : UIView { ...@@ -375,7 +368,7 @@ public class MaterialView : UIView {
// //
internal func prepareShape() { internal func prepareShape() {
if .Circle == shape { if .Circle == shape {
layer.cornerRadius = width / 2 materialLayer.cornerRadius = width / 2
} }
} }
} }
......
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