如何通过touchGesture以编程方式删除UIImageView

时间:2014-04-12 03:34:45

标签: ios objective-c uiimageview uitapgesturerecognizer

好的,所以我一直在寻找如何将UITouchGestures与UIImageViews一起使用来移除View,但是一切都没有意义或没有用,所以我最终决定提问。在点击UIImageView时,如何使用UITapGestureRecognizer删除它?

这是我的.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 

我知道这一定是一场灾难,但我对此很新,所以请记住这一点。我只需要知道如何使用Tap识别器以及如何删除UIImageView,但其他提示表示赞赏。谢谢!

这是我编辑过的代码:

@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

1 个答案:

答案 0 :(得分:1)

点击手势识别器具有指向其附加的视图的视图属性。因此,在识别器的操作方法中,使用它从超级视图中删除自己,

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

很难说出为什么你的方法不起作用。这可能是因为_ZombieView是零(我没有看到你实际创建_ZombieView的任何地方,而不是你创建的ZombieView)。顺便说一下,你应该用小写字母开始你的属性和变量名,以符合通常的objective-c命名约定。