如何检测图像上的触摸?

时间:2014-06-14 08:19:02

标签: ios touchesbegan

这是非常基本的东西,但我似乎无法做到正确(我不熟悉编程)。我想要做的是,让我的Xcode 5应用程序检测是否有一个具有特定名称的图像(比如说,#" go.png"被按下或触摸。我怎么能这样做?只有两个按钮,所以我一直使用(touchLocation.y和.x ...)方法,但我现在需要按下按钮方法。我已粘贴下面的代码。非常感谢大家的帮助。

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

UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];


switch (_flyGo) {
    case HeadMenu:
        if (touchLocation.y < self.size.height * 0.7) {
        } else if (touchLocation.x < self.size.width * 0.3) {
            [self switchNewFly:FlyTutorial];
        } else {
            [self giveRatingToApp];
        }
        break;
    case FlyTutorial:
        [self switchToFly];
        break;
    case FlyStatePlay:
        [self flyPlayer];
        break;
    case FlyDisplayFalling:
        break;
    case FlyDisplayScore:
        break;
    case FlyDisplayDone:
        if (touchLocation.x < self.size.width * 0.6) {
            [self switchNewFly:FlyDisplayTutorial]; 
        } else {
            [self ShareMyScore];
        }
        break;
}




//I would like to get an image for ex. "flyer.png" to be detected as a touch instead of using (touchLocation.x or .y < self.size.width * 0.6) 

感谢!!!!

2 个答案:

答案 0 :(得分:0)

是的,你可以

为UIImageView添加触控识别器(别忘了设置imgView.userInteractionEnabled = YES; )。还要将imgView的tag属性设置为某个值。

 UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAll:)];
 tapRecognizer.numberOfTapsRequired = 1;
 tapRecognizer.numberOfTouchesRequired = 1;
 [imgView addGestureRecognizer:tapRecognizer];

然后

- (void)handleTapAll:(UITapGestureRecognizer *)recognizer {
    UIImageView *img = (UIImageView *)recognizer.view;
    if(img.tag == some_vale) //your code


}

答案 1 :(得分:-1)

ViewController.h
@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
......

ViewController.m
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageNamed:@"yourimage.png"];

imageView.userInteractionEnabled = YES;
imageView.exclusiveTouch = YES;
imageView.multipleTouchEnabled = YES;


// Add tap gesture
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tap setNumberOfTapsRequired:1];
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[self.view addSubview:imageView];


- (IBAction)handleTap:(UIGestureRecognizer *)recognizer { //Your code here }