我如何将IBAction事件与图像相关联?

时间:2010-10-13 09:52:03

标签: iphone objective-c cocoa-touch uiview ios4

我有一个包含动态图像的应用程序,我想使用我的自定义动画为图像设置动画。

如何将IBAction事件与图像相关联?

如何检测用户触摸图像的屏幕上的坐标?

请提出建议。

提前致谢,您的建议最受欢迎。

2 个答案:

答案 0 :(得分:3)

您可以在图片中使用自定义UIButtons。所有触摸处理代码都已内置。


编辑:UIGestureRecognizer也适用于UIImageViews:

UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]] autorelease];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[self.view addSubview:imageView];

答案 1 :(得分:1)

您也可以这样做:

首先定义图像所在的矩形:

touchRect=CGRectMake(screen.width/2-screen.width/8, 0, screen.width/4, screen.height/5);

然后使用touchesEnded方法,您可以定义触摸该区域时要执行的操作:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

        CGPoint targetPoint = [[touches anyObject] locationInView:self];
        if (CGRectContainsPoint(touchRect,  targetPoint)){
            //YOUR CODE HERE
        }
}
相关问题