2010-11-09 133 views
30

有沒有人有任何示例代碼來檢測動態創建的UIView上的觸摸?我發現的touchesBegan一些參考,但無法弄清楚如何實現它?在UIView上觸摸事件

回答

80

一個非常普遍的方式是覆蓋在自定義UIView子類這些方法:響應觸摸EV下

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 

官方的文檔,這些方法是因爲它是UIResponder的一個子類,所以UIView繼承了這些方法 in the UIResponder class docs。更詳細的介紹性文檔可以在Event Handling Guide for iOS中找到。

如果您只想檢測輕擊(觸摸,然後在視圖中觸摸),最簡單的方法是將UIButton作爲視圖的子視圖添加,然後將自己的自定義方法添加爲目標/動作爲那個按鈕配對。自定義按鈕在默認情況下是不可見的,所以它不會影響視圖的外觀。

如果您正在尋找更高級的互動,那麼瞭解the UIGestureRecognizer class也是一件好事。

4

從您通過繼承UIView並重寫子類中,下面的方法創建一個子類創建自己的自定義UIView

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

} 
獲得觸摸
16

在UIView上使用UIAnimation創建觸摸事件,您可以在任何地方管理UIView上的觸摸。

以下代碼:這裏self.finalScore是一個UIView,而cup是一個UIImageView。我處理

UIImageView上的觸摸事件,它存在於UIView中。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch1 = [touches anyObject]; 
CGPoint touchLocation = [touch1 locationInView:self.finalScore]; 
CGRect startRect = [[[cup layer] presentationLayer] frame]; 
CGRectContainsPoint(startRect, touchLocation); 

[UIView animateWithDuration:0.7 
         delay:0.0 
        options:UIViewAnimationCurveEaseOut 
       animations:^{cup.transform = CGAffineTransformMakeScale(1.25, 0.75);} 
       completion:^(BOOL finished) { 
        [UIView animateWithDuration:2.0 
              delay:2.0 
             options:0 
             animations:^{cup.alpha = 0.0;} 
             completion:^(BOOL finished) { 
              [cup removeFromSuperview]; 
              cup = nil;}];     
       }]; 

} 

像UITapGestureRecognizer是處理上的UIView觸摸事件的另一種方式....

UITapGestureRecognizer *touchOnView = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(releaseButAction)] autorelease]; 

// Set required taps and number of touches 
[touchOnView setNumberOfTapsRequired:1]; 
[touchOnView setNumberOfTouchesRequired:1]; 

// Add the gesture to the view 
[[self view] addGestureRecognizer:touchOnView]; 
+0

剪斷第二碼幫我,但更好的做法是'UITapGestureRecognizer * touchOnView = [[[UITapGestureRecognizer頁頭] initWithTarget:自我行動:@selector( releaseButAction :)] autorelease];'然後該方法將是' - (void)releaseButAction:(UIGestureRecognizer *)gestureRecognizer' ..只是fyi! – sixstatesaway 2013-01-09 10:13:40

1

而不是使用手勢捕捉觸摸,我會建議的UIView父類更改爲UIControl,讓它能夠處理touchUpInside事件

來源:

@interface MyView : UIView 

爲:

@interface MyView : UIControl 

,然後添加這一行視圖 -

[self addTarget:self action:@selector(viewTapped:) forControlEvents:UIControlEventTouchUpInside]; 

因爲手勢可能會產生問題,同時滾動。

2

我的方法是在InterfaceBuilder IdentityInspector中簡單地將UIView更改爲UIControl類,那麼ConnectionsInspector將立即使觸摸事件準備好掛鉤。

零碼更改。

3

使用guesturereongizer,很容易發現觸摸事件。

找到一個在故事板從組件庫客串:

enter image description here

例如,阻力點觸手勢識別器到您的視圖。它在視圖UI中未顯示。可以從文檔大綱中故事板發現它在左側面板:

enter image description here

的最後一步是將它連結(由控制阻力)到視圖作爲代表,其中觸摸被監控,並且控制拖動作爲一個動作到你的視圖控制器類。這裏是鏈接連接的圖片。

enter image description here

和行動代碼:

@IBAction func tapAction(_ sender: Any) { 
    // touch is captured here. 
}