2014-10-06 74 views
1

我有一個應用於它的輕擊手勢的視圖。當手指擡起時,我想讓視線「縮小」,並且讓視圖恢復正常。我試圖用UIGestureRecognizerState來達到這個目的,但它不起作用。只有當我移開手指並且不回去時,視圖纔會縮小。這裏是我的代碼:點擊手勢與UIGestureRecognizerState不起作用

@IBAction func shareButton(sender: AnyObject) { 

    if sender.state == UIGestureRecognizerState.Changed { 
     UIView.animateWithDuration(0.1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: nil, animations: { 
      self.shareButton.transform = CGAffineTransformMakeScale(0.9, 0.9) 
     }, completion: nil) 
    } else if sender.state == UIGestureRecognizerState.Ended { 
     UIView.animateWithDuration(0.1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: nil, animations: { 
      self.shareButton.transform = CGAffineTransformMakeScale(0.7, 0.7) 
     }, completion: nil) 
    } 
} 

回答

0

我想你可以嘗試添加兩個目標到你的視圖。針對UIControlEventTouchDown收縮動畫,另一個針對UIControlEventTouchUpInside或其他事件取決於應該發生的情況? Ref iOS docs

+0

不只是與按鈕圖像UIControlEventTouchDown? – mlevi 2014-10-06 05:25:48

1
var delaysTouchesEnded: Bool // default is YES. 

原因touchesEnded交付給後才這個手勢未能識別目標視圖事件。這確保瞭如果手勢被識別,則作爲手勢一部分的觸摸可以被取消。

因此,它將在下次調用動作,因爲只有在執行了點擊動作後纔會執行操作。 觸摸結束後,將執行操作方法。

但是,您可以使用touchesBegin和touchesEnded方法。 如果您正在使用輕擊手勢,則無法使用,因爲它會在您的觸摸被釋放時調用操作方法。您還可以使用長按手勢縮小視圖。

覆蓋FUNC

touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
     UIView.animateWithDuration(1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: nil, 
      animations: { 
       self.vwBlue.transform = CGAffineTransformMakeScale(0.5, 0.5) 
      }, completion: nil) 
    } 

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { 
     UIView.animateWithDuration(1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: nil, 
      animations: { 
       self.vwBlue.transform = CGAffineTransformMakeScale(1, 1) 
      }, completion: nil) 
    } 
+0

我如何確保touchesBegan僅適用於單個視圖? (從來沒有真正使用過touhces之前,有點困惑)。謝謝。 – mlevi 2014-10-06 19:00:49

+0

什麼觸摸開始將做的是,當用戶觸摸屏幕時,它將執行操作,因此它只會在您已經編寫代碼的視圖上執行操作。它會在屏幕上的任何位置檢測觸摸。 此外,您還可以使用觸摸點檢查觸摸點在哪個區域進行檢查並檢查其是否正確,然後僅執行所需的操作。 如需進一步瞭解,請訪問: https://developer.apple.com/library/ios/Documentation/UIKit/Reference/UIResponder_Class/index.html#//apple_ref/occ/instm/UIResponder/touchesBegan:withEvent: – 2014-10-07 05:10:32

+0

所以我認爲這不能用輕拍手勢完成? – mlevi 2014-10-07 15:58:49