您好,登錄后才能下訂單哦!
IOS swift中的動畫的實例詳解
UIView的通用動畫
let view = UIView(frame: CGRectMake(10.0, 10.0, 100.0, 40.0)) self.view.addSubview(view) view.backgroundColor = UIColor.lightGrayColor() // 位置改變 var frame = view.frame UIView.animateWithDuration(0.6, delay: 2.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in frame.origin.x = 200.0 view.frame = frame }) { (finished:Bool) -> Void in UIView.animateWithDuration(0.6) { () -> Void in frame.origin.x = 10.0 view.frame = frame } }
CABasicAnimation核心動畫
1、CABasicAnimation類只有三個屬性:
fromValue:開始值 toValue:結束值 Duration:動畫的時間 repeatCount:重復次數
2、通過animationWithKeyPath鍵值對的方式設置不同的動畫效果
transform.scale transform.scale.x transform.scale.y transform.rotation.z opacity margin zPosition backgroundColor cornerRadius borderWidth bounds contents contentsRect cornerRadius frame hidden mask masksToBounds opacity position shadowColor shadowOffset shadowOpacity shadowRadius
let view = UILabel(frame: CGRectMake((self.view.frame.size.width - 200.0) / 2, 10.0, 200.0, 40.0)) self.view.addSubview(view) view.text = "縮放/淡入淡出" view.textAlignment = .Center view.adjustsFontSizeToFitWidth = true view.backgroundColor = UIColor.lightGrayColor() // let layer = view.layer // 開始動畫 // 縮放 let scaleAnimate = CABasicAnimation(keyPath: "transform.scale") scaleAnimate.fromValue = 1.0 scaleAnimate.toValue = 1.5 scaleAnimate.autoreverses = true scaleAnimate.repeatCount = MAXFLOAT scaleAnimate.duration = 1.0 // 淡入淡出 let opaqueAnimate = CABasicAnimation(keyPath: "opacity") opaqueAnimate.fromValue = 0.1 opaqueAnimate.toValue = 1 opaqueAnimate.autoreverses = true opaqueAnimate.repeatCount = MAXFLOAT opaqueAnimate.duration = 1.0 layer.addAnimation(scaleAnimate, forKey: "scaleAnimate") layer.addAnimation(opaqueAnimate, forKey: "opacityAnimate")
// 組合動畫 let view3 = UILabel(frame: CGRectMake(10.0, (currentView.frame.origin.y + currentView.frame.size.height + 10.0), 120.0, 40.0)) self.view.addSubview(view3) view3.text = "組合動畫" view3.textAlignment = .Center view3.adjustsFontSizeToFitWidth = true view3.backgroundColor = UIColor.lightGrayColor() // let layer3 = view3.layer // CAAnimationGroup組合動畫效果 let rotate: CABasicAnimation = CABasicAnimation() rotate.keyPath = "tranform.rotation" rotate.toValue = M_PI let scale: CABasicAnimation = CABasicAnimation() scale.keyPath = "transform.scale" scale.toValue = 0.0 let move: CABasicAnimation = CABasicAnimation() move.keyPath = "transform.translation" move.toValue = NSValue(CGPoint: CGPoint(x: 217, y: 230)) let animationGroup:CAAnimationGroup = CAAnimationGroup() animationGroup.animations = [rotate, scale, move] animationGroup.duration = 2.0 animationGroup.fillMode = kCAFillModeForwards animationGroup.removedOnCompletion = false animationGroup.repeatCount = MAXFLOAT // layer3.addAnimation(animationGroup, forKey: nil)
CAKeyframeAnimation關鍵幀動畫
主要屬性:
keyPath : 要設置的屬性 path : 路徑 可用UIBezierPath(設置了path,將忽略values) duration : 動畫時長 repeatCount : 重復次數 calculationMode : 動畫計算方式 values:每一個關鍵幀(設置了path,將忽略values) removedOnCompletion:執行完之后不刪除動畫 fillMode:執行完之后保存最新的狀態 delegate:代理
let view = UILabel(frame: CGRectMake((self.view.frame.size.width - 200.0) / 2, 10.0, 200.0, 40.0)) self.view.addSubview(view) view.text = "CAKeyframeAnimation動畫" view.backgroundColor = UIColor.lightGrayColor() // let layer = view.layer // 位移 let keyAnimate = CAKeyframeAnimation(keyPath: "position") // 設定關鍵幀 let value0 = NSValue(CGPoint: layer.position) let value1 = NSValue(CGPoint: CGPointMake(layer.position.x, layer.position.y + 200)) let value2 = NSValue(CGPoint: CGPointMake(layer.position.x - 150, layer.position.y + 200)) let value3 = NSValue(CGPoint: CGPointMake(layer.position.x - 150, layer.position.y)) let value4 = NSValue(CGPoint: layer.position) // 速度曲線 let tf0 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) let tf1 = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) let tf2 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn) let tf3 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) keyAnimate.timingFunctions = [tf0, tf1, tf2, tf3] // 每段執行的時間 keyAnimate.keyTimes = [0.0, 0.5, 0.6, 0.7, 1] // keyAnimate.values = [value0, value1, value2, value3, value4] keyAnimate.autoreverses = false keyAnimate.repeatCount = 3 keyAnimate.duration = 6.0 // keyAnimate.delegate = self // layer.addAnimation(keyAnimate, forKey: "position")
// 代理方法 override func animationDidStart(anim: CAAnimation) { print("開始") } override func animationDidStop(anim: CAAnimation, finished flag: Bool) { print("結束") }
let view3 = UILabel(frame: CGRectMake(10.0, (currentView.frame.origin.y + currentView.frame.size.height + 10.0), 60.0, 60.0)) self.view.addSubview(view3) view3.text = "抖動" view3.backgroundColor = UIColor.lightGrayColor() // let layer3 = view3.layer // 抖動 let animation3 = CAKeyframeAnimation() animation3.keyPath = "transform.rotation" // (-M_PI_4 /90.0 * 5)表示-5度 。 let value31 = NSValue(CGPoint: CGPointMake(CGFloat(-M_PI_4 / 90.0 * 5.0), 0.0)) let value32 = NSValue(CGPoint: CGPointMake(CGFloat(M_PI_4 / 90.0 * 5.0), 0.0)) let value33 = NSValue(CGPoint: CGPointMake(CGFloat(-M_PI_4 / 90.0 * 5.0), 0.0)) animation3.values = [value31, value32, value33]; animation3.removedOnCompletion = false animation3.fillMode = kCAFillModeForwards animation3.duration = 0.2 animation3.repeatCount = MAXFLOAT // layer3.addAnimation(animation3, forKey: nil)
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。