我只是想从 UIScrollView 中获取被点击的 UIImageView。
我在网上找到了两种实现上述目标的方法。
第一种方法:在将uiimageview添加到scrollviewer之前在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。
总的来说,我是可可的新手,我不知道如何以任何其他方式利用这个响应者。
任何建议/提示都会很棒。
提前致谢。