如何增加或减lessSwift中button的大小?

我在我的应用程序中有一个UIButton ,我称之为“buttonPlay”。 我正在使用AutoLayout,但该button没有任何附加的NSLayoutConstraints

我想编码animation来增加和减lessbutton的高度和宽度 。 以下是animation代码的外观:

  // "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 

好的,现在是时候增加或减lessanimation的宽度和高度。 我将把这段代码放在animation块中:

  self.buttonPlay.frame = CGRectMake( originXbutton, originYbutton, originWidthbutton-100, originHeightbutton) self.buttonPlay.frame = CGRectMake( originXbutton, originYbutton, originWidthbutton+100, originHeightbutton) 

animation代码如下所示:

  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) 

让我们运行该应用程序。 呃哦! button(也叫buttonPlay) 不会变大或变小 。 虽然它从右向左移动。 但是,嘿! 我没有触及x和y位置代码。

是什么造成的?

使用UIViewAnimationOptions.RepeatUIViewAnimationOptions.Autoreverse起来做到这一点。 您只需在animation块内设置一个帧更改。 animation块中的同一个属性不能有多个状态变化。

 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:无限地重复animation。

UIViewAnimationOptionAutoreverse:向前和向后运行animation。 必须与UIViewAnimationOptionRepeat选项结合使用。

如果你想上下调整button,

 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 }) 

我在Swift 3解决scheme:

 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) }) } 

在这里输入图像说明

把单独的animation放大和缩小button的大小。试试这个代码。

这将减less与animation的button大小。

  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) 

这将再次增加到原始button的大小。

  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)