使用UItouch时视图之间的区别

时间:2012-03-27 22:19:34

标签: ios xcode view coordinates uitouch

我目前正在开发一个iOS项目,我希望UItouch命令只能在某个"图像"上发送坐标。我已经使用UItouch输出了坐标,但是我无法仅针对一个特定区域进行此操作。

据我所知,唯一的方法是使用视图。所以我在主视图中创建了一个新视图,从那里我遇到了问题,似乎无法使其正常工作。

有没有人这样做过/可以给我任何建议吗?

PS - 我正在使用Xcode 4。

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches  anyObject];
if (touch.view==yourImageView) {
    //Coordinate code
}

}

编辑:或尝试使用UITapGestureRecognizer

在您的界面中添加UIGestureRecognizerDelegate

@interface ViewController : UIViewController <UIGestureRecognizerDelegate> {

然后在你的viewDidLoad中添加这个

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMethod)];
tapped.delegate=self;
tapped.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapped];

然后在你的viewController中添加这两个方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view==yourImageView) {
        return YES;
    }
    return NO;
}

-(void)tapMethod {
    //Coordinate code
}

并确保您拥有[yourImageView setUserInteractionEnabled:YES];