2012-01-06 95 views
0

在我的程序中,我使用滚动视图作为背景视图。然后,我在滚动视图中添加几个图像。另外,我允许用户触摸图像以触发某些操作。但是,滚动视图上附加的图像在触摸时无法检测到。我在触摸事件中使用以下代码:如何让图像在滚动视图上被触摸?

UITouch * touch = [[event allTouches] anyObject]; 

我做错了什么?我怎么解决这个问题?非常感谢你!

回答

3

您可以使用UIImageView来跟踪触摸,并且还设置myImageView.userInteractionEnabled = YES,因为图像浏览的默认值为no。

如果您要检测的图像上的水龙头,你可以这样做:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesSingleTap:)]; 
     [singleTap setNumberOfTapsRequired:1]; 

     [imageView addGestureRecognizer:singleTap]; 

并实现您的选择:

- (void)handlesSingleTap:(UIGestureRecognizer *)gestureRecognizer 
{ 
    //Handle touch 
} 
+0

我已经设置你说的东西。但是,当我触摸图像时没有响应。我认为模拟器只是检测到我只触摸滚动视图,而不是图像。另外,我试图将图像移动到滚动视图之外,然后运行。它是如何来的? – 2012-01-06 13:16:13

+0

@PeterLam你可以发布你的代码?,我在滚动视图上使用此代码,它工作正常。 – 2012-01-06 13:20:08

+0

在.h文件中,我创建了 IBOutLet UIImageView * img1; @property(nonatomic,retain)IBOutLet UIImageView * img1;我创建了 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {UITouch * touch = [[event allTouches] anyObject]; if([touch view] == img1){code is here} ViewDidLoad {super viewDidLoad]; img1.userInteractionEnabled = true; } 我检查过这个程序不能运行的功能touchesBegan {} – 2012-01-06 16:33:10

相关问题