如何在动画完成时启用imageview中的交互

时间:2016-11-10 10:53:28

标签: ios objective-c uiimageview cabasicanimation

我使用CABasic动画来移动UIImageViewUIImageView现在没有检测到动画完成后如何启用用户交互的触摸?

[ImageView setUserInteractionEnabled:NO];
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
theAnimation.duration=1;
theAnimation.repeatCount=1;
theAnimation.autoreverses=NO;

theAnimation.fromValue=[NSNumber numberWithFloat:0];
theAnimation.toValue=[NSNumber numberWithFloat:self.view.frame.size.width/2];

theAnimation.removedOnCompletion = NO;
theAnimation.fillMode = kCAFillModeForwards;

[CATransaction setCompletionBlock:^{
    NSLog(@"finished");
    self.view.userInteractionEnabled = true;
}];
[ImageView.layer addAnimation:theAnimation forKey:@"slide"];
[CATransaction commit];

2 个答案:

答案 0 :(得分:0)

在上面的代码中,而不是

self.view.userInteractionEnabled = true;

使用此

ImageView.userInteractionEnabled = YES;

答案 1 :(得分:0)

你可以试试这个。

BOOL animationPause;

ImageView = [[UIImageView alloc]initWithFrame:CGRectMake(250, 300, 100, 100)];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageViewClicked)];
    singleTap.numberOfTapsRequired = 1;
    [ImageView addGestureRecognizer:singleTap];

    [self.view addSubview:ImageView];

    ImageView.image = [UIImage imageNamed:@"earth.jpeg"];

    [UIView animateWithDuration:1.0 animations:^{
        for (int i = 0; i<5; i++) {
             ImageView.frame = CGRectMake(ImageView.frame.origin.x, ImageView.frame.origin.y+i+5, ImageView.frame.size.width, ImageView.frame.size.height)  ;
            if (i==4) {
                ImageView.userInteractionEnabled = YES;

            }
if (animationPause == YES)
                [UIView setAnimationDidStopSelector:@selector(animateStop)];
            else
                [UIView setAnimationDidStopSelector:@selector(Resume)];
        }

        }
    }];

没有必要使用CABasicAnimation实际上在你的场景中你的图像视图最初有一些框架,你已经添加了手势或其他东西。在为图像设置动画时,根据动画要求,它将变为x或y。仍处于初始帧的dyour imageView帧。所以你的点击事件方法将被调用。

但是,当您点击特定帧时是否有imageView,您会注意到您的方法将被调用。

您已管理 animationPause 以暂停和恢复动画。 希望它会奏效。如果您还有任何疑问,请告知我