gpt4 book ai didi

ios - 当一个 UIButton 被点击时,你如何让它变大,然后使用 Swift 3 回到以前/正常的大小?

转载 作者:行者123 更新时间:2023-12-01 18:41:34 25 4
gpt4 key购买 nike

这是我当前的代码:

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

除了增加分数,我如何让按钮变大,然后使用 Swift 3 恢复到正常大小?

最佳答案

如果您还没有,请为您的按钮创建一个 socket ,以便您可以访问它:

@IBOutlet weak var button: UIButton!

这是一个基本动画,它将按钮增加到其原始大小的两倍,然后将其放回原处。将此代码放入您的 buttonTap功能。

注意我将背景颜色设置为白色,然后是深灰色,然后又是白色,这样更容易看到发生了什么。这不是必需的。您可以根据需要调整速度和效果。
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
})
}

编辑:@Tim 建议使用 animateKeyframes 对动画进行排序的更好方法.以下是您将如何执行此操作,将调用替换为 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)

关于ios - 当一个 UIButton 被点击时,你如何让它变大,然后使用 Swift 3 回到以前/正常的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42309714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com