如何在动画时检测UIImageView上的触摸

时间:2011-08-01 08:05:19

标签: iphone uiimageview

我有一个从一个位置移动到另一个位置的UIImageview。当我尝试在动画触摸事件期间点击图像时,不调用委托方法。但是当我在完成动画后点击图像时,委托方法就会执行。如何在uiimageview改变其位置时检测触摸事件。

我的代码:

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

修改

我的动画代码:

[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:^{ 
                                                                                        vwb5.center = CGPointMake(260, 280);
                                                                                    } ];
                                                                                }];
                                                           }];

                                      }
                      ];

                 }];

感谢
的Pankaj

3 个答案:

答案 0 :(得分:0)

你可以获得职位......

-(CGRect) getPos
{
    CGRect pos = 0;
    CALayer *layer = yourAnimationView.layer.presentationLayer;
    if(layer)
        pos = layer.frame;

    return pos;
}

并在touchesEnded方法中检查CGRectContainsPoint([self getPos],touchPoint) (My old answer

答案 1 :(得分:0)

在我的项目中,我没有检测到UIImageView的任何触摸事件。 我检测到父视图的触摸事件并使用触摸点和UIImageView的rect来检测触摸的UIImageView。 也许你可以尝试一下。

- (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");
}

答案 2 :(得分:0)

我已将正确的解决方案发布为原始问题的编辑