0

我正在开发一个生成图像的项目,您基本上触摸它们并将它们删除。我将如何移除我触摸的对象?我曾想过制作一个可变数组来保存所有对象,但我似乎无法弄清楚任何事情。

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

代码非常混乱,所以请不要以此评判我。

感谢您提供的任何帮助

4

3 回答 3

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];
        }
    }

}

试试这个你的问题将得到解决。不要忘记将选项卡传递给您的图像视图...

于 2013-02-20T05:40:52.150 回答
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];
       }
    }
  }
}

这可以帮助你

于 2013-02-20T06:43:51.597 回答
0

通过使用

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

方法您将获得所选图像的图像视图。只需从超级视图中删除对象。

于 2013-02-20T05:37:51.060 回答