Use of beginAnimations discouraged

前端 未结 2 853
时光取名叫无心
时光取名叫无心 2020-12-19 00:23

I was informed recently by meronix that use of beginAnimations is discouraged. Reading through the UIView class reference I see that this is indeed

相关标签:
2条回答
  • 2020-12-19 01:06

    Check out this method on UIView, which makes it quite simple. The trickiest part nowadays is not allowing a block to have a strong pointer to self:

    //Pushes the view up if one of the table forms is selected for editing
    - (void) keyboardDidShow:(NSNotification *)aNotification
    {
      if ([isRaised boolValue] == NO)
      {
        __block UIView *myView = self.view;
        [UIView animateWithDuration:0.25 animations:^(){
          myView.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount);
         }];
        isRaised = [NSNumber numberWithBool:YES];
      }
    }
    
    0 讨论(0)
  • 2020-12-19 01:15

    They are discouraged because there is a better, cleaner alternative

    In this case all a block animation does is automatically wrap your animation changes (setCenter: for example) in begin and commit calls so you dont forget. It also provides a completion block, which means you don't have to deal with delegate methods.

    Apple's documentation on this is very good but as an example, to do the same animation in block form it would be

    [UIView animateWithDuration:0.25 animations:^{
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount);
    } completion:^(BOOL finished){
    }];
    

    Also ray wenderlich has a good post on block animations: link

    Another way is to think about a possible implementation of block animations

    + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
    {
        [UIView beginAnimations];
        [UIView setAnimationDuration:duration];
        animations();
        [UIView commitAnimations];
    }
    
    0 讨论(0)
提交回复
热议问题