swift之動畫的使用
2.UIView的動畫;
let v1:UIView=UIView.init()
let v2:UIView=UIView.init()
UIView.transition(from: v1, to: v2, duration: 3, options: UIViewAnimationOptions.allowAnimatedContent) { (ist) in
}
UIView.animate(withDuration: 3) {
}
UIView.animateKeyframes(withDuration: 3, delay: 0, options: UIViewKeyframeAnimationOptions
}) { (ist) in
}
UIView.animate(withDuration: 3, delay: 0, options: UIViewAnimationOptions.allowAnimatedContent, animations: {
}, completion: { (ist) in
})
===========================3.UIImageview動畫;
===============================
4.核心動畫繼承自CAAnimation,包括和CAPropertyAnimation(屬性動畫)和CAAnimationGroup(組動畫)和CATransition(轉場動畫)
CAPropertyAnimation包括:CAKeyframeAnimation(幀動畫)和CABasicAnimation;
CABasicAnimation包含CASpringAnimation
*****************CABasicAnimation
import UIKit
class RecordManager : LYBBaseVC,CAAnimationDelegate{
override func viewDidLoad() {
super.viewDidLoad()
//手工建立layer
let redLayer:CALayer=CALayer.init
//設定背景顏色
redLayer.backgroundColor = UIColor.red.cgColor
//設定大小和位置
redLayer.position = CGPoint.init(x: 100, y: 100)
redLayer.bounds = CGRect.init(x: 0, y: 0, width: 100, height: 100)
view.layer.addSublayer(redLayer)
//基本動畫--旋轉
//1.建立動畫物件
let basic:CABasicAnimation=CABasicAnimation.init()
//2.設定屬性 (預設繞著z軸旋轉)
// fromValue--------------起始值;
// toValue----------------移動到多少;
// byValue---------------- 移動了多少
basic.keyPath = "transform.rotation";
//設定開始、結束值
basic.fromValue=0.1
basic.toValue = 0.5
//**縮放
basic.keyPath="transform.scale"
basic.byValue=0.65
//**
basic.keyPath="position"//或者 basic.keyPath="transform.translation"
basic.fromValue=NSValue.init(cgPoint: CGPoint.init(x: 0, y: 10))
basic.toValue=NSValue.init(cgPoint: CGPoint.init(x: 10, y: 10))
//2.2 設定動畫時間
basic.duration = 2
basic.repeatCount=5//重複5次
basic.repeatDuration=1//
//設定代理(隱式代理)
basic.delegate=self
//設定動畫結束後保持當前狀態
basic.isRemovedOnCompletion=true
basic.fillMode = kCAFillModeForwards
//3.新增到要作用的layer上
redLayer.add(basic, forKey: nil)
}
//動畫開始的代理方法
func animationDidStart(_ anim: CAAnimation) {
}
//動畫結束的代理方法
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
}
}
********************CAKeyframeAnimation關鍵幀動畫import UIKit
class RecordManager : LYBBaseVC,CAAnimationDelegate{
override func viewDidLoad() {
super.viewDidLoad()
//手工建立layer
let redLayer:CALayer=CALayer.init()
//設定背景顏色
redLayer.backgroundColor = UIColor.red.cgColor
//設定大小和位置
redLayer.position = CGPoint.init(x: 100, y: 100)
redLayer.bounds = CGRect.init(x: 0, y: 0, width: 100, height: 100)
view.layer.addSublayer(redLayer)
//基本動畫--旋轉
//1.建立動畫物件
let basic:CAKeyframeAnimation=CAKeyframeAnimation.init()
//2.設定屬性 (預設繞著z軸旋轉)
// fromValue--------------起始值;
// toValue----------------移動到多少;
// byValue---------------- 移動了多少
basic.keyPath = "transform.rotation";
//**縮放
basic.keyPath="transform.scale"
//**
basic.keyPath="position"//或者
basic.values=[Double.pi/4,Double.pi/2,Double.pi/3]
//建立路徑物件(如果設定了path,value就被忽略了,path只對anchorpoint和position起作用)
let path:UIBezierPath=UIBezierPath.init(ovalIn: CGRect.init(x: 20, y: 100, width: 300, height: 200))
basic.path = path.CGPath;
//速度控制函式,控制動畫的節奏
basic.timingFunction=CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
//2.2 設定動畫時間
basic.duration = 2
basic.repeatCount=5//重複5次
basic.repeatDuration=1//
//設定代理(隱式代理)
basic.delegate=self
//設定動畫結束後保持當前狀態
basic.isRemovedOnCompletion=true
basic.fillMode = kCAFillModeForwards
//3.新增到要作用的layer上
redLayer.add(basic, forKey: nil)
}
//動畫開始的代理方法
func animationDidStart(_ anim: CAAnimation) {
}
//動畫結束的代理方法
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
}
}
************CAAnimationGroup組動畫
//建立組動畫
let animationGroup:CAAnimationGroup=CAAnimationGroup.init()
//3.把子動畫新增到組動畫中
animationGroup.animations = [basicAni,rotationAni,keyframeAni];
//設定組動畫時間
animationGroup.duration = 5;
animationGroup.autoreverses =true;//動畫完成後自動重新開始,預設為NO
//4.把組動畫新增到要作用的layer上
redLayer.add(animationGroup, forKey: nil)
*************
=============
5.CATransaction(隱式動畫)
非rootLayer,即手動建立的layer都包含隱式動畫;(position,bounds,backgroundColor,)
CATransaction.begin()//開始動畫
CATransaction.setAnimationDuration(CFTimeInterval.init(3))//設定動畫時間
CATransaction.setDisableActions(true)//不顯示動畫
//改變layer的屬性,屬性的改變必須放在提交動畫之前
CATransaction.commit()//提交動畫
==============
6.CATransition(轉場動畫)
let changeAnimation:CATransition=CATransition.init()
changeAnimation.delegate = self
changeAnimation.duration = 0.45
// CATransition 動畫主要在過渡時使用,比如兩個頁面層級改變的時候新增一個轉場效果。CATransition 分為兩類,一類是公開的動畫效果,一類是非公開的動畫效果。
/*轉場動畫的屬性
fade---交叉淡化過渡
push---新檢視把舊檢視推出
moveIn----新檢視移動到舊檢視上面
reveal----把舊檢視移開,顯示下面的新檢視
cube---立體翻滾效果
oglFlip---上下左右翻轉
1,公開動畫效果:
kCATransitionFade:翻頁
kCATransitionMoveIn:彈出
kCATransitionPush:推出
kCATransitionReveal:移除
2,非公開動畫效果:
"cube":立方體
"suckEffect":收縮效果,如布塊一樣被抽走
"oglFlip":翻轉
"rippleEffect":波紋
"pageCurl":卷頁、翻頁
"cameraIrisHollowOpen":鏡頭開
"cameraIrisHollowClose":鏡頭關
*/
changeAnimation.type = "oglFlip"//專場動畫的屬性
changeAnimation.subtype = kCATransitionFromRight//控制方向
changeAnimation.timingFunction = UIViewAnimationCurveEaseInOut
view.layer.addSublayer(changeAnimation)
==============imageview動畫=============
let ar:[String]=["dw","xw","h01","h02","h03","h04","h05","h06","h07","h08","h09","h10","h11","h12","h13","ho01","ho02","ho03","ho04","ho05","ho06","ho07","ho08","ho09","ho10","ho11","ho12","ho13","f01","f02","f03","f04","f05","f06","f07","f08","f09","f10","f11","f12","f13","m01","m02","m03","m04","m05","m06","m07","m08","m09","m10","m11","m12","m13"]
var imgAr=[UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<54{
let im:UIImage=UIImage.init(named: ar[i])!
imgAr.append(im)
}
let imagv:UIImageView=UIImageView.init(frame: CGRect.init(x: 50, y: 0, width: 50, height: 70))
view.addSubview(imagv)
imagv.animationImages=imgAr
//設定動畫
imagv.animationDuration=20//動畫時長
imagv.animationRepeatCount=1//重複播放次數
imagv.startAnimating()//開始動畫
// imagv.stopAnimating()//停止動畫
// let isA = imagv.isAnimating//是否正在動畫
}