Commit 55f9e5a3 by Daniel Dahan

cleaned animation in MaterialView additions

parent 9e0f67a1
...@@ -18,6 +18,31 @@ ...@@ -18,6 +18,31 @@
import UIKit import UIKit
public typealias MaterialAnimationFillModeType = String
public enum MaterialAnimationFillMode {
case Forwards
case Backwards
case Both
case Removed
}
/**
:name: MaterialAnimationFillModeToValue
*/
public func MaterialAnimationFillModeToValue(mode: MaterialAnimationFillMode) -> MaterialAnimationFillModeType {
switch mode {
case .Forwards:
return kCAFillModeForwards
case .Backwards:
return kCAFillModeBackwards
case .Both:
return kCAFillModeBoth
case .Removed:
return kCAFillModeRemoved
}
}
public struct MaterialAnimation { public struct MaterialAnimation {
/** /**
:name: disableAnimation :name: disableAnimation
...@@ -32,36 +57,12 @@ public struct MaterialAnimation { ...@@ -32,36 +57,12 @@ public struct MaterialAnimation {
/** /**
:name: groupAnimation :name: groupAnimation
*/ */
public static func groupAnimation(view: UIView, animations: Array<CAAnimation>, duration: NSTimeInterval = 0.5) { public static func groupAnimation(animations: Array<CAAnimation>, duration: NSTimeInterval = 0.5) -> CAAnimationGroup {
groupAnimation(view.layer, animations: animations, duration: duration)
}
/**
:name: groupAnimation
*/
public static func groupAnimation(layer: CALayer, animations: Array<CAAnimation>, duration: NSTimeInterval = 0.5) {
let group: CAAnimationGroup = CAAnimationGroup() let group: CAAnimationGroup = CAAnimationGroup()
group.fillMode = MaterialAnimationFillModeToValue(.Forwards)
group.removedOnCompletion = false
group.animations = animations group.animations = animations
group.duration = duration group.duration = duration
layer.addAnimation(group, forKey: nil) return group
}
/**
:name: applyBasicAnimation
*/
internal static func applyBasicAnimation(animation: CABasicAnimation, toLayer layer: CALayer) {
// groupAnimation(layer, animations: [animation], duration: animation.duration)
// CATransaction.begin()
// CATransaction.setAnimationDuration(animation.duration)
// layer.setValue(nil == animation.toValue ? animation.byValue : animation.toValue, forKey: animation.keyPath!)
// CATransaction.commit()
}
/**
:name: applyKeyframeAnimation
*/
internal static func applyKeyframeAnimation(animation: CAKeyframeAnimation, toLayer layer: CALayer) {
// use presentation layer if available
(nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).addAnimation(animation, forKey: animation.keyPath!)
} }
} }
...@@ -20,97 +20,67 @@ import UIKit ...@@ -20,97 +20,67 @@ import UIKit
public extension MaterialAnimation { public extension MaterialAnimation {
/** /**
:name: backgroundColorAnimation :name: backgroundColor
*/ */
public static func backgroundColorAnimation(color: UIColor) -> CABasicAnimation { public static func backgroundColor(color: UIColor, duration: CFTimeInterval? = nil) -> CABasicAnimation {
let animation: CABasicAnimation = CABasicAnimation() let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "backgroundColor" animation.keyPath = "backgroundColor"
animation.toValue = color.CGColor animation.toValue = color.CGColor
if let d = duration {
animation.duration = d
}
return animation return animation
} }
/** /**
:name: backgroundColor :name: cornerRadius
*/
public static func backgroundColor(color: UIColor, duration: CFTimeInterval = 0.25) -> CABasicAnimation {
let animation: CABasicAnimation = backgroundColorAnimation(color)
animation.duration = duration
return animation
}
/**
:name: cornerRadiusAnimation
*/ */
public static func cornerRadiusAnimation(radius: CGFloat) -> CABasicAnimation { public static func cornerRadius(radius: CGFloat, duration: CFTimeInterval? = nil) -> CABasicAnimation {
let animation: CABasicAnimation = CABasicAnimation() let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "cornerRadius" animation.keyPath = "cornerRadius"
animation.toValue = radius animation.toValue = radius
if let d = duration {
animation.duration = d
}
return animation return animation
} }
/** /**
:name: cornerRadius :name: rotation
*/
public static func cornerRadius(radius: CGFloat, duration: CFTimeInterval = 0.25) -> CABasicAnimation {
let animation: CABasicAnimation = cornerRadiusAnimation(radius)
animation.duration = duration
return animation
}
/**
:name: rotationAnimation
*/ */
public static func rotationAnimation(rotations: Int = 1) -> CABasicAnimation { public static func rotation(rotations: Int = 1, duration: CFTimeInterval? = nil) -> CABasicAnimation {
let animation: CABasicAnimation = CABasicAnimation() let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform.rotation" animation.keyPath = "transform.rotation"
animation.byValue = M_PI * 2 * Double(rotations) animation.byValue = M_PI * 2 * Double(rotations)
if let d = duration {
animation.duration = d
}
return animation return animation
} }
/**
:name: rotation
*/
public static func rotation(rotations: Int = 1, duration: CFTimeInterval = 0.5) -> CABasicAnimation {
let animation: CABasicAnimation = rotationAnimation(rotations)
animation.duration = duration
return animation
}
/** /**
:name: scaleAnimation :name: scale
*/ */
public static func scaleAnimation(transform: CATransform3D) -> CABasicAnimation { public static func scale(transform: CATransform3D, duration: CFTimeInterval? = nil) -> CABasicAnimation {
let animation: CABasicAnimation = CABasicAnimation() let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform" animation.keyPath = "transform"
animation.toValue = NSValue(CATransform3D: transform) animation.toValue = NSValue(CATransform3D: transform)
if let d = duration {
animation.duration = d
}
return animation return animation
} }
/** /**
:name: scale :name: position
*/
public static func scale(transform: CATransform3D, duration: CFTimeInterval = 0.25) -> CABasicAnimation {
let animation: CABasicAnimation = scaleAnimation(transform)
animation.duration = duration
return animation
}
/**
:name: positionAnimation
*/ */
public static func positionAnimation(point: CGPoint) -> CABasicAnimation { public static func position(point: CGPoint, duration: CFTimeInterval? = nil) -> CABasicAnimation {
let animation: CABasicAnimation = CABasicAnimation() let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "position" animation.keyPath = "position"
animation.toValue = NSValue(CGPoint: point) animation.toValue = NSValue(CGPoint: point)
return animation if let d = duration {
} animation.duration = d
}
/**
:name: position
*/
public static func position(point: CGPoint, duration: CFTimeInterval = 0.5) -> CABasicAnimation {
let animation: CABasicAnimation = positionAnimation(point)
animation.duration = duration
return animation return animation
} }
} }
\ No newline at end of file
...@@ -42,22 +42,16 @@ public func MaterialAnimationRotationModeToValue(mode: MaterialAnimationRotation ...@@ -42,22 +42,16 @@ public func MaterialAnimationRotationModeToValue(mode: MaterialAnimationRotation
public extension MaterialAnimation { public extension MaterialAnimation {
/** /**
:name: path :name: path
*/ */
public static func pathAnimation(bezierPath: UIBezierPath, mode: MaterialAnimationRotationMode = .Auto) -> CAKeyframeAnimation { public static func path(bezierPath: UIBezierPath, duration: CFTimeInterval?, mode: MaterialAnimationRotationMode = .Auto) -> CAKeyframeAnimation {
let animation: CAKeyframeAnimation = CAKeyframeAnimation() let animation: CAKeyframeAnimation = CAKeyframeAnimation()
animation.keyPath = "position" animation.keyPath = "position"
animation.path = bezierPath.CGPath animation.path = bezierPath.CGPath
animation.rotationMode = MaterialAnimationRotationModeToValue(mode) animation.rotationMode = MaterialAnimationRotationModeToValue(mode)
return animation if let d = duration {
} animation.duration = d
}
/**
:name: path
*/
public static func path(bezierPath: UIBezierPath, mode: MaterialAnimationRotationMode = .Auto, duration: CFTimeInterval = 1) -> CAKeyframeAnimation {
let animation: CAKeyframeAnimation = pathAnimation(bezierPath, mode: mode)
animation.duration = duration
return animation return animation
} }
} }
\ No newline at end of file
...@@ -64,12 +64,12 @@ public class MaterialPulseView : MaterialView { ...@@ -64,12 +64,12 @@ public class MaterialPulseView : MaterialView {
public override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { public override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event) super.touchesBegan(touches, withEvent: event)
let point: CGPoint = touches.first!.locationInView(self) let point: CGPoint = touches.first!.locationInView(self)
let w: CGFloat = (width < height ? height : width) / 2 let s: CGFloat = (width < height ? height : width) / 2
MaterialAnimation.disableAnimation({ _ in MaterialAnimation.disableAnimation({
self.pulseLayer.bounds = CGRectMake(0, 0, w, w) self.pulseLayer.bounds = CGRectMake(0, 0, s, s)
self.pulseLayer.position = point self.pulseLayer.position = point
self.pulseLayer.cornerRadius = CGFloat(w / 2) self.pulseLayer.cornerRadius = s / 2
}) })
pulseLayer.hidden = false pulseLayer.hidden = false
...@@ -100,6 +100,20 @@ public class MaterialPulseView : MaterialView { ...@@ -100,6 +100,20 @@ public class MaterialPulseView : MaterialView {
return nil // returning nil enables the animations for the layer property that are normally disabled. return nil // returning nil enables the animations for the layer property that are normally disabled.
} }
/**
:name: addAnimation
*/
public override func addAnimation(animation: CAAnimation) {
super.addAnimation(animation)
if let a = animation as? CABasicAnimation {
touchesLayer.addAnimation(a, forKey: a.keyPath!)
} else if let a = animation as? CAKeyframeAnimation {
touchesLayer.addAnimation(a, forKey: a.keyPath!)
} else if let a = animation as? CAAnimationGroup {
touchesLayer.addAnimation(a, forKey: nil)
}
}
// //
// :name: prepareView // :name: prepareView
// //
...@@ -109,7 +123,7 @@ public class MaterialPulseView : MaterialView { ...@@ -109,7 +123,7 @@ public class MaterialPulseView : MaterialView {
backgroundColor = MaterialTheme.pulseView.backgroundColor backgroundColor = MaterialTheme.pulseView.backgroundColor
pulseColorOpacity = MaterialTheme.pulseView.pulseColorOpacity pulseColorOpacity = MaterialTheme.pulseView.pulseColorOpacity
pulseColor = MaterialTheme.pulseView.pulseColor pulseColor = MaterialTheme.pulseView.pulseColor
contentsRect = MaterialTheme.pulseView.contentsRect contentsRect = MaterialTheme.pulseView.contentsRect
contentsCenter = MaterialTheme.pulseView.contentsCenter contentsCenter = MaterialTheme.pulseView.contentsCenter
contentsScale = MaterialTheme.pulseView.contentsScale contentsScale = MaterialTheme.pulseView.contentsScale
......
...@@ -305,31 +305,38 @@ public class MaterialView : UIView { ...@@ -305,31 +305,38 @@ public class MaterialView : UIView {
if let a = animation as? CABasicAnimation { if let a = animation as? CABasicAnimation {
a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!) a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
a.delegate = self a.delegate = self
a.fillMode = kCAFillModeForwards layer.addAnimation(a, forKey: a.keyPath!)
a.removedOnCompletion = false visualLayer.addAnimation(a, forKey: a.keyPath!)
layer.addAnimation(animation, forKey: a.keyPath!)
} else if let a = animation as? CAKeyframeAnimation { } else if let a = animation as? CAKeyframeAnimation {
a.delegate = self a.delegate = self
a.fillMode = kCAFillModeForwards layer.addAnimation(a, forKey: a.keyPath!)
a.removedOnCompletion = false visualLayer.addAnimation(a, forKey: a.keyPath!)
layer.addAnimation(animation, forKey: a.keyPath!) } else if let a = animation as? CAAnimationGroup {
a.delegate = self
layer.addAnimation(a, forKey: nil)
visualLayer.addAnimation(a, forKey: nil)
} }
} }
/** /**
:name: animationDidStart :name: animationDidStart
*/ */
public override func animationDidStart(anim: CAAnimation) {} public override func animationDidStart(anim: CAAnimation) {
print("STARTED")
}
/** /**
:name: animationDidStop :name: animationDidStop
*/ */
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) { public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a = anim as? CABasicAnimation { if let a = anim as? CABasicAnimation {
layer.setValue(nil == a.toValue ? a.byValue : a.toValue, forKey: a.keyPath!) visualLayer.removeAnimationForKey(a.keyPath!)
layer.removeAnimationForKey(a.keyPath!)
} else if let a = anim as? CAKeyframeAnimation { } else if let a = anim as? CAKeyframeAnimation {
layer.removeAnimationForKey(a.keyPath!) visualLayer.removeAnimationForKey(a.keyPath!)
} else if let a = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
} }
} }
...@@ -339,7 +346,7 @@ public class MaterialView : UIView { ...@@ -339,7 +346,7 @@ public class MaterialView : UIView {
internal func prepareView() { internal func prepareView() {
userInteractionEnabled = MaterialTheme.view.userInteractionEnabled userInteractionEnabled = MaterialTheme.view.userInteractionEnabled
backgroundColor = MaterialTheme.view.backgroundColor backgroundColor = MaterialTheme.view.backgroundColor
contentsRect = MaterialTheme.view.contentsRect contentsRect = MaterialTheme.view.contentsRect
contentsCenter = MaterialTheme.view.contentsCenter contentsCenter = MaterialTheme.view.contentsCenter
contentsScale = MaterialTheme.view.contentsScale contentsScale = MaterialTheme.view.contentsScale
......
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