当我向上滑动时,我有一个向上移动的层,然后当我向下滑动时,同一层向下移动。
当我向上滑动时,我不希望用户能够向下滑动以激活该动画,直到向上滑动动画完成,反之亦然。
我该如何做到这一点?我尝试使用“isEnabled”禁用滑动手势,但没有骰子。其他一些类似问题的答案来自很久以前,语法也非常不同。我正在使用最新版本的xcode。
我对xcode比较新,所以请原谅我对此缺乏信息。如果您有任何疑问,请告诉我。
谢谢!
这个问题可以通过添加布尔变量,IF语句和DispatchQueue函数来解决,以防止在当前动画完成之前发生另一个动画。
如果这有帮助,请参见下文并请求upvote :)
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
})
}
}