Sumi Sumi - 23 days ago 13
Objective-C Question

How to enable interaction in imageview on animation completion

I am using

CABasic
animation to move an
UIImageView
.
UIImageView
is not detecting touches now how to enable user interaction after the completion of animation?

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

Answer

You can try This.

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

        }
    }];

There is no need to use CABasicAnimation Actually in your scenario your image view having some frame initially and you have added gesture or something. Right when you animate your image will go x or y based on animation requirement. an dyour imageView frame having still at initial frame. so your click event Method will called.

But yes when you click on particular frame whether there is imageView or not you will notice your method will called.

You have manage animationPause for pausing and resuming animation. Hope it will work. if you still any query let me inform

Comments