有没有人有任何示例代码来检测动态创建的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. 在UITableView上触摸UIView上的事件
- 2. 在UIView中触摸事件
- 3. UIView子类,触摸事件
- 4. 在UIView上检测触摸
- 5. 在UITableView上触摸事件?
- 6. 在ipad上触摸事件
- 7. UIView关闭触摸事件的方式
- 8. 的UIView未接收到触摸事件
- 9. UIView动画防止触摸事件?
- 10. UIView中的多点触摸事件
- 11. 从uiview向uiwebview发送触摸事件
- 12. 从Nib的UIView接收触摸事件
- 13. 从UIView传递触摸事件在UITableView上
- 14. 如何在iPhone上手动调用UIView的触摸事件?
- 15. 忽略UIView上除了按钮上的触摸事件。
- 16. 触摸事件上UTableViewController
- 17. 触摸事件上下andengine
- 18. 上的触摸事件iPhone
- 19. 触摸UIView外
- 20. 在UIwindow上的UIimageView上触摸事件
- 21. 用触摸事件检测UIview上的文本
- 22. 触摸事件
- 23. 触摸事件
- 24. 在uiview之上触摸不到webView
- 25. 在ColorPicker UIView上限制触摸 - iOS
- 26. SIGABRT在任何UIView触摸上
- 27. 在苹果设备上触摸事件
- 28. 在石英圆图上触摸事件?
- 29. 在Windows Phone上取消触摸事件
- 30. Android,在ImageView上丢失触摸事件
剪断第二码帮我,但更好的做法是'UITapGestureRecognizer * touchOnView = [[[UITapGestureRecognizer页头] initWithTarget:自我行动:@selector( releaseButAction :)] autorelease];'然后该方法将是' - (void)releaseButAction:(UIGestureRecognizer *)gestureRecognizer' ..只是fyi! – sixstatesaway 2013-01-09 10:13:40