UIView Animation on multiple UIImageViews in ARC

2019-03-06 14:46发布

I have an animation in my app that grows a UIImageView and then shrinks it (really two animations). Throughout the app this may happen on several different UIImageViews. I found a way to do this that worked really well, but it now doesn't seem to be compatible with Automatic Reference Counting. Here is my code:

[UIView beginAnimations:@"growImage" context:imageName];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
imageName.transform = CGAffineTransformMakeScale(1.2, 1.2);
[UIView commitAnimations];

and then:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(UIImageView *)context {
    if (animationID == @"growImage") {
    [UIView beginAnimations:@"shrinkImage" context:context];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDelegate:self];
    context.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView commitAnimations];
    }
}

This worked perfectly and I was very happy with it, until I tried converting my project to ARC. I now get the error "Implicit conversion of an Objective-C pointer to 'void *' is disallowed with ARC" on these lines in which I try to pass a UIImageView as the context for the animation:

[UIView beginAnimations:@"growImage" context:imageName];
[UIView beginAnimations:@"shrinkImage" context:context];

Does anybody know of another way that I can alert the "animationDidStop" function of which UIImageView I want it to act on that would be compliant with ARC?

Thanks so much in advance!

2条回答
孤傲高冷的网名
2楼-- · 2019-03-06 15:40

You can do as follows:

[UIView beginAnimations:@"growImage"
                context:(__bridge void*)imageName];
imageName.transform = ...
[UIView commitAnimations];
查看更多
狗以群分
3楼-- · 2019-03-06 15:50

Any reason you are not using the much simpler block based animation?

[UIView animateWithDuration:0.5 animation:^{
    imageName.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion ^(BOOL finished) {
    [UIView animateWithDuration:0.5 animation:^{
        imageName.transform = CGAffineTransformMakeScale(0.01, 0.01);
    }];
}];
查看更多
登录 后发表回答