Commit 785bd813 by Daniel Dahan

updated hitTest with containsPoint for animation accuracy

parent 55f9e5a3
...@@ -44,7 +44,7 @@ public extension MaterialAnimation { ...@@ -44,7 +44,7 @@ public extension MaterialAnimation {
/** /**
:name: path :name: path
*/ */
public static func path(bezierPath: UIBezierPath, duration: CFTimeInterval?, mode: MaterialAnimationRotationMode = .Auto) -> CAKeyframeAnimation { public static func path(bezierPath: UIBezierPath, mode: MaterialAnimationRotationMode = .Auto, duration: CFTimeInterval? = nil) -> CAKeyframeAnimation {
let animation: CAKeyframeAnimation = CAKeyframeAnimation() let animation: CAKeyframeAnimation = CAKeyframeAnimation()
animation.keyPath = "position" animation.keyPath = "position"
animation.path = bezierPath.CGPath animation.path = bezierPath.CGPath
......
...@@ -20,11 +20,6 @@ import UIKit ...@@ -20,11 +20,6 @@ import UIKit
public class MaterialPulseView : MaterialView { public class MaterialPulseView : MaterialView {
// //
// :name: touchesLayer
//
internal lazy var touchesLayer: CAShapeLayer = CAShapeLayer()
//
// :name: pulseLayer // :name: pulseLayer
// //
internal lazy var pulseLayer: CAShapeLayer = CAShapeLayer() internal lazy var pulseLayer: CAShapeLayer = CAShapeLayer()
...@@ -50,20 +45,13 @@ public class MaterialPulseView : MaterialView { ...@@ -50,20 +45,13 @@ public class MaterialPulseView : MaterialView {
} }
/** /**
:name: layoutSubviews
*/
public override func layoutSubviews() {
super.layoutSubviews()
touchesLayer.frame = bounds
touchesLayer.cornerRadius = layer.cornerRadius
}
/**
:name: touchesBegan :name: touchesBegan
*/ */
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) var point: CGPoint = touches.first!.locationInView(self)
point = layer.convertPoint(point, fromLayer: layer)
if true == layer.containsPoint(point) {
let s: CGFloat = (width < height ? height : width) / 2 let s: CGFloat = (width < height ? height : width) / 2
MaterialAnimation.disableAnimation({ MaterialAnimation.disableAnimation({
...@@ -76,6 +64,7 @@ public class MaterialPulseView : MaterialView { ...@@ -76,6 +64,7 @@ public class MaterialPulseView : MaterialView {
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)
} }
}
/** /**
:name: touchesEnded :name: touchesEnded
...@@ -100,20 +89,6 @@ public class MaterialPulseView : MaterialView { ...@@ -100,20 +89,6 @@ 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
// //
...@@ -136,14 +111,10 @@ public class MaterialPulseView : MaterialView { ...@@ -136,14 +111,10 @@ public class MaterialPulseView : MaterialView {
borderWidth = MaterialTheme.pulseView.borderWidth borderWidth = MaterialTheme.pulseView.borderWidth
borderColor = MaterialTheme.pulseView.bordercolor borderColor = MaterialTheme.pulseView.bordercolor
// touchesLayer
touchesLayer.zPosition = 1000
touchesLayer.masksToBounds = true
layer.addSublayer(touchesLayer)
// pulseLayer // pulseLayer
pulseLayer.hidden = true pulseLayer.hidden = true
touchesLayer.addSublayer(pulseLayer) pulseLayer.zPosition = 1
visualLayer.addSublayer(pulseLayer)
} }
// //
......
...@@ -299,21 +299,21 @@ public class MaterialView : UIView { ...@@ -299,21 +299,21 @@ public class MaterialView : UIView {
} }
/** /**
:name: addAnimation :name: animation
*/ */
public func addAnimation(animation: CAAnimation) { public func animation(animation: CAAnimation) {
if let a = animation as? CABasicAnimation { animation.delegate = self
if let a: CABasicAnimation = 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 }
layer.addAnimation(a, forKey: a.keyPath!) if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
visualLayer.addAnimation(a, forKey: a.keyPath!)
} else if let a = animation as? CAKeyframeAnimation {
a.delegate = self
layer.addAnimation(a, forKey: a.keyPath!) layer.addAnimation(a, forKey: a.keyPath!)
if true == filterAnimations(a) {
visualLayer.addAnimation(a, forKey: a.keyPath!) visualLayer.addAnimation(a, forKey: a.keyPath!)
} else if let a = animation as? CAAnimationGroup { }
a.delegate = self } else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil) layer.addAnimation(a, forKey: nil)
filterAnimations(a)
visualLayer.addAnimation(a, forKey: nil) visualLayer.addAnimation(a, forKey: nil)
} }
} }
...@@ -321,19 +321,22 @@ public class MaterialView : UIView { ...@@ -321,19 +321,22 @@ public class MaterialView : UIView {
/** /**
: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: CAPropertyAnimation = anim as? CAPropertyAnimation {
visualLayer.removeAnimationForKey(a.keyPath!) if let b: CABasicAnimation = a as? CABasicAnimation {
} else if let a = anim as? CAKeyframeAnimation { CATransaction.begin()
CATransaction.setDisableActions(true)
layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
CATransaction.commit()
}
layer.removeAnimationForKey(a.keyPath!)
visualLayer.removeAnimationForKey(a.keyPath!) visualLayer.removeAnimationForKey(a.keyPath!)
} else if let a = anim as? CAAnimationGroup { } else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! { for x in a.animations! {
animationDidStop(x, finished: true) animationDidStop(x, finished: true)
} }
...@@ -341,6 +344,22 @@ public class MaterialView : UIView { ...@@ -341,6 +344,22 @@ public class MaterialView : UIView {
} }
// //
// :name: filterAnimations
//
internal func filterAnimations(animation: CAAnimation) -> Bool? {
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
return "position" != a.keyPath
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
for var i: Int = a.animations!.count - 1; 0 <= i; --i {
if false == filterAnimations(a.animations![i]) {
a.animations!.removeAtIndex(i)
}
}
}
return nil
}
//
// :name: prepareView // :name: prepareView
// //
internal func prepareView() { internal func prepareView() {
......
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