Commit 68026aad by Daniel Dahan

added base animations to MaterialAnimation

parent e73d83f3
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
import UIKit import UIKit
public class BasicCardView: MaterialPulseView { public class BasicCardView : MaterialPulseView {
// //
// :name: dividerLayer // :name: dividerLayer
// //
...@@ -208,14 +208,14 @@ public class BasicCardView: MaterialPulseView { ...@@ -208,14 +208,14 @@ public class BasicCardView: MaterialPulseView {
:name: init :name: init
*/ */
public convenience init() { public convenience init() {
self.init(frame: CGRectZero) self.init(frame: CGRectNull)
} }
/** /**
:name: init :name: init
*/ */
public convenience init?(titleLabel: UILabel? = nil, detailLabel: UILabel? = nil, leftButtons: Array<MaterialButton>? = nil, rightButtons: Array<MaterialButton>? = nil) { public convenience init?(titleLabel: UILabel? = nil, detailLabel: UILabel? = nil, leftButtons: Array<MaterialButton>? = nil, rightButtons: Array<MaterialButton>? = nil) {
self.init(frame: CGRectZero) self.init(frame: CGRectNull)
self.prepareProperties(titleLabel, detailLabel: detailLabel, leftButtons: leftButtons, rightButtons: rightButtons) self.prepareProperties(titleLabel, detailLabel: detailLabel, leftButtons: leftButtons, rightButtons: rightButtons)
} }
......
...@@ -19,13 +19,6 @@ ...@@ -19,13 +19,6 @@
import UIKit import UIKit
public class FabButton : MaterialButton { public class FabButton : MaterialButton {
/**
:name: layoutSubviews
*/
public override func layoutSubviews() {
super.layoutSubviews()
}
// //
// :name: prepareView // :name: prepareView
// //
......
...@@ -20,13 +20,85 @@ import UIKit ...@@ -20,13 +20,85 @@ import UIKit
public struct MaterialAnimation { public struct MaterialAnimation {
/** /**
:name: spin :name: backgroundColor
*/ */
public static func spin(view: UIView, duration: CFTimeInterval = 1, rotations: Int = 1, completion: (() -> Void)? = nil) { public static func backgroundColor(layer: CALayer, color: UIColor, duration: CFTimeInterval = 0.25, completion: (() -> Void)? = nil) {
let a: CABasicAnimation = CABasicAnimation() let animation: CABasicAnimation = CABasicAnimation()
a.keyPath = "transform.rotation" animation.keyPath = "backgroundColor"
a.duration = duration animation.duration = duration
a.byValue = M_PI * 2 * Double(rotations) animation.toValue = color.CGColor
view.layer.addAnimation(a, forKey: nil) applyBasicAnimation(animation, layer: layer, completion: completion)
}
/**
:name: rotate
*/
public static func rotate(layer: CALayer, rotations: Int = 1, duration: CFTimeInterval = 0.5, completion: (() -> Void)? = nil) {
let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform.rotation"
animation.duration = duration
animation.byValue = M_PI * 2 * Double(rotations)
applyBasicAnimation(animation, layer: layer, completion: completion)
}
/**
:name: transform
*/
public static func transform(layer: CALayer, scale: CATransform3D, duration: CFTimeInterval = 0.25, completion: (() -> Void)? = nil) {
let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform"
animation.duration = duration
animation.toValue = NSValue(CATransform3D: scale)
applyBasicAnimation(animation, layer: layer, completion: completion)
}
/**
:name: position
*/
public static func position(layer: CALayer, point: CGPoint, duration: CFTimeInterval = 0.25, completion: (() -> Void)? = nil) {
let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "position"
animation.duration = duration
animation.toValue = NSValue(CGPoint: point)
applyBasicAnimation(animation, layer: layer, completion: completion)
}
/**
:name: cornerRadius
*/
public static func cornerRadius(layer: CALayer, radius: CGFloat, duration: CFTimeInterval = 0.25, completion: (() -> Void)? = nil) {
let animation: CABasicAnimation = CABasicAnimation()
animation.keyPath = "cornerRadius"
animation.duration = duration
animation.toValue = radius
applyBasicAnimation(animation, layer: layer, completion: completion)
}
/**
:name: applyBasicAnimation
*/
public static func applyBasicAnimation(animation: CABasicAnimation, layer: CALayer, completion: (() -> Void)? = nil) {
// use presentation layer if available
animation.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(animation.keyPath!)
CATransaction.begin()
CATransaction.setDisableActions(true)
CATransaction.setCompletionBlock(completion)
if let v = animation.toValue {
layer.setValue(v, forKey: animation.keyPath!)
} else if let v = animation.byValue {
layer.setValue(v, forKey: animation.keyPath!)
}
CATransaction.commit()
layer.addAnimation(animation, forKey: nil)
}
/**
:name: disableAnimation
*/
public static func disableAnimation(block: (() -> Void)) {
CATransaction.begin()
CATransaction.setAnimationDuration(0)
block()
CATransaction.commit()
} }
} }
...@@ -70,10 +70,10 @@ public class MaterialButton : UIButton { ...@@ -70,10 +70,10 @@ public class MaterialButton : UIButton {
*/ */
public var x: CGFloat { public var x: CGFloat {
get { get {
return frame.origin.x return layer.frame.origin.x
} }
set(value) { set(value) {
frame.origin.x = value layer.frame.origin.x = value
} }
} }
...@@ -82,10 +82,10 @@ public class MaterialButton : UIButton { ...@@ -82,10 +82,10 @@ public class MaterialButton : UIButton {
*/ */
public var y: CGFloat { public var y: CGFloat {
get { get {
return frame.origin.y return layer.frame.origin.y
} }
set(value) { set(value) {
frame.origin.y = value layer.frame.origin.y = value
} }
} }
...@@ -94,12 +94,12 @@ public class MaterialButton : UIButton { ...@@ -94,12 +94,12 @@ public class MaterialButton : UIButton {
*/ */
public var width: CGFloat { public var width: CGFloat {
get { get {
return frame.size.width return layer.frame.size.width
} }
set(value) { set(value) {
frame.size.width = value layer.frame.size.width = value
if nil != shape { if nil != shape {
frame.size.height = value layer.frame.size.height = value
} }
} }
} }
...@@ -109,12 +109,12 @@ public class MaterialButton : UIButton { ...@@ -109,12 +109,12 @@ public class MaterialButton : UIButton {
*/ */
public var height: CGFloat { public var height: CGFloat {
get { get {
return frame.size.height return layer.frame.size.height
} }
set(value) { set(value) {
frame.size.height = value layer.frame.size.height = value
if nil != shape { if nil != shape {
frame.size.width = value layer.frame.size.width = value
} }
} }
} }
...@@ -183,9 +183,9 @@ public class MaterialButton : UIButton { ...@@ -183,9 +183,9 @@ public class MaterialButton : UIButton {
didSet { didSet {
if nil != shape { if nil != shape {
if width < height { if width < height {
frame.size.width = height layer.frame.size.width = height
} else { } else {
frame.size.height = width layer.frame.size.height = width
} }
} }
} }
...@@ -258,7 +258,7 @@ public class MaterialButton : UIButton { ...@@ -258,7 +258,7 @@ public class MaterialButton : UIButton {
:name: init :name: init
*/ */
public convenience init() { public convenience init() {
self.init(frame: CGRectZero) self.init(frame: CGRectNull)
} }
/** /**
...@@ -288,23 +288,17 @@ public class MaterialButton : UIButton { ...@@ -288,23 +288,17 @@ public class MaterialButton : UIButton {
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)
// set start position
CATransaction.begin()
CATransaction.setAnimationDuration(0)
let w: CGFloat = (width < height ? height : width) / 2 let w: CGFloat = (width < height ? height : width) / 2
pulseLayer.hidden = false
pulseLayer.position = point
pulseLayer.bounds = CGRectMake(0, 0, w, w)
pulseLayer.cornerRadius = CGFloat(w / 2)
CATransaction.commit()
// expand MaterialAnimation.disableAnimation({ _ in
CATransaction.begin() self.pulseLayer.bounds = CGRectMake(0, 0, w, w)
CATransaction.setAnimationDuration(0.25) self.pulseLayer.position = point
pulseLayer.transform = CATransform3DMakeScale(3, 3, 3) self.pulseLayer.cornerRadius = CGFloat(w / 2)
layer.transform = CATransform3DMakeScale(1.05, 1.05, 1.05) })
CATransaction.commit()
pulseLayer.hidden = false
MaterialAnimation.transform(pulseLayer, scale: CATransform3DMakeScale(3, 3, 3))
MaterialAnimation.transform(layer, scale: CATransform3DMakeScale(1.05, 1.05, 1.05))
} }
/** /**
...@@ -368,11 +362,8 @@ public class MaterialButton : UIButton { ...@@ -368,11 +362,8 @@ public class MaterialButton : UIButton {
// :name: shrink // :name: shrink
// //
internal func shrink() { internal func shrink() {
CATransaction.begin()
CATransaction.setAnimationDuration(0.25)
pulseLayer.hidden = true pulseLayer.hidden = true
pulseLayer.transform = CATransform3DIdentity MaterialAnimation.transform(pulseLayer, scale: CATransform3DIdentity)
layer.transform = CATransform3DIdentity MaterialAnimation.transform(layer, scale: CATransform3DIdentity)
CATransaction.commit()
} }
} }
\ No newline at end of file
...@@ -157,7 +157,7 @@ public class MaterialLabel : UILabel { ...@@ -157,7 +157,7 @@ public class MaterialLabel : UILabel {
:name: init :name: init
*/ */
public convenience init() { public convenience init() {
self.init(frame: CGRectZero) self.init(frame: CGRectNull)
} }
// //
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
import UIKit import UIKit
public class MaterialPulseView: MaterialView { public class MaterialPulseView : MaterialView {
// //
// :name: touchesLayer // :name: touchesLayer
// //
...@@ -53,7 +53,7 @@ public class MaterialPulseView: MaterialView { ...@@ -53,7 +53,7 @@ public class MaterialPulseView: MaterialView {
:name: init :name: init
*/ */
public convenience init() { public convenience init() {
self.init(frame: CGRectZero) self.init(frame: CGRectNull)
} }
/** /**
...@@ -71,23 +71,17 @@ public class MaterialPulseView: MaterialView { ...@@ -71,23 +71,17 @@ 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)
// set start position
CATransaction.begin()
CATransaction.setAnimationDuration(0)
let w: CGFloat = (width < height ? height : width) / 2 let w: CGFloat = (width < height ? height : width) / 2
pulseLayer.hidden = false
pulseLayer.position = point
pulseLayer.bounds = CGRectMake(0, 0, w, w)
pulseLayer.cornerRadius = CGFloat(w / 2)
CATransaction.commit()
// expand MaterialAnimation.disableAnimation({ _ in
CATransaction.begin() self.pulseLayer.bounds = CGRectMake(0, 0, w, w)
CATransaction.setAnimationDuration(0.25) self.pulseLayer.position = point
self.pulseLayer.cornerRadius = CGFloat(w / 2)
})
pulseLayer.hidden = false
pulseLayer.transform = CATransform3DMakeScale(3, 3, 3) pulseLayer.transform = CATransform3DMakeScale(3, 3, 3)
layer.transform = CATransform3DMakeScale(1.05, 1.05, 1.05) layer.transform = CATransform3DMakeScale(1.05, 1.05, 1.05)
CATransaction.commit()
} }
/** /**
...@@ -156,11 +150,8 @@ public class MaterialPulseView: MaterialView { ...@@ -156,11 +150,8 @@ public class MaterialPulseView: MaterialView {
// :name: shrink // :name: shrink
// //
internal func shrink() { internal func shrink() {
CATransaction.begin()
CATransaction.setAnimationDuration(0.25)
pulseLayer.hidden = true pulseLayer.hidden = true
pulseLayer.transform = CATransform3DIdentity pulseLayer.transform = CATransform3DIdentity
layer.transform = CATransform3DIdentity layer.transform = CATransform3DIdentity
CATransaction.commit()
} }
} }
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
import UIKit import UIKit
public class MaterialView: UIView { public class MaterialView : UIView {
// //
// :name: visualLayer // :name: visualLayer
// //
...@@ -86,10 +86,10 @@ public class MaterialView: UIView { ...@@ -86,10 +86,10 @@ public class MaterialView: UIView {
*/ */
public var x: CGFloat { public var x: CGFloat {
get { get {
return frame.origin.x return layer.frame.origin.x
} }
set(value) { set(value) {
frame.origin.x = value layer.frame.origin.x = value
} }
} }
...@@ -98,10 +98,10 @@ public class MaterialView: UIView { ...@@ -98,10 +98,10 @@ public class MaterialView: UIView {
*/ */
public var y: CGFloat { public var y: CGFloat {
get { get {
return frame.origin.y return layer.frame.origin.y
} }
set(value) { set(value) {
frame.origin.y = value layer.frame.origin.y = value
} }
} }
...@@ -110,12 +110,12 @@ public class MaterialView: UIView { ...@@ -110,12 +110,12 @@ public class MaterialView: UIView {
*/ */
public var width: CGFloat { public var width: CGFloat {
get { get {
return frame.size.width return layer.frame.size.width
} }
set(value) { set(value) {
frame.size.width = value layer.frame.size.width = value
if nil != shape { if nil != shape {
frame.size.height = value layer.frame.size.height = value
} }
} }
} }
...@@ -125,12 +125,12 @@ public class MaterialView: UIView { ...@@ -125,12 +125,12 @@ public class MaterialView: UIView {
*/ */
public var height: CGFloat { public var height: CGFloat {
get { get {
return frame.size.height return layer.frame.size.height
} }
set(value) { set(value) {
frame.size.height = value layer.frame.size.height = value
if nil != shape { if nil != shape {
frame.size.width = value layer.frame.size.width = value
} }
} }
} }
...@@ -199,9 +199,9 @@ public class MaterialView: UIView { ...@@ -199,9 +199,9 @@ public class MaterialView: UIView {
didSet { didSet {
if nil != shape { if nil != shape {
if width < height { if width < height {
frame.size.width = height layer.frame.size.width = height
} else { } else {
frame.size.height = width layer.frame.size.height = width
} }
} }
} }
...@@ -265,7 +265,7 @@ public class MaterialView: UIView { ...@@ -265,7 +265,7 @@ public class MaterialView: UIView {
:name: init :name: init
*/ */
public convenience init() { public convenience init() {
self.init(frame: CGRectZero) self.init(frame: CGRectNull)
} }
/** /**
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
import UIKit import UIKit
public class NavigationBarView: MaterialView { public class NavigationBarView : MaterialView {
/** /**
:name: statusBarStyle :name: statusBarStyle
*/ */
......
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