2013-02-20 186 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