Search code examples
iphoneiosxcodeimagetouches

iPhone SDK - Using touches to delete images


how can you delete an image by using touches in the iphone sdk? Im making a game with images so i was wondering if there is a way were if you touch an image it gets deleted from the view.


Solution

  • Yes. You can add UITapGestureRecognizer to your image view, and in the recognizer action you can get the image view by using recognizer.view propery. And you can delete/remove it from its super view by using [image removeFromSuperview] method.

    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTapped:)];
    [imageView addGestureRecognizer:tgr];
    [tgr release];
    

    And the onImageTapped: method looks like this,

    - (void)onImageTapped:(UITapGestureRecognizer*)recognizer {
    
        UIImageView *imgView = (UIImageView*) recognizer.view;
        [imgView removeFromSuperview];
    }