如何增大或减小 Swift 中按钮的大小?

How do I increase or decrease the size of a button in 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)
}

要获取 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 位置代码。

是什么原因造成的?

同时使用 UIViewAnimationOptions.RepeatUIViewAnimationOptions.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 : Repeat the animation indefinitely.

UIViewAnimationOptionAutoreverse : Run the animation backwards and forwards. Must be combined with the UIViewAnimationOptionRepeat option.

如果要上下缩放按钮,

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

})

为增大和减小按钮大小放置单独的动画。试试这个代码。

这将通过动画减小按钮大小。

    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)

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