我的应用中有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)
}
要获取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
好的,现在是时候增加或减少动画的宽度和高度了。我将此代码放在animation
块中:
self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton-100,
originHeightbutton)
self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton+100,
originHeightbutton)
动画代码如下所示:
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)
让我们运行应用程序。哦哦!按钮(也称为buttonPlay)不会以任何方式变大或变小。虽然它从右向左移动。但是嘿!我没有触及x和y位置代码。
是什么导致的?
答案 0 :(得分:5)
一起使用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
})
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
})
答案 1 :(得分:1)
添加单独的动画以增加和减少按钮大小。请尝试使用此代码。
这将减少动画的按钮大小。
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)
}, completion: nil)
这会再次将尺寸增加到原始按钮。
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)
}, completion: nil)
答案 2 :(得分:1)
我在Swift 3
的解决方案:
func animate(sender: UIButton) {
let buttonSize: CGRect = sender.frame
let originXbutton = buttonSize.origin.x
let originYbutton = buttonSize.origin.y
let originWidthbutton = buttonSize.size.width
let originHeightbutton = buttonSize.size.height
UIView.animate(withDuration: 0.2, animations: {
sender.frame = CGRect(x: originXbutton, y: originYbutton - 5, width: originWidthbutton + 3, height: originHeightbutton + 3)
}, completion: { finished in
sender.frame = CGRect(x: originXbutton, y: originYbutton, width: originWidthbutton, height: originHeightbutton)
})
}