简体   繁体   中英

UIView transitionWIthView simple change image animation — how to explicitly mention ‘self’

Background

I have an iPhone app I wrote in ObjectiveC about eight years ago that works just fine. However it is five years since I last updated it (or indeed did any iOS work) and Apple is threatening to throw it off the App Store unless I submit an update. It is of use to a niche audience and I wish to keep it there. I have managed to deal with most of the “issues” raised (deprecated methods etc.) but there is one “Semantic Issue” regarding an image flip animation that I cannot seem to find an answer to.

Semantic Issue

Block implicitly retains 'self'; explicitly mention 'self' to indicate this is the intended behaviour.

Code that generates this issue

I have marked the two lines that Xcode flags with ⌫

if (segControl.selectedSegmentIndex == 0)
{
    [UIView transitionWithView:pictureView
                        duration:0.5
                        options:UIViewAnimationOptionTransitionFlipFromRight
                        animations:^{ pictureView.image = image;}  ⌫
                        completion:nil];
}
else
{
    [UIView transitionWithView:pictureView
                        duration:0.5
                        options:UIViewAnimationOptionTransitionFlipFromRight
                        animations:^{ pictureView.image = digImage;}   ⌫
                        completion:nil];
}

pictureView is the UIView which is an instance variable of the class associated with an IBOutlet.

This block of code is contained in a method (IBAction)changeSegment which first initializes two UIimages, image and digImage , to a pair of jpgs appropriate to the instance.

The idea — obvious, I am sure — is that on taping pictureView the image associated with it (initially a photo) is flipped (to a corresponding diagram); on tapping it again it is flipped back.

I can't remember where I got the code originally (as I said, it works), but the animation section differs from examples I can find on the web and on Apple's Developer page . I tried throwing 'selfs' in everywhere I could think of, but, not really understanding the problem, was not too surprised that this failed.

Question

So how do I modify this code to “explicitly mention self”?

Implicitly means, that written pictureView.image = image; but will be compiled as: self.pictureView.image = image;

So just replace it to how it will be compiled, to make compiler sure, that you know that you are passing self inside of block and you doesn't care about possible retain cycled caused by passing self inside of block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM