And*_*w M 30 iphone objective-c uikit ios
有没有人有任何示例代码来检测动态创建的UIView上的触摸?我找到了一些touchesBegan的引用,但无法弄清楚如何实现它...
Tyl*_*ler 82
获取触摸的一般方法是在自定义UIView子类中重写这些方法:
- (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
Run Code Online (Sandbox Code Playgroud)
这些方法的官方文档位于UIResponder类文档中的响应触摸事件(UIView继承了这些方法,因为它是UIResponder的子类).iOS的" 事件处理指南"中可以找到更长和更多的介绍性文档.
如果您只想检测点击(触摸,然后在视图UIButton中进行修饰),最简单的方法是添加一个视图的子视图,并添加自己的自定义方法作为该按钮的目标/操作对.自定义按钮默认不可见,因此不会影响视图的外观.
如果您正在寻找更高级的互动,那么了解该UIGestureRecognizer课程也是一件好事.
小智 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;}];
}];
}
Run Code Online (Sandbox Code Playgroud)
像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];
Run Code Online (Sandbox Code Playgroud)
UIView通过继承UIView并覆盖子类中的以下方法,从创建的子类中创建自己的自定义项,
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75208 次 |
| 最近记录: |