检测包含“n”的 UIScrollView 上的单击UI图像视图
我只是想从 UIScrollView 中获取被点击的 UIImageView 。
我在网上找到了两种实现上述目标的方法。
第一种方法:在将其添加到滚动查看器之前在 uiimageview 上创建点击手势。
这个方法对我来说没有用。 handleSingleTap 方法永远不会被调用。
我不知道我做错了什么/为什么这不起作用。
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:singleTap];
[singleTap release];
[framesSourceScrollview addSubview:imageView];
[imageView release];
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
NSLog(@"image tapped!!!");
}
第二种方法:子类 UIScrollView
@interface SingleTapScrollViewer : UIScrollView {
}
@end
@implementation SingleTapScrollViewer
- (id)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
@end
In the ViewController
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// Process the single tap here
NSLog(@"Scroll view single tapped. touches count : %d", touches.count);
UITouch *touch = [touches anyObject];
UIImageView *imgView = (UIImageView*)touch.view;
NSLog(@"tag is %@", imgView.tag);
}
使用此方法,touchesDown 响应程序确实会被调用,但“[touches anyobject]”不是被点击的 UIImageView。
我尝试在每个 UIImageView 上设置“标签”,并使用递增的计数器添加到滚动视图中,但无论我点击哪个图像视图,我都会返回 0。
总的来说,我对可可很陌生,我不知道如何以任何其他方式利用这个响应器。
任何建议/提示都会很棒。
提前致谢。
I am simply trying to get hold of the UIImageView that was tapped on, from the UIScrollView.
I have found 2 ways of achieving the above on the Web.
1st method : create a tap gesture on the uiimageview before adding it to scrollviewer.
This method has not worked for me. the handleSingleTap method never gets called.
I am out of ideas on what I am doing wrong/why this is not working.
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:singleTap];
[singleTap release];
[framesSourceScrollview addSubview:imageView];
[imageView release];
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
NSLog(@"image tapped!!!");
}
2nd Method : subclass UIScrollView
@interface SingleTapScrollViewer : UIScrollView {
}
@end
@implementation SingleTapScrollViewer
- (id)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
@end
In the ViewController
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// Process the single tap here
NSLog(@"Scroll view single tapped. touches count : %d", touches.count);
UITouch *touch = [touches anyObject];
UIImageView *imgView = (UIImageView*)touch.view;
NSLog(@"tag is %@", imgView.tag);
}
Using this method, the touchesDown responder does get called but the '[touches anyobject]' isn't the UIImageView which was tapped on.
I tried setting the 'tag' on every UIImageView I am adding to the scrollview with an increasing counter but I get back 0 no matter which imageview i tap on.
I am new to cocoa in general and I am not sure how to leverage this responder in any other fashion.
Any suggestions/hints would be great.
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在 UIImageView 上设置了 userInteractionEnabled=YES?默认情况下它是关闭的。
did you set the userInteractionEnabled=YES on the UIImageView. Its turned off by default.