IOS swift中的动画的实例详解
程序员文章站
2023-12-18 15:30:22
ios swift中的动画的实例详解
uiview的通用动画
let view = uiview(frame: cgrectmake(10.0, 10.0, 1...
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)
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!