有沒有人有任何示例代碼來檢測動態創建的UIView上的觸摸?我發現的touchesBegan一些參考,但無法弄清楚如何實現它?在UIView上觸摸事件
30
A
回答
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];
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
相關問題
- 1. UIView子類,觸摸事件
- 2. 在UITableView上觸摸事件?
- 3. 從Nib的UIView接收觸摸事件
- 4. 從uiview向uiwebview發送觸摸事件
- 5. 的UIView未接收到觸摸事件
- 6. UIView關閉觸摸事件的方式
- 7. 如何在iPhone上手動調用UIView的觸摸事件?
- 8. 從UIView傳遞觸摸事件在UITableView上
- 9. 忽略UIView上除了按鈕上的觸摸事件。
- 10. 觸摸事件上UTableViewController
- 11. 上的觸摸事件iPhone
- 12. 觸摸事件
- 13. 在ColorPicker UIView上限制觸摸 - iOS
- 14. SIGABRT在任何UIView觸摸上
- 15. 在uiview之上觸摸不到webView
- 16. 在石英圓圖上觸摸事件?
- 17. 在sw圖上觸摸事件3
- 18. 如何在觸摸事件上畫線?
- 19. 在蘋果設備上觸摸事件
- 20. 在Windows Phone上取消觸摸事件
- 21. 在Apple上處理觸摸事件 - Highcharts
- 22. 當我觸摸UIVIew
- 23. 事件觸摸屏輕輕觸摸
- 24. UIView觸摸事件在屏幕底部未被解僱
- 25. 當用戶在UIView之外觸摸時,如何處理事件?
- 26. 觸摸事件在iPhone
- 27. MKMapView觸摸事件
- 28. UIImageView觸摸事件
- 29. Recyclerview觸摸事件
- 30. UIwebview觸摸事件
剪斷第二碼幫我,但更好的做法是'UITapGestureRecognizer * touchOnView = [[[UITapGestureRecognizer頁頭] initWithTarget:自我行動:@selector( releaseButAction :)] autorelease];'然後該方法將是' - (void)releaseButAction:(UIGestureRecognizer *)gestureRecognizer' ..只是fyi! – sixstatesaway 2013-01-09 10:13:40