|
//
// UIView+Alert.swift
// PaiAi
//
// Created by mac on 16/5/12.
// Copyright © 2016年 FFIB. All rights reserved.
//
import UIKit
public enum AnimationStyle {
case bottom
case middle
case left
}
extension UIView : CAAnimationDelegate, UIGestureRecognizerDelegate {
var animationStyle: AnimationStyle {
get {
return objc_getAssociatedObject(self, UnsafeRawPointer(bitPattern: "animationStyle".hashValue)) as! AnimationStyle
}
set {
objc_setAssociatedObject(self, UnsafeRawPointer(bitPattern: "animationStyle".hashValue), newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
}
func showShiftAnimatied() {
let animation = CATransition()
animation.duration = 0.2
animation.subtype = kCATransitionFromTop
animation.type = kCATransitionMoveIn
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissBottomAnimated))
tap.delegate = self
self.superview?.addGestureRecognizer(tap)
self.layer.add(animation, forKey: nil)
}
func showAnimated() {
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(dismissAnimated))
tap.delegate = self
self.superview?.addGestureRecognizer(tap)
self.layer.add(animation, forKey: nil)
}
func showBottomAnimated() {
animationStyle = .bottom
showShiftAnimatied()
}
func dismissBottomAnimated() {
let position = self.layer.position
let positions = CGPoint(x: kScreenWidth / 2, y: kScreenHeight + self.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
self.layer.add(basicPosition, forKey: nil)
}
func dismissAnimated() {
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
self.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.getCurrentViewController().removeControllerAndViewFromParent()
}
}
|