gpt4 book ai didi

swift - 如何使用同一个 UIButton 在不同时间执行多个不同的动画?

转载 作者:行者123 更新时间:2023-11-30 10:37:34 24 4
gpt4 key购买 nike

我正在创建一个动画序列,当按下一个按钮时,一个对象就会淡出。当再次按下同一按钮时,另一个对象就会淡出。这种情况持续发生,直到 8 个对象淡出为止。我已经尝试过使用代码,到目前为止已经得到了代码来淡出一个对象,并且一次淡出两个或多个对象,但还没有成功地按照上面描述的那样做到这一点。这是我到目前为止的代码:

import UIKit

class DrinkWaterViewController: UIViewController {

@IBOutlet weak var glassOne: UIImageView!
@IBOutlet weak var glassTwo: UIImageView!
@IBOutlet weak var glassThree: UIImageView!
@IBOutlet weak var glassFour: UIImageView!
@IBOutlet weak var glassFive: UIImageView!
@IBOutlet weak var glassSix: UIImageView!
@IBOutlet weak var glassSeven: UIImageView!
@IBOutlet weak var glassEight: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

self.glassOne.alpha = 1.0
self.glassTwo.alpha = 1.0
}

@IBAction func drunkOne(_ sender: Any){

if self.glassOne.alpha == 1.0{

UIView.animate(withDuration: 1.5, delay: 0.2, animations:
{self.glassOne.alpha = CGFloat(0.1)}
)
}
}

最佳答案

将您的眼镜放入数组 glasses 中,并添加一个属性来跟踪 nextToFade。然后淡出 glasses[nextToFade] 并将 nextToFade 增加 1:

class DrinkWaterViewController: UIViewController {

@IBOutlet weak var glassOne: UIImageView!
@IBOutlet weak var glassTwo: UIImageView!
@IBOutlet weak var glassThree: UIImageView!
@IBOutlet weak var glassFour: UIImageView!
@IBOutlet weak var glassFive: UIImageView!
@IBOutlet weak var glassSix: UIImageView!
@IBOutlet weak var glassSeven: UIImageView!
@IBOutlet weak var glassEight: UIImageView!

// array to hold the glasses so that they can be indexed
var glasses = [UIImageView]()

// index of next glass to fade
var nextToFade = 0

override func viewDidLoad() {
super.viewDidLoad()

// put the glasses into the array
glasses = [glassOne, glassTwo, glassThree, glassFour,
glassFive, glassSix, glassSeven, glassEight]

// make sure they're all visible
glasses.forEach { $0.alpha = 1.0 }
}

@IBAction func drunkOne(_ sender: Any) {
// make sure not to index beyond the end of the array
guard nextToFade < glasses.count else { return }

let glass = glasses[nextToFade]
nextToFade += 1
UIView.animate(withDuration: 1.5, delay: 0.2, animations:
{ glass.alpha = 0.1 }
)
}
}

关于swift - 如何使用同一个 UIButton 在不同时间执行多个不同的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57595260/

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