Commit 0b014dfd by Daniel Dahan

added spin animation and cleaned up pulseView

parent cb4c9195
...@@ -20,24 +20,13 @@ import UIKit ...@@ -20,24 +20,13 @@ import UIKit
public struct MaterialAnimation { public struct MaterialAnimation {
/** /**
:name: rotate :name: spin
*/ */
public static func rotate(layer: CALayer, duration: CFTimeInterval, completion: (() -> Void)? = nil) { public static func spin(layer: CALayer, duration: CFTimeInterval, rotations: Int = 1, completion: (() -> Void)? = nil) {
CATransaction.begin() let a: CABasicAnimation = CABasicAnimation()
CATransaction.setAnimationDuration(duration) a.keyPath = "transform.rotation"
let transform: CGAffineTransform = CGAffineTransformRotate(layer.affineTransform(), CGFloat(M_PI)) a.duration = duration
layer.setAffineTransform(transform) a.byValue = M_PI * 2 * Double(rotations)
CATransaction.setCompletionBlock(completion) layer.addAnimation(a, forKey: nil)
CATransaction.commit()
}
/**
:name: pulse
*/
public static func pulse(layer: CALayer, duraction: CFTimeInterval, completion: (() -> Void)? = nil) {
CATransaction.begin()
CATransaction.setAnimationDuration(1.0)
CATransaction.commit()
} }
} }
...@@ -27,7 +27,11 @@ public class MaterialPulseView: MaterialView { ...@@ -27,7 +27,11 @@ public class MaterialPulseView: MaterialView {
/** /**
:name: pulseColor :name: pulseColor
*/ */
public var pulseColor: UIColor? public var pulseColor: UIColor? {
didSet {
pulseLayer.backgroundColor = pulseColor?.colorWithAlphaComponent(0.5).CGColor
}
}
/** /**
:name: init :name: init
...@@ -43,6 +47,7 @@ public class MaterialPulseView: MaterialView { ...@@ -43,6 +47,7 @@ public class MaterialPulseView: MaterialView {
super.touchesBegan(touches, withEvent: event) super.touchesBegan(touches, withEvent: event)
let point: CGPoint = touches.first!.locationInView(self) let point: CGPoint = touches.first!.locationInView(self)
if nil != visualLayer.presentationLayer()?.hitTest(point) { if nil != visualLayer.presentationLayer()?.hitTest(point) {
// set start position
CATransaction.begin() CATransaction.begin()
CATransaction.setAnimationDuration(0) CATransaction.setAnimationDuration(0)
let w: CGFloat = width / 2 let w: CGFloat = width / 2
...@@ -52,6 +57,7 @@ public class MaterialPulseView: MaterialView { ...@@ -52,6 +57,7 @@ public class MaterialPulseView: MaterialView {
pulseLayer.cornerRadius = CGFloat(w / 2) pulseLayer.cornerRadius = CGFloat(w / 2)
CATransaction.commit() CATransaction.commit()
// expand
CATransaction.begin() CATransaction.begin()
CATransaction.setAnimationDuration(0.3) CATransaction.setAnimationDuration(0.3)
pulseLayer.transform = CATransform3DMakeScale(2, 2, 2) pulseLayer.transform = CATransform3DMakeScale(2, 2, 2)
...@@ -65,6 +71,7 @@ public class MaterialPulseView: MaterialView { ...@@ -65,6 +71,7 @@ public class MaterialPulseView: MaterialView {
*/ */
public override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { public override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event) super.touchesEnded(touches, withEvent: event)
// contract
CATransaction.begin() CATransaction.begin()
CATransaction.setAnimationDuration(0.3) CATransaction.setAnimationDuration(0.3)
pulseLayer.hidden = true pulseLayer.hidden = true
...@@ -109,7 +116,6 @@ public class MaterialPulseView: MaterialView { ...@@ -109,7 +116,6 @@ public class MaterialPulseView: MaterialView {
// pulseLayer // pulseLayer
pulseLayer.hidden = true pulseLayer.hidden = true
pulseLayer.backgroundColor = pulseColor?.colorWithAlphaComponent(0.5).CGColor visualLayer.insertSublayer(pulseLayer, atIndex: 1000)
visualLayer.addSublayer(pulseLayer)
} }
} }
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