Commit f5ceb503 by Daniel Dahan

added focus animation to pulse

parent ff210ebf
......@@ -59,6 +59,7 @@ class ViewController: UIViewController {
pulseView.shape = .Square
pulseView.depth = .Depth1
pulseView.cornerRadiusPreset = .Radius3
pulseView.pulseFocus = true
// Add pulseView to UIViewController.
view.addSubview(pulseView)
......
......@@ -33,56 +33,96 @@ import UIKit
internal extension MaterialAnimation {
/**
Triggers the pulse animation.
- Parameter layer: Container CALayer.
- Parameter visualLayer: A CAShapeLayer for the pulseLayer.
- Parameter color: The UIColor for the pulse.
- Parameter point: A point to pulse from.
- Returns: A Ooptional CFTimeInternal if the point exists within
the view. The time internal represents the animation time.
- Parameter width: Container width.
- Parameter height: Container height.
- Parameter duration: Animation duration.
- Parameter pulseLayer: An Optional pulseLayer to use in the animation.
*/
internal static func pulseAnimation(layer: CALayer, visualLayer: CALayer, color: UIColor, opacity: CGFloat, point: CGPoint, width: CGFloat, height: CGFloat, duration: NSTimeInterval) {
internal static func pulseAnimation(layer: CALayer, visualLayer: CALayer, color: UIColor, point: CGPoint, width: CGFloat, height: CGFloat, duration: NSTimeInterval, var pulseLayer: CAShapeLayer? = nil) {
let r: CGFloat = (width < height ? height : width) / 2
let f: CGFloat = 3
let v: CGFloat = r / f
let d: CGFloat = 2 * f
let pulseLayer: CAShapeLayer = CAShapeLayer()
var b: Bool = false
pulseLayer.hidden = true
pulseLayer.zPosition = 1
pulseLayer.backgroundColor = color.colorWithAlphaComponent(opacity).CGColor
visualLayer.addSublayer(pulseLayer)
if nil == pulseLayer {
pulseLayer = CAShapeLayer()
b = true
}
pulseLayer!.hidden = true
pulseLayer!.zPosition = 1
pulseLayer!.backgroundColor = color.CGColor
visualLayer.addSublayer(pulseLayer!)
MaterialAnimation.animationDisabled {
pulseLayer.bounds = CGRectMake(0, 0, v, v)
pulseLayer.position = point
pulseLayer.cornerRadius = r / d
pulseLayer.hidden = false
pulseLayer!.bounds = CGRectMake(0, 0, v, v)
pulseLayer!.position = point
pulseLayer!.cornerRadius = r / d
pulseLayer!.hidden = false
}
pulseLayer.addAnimation(MaterialAnimation.scale(3 * d, duration: duration), forKey: nil)
pulseLayer!.addAnimation(MaterialAnimation.scale(3 * d, duration: duration), forKey: nil)
if b {
MaterialAnimation.delay(duration) {
MaterialAnimation.animateWithDuration(duration, animations: {
pulseLayer.hidden = true
pulseLayer?.hidden = true
}) {
pulseLayer.removeFromSuperlayer()
pulseLayer?.removeFromSuperlayer()
}
}
} else {
MaterialAnimation.delay(duration / 2) {
pulseLayer?.addAnimation(MaterialAnimation.scale(1.10 * d, duration: duration), forKey: nil)
}
}
}
/**
Triggers the expanding animation.
- Parameter layer: Container CALayer.
- Parameter scale: The scale factor when expanding.
- Parameter duration: Animation duration.
*/
internal static func expandAnimation(layer: CALayer, scale: CGFloat, duration: NSTimeInterval) {
layer.addAnimation(MaterialAnimation.scale(scale, duration: duration), forKey: nil)
}
internal static func shrinkAnimation(layer: CALayer, width: CGFloat, duration: NSTimeInterval) {
/**
Triggers the shrinking animation.
- Parameter layer: Container CALayer.
- Parameter width: Container width.
- Parameter duration: Animation duration.
- Parameter pulseLayer: An Optional pulseLayer to use in the animation.
*/
internal static func shrinkAnimation(layer: CALayer, width: CGFloat, duration: NSTimeInterval, pulseLayer: CAShapeLayer? = nil) {
if let v: CAShapeLayer = pulseLayer {
MaterialAnimation.animateWithDuration(duration, animations: {
v.hidden = true
}) {
v.removeFromSuperlayer()
}
}
layer.addAnimation(MaterialAnimation.scale(1, duration: duration), forKey: nil)
}
/**
Retrieves the animation duration time.
- Parameter width: Container width.
- Returns: An NSTimeInterval value that represents the animation time.
*/
internal static func pulseDuration(width: CGFloat) -> NSTimeInterval {
var t: CFTimeInterval = CFTimeInterval(1.5 * width / MaterialDevice.width)
if 0.55 < t || 0.25 > t {
t = 0.55
}
t /= 1.3
return t
return t / 1.3
}
}
\ No newline at end of file
......@@ -31,6 +31,12 @@
import UIKit
public class MaterialPulseView : MaterialView {
/// To use a single pulse and have it focused when held.
public var pulseFocus: Bool = false
/// A pulse layer for focus handling.
public private(set) var pulseLayer: CAShapeLayer?
/// Sets whether the scaling animation should be used.
public lazy var pulseScale: Bool = true
......@@ -53,7 +59,7 @@ public class MaterialPulseView : MaterialView {
let duration: NSTimeInterval = MaterialAnimation.pulseDuration(width)
if let v: UIColor = pulseColor {
MaterialAnimation.pulseAnimation(layer, visualLayer: visualLayer, color: v, opacity: pulseColorOpacity, point: point!, width: width, height: height, duration: duration)
MaterialAnimation.pulseAnimation(layer, visualLayer: visualLayer, color: v.colorWithAlphaComponent(pulseColorOpacity), point: point!, width: width, height: height, duration: duration)
}
if pulseScale {
......@@ -78,10 +84,14 @@ public class MaterialPulseView : MaterialView {
super.touchesBegan(touches, withEvent: event)
let duration: NSTimeInterval = MaterialAnimation.pulseDuration(width)
if pulseFocus {
pulseLayer = CAShapeLayer()
}
if let v: UIColor = pulseColor {
let point: CGPoint = layer.convertPoint(touches.first!.locationInView(self), fromLayer: layer)
MaterialAnimation.pulseAnimation(layer, visualLayer: visualLayer, color: v, opacity: pulseColorOpacity, point: point, width: width, height: height, duration: duration)
MaterialAnimation.pulseAnimation(layer, visualLayer: visualLayer, color: v.colorWithAlphaComponent(pulseColorOpacity), point: point, width: width, height: height, duration: duration, pulseLayer: pulseLayer)
}
if pulseScale {
......@@ -97,8 +107,7 @@ public class MaterialPulseView : MaterialView {
*/
public override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
let duration: NSTimeInterval = MaterialAnimation.pulseDuration(width)
MaterialAnimation.shrinkAnimation(layer, width: width, duration: duration)
MaterialAnimation.shrinkAnimation(layer, width: width, duration: MaterialAnimation.pulseDuration(width), pulseLayer: pulseLayer)
}
/**
......@@ -109,8 +118,7 @@ public class MaterialPulseView : MaterialView {
*/
public override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
let duration: NSTimeInterval = MaterialAnimation.pulseDuration(width)
MaterialAnimation.shrinkAnimation(layer, width: width, duration: duration)
MaterialAnimation.shrinkAnimation(layer, width: width, duration: MaterialAnimation.pulseDuration(width), pulseLayer: 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