主要用到两个方法:
/**
* 添加关键帧方法
*
* @param duration 动画时长
* @param delay 动画延迟
* @param options 动画效果选项
* @param animations 动画执行代码
* @param completion 动画结束执行代码
*/
class func animateKeyframes(withDuration
duration: TimeInterval,
delay: TimeInterval,
options: UIViewKeyframeAnimationOptions = [],
animations: @escaping () -> Swift.Void,
completion: (@escaping (Bool) -> Swift.Void)? = nil)
/**
* 添加关键帧
*
* @param frameStartTime 动画相对开始时间,取值为0-1.0
* @param frameDuration 动画相对持续时间,取值为0-1.0
* @param animations 动画执行代码
*/
class func addKeyframe(withRelativeStartTime
frameStartTime: Double,
relativeDuration frameDuration: Double,
animations: @escaping () -> Swift.Void)
例子
func Animation(){
let animationView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
animationView.backgroundColor = UIColor.green
self.view.addSubview(animationView)
UIView.animateKeyframes(withDuration: 3, delay: 0, options: .calculationModeLinear, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration:0.2) {
animationView.frame.origin.y = 500
}
UIView.addKeyframe(withRelativeStartTime: 0.2, relativeDuration: 0.5) {
animationView.frame.origin.x = 300
}
UIView.addKeyframe(withRelativeStartTime: 0.7, relativeDuration: 0.3) {
animationView.frame.origin.y = 300
}
}) { (finished) in
//动画完成
}
}
翻转和翻页动画
主要方法
/**
*
* 为view设置翻转货翻页动画
* @param transition 设置动画类型
* @param for view 执行动画的view
* @param cache
*/
class func setAnimationTransition(_ transition: UIViewAnimationTransition,
for view: UIView,
cache: Bool)
设置动画类型
public enum UIViewAnimationTransition : Int {
case none
case flipFromLeft //从左往右翻转
case flipFromRight //从右往左翻转
case curlUp //从上往下翻转
case curlDown //从下往上翻转
}
例子:
func Animation6(){
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIView.setAnimationCurve(.easeInOut)
//从左向右翻转
UIView.setAnimationTransition(UIViewAnimationTransition.flipFromLeft, for: animationView, cache: false)//从左向右翻转
UIView.commitAnimations()
}
旋转,缩放,平移,取反
实现方法为设置View 的transform属性
创建一个CGAffineTransform实例
//向右向下平移是个单位,中心点位置改变
let transform = CGAffineTransform(translationX: 10, y: 10)
//旋转PI /4 个角度,中心点位置不变
let transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_4))
//宽缩放为原来的2倍,高缩放为原来的3倍,中心点位置不变
let transform = CGAffineTransform(scaleX: 2, y: 3)
//取反是吧CGAffineTransform的值取反,不是恢复到原来的状态
//例如:顺时针旋转90度取反就是逆时针旋转90度。
let transform = CGAffineTransform.inverted(animationView.transform)
例子:
//平移
func Animation11(){
let transform = CGAffineTransform(translationX: 10, y: 10)
UIView.animate(withDuration: 2, animations: {
self.animationView.transform = transform
}) { (finished) in
print(NSStringFromCGRect(self.animationView.frame))
}
}
在主线程中调用Animation()方法即可查看效果。
本文参考文章:
http://www.tuicool.com/articles/BjMrQne
http://blog.csdn.net/mad2man/article/details/17554621