user3447979
user3447979

Reputation: 7

How to Programmatically Remove A UIImageView By touchGesture

Ok, so I have been looking forever on how to use UITouchGestures with UIImageViews to remove the View, but everything either didn't make sense or didn't work so I finally decided to ask the question. How do you use A UITapGestureRecognizer to remove a UIImageView when it is tapped?

This is my .m:

       - (void)viewDidLoad
{

[super viewDidLoad];


//this makes a zombie/person
UIImageView *ZombieView =[[UIImageView alloc] initWithFrame: CGRectMake(135 , -100, 50, 75)];
UIImage *Zombie=[UIImage imageNamed:@"free-vector-stick-figure-clip-art_105575_Stick_Figure_clip_art_hight.png"];
[ZombieView setImage:Zombie];
//[self.view addSubview:ZombieView];
[self.view insertSubview:ZombieView belowSubview:_Railing];
[ZombieView setUserInteractionEnabled:TRUE];

// double tap gesture recognizer
UITapGestureRecognizer *Touch2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped2:)];
[Touch2 setDelegate: self];
[Touch2 setNumberOfTapsRequired: 2];
[_ZombieView addGestureRecognizer:Touch2];


UITapGestureRecognizer *Touch = [[UITapGestureRecognizer alloc] initWithTarget:ZombieView action:@selector(tapped:)];
[Touch setDelegate:self];
[Touch setNumberOfTapsRequired: 1];
[Touch requireGestureRecognizerToFail: Touch2];
[_ZombieView addGestureRecognizer:Touch];

//This makes the Person move down until he is behind the railing
[UIView animateWithDuration:7.5
                    delay:0.0
                    options: UIViewAnimationOptionCurveLinear
                    animations:^{
                    CGAffineTransform trans = CGAffineTransformTranslate(ZombieView.transform, 0, 420);
                        ZombieView.transform=trans;
                    }
                 completion:nil];}

-(void) tapped:(UITapGestureRecognizer *)recognizer{
[_ZombieView removeFromSuperview];
}

-(void) tapped2:(UITapGestureRecognizer *)recognizer{
}

-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end 

I know it must be a disaster, but I am fairly new to this so please keep this in mind. I just need know how to use the Tap Recognizers and how to delete a UIImageView, but other tips are appreciated. Thank you!

This is my edited code:

@interface AbcViewController ()

@end

@implementation AbcViewController




 - (void)viewDidLoad
{

[super viewDidLoad];



//this makes a zombie/person
UIImageView *zombieView =[[UIImageView alloc] initWithFrame: CGRectMake(135 , -100, 50, 75)];
UIImage *zombie=[UIImage imageNamed:@"free-vector-stick-figure-clip-art_105575_Stick_Figure_clip_art_hight.png"];
[zombieView setImage:zombie];
//[self.view addSubview:zombieView];
[self.view insertSubview:zombieView belowSubview:_Railing];
[zombieView setUserInteractionEnabled:TRUE];

//This makes the Person move down until he is behind the railing
[UIView animateWithDuration:7.5
                    delay:0.0
                    options: UIViewAnimationOptionCurveLinear
                    animations:^{
                    CGAffineTransform trans = CGAffineTransformTranslate(zombieView.transform, 0, 420);
                        zombieView.transform=trans;
                    }
                 completion:nil];


// double tap gesture recognizer
UITapGestureRecognizer *touch2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped2:)];
[touch2 setDelegate: self];
[touch2 setNumberOfTapsRequired: 2];
[zombieView addGestureRecognizer:touch2];


UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:zombieView action:@selector(tapped:)];
[touch setDelegate:self];
[touch setNumberOfTapsRequired: 1];
[touch requireGestureRecognizerToFail: touch2];
    [zombieView addGestureRecognizer:touch];


}

-(void) tapped:(UITapGestureRecognizer *)recognizer{
   [recognizer.view removeFromSuperview];
}

-(void) tapped2:(UITapGestureRecognizer *)recognizer{

[recognizer.view removeFromSuperview];
}



-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

@end

Upvotes: 0

Views: 462

Answers (1)

rdelmar
rdelmar

Reputation: 104092

A tap gesture recognizer has a view property that points to the view it's attached to. So, in the action method for the recognizer, use it to remove itself from its superview,

-(void) tapped:(UITapGestureRecognizer *)recognizer{
[recognizer.view removeFromSuperview];
}

It's hard to tell why your method isn't working. It could be because _ZombieView is nil (I don't see anywhere you actually create _ZombieView as opposed to ZombieView which you do create). BTW, you should start your property and variable names with a lowercase letter to conform to the usual objective-c naming conventions.

Upvotes: 1

Related Questions