如何在完成动画之前禁用按钮

时间:2015-12-13 21:28:48

标签: ios swift animation uiimage

我正在使用下面的代码。如何在完成动画效果之前禁用按钮?在那种情况下,当我完成动画之前点击按钮而不是动画图像重新开始并且它不是很平滑。

@IBAction func btn_LeeStatue(sender: AnyObject) {    
    lee_statue_img.animationImages = [
        UIImage(named: "lee_statueAni0001.png")!,
        UIImage(named: "lee_statueAni0002.png")!,
        UIImage(named: "lee_statueAni0003.png")!,
        UIImage(named: "lee_statueAni0004.png")!,
        UIImage(named: "lee_statueAni0005.png")!,
    ] 

    lee_statue_img.animationDuration = 3
    lee_statue_img.animationRepeatCount = 1
    lee_statue_img.startAnimating()     
}

2 个答案:

答案 0 :(得分:0)

UIImageView图像动画没有"完成处理程序"。它比那更简单。

如果你想要一个完成处理程序,请使用某种具有完成处理程序的动画。

(在你的情况下,我不明白为什么你甚至需要一个完成处理程序。你知道这个动画什么时候结束:从现在起X秒。所以只需设置一个NSTimer来给你回电。) 所以你的代码将是:

func showButton(timer : NSTimer) {
   YourButtonOutlet.enabled = true
   //this will enable you button again
}

@IBAction func btn_LeeStatue(sender: AnyObject) {    
lee_statue_img.animationImages = [
    UIImage(named: "lee_statueAni0001.png")!,
    UIImage(named: "lee_statueAni0002.png")!,
    UIImage(named: "lee_statueAni0003.png")!,
    UIImage(named: "lee_statueAni0004.png")!,
    UIImage(named: "lee_statueAni0005.png")!,
] 

//Disable the button
YourButtonOutlet.enabled= false
lee_statue_img.animationDuration = 3
lee_statue_img.animationRepeatCount = 1
lee_statue_img.startAnimating()
//set the timer for X seconds (4s in this example)  
let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("showButton:"), userInfo: nil, repeats: false)
}

你也可以在之后使用派遣, 只需添加此功能 - >

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
} 

你可以这样调用它 - >(0.75是应用程序在关闭中执行什么之前应该等待的时间)

delay(0.75, closure: {
    //write here the code to be executed after 0.75 second
    //in your case it will be YourButtonOutlet.enabled = true
})

PS:您必须在动画开始前设置YourButtonOutlet.enabled = false

答案 1 :(得分:0)

我认为您要禁用用户触摸动画的按钮,然后启用它以便用户触摸。为此,您切换userInteractionEnabled属性。

let button: UIButton = UIButton()
button.userInteractionEnabled = false
button.animateMethod()
button.userInteractionEnabled = true