2013-02-20 187 views
0

我正在一個項目中產生圖像,你基本上觸摸它們,它們被刪除。我將如何移除我碰到的物體?我想過讓一個可變陣列來容納所有的物體,但我似乎無法找出任何東西。刪除觸摸的UIImageView

GameViewController.m 

#import "GameViewController.h" 
#import "Cig.h" 

@interface GameViewController() 

@end 

@implementation GameViewController 
@synthesize scoreLbl, timeLbl; 

//CLASS ONLY VARS 
BOOL isGameOver; 
int timeInt; 
int scoreInt; 
int cigsOnScreen; 
NSMutableArray *spawnedCigs; 
//CLASS ONLY VARS 

//TIMER 
-(void)count { 
    timeInt--; 
    timeLbl.text = [NSString stringWithFormat:@"Time: %i", timeInt]; 

    if(timeInt == 0){ 
     isGameOver = YES; 
     NSLog(@"Your Score For This Round: %i", scoreInt); 
    } 

    if(isGameOver == NO){ 
     [self performSelector:@selector(count) withObject:nil afterDelay:1]; 
    } 
} 
//TIMER 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

-(void)spawnCigs { 
    for(int i =0 ; i < 5; i++){ 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(arc4random()%760, arc4random()%430, 100, 23)]; 
     UIImage *image = [UIImage imageNamed:@"Cig.png"]; 
     [imageView setImage:image]; 
     Cig *cig = [[Cig alloc] init]; 
     [cig setTag:arc4random()%666]; 
     [cig setImage:imageView]; 
     [spawnedCigs addObject:cig]; 
     [self.view addSubview:imageView]; 
    } 
    [self performSelector:@selector(spawnCigs) withObject:nil afterDelay:5]; 
} 

-(void)reset { 
    scoreLbl.text = @"Score:"; 
    timeLbl.text = @"Time:"; 
    isGameOver = NO; 
    timeInt = 60; 
    scoreInt = 0; 
    cigsOnScreen = 0; 
    spawnedCigs = [NSMutableArray arrayWithObjects:nil, nil]; 

    [self performSelector:@selector(count) withObject:nil afterDelay:1]; 
} 

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

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self reset]; 
    [self spawnCigs]; 
} 

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

@end 

代碼很凌亂,所以請不要評論我。

感謝您的提供

回答

1
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch=[[event allTouches]anyObject]; 
    CGPoint touchPoint = [touch locationInView:touch.view];; 
    for (UIView *view in [self.view subviews]) 
    { 
    if([view isMemberOfClass:[UIImageView class]]) 
    { 
     if (CGRectContainsPoint(view.frame,touchPoint)) 
     { 
     [view removeFromSuperview]; 
     } 
    } 
    } 
} 

這可以幫助你

+0

謝謝,我不完全理解這一點,但其他人確實幫助理解了這一點。我結合了2個答案來獲得我想要的結果,謝謝 – 2013-02-21 00:35:46

0

通過使用

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

方法,你會得到所選圖像的圖像視圖的任何幫助。只需從超級視圖中刪除對象。

1

使用

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
for (UIImageView *view in [self.view subviews]) 
    { 
     if (view.tag==1) 
     { 
      [view removeFromSuperview]; 
     } 
     if (view.tag==2) 
     { 
      [view removeFromSuperview]; 
     } 
    } 

} 

試試這個您的問題將得到解決。不要忘了標籤傳遞給你的imageviews ...

+0

不要直接使用的代碼。試着找出解決方案。解釋一下他如何用自己的方法來實現。 – Madhu 2013-02-20 05:46:49

+0

謝謝,我用這個找到我的解決方案 – 2013-02-21 00:36:05