如何在 UIImageView 动画时检测触摸

发布于 2024-11-27 09:40:04 字数 2518 浏览 1 评论 0原文

我有一个 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);
                                                                                    } ];
                                                                                }];
                                                           }];

                                      }
                      ];

                 }];

谢谢
潘卡伊

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

                                      }
                      ];

                 }];

Thanks
Pankaj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你与清晨阳光 2024-12-04 09:40:04

您可以获取位置...

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

    return pos;
}

并在touchesEnded方法中检查CGRectContainsPoint([self getPos],touchPoint)
我的旧答案

You 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)

乖乖公主 2024-12-04 09:40:04

在我的项目中,我没有检测到UIImageView的任何触摸事件。
我检测到父视图的触摸事件,并使用触摸点和 UIImageView 的矩形来检测 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");
}

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");
}
江湖彼岸 2024-12-04 09:40:04

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

I have posted correct solution as edit to my original question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文