Commit 785bd813 by Daniel Dahan

updated hitTest with containsPoint for animation accuracy

parent 55f9e5a3
......@@ -44,7 +44,7 @@ public extension MaterialAnimation {
/**
: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()
animation.keyPath = "position"
animation.path = bezierPath.CGPath
......
......@@ -20,11 +20,6 @@ import UIKit
public class MaterialPulseView : MaterialView {
//
// :name: touchesLayer
//
internal lazy var touchesLayer: CAShapeLayer = CAShapeLayer()
//
// :name: pulseLayer
//
internal lazy var pulseLayer: CAShapeLayer = CAShapeLayer()
......@@ -50,31 +45,25 @@ public class MaterialPulseView : MaterialView {
}
/**
:name: layoutSubviews
*/
public override func layoutSubviews() {
super.layoutSubviews()
touchesLayer.frame = bounds
touchesLayer.cornerRadius = layer.cornerRadius
}
/**
:name: touchesBegan
*/
public override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
let point: CGPoint = touches.first!.locationInView(self)
let s: CGFloat = (width < height ? height : width) / 2
MaterialAnimation.disableAnimation({
self.pulseLayer.bounds = CGRectMake(0, 0, s, s)
self.pulseLayer.position = point
self.pulseLayer.cornerRadius = s / 2
})
pulseLayer.hidden = false
pulseLayer.transform = CATransform3DMakeScale(3, 3, 3)
layer.transform = CATransform3DMakeScale(1.05, 1.05, 1.05)
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
MaterialAnimation.disableAnimation({
self.pulseLayer.bounds = CGRectMake(0, 0, s, s)
self.pulseLayer.position = point
self.pulseLayer.cornerRadius = s / 2
})
pulseLayer.hidden = false
pulseLayer.transform = CATransform3DMakeScale(3, 3, 3)
layer.transform = CATransform3DMakeScale(1.05, 1.05, 1.05)
}
}
/**
......@@ -100,20 +89,6 @@ 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
//
......@@ -136,14 +111,10 @@ public class MaterialPulseView : MaterialView {
borderWidth = MaterialTheme.pulseView.borderWidth
borderColor = MaterialTheme.pulseView.bordercolor
// touchesLayer
touchesLayer.zPosition = 1000
touchesLayer.masksToBounds = true
layer.addSublayer(touchesLayer)
// pulseLayer
pulseLayer.hidden = true
touchesLayer.addSublayer(pulseLayer)
pulseLayer.zPosition = 1
visualLayer.addSublayer(pulseLayer)
}
//
......
......@@ -299,21 +299,21 @@ public class MaterialView : UIView {
}
/**
:name: addAnimation
:name: animation
*/
public func addAnimation(animation: CAAnimation) {
if let a = animation as? CABasicAnimation {
public func animation(animation: CAAnimation) {
animation.delegate = self
if let a: CABasicAnimation = animation as? CABasicAnimation {
a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
a.delegate = self
layer.addAnimation(a, forKey: a.keyPath!)
visualLayer.addAnimation(a, forKey: a.keyPath!)
} else if let a = animation as? CAKeyframeAnimation {
a.delegate = self
}
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
layer.addAnimation(a, forKey: a.keyPath!)
visualLayer.addAnimation(a, forKey: a.keyPath!)
} else if let a = animation as? CAAnimationGroup {
a.delegate = self
if true == filterAnimations(a) {
visualLayer.addAnimation(a, forKey: a.keyPath!)
}
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil)
filterAnimations(a)
visualLayer.addAnimation(a, forKey: nil)
}
}
......@@ -321,19 +321,22 @@ public class MaterialView : UIView {
/**
:name: animationDidStart
*/
public override func animationDidStart(anim: CAAnimation) {
print("STARTED")
}
public override func animationDidStart(anim: CAAnimation) {}
/**
:name: animationDidStop
*/
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a = anim as? CABasicAnimation {
visualLayer.removeAnimationForKey(a.keyPath!)
} else if let a = anim as? CAKeyframeAnimation {
if let a: CAPropertyAnimation = anim as? CAPropertyAnimation {
if let b: CABasicAnimation = a as? CABasicAnimation {
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!)
} else if let a = anim as? CAAnimationGroup {
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
......@@ -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
//
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