XCode等待动画完成之前完成下一个任务



我有一个层,当我向上滑动时移动,然后在我滑动时相同的一层移动。

当我向上滑动时,我不希望用户滑动以激活该动画,直到刷新动画完成并且反之亦然。

我该如何完成?我尝试使用" IseNabled"但没有骰子来禁用滑动手势。其他一些类似的问题也有很久以前的答案,语法却大不相同。我正在使用Xcode的最新版本。

可以通过添加boolean变量来解决此问题,如果语句以及discatchqueue函数,以防止其他动画发生直到当前动画完成。

import UIKit
var animation_active = false
let animation_duration = 2 //For easy maintenance
func swipe_down() {
    if animation_active == false {
        animation_active == true
        //Animation code goes here. Variable 'animation_active' should be used when performing the animation for easy matinence
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(animation_duration), execute: {
            animation_active = false
        })
    }
}
 
func swipe_up() { //This is exactly the same as swipe_up. Yet it will prevent the swipe animation from occuring when the swipe down animation is occuruing thanks to the variable 'animation_active'
    if animation_active == false {
        animation_active == true
        //Animation code goes here. Variable 'animation_active' should be used when performing the animation for easy matinence
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(animation_duration), execute: {
            animation_active = false
        })
    }
}

相关内容

最新更新