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 
{
  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 に答える
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 に答える
0

使用することで

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

メソッドを使用すると、選択した画像の画像ビューを取得できます。スーパービューからオブジェクトを削除するだけです。

于 2013-02-20T05:37:51.060 に答える