Commit 55f9e5a3 by Daniel Dahan

cleaned animation in MaterialView additions

parent 9e0f67a1
......@@ -18,6 +18,31 @@
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 {
/**
:name: disableAnimation
......@@ -32,36 +57,12 @@ public struct MaterialAnimation {
/**
:name: groupAnimation
*/
public static func groupAnimation(view: UIView, animations: Array<CAAnimation>, duration: NSTimeInterval = 0.5) {
groupAnimation(view.layer, animations: animations, duration: duration)
}
/**
:name: groupAnimation
*/
public static func groupAnimation(layer: CALayer, animations: Array<CAAnimation>, duration: NSTimeInterval = 0.5) {
public static func groupAnimation(animations: Array<CAAnimation>, duration: NSTimeInterval = 0.5) -> CAAnimationGroup {
let group: CAAnimationGroup = CAAnimationGroup()
group.fillMode = MaterialAnimationFillModeToValue(.Forwards)
group.removedOnCompletion = false
group.animations = animations
group.duration = duration
layer.addAnimation(group, forKey: nil)
}
/**
: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!)
return group
}
}
......@@ -20,97 +20,67 @@ import UIKit
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()
animation.keyPath = "backgroundColor"
animation.toValue = color.CGColor
if let d = duration {
animation.duration = d
}
return animation
}
/**
:name: backgroundColor
*/
public static func backgroundColor(color: UIColor, duration: CFTimeInterval = 0.25) -> CABasicAnimation {
let animation: CABasicAnimation = backgroundColorAnimation(color)
animation.duration = duration
return animation
}
/**
:name: cornerRadiusAnimation
:name: cornerRadius
*/
public static func cornerRadiusAnimation(radius: CGFloat) -> CABasicAnimation {
public static func cornerRadius(radius: CGFloat, duration: CFTimeInterval? = nil) -> CABasicAnimation {
let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "cornerRadius"
animation.toValue = radius
if let d = duration {
animation.duration = d
}
return animation
}
/**
:name: cornerRadius
*/
public static func cornerRadius(radius: CGFloat, duration: CFTimeInterval = 0.25) -> CABasicAnimation {
let animation: CABasicAnimation = cornerRadiusAnimation(radius)
animation.duration = duration
return animation
}
/**
:name: rotationAnimation
:name: rotation
*/
public static func rotationAnimation(rotations: Int = 1) -> CABasicAnimation {
public static func rotation(rotations: Int = 1, duration: CFTimeInterval? = nil) -> CABasicAnimation {
let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform.rotation"
animation.byValue = M_PI * 2 * Double(rotations)
if let d = duration {
animation.duration = d
}
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()
animation.keyPath = "transform"
animation.toValue = NSValue(CATransform3D: transform)
if let d = duration {
animation.duration = d
}
return animation
}
/**
:name: scale
*/
public static func scale(transform: CATransform3D, duration: CFTimeInterval = 0.25) -> CABasicAnimation {
let animation: CABasicAnimation = scaleAnimation(transform)
animation.duration = duration
return animation
}
/**
:name: positionAnimation
:name: position
*/
public static func positionAnimation(point: CGPoint) -> CABasicAnimation {
public static func position(point: CGPoint, duration: CFTimeInterval? = nil) -> CABasicAnimation {
let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "position"
animation.toValue = NSValue(CGPoint: point)
return animation
}
/**
:name: position
*/
public static func position(point: CGPoint, duration: CFTimeInterval = 0.5) -> CABasicAnimation {
let animation: CABasicAnimation = positionAnimation(point)
animation.duration = duration
if let d = duration {
animation.duration = d
}
return animation
}
}
\ No newline at end of file
......@@ -42,22 +42,16 @@ public func MaterialAnimationRotationModeToValue(mode: MaterialAnimationRotation
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()
animation.keyPath = "position"
animation.path = bezierPath.CGPath
animation.rotationMode = MaterialAnimationRotationModeToValue(mode)
return animation
}
/**
: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
if let d = duration {
animation.duration = d
}
return animation
}
}
\ No newline at end of file
......@@ -64,12 +64,12 @@ public class MaterialPulseView : MaterialView {
public override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
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
self.pulseLayer.bounds = CGRectMake(0, 0, w, w)
MaterialAnimation.disableAnimation({
self.pulseLayer.bounds = CGRectMake(0, 0, s, s)
self.pulseLayer.position = point
self.pulseLayer.cornerRadius = CGFloat(w / 2)
self.pulseLayer.cornerRadius = s / 2
})
pulseLayer.hidden = false
......@@ -100,6 +100,20 @@ public class MaterialPulseView : MaterialView {
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
//
......@@ -109,7 +123,7 @@ public class MaterialPulseView : MaterialView {
backgroundColor = MaterialTheme.pulseView.backgroundColor
pulseColorOpacity = MaterialTheme.pulseView.pulseColorOpacity
pulseColor = MaterialTheme.pulseView.pulseColor
contentsRect = MaterialTheme.pulseView.contentsRect
contentsCenter = MaterialTheme.pulseView.contentsCenter
contentsScale = MaterialTheme.pulseView.contentsScale
......
......@@ -305,31 +305,38 @@ public class MaterialView : UIView {
if let a = animation as? CABasicAnimation {
a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
a.delegate = self
a.fillMode = kCAFillModeForwards
a.removedOnCompletion = false
layer.addAnimation(animation, forKey: a.keyPath!)
layer.addAnimation(a, forKey: a.keyPath!)
visualLayer.addAnimation(a, forKey: a.keyPath!)
} else if let a = animation as? CAKeyframeAnimation {
a.delegate = self
a.fillMode = kCAFillModeForwards
a.removedOnCompletion = false
layer.addAnimation(animation, forKey: a.keyPath!)
layer.addAnimation(a, forKey: a.keyPath!)
visualLayer.addAnimation(a, 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
*/
public override func animationDidStart(anim: CAAnimation) {}
public override func animationDidStart(anim: CAAnimation) {
print("STARTED")
}
/**
:name: animationDidStop
*/
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a = anim as? CABasicAnimation {
layer.setValue(nil == a.toValue ? a.byValue : a.toValue, forKey: a.keyPath!)
layer.removeAnimationForKey(a.keyPath!)
visualLayer.removeAnimationForKey(a.keyPath!)
} 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 {
internal func prepareView() {
userInteractionEnabled = MaterialTheme.view.userInteractionEnabled
backgroundColor = MaterialTheme.view.backgroundColor
contentsRect = MaterialTheme.view.contentsRect
contentsCenter = MaterialTheme.view.contentsCenter
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