Commit 2017544d by Daniel Dahan

added delgation methods for MaterialCollectionViewCell

parent 6f3a9732
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
import UIKit import UIKit
@objc(MaterialAnimationDelegate) @objc(MaterialAnimationDelegate)
public protocol MaterialAnimationDelegate { public protocol MaterialAnimationDelegate : MaterialDelegate {
/** /**
:name: materialAnimationDidStart :name: materialAnimationDidStart
*/ */
......
...@@ -33,7 +33,7 @@ public class MaterialButton : UIButton { ...@@ -33,7 +33,7 @@ public class MaterialButton : UIButton {
/** /**
:name: delegate :name: delegate
*/ */
public weak var delegate: MaterialAnimationDelegate? public weak var delegate: MaterialDelegate?
/** /**
:name: pulseScale :name: pulseScale
...@@ -355,7 +355,7 @@ public class MaterialButton : UIButton { ...@@ -355,7 +355,7 @@ public class MaterialButton : UIButton {
:name: animationDidStart :name: animationDidStart
*/ */
public override func animationDidStart(anim: CAAnimation) { public override func animationDidStart(anim: CAAnimation) {
delegate?.materialAnimationDidStart?(anim) (delegate as? MaterialAnimationDelegate)?.materialAnimationDidStart?(anim)
} }
/** /**
...@@ -368,7 +368,7 @@ public class MaterialButton : UIButton { ...@@ -368,7 +368,7 @@ public class MaterialButton : UIButton {
self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!) self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
}) })
} }
delegate?.materialAnimationDidStop?(anim, finished: flag) (delegate as? MaterialAnimationDelegate)?.materialAnimationDidStop?(anim, finished: flag)
layer.removeAnimationForKey(a.keyPath!) layer.removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup { } else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! { for x in a.animations! {
......
...@@ -18,6 +18,17 @@ ...@@ -18,6 +18,17 @@
import UIKit import UIKit
@objc(MaterialCollectionViewCellDelegate)
public protocol MaterialCollectionViewCellDelegate : MaterialDelegate {
optional func materialCollectionViewCellWillRevealLeftLayer(cell: MaterialCollectionViewCell)
optional func materialCollectionViewCellWillRevealRightLayer(cell: MaterialCollectionViewCell)
optional func materialCollectionViewCellDidRevealLeftLayer(cell: MaterialCollectionViewCell)
optional func materialCollectionViewCellDidRevealRightLayer(cell: MaterialCollectionViewCell)
optional func materialCollectionViewCellDidCloseLeftLayer(cell: MaterialCollectionViewCell)
optional func materialCollectionViewCellDidCloseRightLayer(cell: MaterialCollectionViewCell)
}
@objc(MaterialCollectionViewCell)
public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecognizerDelegate { public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecognizerDelegate {
// //
// :name: panRecognizer // :name: panRecognizer
...@@ -50,9 +61,14 @@ public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecogni ...@@ -50,9 +61,14 @@ public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecogni
public private(set) lazy var pulseLayer: CAShapeLayer = CAShapeLayer() public private(set) lazy var pulseLayer: CAShapeLayer = CAShapeLayer()
/** /**
:name: revealed
*/
public private(set) lazy var revealed: Bool = false
/**
:name: delegate :name: delegate
*/ */
public weak var delegate: MaterialAnimationDelegate? public weak var delegate: MaterialDelegate?
/** /**
:name: pulseScale :name: pulseScale
...@@ -341,7 +357,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecogni ...@@ -341,7 +357,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecogni
:name: animationDidStart :name: animationDidStart
*/ */
public override func animationDidStart(anim: CAAnimation) { public override func animationDidStart(anim: CAAnimation) {
delegate?.materialAnimationDidStart?(anim) (delegate as? MaterialAnimationDelegate)?.materialAnimationDidStart?(anim)
} }
/** /**
...@@ -354,7 +370,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecogni ...@@ -354,7 +370,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecogni
self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!) self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
}) })
} }
delegate?.materialAnimationDidStop?(anim, finished: flag) (delegate as? MaterialAnimationDelegate)?.materialAnimationDidStop?(anim, finished: flag)
layer.removeAnimationForKey(a.keyPath!) layer.removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup { } else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! { for x in a.animations! {
...@@ -477,17 +493,35 @@ public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecogni ...@@ -477,17 +493,35 @@ public class MaterialCollectionViewCell : UICollectionViewCell, UIGestureRecogni
rightOnDragRelease = x < -width / 2 rightOnDragRelease = x < -width / 2
leftOnDragRelease = x > width / 2 leftOnDragRelease = x > width / 2
if !revealed && (leftOnDragRelease || rightOnDragRelease) {
revealed = true
if leftOnDragRelease {
(delegate as? MaterialCollectionViewCellDelegate)?.materialCollectionViewCellWillRevealLeftLayer?(self)
} else if rightOnDragRelease {
(delegate as? MaterialCollectionViewCellDelegate)?.materialCollectionViewCellWillRevealRightLayer?(self)
}
}
if leftOnDragRelease {
(delegate as? MaterialCollectionViewCellDelegate)?.materialCollectionViewCellDidRevealLeftLayer?(self)
} else if rightOnDragRelease {
(delegate as? MaterialCollectionViewCellDelegate)?.materialCollectionViewCellDidRevealRightLayer?(self)
}
case .Ended: case .Ended:
revealed = false
// snap back // snap back
let a: CABasicAnimation = MaterialAnimation.position(CGPointMake(width / 2, y + height / 2), duration: 0.25) let a: CABasicAnimation = MaterialAnimation.position(CGPointMake(width / 2, y + height / 2), duration: 0.25)
a.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) a.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
animation(a) animation(a)
if rightOnDragRelease { if leftOnDragRelease {
(delegate as? MaterialCollectionViewCellDelegate)?.materialCollectionViewCellDidCloseLeftLayer?(self)
} else if leftOnDragRelease { } else if rightOnDragRelease {
(delegate as? MaterialCollectionViewCellDelegate)?.materialCollectionViewCellDidCloseRightLayer?(self)
} }
default: default:
break break
} }
......
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
import UIKit import UIKit
@objc(MaterialDelegate)
public protocol MaterialDelegate {}
@objc(MaterialLayer) @objc(MaterialLayer)
public class MaterialLayer : CAShapeLayer { public class MaterialLayer : CAShapeLayer {
/** /**
...@@ -244,7 +247,7 @@ public class MaterialLayer : CAShapeLayer { ...@@ -244,7 +247,7 @@ public class MaterialLayer : CAShapeLayer {
public func animation(animation: CAAnimation) { public func animation(animation: CAAnimation) {
animation.delegate = self animation.delegate = self
if let a: CABasicAnimation = animation as? CABasicAnimation { if let a: CABasicAnimation = animation as? CABasicAnimation {
a.fromValue = (nil == presentationLayer() ? self : presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!) a.fromValue = valueForKeyPath(a.keyPath!) //(nil == presentationLayer() ? self : presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
} }
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation { if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
addAnimation(a, forKey: a.keyPath!) addAnimation(a, forKey: a.keyPath!)
......
...@@ -28,7 +28,7 @@ public class MaterialView : UIView { ...@@ -28,7 +28,7 @@ public class MaterialView : UIView {
/** /**
:name: visualLayer :name: visualLayer
*/ */
public weak var delegate: MaterialAnimationDelegate? public weak var delegate: MaterialDelegate?
/** /**
:name: image :name: image
...@@ -347,7 +347,7 @@ public class MaterialView : UIView { ...@@ -347,7 +347,7 @@ public class MaterialView : UIView {
:name: animationDidStart :name: animationDidStart
*/ */
public override func animationDidStart(anim: CAAnimation) { public override func animationDidStart(anim: CAAnimation) {
delegate?.materialAnimationDidStart?(anim) (delegate as? MaterialAnimationDelegate)?.materialAnimationDidStart?(anim)
} }
/** /**
...@@ -360,7 +360,7 @@ public class MaterialView : UIView { ...@@ -360,7 +360,7 @@ public class MaterialView : UIView {
self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!) self.layer.setValue(nil == b.toValue ? b.byValue : b.toValue, forKey: b.keyPath!)
}) })
} }
delegate?.materialAnimationDidStop?(anim, finished: flag) (delegate as? MaterialAnimationDelegate)?.materialAnimationDidStop?(anim, finished: flag)
layer.removeAnimationForKey(a.keyPath!) layer.removeAnimationForKey(a.keyPath!)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup { } else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! { for x in a.animations! {
......
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