2015-12-13 41 views
-1

我正在使用下面的代碼。如何在完成動畫效果之前禁用按鈕?在那種情況下,當我在完成動畫之前點擊按鈕而不是動畫圖像重新啓動並且它不是非常流暢。如何在完成動畫之前禁用按鈕

@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()  
} 

回答

0

UIImageView圖像動畫沒有「完成處理程序」。它比這更簡單。

如果您想要完成處理程序,請使用某種具有完成處理程序的動畫。 (你的情況,我不明白爲什麼你甚至需要一個完成處理程序,你知道什麼時候這個動畫會結束:從現在起第二秒,所以只需設置一個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動畫開始

+0

感謝您的意見Mr.Abida,但我想更簡單的代碼..有沒有辦法更簡單的代碼?我不想使用NSTimer ..; - > – Kyu

+0

相信我,使用NSTimer是最簡單的方法,如果你想使用完成處理程序(在動畫完成時得到通知,你必須使用另一種動畫) –

+0

In這個時代,我會選擇'dispatch_after()'而不是'NSTimer' ...(我的意見) –

0

之前,我想你想禁用按鈕從用戶的觸摸動畫它,然後它使用戶的觸摸。爲此,請切換userInteractionEnabled屬性。

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

我不明白那個代碼; -0我是首發赦免..我想在動畫過程中禁用按鈕,我插入代碼不工作; -0 – Kyu

+0

你如何創建按鈕? –

+0

'@IBAction func btn_LeeStatue(sender:AnyObject)'我在** Main.stroryboard **中創建了按鈕,並將按鈕圖像拖到** ViewController.swift **上,並按下了控制鍵 – Kyu

相關問題