UITapRecognizer and ImageView
I have a UIImageView with a UITapGestureRecognizer attached. This is just a ball moving around the scree开发者_运维问答n. It moves once a second.
ball.image = [UIImage imageNamed:@"chicken.png"];
ball.frame = CGRectMake(160, 160, 50, 50);
ball.autoresizesSubviews = NO;
speed = 10;
objTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ballMove:) userInfo:nil repeats:YES];
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
The method called when the imageView is clicked is
-(void)handleTap:(UITapGestureRecognizer *)sender
{
CGPoint obj = [sender locationInView:ball];
NSLog(@"x = %f",obj.x);
NSLog(@"y = %f",obj.y);
.......
}
It doesn't grab all taps. It only picks up taps where y is less than 1 however.
x = 14.958618
y = 0.879913
x = 23.996643
y = 0.975830
x = 24.542923
y = 0.557907
And so on...
The imageView description is: <UIImageView: 0x6814800; frame = (161.747 183.826; 50 1); opaque = NO; autoresize = W+H; autoresizesSubviews = NO; layer = <CALayer: 0x6814880>>
Check whether your UIImageView
interactions are enabled:
ball.userInteractionEnabled = YES;
精彩评论