在我的应用程序中,我为每个图像动态创建了 imageView,并将它们放在滚动视图中。如何通过单击每个图像来生成执行导航操作的事件?
问问题
118 次
2 回答
0
快速且易于实施
并使用. UITapGestureRecognizer
_UIImageView
addGestureRecognizer
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[theImageView addGestureRecognizer:singleTap];
[theImageView setUserInteractionEnabled:YES];
现在点击视图时会触发以下内容:
- (void)imageViewTapped:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"%@", [gestureRecognizer view]);
}
替代解决方案
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([[touches anyObject] view] == theImageView)
{
// ImageView tapped.
}
}
于 2012-09-17T21:54:00.793 回答
0
使用UIGestureRecognizer
. 在您的情况下,只需UITapGestureRecognizer
在每个UIImageView
. 不要忘记打开userInteractionEnabled
您UIImageView
的电源,以便考虑水龙头。
UITapGestureRecognizer* tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewAction:)];
[imageView addGestureRecognizer:tapGR];
imageView.userInteractionEnabled = YES;
-(void)imageViewAction:(UITapGestureRecognizer*)tapGR
{
UIImageView* imageView = tapGR.view;
// Do something with your imageView
}
阅读iOS 的事件处理指南(Apple Doc),了解有关手势识别器如何工作以及如何使用它们以及示例和内容的详细信息。这份 Apple 指南非常值得一读,其中包含非常有用的概念和解释。
于 2012-09-17T21:55:16.607 回答