Ces*_*are 1 animation coordinates ios swift
我UIButton在我的应用程序中有一个叫做"buttonPlay"的应用程序.我正在使用AutoLayout但按钮没有任何NSLayoutConstraints附加到它.
我想编码动画来增加和减少按钮的高度和宽度.以下是动画代码的外观:
// "Repeat" because the animation must repeat itself
UIView.animateWithDuration(1.0, delay: 0.6, options: UIViewAnimationOptions.Repeat, animations: {
// Animation code goes here
// nil: the animation is not supposed to end
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
要获取x位置,y位置,宽度和高度的原始值,我使用以下代码:
var frmPlay : CGRect = self.buttonPlay.frame
// get the current x and y positions
let originXbutton = frmPlay.origin.x
let originYbutton = frmPlay.origin.y
// get the current width and height of the button
let originWidthbutton = frmPlay.size.width
let originHeightbutton = frmPlay.size.height
// set the frame (frmPlay) to the button frame
self.buttonPlay.frame = frmPlay
Run Code Online (Sandbox Code Playgroud)
好的,现在是时候增加或减少动画的宽度和高度了.我将把这段代码放在animation块中:
self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton-100,
originHeightbutton)
self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton+100,
originHeightbutton)
Run Code Online (Sandbox Code Playgroud)
动画代码看起来像这样:
UIView.animateWithDuration(1.0, delay: 0.6, options: UIViewAnimationOptions.Repeat, animations: {
println("Animation function animateStuff() started!")
var frmPlay : CGRect = self.buttonPlay.frame
let originXbutton = frmPlay.origin.x
let originYbutton = frmPlay.origin.y
let originWidthbutton = frmPlay.size.width
let originHeightbutton = frmPlay.size.height
self.buttonPlay.frame = frmPlay
self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton-100,
originHeightbutton)
self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton+100,
originHeightbutton)
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)
让我们运行应用程序.哦哦!按钮(也称为buttonPlay)不会以任何方式变大或变小.虽然它从右向左移动.但是嘿!我没有触及x和y位置代码.
是什么导致的?
使用UIViewAnimationOptions.Repeat并UIViewAnimationOptions.Autoreverse一起做到这一点.您只需在动画块内设置一个帧更改.您不能对动画块内的同一属性进行多次状态更改.
UIView.animateWithDuration(1.0, delay: 0.6, options:
UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.AllowUserInteraction, animations: {
println("Animation function animateStuff() started!")
var frmPlay : CGRect = self.buttonPlay.frame
let originXbutton = frmPlay.origin.x
let originYbutton = frmPlay.origin.y
let originWidthbutton = frmPlay.size.width
let originHeightbutton = frmPlay.size.height
self.buttonPlay.frame = frmPlay
self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton+100,
originHeightbutton)
}, completion: { finished in
})
Run Code Online (Sandbox Code Playgroud)
UIViewAnimationOptionRepeat:无限期重复动画.
UIViewAnimationOptionAutoreverse:向后和向前运行动画.必须与UIViewAnimationOptionRepeat选项结合使用.
如果要上下按下按钮,
UIView.animateWithDuration(1.0, delay: 0.6, options:
UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.AllowUserInteraction, animations: {
self.buttonPlay.transform = CGAffineTransformMakeScale(1.5, 1.5)
}, completion: { finished in
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10123 次 |
| 最近记录: |