如何从 UIScrollView 中的多个 imageView 中识别特定 imageView 上的触摸?
我在 UIScrollView 中有多个 ImageView,并且我想识别我触摸的特定 imageView。这样我就可以采取一些行动。 (例如,显示带有图像名称的警报)。
我看过一些类似的帖子,但他们没有正确的答案。有些答案对我来说很复杂,因为我对 iPhone 编程还很陌生。
如果有人能帮忙举一个简单的例子,那就太好了。 :-)
我的代码是这样的
- (void)viewDidLoad {
imgScrollView.userInteractionEnabled =YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= 5; i++)
{
NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width
CGRect rect = imageView.frame;
rect.size.height = kHeight;
rect.size.width = kWidth;
imageView.frame = rect;
imageView.tag = i;
[imgScrollView addSubview:imageView];
[imageView release];
}
[self layoutImages];
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// WHAT CODE SHOULD I GIVE HERE ?????????? :-)
}
I have multiple ImageViews in a UIScrollView and I want to identify the specific imageView on which I touched. So that I could do some actions. (for example, showing Alert with the image name).
I have seen some similar posts, but they did not have proper answer. And some answers were complex to me as I am pretty new to iPhone programming.
It would be great, If anybody helps with a simple example. :-)
my code is like this
- (void)viewDidLoad {
imgScrollView.userInteractionEnabled =YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= 5; i++)
{
NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width
CGRect rect = imageView.frame;
rect.size.height = kHeight;
rect.size.width = kWidth;
imageView.frame = rect;
imageView.tag = i;
[imgScrollView addSubview:imageView];
[imageView release];
}
[self layoutImages];
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// WHAT CODE SHOULD I GIVE HERE ?????????? :-)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您查看 的文档和示例UIGestureRecognizer。还有一个更具解释性的事件处理指南< /a> .
这将引导您将手势识别器添加到特定视图(例如您的 imageView),这些视图可以根据您的喜好处理滑动、点击和多点触控手势。
I recommend you look through the documentation and examples for UIGestureRecognizer. There is also a more explanatory Event Handling Guide .
This will walk you through adding Gesture Recognizers to specific views (e.g. your imageView) that can handle swipes, taps and multitouch gestures however you like.
您可以使用 UIButton 而不是图像视图。为每个按钮分配标签,然后使用 switch(...) 识别它们。
You can use UIButton instead of image view. Assign tags for each button and then identify them using switch(...).
将唯一的标签值与每个 UIImageView 实例相关联:
在 viewDidLoad 中或初始化图像的任何位置:
然后尝试手势或类似的操作:
以前没有尝试过类似的操作,但它可能应该有效。
Associate a unique tag value with each UIImageView instance:
In viewDidLoad or wherever your images are initialised:
then try a gesture or something like:
haven't tried something like this before but it should probably work.