Brak opisu

FFAlertController.swift 4.1KB

    // // FFAlertController.swift // PaiAi // // Created by zhengjianfei on 2017/3/9. // Copyright © 2017年 FFIB. All rights reserved. // import UIKit enum FFAlertStyle { case bottom case middle } protocol AlertAnimation: CAAnimationDelegate, UIGestureRecognizerDelegate { var animationStyle: FFAlertStyle {set get} func showShiftAnimatied(animationView : UIView) func showAnimated(animationView : UIView) func tapAction(tap: UIGestureRecognizer) func showBottomAnimated(animationView : UIView) func dismissBottomAnimated(animationView : UIView) func dismissAnimated(animationView : UIView) } var animationStyleKey = "animationStyleKey" extension UIViewController: AlertAnimation { var animationStyle: FFAlertStyle { get { return associatedObject(key: &animationStyleKey, initialiser: { () -> FFAlertStyle in return FFAlertStyle.middle }) } set { associateObject(key: &animationStyleKey, value: newValue) } } func showShiftAnimatied(animationView : UIView) { let animation = CATransition() animation.duration = 0.2 animation.subtype = kCATransitionFromTop animation.type = kCATransitionMoveIn let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(tap:))) tap.delegate = self view.addGestureRecognizer(tap) animationView.layer.add(animation, forKey: nil) } func showAnimated(animationView : UIView) { animationStyle = .middle let animation = CAKeyframeAnimation(keyPath: "transform") animation.duration = 0.2 let array = [NSValue(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)), NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0))] animation.values = array as [AnyObject] let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(tap:))) tap.delegate = self view.addGestureRecognizer(tap) animationView.layer.add(animation, forKey: nil) } @objc func tapAction(tap: UIGestureRecognizer) { switch animationStyle { case .middle: dismissAnimated(animationView: tap.view?.subviews.first ?? UIView()) default: dismissBottomAnimated(animationView: tap.view?.subviews.first ?? UIView()) } } func showBottomAnimated(animationView : UIView) { animationStyle = .bottom showShiftAnimatied(animationView: animationView) } func dismissBottomAnimated(animationView : UIView) { let position = animationView.layer.position let positions = CGPoint(x: kScreenWidth / 2, y: kScreenHeight + view.height) let basicPosition = CABasicAnimation(keyPath: "position") basicPosition.delegate = self basicPosition.fromValue = NSValue(cgPoint: position) basicPosition.toValue = NSValue(cgPoint: positions) basicPosition.duration = 0.2 basicPosition.fillMode = kCAFillModeForwards basicPosition.isRemovedOnCompletion = false animationView.layer.add(basicPosition, forKey: nil) } func dismissAnimated(animationView : UIView) { let animation = CAKeyframeAnimation(keyPath: "transform") animation.duration = 0.4 let array = [NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)), NSValue(caTransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)), NSValue(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0))] animation.values = array as [AnyObject] animation.delegate = self animation.isRemovedOnCompletion = false animation.fillMode = kCAFillModeForwards animationView.layer.add(animation, forKey: nil) } public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { if (touch.view?.superview?.isKind(of: UITableViewCell.classForCoder()))! { return false } return true } public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { self.removeControllerAndViewFromParent() } }