Cocoa touch 如何检测UI视图中的触摸事件?
在我的程序中,我希望在触摸时可以检测到滚动视图中的图像。我在网上看到有一些代码可以帮助解决这个问题 在subclass.h文件中:Cocoa touch 如何检测UI视图中的触摸事件?,cocoa-touch,Cocoa Touch,在我的程序中,我希望在触摸时可以检测到滚动视图中的图像。我在网上看到有一些代码可以帮助解决这个问题 在subclass.h文件中: @interface AllScrollView : UIScrollView { } @end 在subclass.m文件中: #import "AllScrollView.h" @implementation AllScrollView - (id)initWithFrame:(CGRect)frame { return [super init
@interface AllScrollView : UIScrollView
{
}
@end
在subclass.m文件中:
#import "AllScrollView.h"
@implementation AllScrollView
- (id)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
@end
然后我更改了主类上的方法-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event
。但是,没有发生任何事情,我无法检测到触摸。我错过什么了吗?谁能给出更详细的解决方案?非常感谢你 我使用滚动视图,它们工作正常。在您的情况下,我会选择一个单键识别器,如下所示:
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSingleTap:)];
singleFingerDTap.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleFingerTap];
- (IBAction)handleSingleTap:(UIGestureRecognizer *)sender {
NSLog(@"Single tap detected");
}
ScrollView自动检测和处理触摸事件:您好,因为我是xcode的初学者,您能给我更多关于上述代码的信息吗?我应该把这个代码放在哪里?iAction是否可以链接到.xib文件中的图像。多谢各位much@PeterLam设置识别器的第一个片段进入视图的指定初始值设定项(通常是
-(id)initWithFrame:(CGRect)frame
方法)。该方法用于视图类的实现。进入handleSingleTap:
方法后,调用[发送方位置查看:self]
在视图中找到点击点。如果您想独立于手势识别器使用相同的动作,可以在界面生成器中链接到iAction
。self.theView的含义是什么?@PeterLam Oops,我从一个位置复制了代码,该位置是从视图控制器的init方法添加识别器的。顺便说一句,这是另一个有效的选择:如果执行此操作,请将handleSingleTap也移动到视图控制器。我把答案改为self
而不是self.theView
。你能给我整个代码的链接吗?因为我想看看这个程序是怎么完成的?我想要一个例子来熟悉手势识别器的使用。多谢各位