How to detect touches on UIImageView while it's animating
I have an UIImageview which moves from a position to another. When I try to tap on the image during the animation touch event delegate method is not called. But when I tap on image after the completion of the animation, the delegate method executes. How can I detect the touch event on the uiimageview while it is changing its position.
my code:
vwb2 = [[UIView alloc] initWithFrame:CGRectMake(240, 500, 50, 50)];
[self.view addSubview:vwb2];
ballon2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"b2.png"]];
ballon2.userInteractionEnabled = TRUE;
ballon2.frame = CGRectMake(0, 0, 50, 50);
[vwb2 addSubview:ballon2];
[ballon2 release];
Edit:
My code for animation:
[UIView animateWithDuration:2.0
animations:^{
vwb1.center = CGPointMake(260, 40);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.5
animations:^{
vwb2.center = CGPointMake(260, 100);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
animations:^{
vwb3.center = CGPointMake(260, 160);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
animations:^{
vwb4.center = CGPointMake(260, 220);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0 animations:^{
开发者_StackOverflow社区 vwb5.center = CGPointMake(260, 280);
} ];
}];
}];
}
];
}];
Thanks
PankajYou can get position...
-(CGRect) getPos
{
CGRect pos = 0;
CALayer *layer = yourAnimationView.layer.presentationLayer;
if(layer)
pos = layer.frame;
return pos;
}
And check CGRectContainsPoint([self getPos],touchPoint) in touchesEnded method
(My old answer)
In my project, I did not detect any touch event of UIImageView. I detected touch event of parent view and used touch point and UIImageView's rect to detect the UIImageView was touched. Maybe you can try it.
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
bool inX = NO;
bool inY = NO;
if(pt.x >= myImgView.frame.origin.x && pt.x <= myImgView.frame.origin.x + myImgView.frame.size.width)
inX = YES;
if(pt.y >= myImgView.frame.origin.y && pt.y <= myImgView.frame.origin.y + myImgView.frame.size.height)
inY = YES;
if(inX && inY)
NSLog(@"myImgView is touched");
}
I have posted correct solution as edit to my original question
精彩评论