我该如何增加或减少按钮的大小斯威夫特? [英] How do I increase or decrease the size of a button in Swift?
问题描述
我有一个的UIButton
在我的应用程序,我称之为buttonPlay。我使用自动版式但按钮没有任何连接到它 NSLayoutConstraints
。
我想code动画增加和减少的高度和按钮的宽度。这里的动画code的样子:
//重复因为动画必须重演
UIView.animateWithDuration(1.0,延迟:0.6,选择:UIViewAnimationOptions.Repeat,动画:{
//动画code到这里
//零:动画不应该结束
}完成:无)
}
要得到的x位置,y位置,宽度和高度在初始值我用下面的code:
VAR frmPlay:=的CGRect self.buttonPlay.frame//获得当前x和y位置
让originXbutton = frmPlay.origin.x
让originYbutton = frmPlay.origin.y//获取按钮的当前的宽度和高度
让originWidthbutton = frmPlay.size.width
让originHeightbutton = frmPlay.size.height//帧(frmPlay)设置为按钮帧
self.buttonPlay.frame = frmPlay
好,它现在是时间,以增加或减少的宽度和动画的高度。我会把这个code中的<$ C C>动画里面块:
self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton-100,
originHeightbutton) self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton +100,
originHeightbutton)
动画code会是这个样子:
UIView.animateWithDuration(1.0,延迟:0.6,选择:UIViewAnimationOptions.Repeat,动画:{ 的println(动画功能开始animateStuff()!) VAR frmPlay:=的CGRect self.buttonPlay.frame 让originXbutton = frmPlay.origin.x
让originYbutton = frmPlay.origin.y 让originWidthbutton = frmPlay.size.width
让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) }完成:无)
让我们来运行应用程序。嗯哦!按钮(也称为buttonPlay)的没有得到以任何方式扩大或缩小即可。虽然从右侧移到左侧。但是,嘿!我没碰过的x和y的位置code。
什么原因呢?
使用 UIViewAnimationOptions.Repeat
和 UIViewAnimationOptions.Autoreverse
一起做。你只需要设置动画块中一帧的变化。你不能有一个以上的状态变化到一个动画块内的同一属性。
UIView.animateWithDuration(1.0,延迟:0.6,选择:
UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.AllowUserInteraction,动画:{ 的println(动画功能开始animateStuff()!) VAR frmPlay:=的CGRect self.buttonPlay.frame 让originXbutton = frmPlay.origin.x
让originYbutton = frmPlay.origin.y 让originWidthbutton = frmPlay.size.width
让originHeightbutton = frmPlay.size.height self.buttonPlay.frame = frmPlay self.buttonPlay.frame = CGRectMake(
originXbutton,
originYbutton,
originWidthbutton + 100,
originHeightbutton) }完成:{完成了})
UIViewAnimationOptionRepeat:无限期地重复动画
。
UIViewAnimationOptionAutoreverse:倒着跑的动画和
前锋。必须与UIViewAnimationOptionRepeat结合
选项。
块引用>如果你想上下缩放按钮,
UIView.animateWithDuration(1.0,延迟:0.6,选择:
UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.AllowUserInteraction,动画:{ self.buttonPlay.transform = CGAffineTransformMakeScale(1.5,1.5)
}完成:{完成了})I have a
UIButton
in my app that I call "buttonPlay". I am using AutoLayout but the button doesn't have anyNSLayoutConstraints
attached to it.I would like to code an animation to increase and the decrease the height and the width of the button. Here's how the animation code looks like:
// "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) }
To get the original values of the x position, y position, width and height I am using the following code:
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
Okay, it's now time to increase or decrease the width and the height of the animation. I'll put this code inside the
animation
block:self.buttonPlay.frame = CGRectMake( originXbutton, originYbutton, originWidthbutton-100, originHeightbutton) self.buttonPlay.frame = CGRectMake( originXbutton, originYbutton, originWidthbutton+100, originHeightbutton)
The animation code will look something like this:
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)
Let's run the app. Uh oh! The button (also called buttonPlay) doesn't get bigger or smaller in any way. Though it moves from right to left. But hey! I haven't touched the x and the y positions code.
What causes it?
解决方案use
UIViewAnimationOptions.Repeat
andUIViewAnimationOptions.Autoreverse
together to do that. You just have to set one frame change inside the animation block. You cannot have more than one state change to the same property inside an animation block.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 : Repeat the animation indefinitely.
UIViewAnimationOptionAutoreverse : Run the animation backwards and forwards. Must be combined with the UIViewAnimationOptionRepeat option.
If you want to scale the button up and down,
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 })
这篇关于我该如何增加或减少按钮的大小斯威夫特?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!