简体   繁体   English

当点击UIButton时,你如何使它变得更大,然后使用Swift 3回到之前/正常大小?

[英]When a UIButton is tapped, how do you made it get bigger, then go back to the previous/normal size using Swift 3?

This is my current code: 这是我目前的代码:

@IBAction func buttonTap(_ sender: Any) {
    if time >= 0 && gameStarted == true {
        score = score + 1
    }
}

Along with adding to the score, how do I make the button get bigger and then go back to regular size using Swift 3? 除了添加分数,如何使按钮变大,然后使用Swift 3恢复到常规尺寸?

If you don't have one already, create an outlet for your button so you can access it: 如果您还没有,请为按钮创建一个插座,以便您可以访问它:

@IBOutlet weak var button: UIButton!

Here is a basic animation that will increase the button to twice its original size, and then put it back again. 这是一个基本动画,将按钮增加到原始大小的两倍,然后再将其放回原处。 Put this code in your buttonTap function. 将此代码放在buttonTap函数中。

Note I set the background color to white, then dark gray, then white again, so that is easier to see what is happening. 注意我将背景颜色设置为白色,然后设置为深灰色,然后再设置为白色,以便更容易看到发生的情况。 This is not necessary. 这不是必需的。 You can adjust the speed and effects to suit your needs. 您可以根据需要调整速度和效果。

self.button.backgroundColor = UIColor.white

// save the original size and location of the button so we can restore it later    
let originalFrame = self.button.frame

// save the background color, too
let originalBackgroundColor = self.button.backgroundColor

UIView.animate(withDuration: 1.0, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {

    // make the button grow to twice its original size
    self.button.frame.size.height = self.button.frame.size.height * 2
    self.button.frame.size.width = self.button.frame.size.width * 2

    // adjust the button's x,y coordinates so it appears to stay in place
    self.button.frame.origin.x = self.button.frame.origin.x - (self.button.frame.size.width / 4)
    self.button.frame.origin.y = self.button.frame.origin.y - (self.button.frame.size.height / 4)

    // change the background color so it's easier to see what's happening - not required
    self.button.backgroundColor = UIColor.darkGray

}) { (finished: Bool) in
    // after the previous animation finishes, restore the button to its original state
    UIView.animate(withDuration: 1.0, animations: {
        self.button.frame = originalFrame
        self.button.backgroundColor = originalBackgroundColor
    })    
}

EDIT: @Tim suggests a better way to sequence animations using animateKeyframes . 编辑:@Tim建议使用animateKeyframes更好地对动画进行animateKeyframes Here is how you would do that, replacing the call to UIView.animate (the effect is the same): 以下是如何做到这一点,将调用替换为UIView.animate (效果相同):

UIView.animateKeyframes(withDuration: 2.0, delay: 0, options: UIViewKeyframeAnimationOptions.calculationModeLinear, animations: {

    // make the button grow and become dark gray
    UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5) {
        self.button.frame.size.height = self.button.frame.size.height * 2
        self.button.frame.size.width = self.button.frame.size.width * 2

        self.button.frame.origin.x = self.button.frame.origin.x - (self.button.frame.size.width / 4)
        self.button.frame.origin.y = self.button.frame.origin.y - (self.button.frame.size.height / 4)

        self.button.backgroundColor = UIColor.darkGray
    }

    // restore the button to original size and color
    UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
        self.button.frame = originalFrame
        self.button.backgroundColor = originalBackgroundColor
    }
}, completion: nil)

If you just want to make your button bigger and then to normal stage, when it is tapped, it would be relatively less complex and easy using CGAffineTransform. 如果你只是想让你的按钮变大,然后再进入正常阶段,那么当它被轻敲时,使用CGAffineTransform就会相对简单和容易。 It can be used to rotate, scale and translate UIView objects. 它可用于旋转,缩放和翻译UIView对象。 Every UIView subclass have transform property which can be used to scale, rotate or translate its position. 每个UIView子类都有transform属性,可用于缩放,旋转或平移其位置。 This is known as affine transform. 这被称为仿射变换。

@IBAction func buttonTap(_ sender: Any) {
    if self.button.transform == CGAffineTransform.identity {
            UIView.animate(withDuration: 1, animations: {
                  self.bottom.transform = CGAffineTransform(scaleX: 2, y: 2)
            }) { (true) in
                UIView.animate(withDuration: 0.5, animations: {

                })
            }
        } else {
            UIView.animate(withDuration: 1, animations: {
                self.button.transform = .identity
            })
        }
}

When the button is clicked it gets 2 times bigger than its size. 单击按钮时,它的大小比其大2倍。 And returns back to normal position for second time click. 并返回正常位置进行第二次点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift - 轻触UIButton时更改文本的颜色和大小 - Swift - Change color and size of text when UIButton is tapped 快速点击时如何更改UIButton的背景颜色? - How to change the background color of a UIButton when tapped in swift? Swift:如何在点击 UIButton 时打开新应用 - Swift: How to open a new app when UIButton is tapped 如何在 Swift 3 中突出显示(或点击)按钮时更改 UIButton 边框颜色? - How to change UIButton border color when button is highlighted (or tapped on) in Swift 3? 在Swift 4中使用NSMutableParagraphStyle和paragraphSpacingBefore时如何获得正确的插入符号大小和位置 - How do you get correct caret size AND position when using NSMutableParagraphStyle and paragraphSpacingBefore in Swift 4 轻按时UIButton的“缩小”图像大小 - 'Shrink' image size of UIButton when tapped Swift - 如何在点击按钮时测试标签是否已更新 - Swift - How do you test whether a label has been updated when a button is tapped 当触发UIButton时,如何获得touchesBegan坐标? - How do you get the touchesBegan coordinates when a UIButton is triggered? 如何设置UIButton转到上一个屏幕? - How do I set UIButton to go to the previous screen? 如何在Swift中按下按钮时防止uibutton alpha变为零? - How to prevent uibutton alpha from going to zero when button tapped in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM