您可以通过类似于此的返回按钮,将动画转发并返回到先前的动画状态。
步骤1:声明两个整数类型变量
var tapCount = 0 //For forwarding your animations from first to third
var currentAnimation = 0 // For reversing the animation from current animation
步骤2:在您的IBAction函数中
@IBAction func nextAnimation(_ sender: Any)
{
if tapCount == 0
{
if currentAnimation == 1
{
Animation2()
}
else
{
Animation1()
}
tapCount += 1
}
else if tapCount == 1
{
if currentAnimation == 2
{
Animation3()
}
else
{
Animation2()
}
tapCount += 1
}
else if tapCount == 2
{
if currentAnimation == 3
{
Animation1()
}
else
{
Animation3()
}
tapCount = 0
}
}
步骤3:在您的函数中
func Animation1() {
currentAnimation = 1
print("First Animation")
}
func Animation2() {
currentAnimation = 2
print("Second Animation")
}
func Animation3() {
currentAnimation = 3
print("third Animation")
}
步骤4:最后,用于从当前状态反转动画
@IBAction func backAnimation(_ sender: Any)
{
if currentAnimation == 2
{
Animation1()
}
else if currentAnimation == 3
{
Animation2()
}
else
{
}
tapCount = 0
}
希望能有所帮助!!
对于
addTarget(_:action:for:) 方法,可以在不连接
IBAction 并在需要时在 viewWillAppear 或 viewDidLoad 中声明的情况下完成。它将把目标对象和操作方法与控件关联起来。
例如:
override func viewDidLoad() {
super.viewDidLoad()
nextButton.addTarget(self, action: #selector(self. nextAnimation), for: .touchUpInside)
backButton.addTarget(self, action: #selector(self. backAnimation), for: .touchUpInside)
}
@objc func nextAnimation(sender: UIButton) {
}
@objc func backAnimation(sender: UIButton) {
}