2014-04-12 58 views
0

好吧,所以我一直在尋找如何使用UITouchGestures與UIImageViews去除視圖,但一切都沒有意義或不工作,所以我最終決定問這個問題。如何使用UITapGestureRecognizer刪除一個UIImageView,當它被點擊時?如何以編程方式刪除UIImageView通過touchGesture

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

我知道那一定是一場災難,但我相當的新本,所以請記住這一點。我只需要知道如何使用輕觸識別器以及如何刪除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

敲擊手勢識別有一個指向它的連接到視圖的視圖屬性。因此,在識別器的操作方法,用它自己從它的父刪除,

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

這很難說,爲什麼你的方法是行不通的。這可能是因爲_ZombieView是零(我沒有看到你實際創建_ZombieView的地方,而不是你創建的ZombieView)。順便說一句,你應該用小寫字母開始你的財產和變量名稱,以符合通常的Objective-C命名約定。

+0

我試過了,然後在Thread 1:SIGART中得到了同樣的錯誤0_pthread_kill。你有什麼建議嗎? (當我點擊或點擊該人時,我得到了這個。) – user3447979

+0

@ user3447979,我想這可能是因爲_Zombie視圖不存在(並且這是您附加到手勢識別器的視圖)。將您添加手勢識別器的2行更改爲視圖,以使ZombieView代替_ZombieView。 – rdelmar

+0

我正在調查這一點,並在調試欄中發現錯誤與int main中的argv字符有關。它說:** argv =(char)'/'是否意味着什麼。哦,我現在將我的名字改爲通常的Obj-c命名約定,例如ZombieView是zombieView。我也在做一些測試,看來我的UIGestureRecognizer沒有調用(void)tapped :. – user3447979