How do I check to see if my subview is being touched?

Posted by Amy on Stack Overflow See other posts from Stack Overflow or by Amy
Published on 2010-03-31T21:20:47Z Indexed on 2010/03/31 21:23 UTC
Read the original article Hit count: 569

Filed under:
|

I went through this tutorial about how to animate sprites: http://icodeblog.com/2009/07/24/iphone-programming-tutorial-animating-a-game-sprite/

I've been attempting to expand on the tutorial by trying to make Ryu animate only when he is touched. However, the touch is not even being registered and I believe it has something to do with it being a subview. Here is my code:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
 UITouch *touch = [touches anyObject];

 if([touch view] == ryuView){
  NSLog(@"Touch");
 } else {
  NSLog(@"No touch");
 }
}

-(void) ryuAnims{
 NSArray *imageArray = [[NSArray alloc] initWithObjects:
         [UIImage imageNamed:@"1.png"],
         [UIImage imageNamed:@"2.png"],
         [UIImage imageNamed:@"3.png"],
         [UIImage imageNamed:@"4.png"],
         [UIImage imageNamed:@"5.png"],
         [UIImage imageNamed:@"6.png"],
         [UIImage imageNamed:@"7.png"],
         [UIImage imageNamed:@"8.png"],
         [UIImage imageNamed:@"9.png"],
         [UIImage imageNamed:@"10.png"],
         [UIImage imageNamed:@"11.png"],
         [UIImage imageNamed:@"12.png"],
         nil];

 ryuView.animationImages = imageArray;
 ryuView.animationDuration = 1.1;
 [ryuView startAnimating];


}



-(void)viewDidLoad {
       [super viewDidLoad];

       UIImageView *image = [[UIImageView alloc] initWithFrame:
        CGRectMake(100, 125, 150, 130)];
 ryuView = image;


 ryuView.image = [UIImage imageNamed:@"1.png"];
 ryuView.contentMode = UIViewContentModeBottomLeft; 
 [self.view addSubview:ryuView];
        [image release];
}

This code compiles fine, however, when touching or clicking on ryu, nothing happens. I've also tried

if([touch view] == ryuView.image) 

but that gives me this error: "Comparison of distinct Objective-C type 'struct UIImage *' and 'struct UIView *' lacks a cast." What am I doing wrong?

© Stack Overflow or respective owner

Related posts about subview

Related posts about touch