2017-07-26 111 views
0

我有一個UIImageView與點擊手勢識別器。點擊時,我想讓UIImageView在2張圖片之間切換。我已經實現了這個代碼,但是當UIImageView被反覆快速切換時,UIImageView似乎滯後了。UIImageView圖像更改延遲時點擊

我的代碼:

@IBOutlet weak var moneyButton: UIImageView! 

let benchUpImage = UIImage(named: "benchUp") 
let benchDownImage = UIImage(named: "benchDown") 
var benchIsDown = false 

func moneyButtonPressed(sender: UITapGestureRecognizer){ 

    if benchIsDown == false{ 
     moneyButton.image = benchDownImage 
     benchIsDown = true 
    }else{ 
     moneyButton.image = benchUpImage 
     benchIsDown = false 
    } 

} 
+0

你是什麼意思「落後?」 - 什麼是快速點擊? – Appygix

+0

也許圖像太大? – GeneCode

+0

非常快速地重複點擊圖像視圖 –

回答

0

雨燕3.0

你可以簡單地允許用戶與一些延遲挖掘。考慮用下面的代碼替換你的代碼

var canUserTap = true 
let delayTime = 0.4 //setDelayTimming in seconds 
@IBAction func moneyButtonPressed(_ sender : UITapGestureRecognizer){ 
    if(canUserTap){ 
     if benchIsDown == false{ 
      moneyButton.image = benchDownImage 
      benchIsDown = true 
     }else{ 
      moneyButton.image = benchUpImage 
      benchIsDown = false 
     } 
     canUserTap = false 
     Timer.scheduledTimer(timeInterval: delayTime, 
          target:self, 
          selector:#selector(allowUserTap(_:)), 
          userInfo:nil, 
          repeats:false) 
    } 

} 

func allowUserTap(_ timer:Timer){ 
    canUserTap = true 

} 
0

我認爲你的代碼的問題是你不處理識別器的狀態。因此,多次敲擊可以迅速使圖像在每個狀態(開始,結束等)發生變化。嘗試將更改後的代碼置於「結束」狀態:

func moneyButtonPressed(sender: UITapGestureRecognizer){ 

    if sender.state == .Ended { 
    if benchIsDown == false{ 
     moneyButton.image = benchDownImage 
     benchIsDown = true 
    }else{ 
     moneyButton.image = benchUpImage 
     benchIsDown = false 
    } 
    } 

}