How do I increase or decrease the size of a button in Swift?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 18:13:19

use UIViewAnimationOptions.Repeat and UIViewAnimationOptions.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

})

Put separate animations for increasing and decreasing button size .Try this code .

This will decrease the button size with 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)
    }, completion: nil)

This will increase the size again to original 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)

My solution in 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)
    })
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!