Search code examples
iosobjective-canimationuiviewpropertyanimator

animator.startAnimation -- What does this warning mean?


I'm trying to learn how to use UIViewPropertyAnimator in objc. I made a simple test app with an object called 'blueBox'. I want to vary the properties of blueBox.

I declare 'animator' outside of @implementation ... @end:

UIViewPropertyAnimator *animator;

then define it like so:

- (void)viewDidLoad {
   [super viewDidLoad];
   CGRect newFrame = CGRectMake(150.0, 350.0, 100.0, 150.0);
   animator = [[UIViewPropertyAnimator alloc]
               initWithDuration:2.0
               curve:UIViewAnimationCurveLinear
               animations:^(void){
      self.blueBox.frame = newFrame;
      self.blueBox.backgroundColor = [UIColor redColor];
   }];
}

When I want to use it I write:

animator.startAnimation;

It works as expected (changes the object's color and frame) but there is a warning on 'animator.startAnimation;' that says "Property access result unused - getters should not be used for side effects". What property access result is that referring to? How should I write that so I don't get a warning?


Solution

  • startAnimation is a method, not a property. You should write:

    [animator startAnimation];
    

    Though Objective-C does allow you to use property syntax when calling a method that takes no parameters, your use is written like you are attempting to read a property value. But since (obviously) you make no attempt to store the result (there isn't one), the compiler complains you are ignoring the accessed value.

    Simply avoid the wrong syntax and you avoid the issue.

    BTW, you claim that the line:

    UIViewPropertyAnimator *animator;
    

    is outside the @implementation / @end pair. That makes it a file global variable. Is that what you really want? If you want it to be an instance variable of the class (which is probably what you really want), it should be:

    @implementation YourClass {
        UIViewPropertyAnimator *animator; //instance variable
    }
    
    // your methods
    
    @end