开发者

Display an UIAlert when a UIImageView is touch

I have an ImageView and I want that when user press the image view to display a message as an alert. What method sho开发者_运维知识库uld I use? Can you provide me an example ?

Thanks in advance..


Add a UITapGestureRecognizer:

imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleTap:)];
[imageView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

And then your callback...

- (void)handleTap:(UITapGestureRecognizer *)tapGestureRecognizer
{
  //show your alert...
}


Use Touch Events method to do ur task

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

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

    CGPoint location= [touch locationInView:self.view];

    if(CGRectContainsPoint(urImageView.frame, location)) {
        //AlertView COde
    }
}


UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
[self.view addGestureRecognizer:singleTapOne]; [singleTapOne release];


- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer


First of all, Enable property

yourImgView.setUserInteractionEnabled = TRUE;

Then apply the below code on viewDidLoad;

UITapGestureRecognizer *tapOnImg = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTapOnImgView:)];
tapOnImg.numberOfTapsRequired = 1; tapOnImg.delegate = self;
[yourImgView addGestureRecognizer:tapOnImg]; 


If you don't intent to subclass UIImageView to override touch event methods -- or implement the touch methods in the ViewController and check the frame the touch lies in the imageview frame -- you can (and that's probably easier) add an UITapGestureRecognizer to your UIImageView.

See here in the documentation for more details

UITapGestureRecognizer* tapGR = [[UITapGestureRecognizer alloc]
    initWithTarget:self action:@selector(tapOnImage:)];
[yourImageView addGestureRecognizer:tapGR];
[tagGR release];

Then implement the -(void)tapOnImage:(UIGestureRecognizer*)tapGR method as you wish


Use touch delegate method and find your image view there,


Instead of using a UITapGestureRecognizer if the class where you have your imageview overrides UIResponder you could just override touchesBegan:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
      if ([touch view] == YOUR_IMAGE_VIEW) {
          // User clicked on the image, display the dialog.
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image Clicked"     message:@"You clicked an image." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
          [alert show];
          [alert release];
      }
}

You must be sure that userInteractionEnabled is YES for your image view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜