Good articles to my personal technology blog: https://cainluo.github.io/14972802953896.html


UIViewPropertyAnimator

When we were doing basic animations, the basic code was:

    [UIView animateWithDuration:1
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         
                         // Do Something
                     } completion:^(BOOL finished) {
                         
                         // Do Something
                     }];
Copy the code

Generally will go to their own package, so that the large string of code here disgusting….

But that’s no longer true, since Apple’s dad came out with a framework called UIViewPropertyAnimator in iOS 10, which is a much simpler way to get rid of a lot of code, and I have the Objective-C version here, so if you want to see the Swift version, you can go to the Swift version


Create a project

Without further ado, let’s look at the code:

The methods of opening to the outside world include:

The code for running an animation with UIViewPropertyAnimator:

Effect:

I think a lot of people are going to say, well, what’s wrong with this, I can fix that API, so let’s move on.


Why UIViewPropertyAnimator

Here’s the official version:

The UIViewPropertyAnimator API is well designed and extensible. It covers most of the functions of a traditional UIView animation and greatly increases your control over the animation process. Specifically, you can pause at any point in the animation, continue later, and even dynamically change the properties of the animation during the animation (for example, you can change the ending point of the animation to the upper right corner of the screen from the lower left corner).

After reading the introduction, let’s add a code to see if it is true:

    // Add one more fading animation
    [propertyAnimator addAnimations:^{
        self.basicsView.alpha = 0;
    }];
Copy the code

Effect:

In addition to this, if you want to add post-animation processing, even some operations during animation can also be added:

    [propertyAnimator addCompletion:^(UIViewAnimatingPosition finalPosition) {
        
        NSLog("Animation is over.");
    }];
    
    [propertyAnimator addCompletion:^(UIViewAnimatingPosition finalPosition) {
        
        switch (finalPosition) {
                
            case UIViewAnimatingPositionStart:
                
                NSLog(@" Animation started");
                
                break;
                
            case UIViewAnimatingPositionCurrent:
                
                NSLog("Animation is over.");

                break;
                
            case UIViewAnimatingPositionEnd:
                
                NSLog(@" Performing animation");

                break;
            default:
                break; }}];Copy the code

Add a dragger

UIViewPropertyAnimator can not only write specified animations, but can also be used with other controls, such as UISlider, for convenience, I’m just going to set UIViewPropertyAnimator *propertyAnimator to a global property:

@property (nonatomic.strong) UIViewPropertyAnimator *propertyAnimator;
Copy the code

Then start implementing our drag animation:

- (void)addSliderWithSuperView {
    
    self.propertyAnimator = [[UIViewPropertyAnimator alloc] initWithDuration:5
                                                                       curve:UIViewAnimationCurveEaseIn
                                                                  animations:^{
                                                                                         
                                                                      [self.basicsView moveViewToBottmRight];
                                                                  }];

    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0.100.self.view.frame.size.width, 40)];
    
    [slider addTarget:self
               action:@selector(sliderValueChanged:)
     forControlEvents:UIControlEventValueChanged];
    
    [self.basicsView addSubview:slider];
}

- (void)sliderValueChanged:(UISlider *)slider {
    
    self.propertyAnimator.fractionComplete = slider.value;
}
Copy the code

Effect:


Reverse the animation

If you think that’s the end of it, it’s boring:

/** reverse animation */
- (void)viewAnimationCurveEaseInOut {
    
    UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width / 2) - 100.100.200.50)];
    
    [resetButton setTitleColor:[UIColor blackColor]
                      forState:UIControlStateNormal];
    [resetButton setTitle:@" Start backanimation"
                 forState:UIControlStateNormal];
    [resetButton addTarget:self
                    action:@selector(resetButtonAction)
          forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:resetButton];
    
    self.propertyAnimator = [[UIViewPropertyAnimator alloc] initWithDuration:3
                                                                       curve:UIViewAnimationCurveEaseInOut
                                                                  animations:^{
                                                                      
                                                                      [self.basicsView moveViewToBottmRight];
                                                                  }];
    [self.propertyAnimator startAnimation];
}

/** button click event */
- (void)resetButtonAction {
    
    self.propertyAnimator.reversed = YES;
}
Copy the code

Effect:


UISpringTimingParameters

This can also be used in conjunction with UISpringTimingParameters:

- (void)springTimingParameters {
    
    weakSelf(weakSelf);

    UISpringTimingParameters *springTimingParameters = [[UISpringTimingParameters alloc] initWithDampingRatio:0.3
                                                                                              initialVelocity:CGVectorMake(0.0)];
    
    self.propertyAnimator = [[UIViewPropertyAnimator alloc] initWithDuration:4
                                                            timingParameters:springTimingParameters];
    
    [self.propertyAnimator addAnimations:^{
        
                            [weakSelf.basicsView moveViewToBottmRight];
    } delayFactor:3.0];
    
    [self.propertyAnimator startAnimation];
}
Copy the code

Effect:


UICubicTimingParameters

You can also play with UICubicTimingParameters

/** UICubicTimingParameters animation */
- (void)cubicTimingParameters {
    
    weakSelf(weakSelf);

    UICubicTimingParameters *cubicTimingParameters = [[UICubicTimingParameters alloc] initWithControlPoint1:CGPointMake(0.05.0.95)
                                                                                              controlPoint2:CGPointMake(0.15.0.95)];
    
    self.propertyAnimator = [[UIViewPropertyAnimator alloc] initWithDuration:4
                                                            timingParameters:cubicTimingParameters];
    
    
    [self.propertyAnimator addAnimations:^{
        
        [weakSelf.basicsView moveViewToBottmRight];
    } delayFactor:0.25];
    
    [self.propertyAnimator startAnimation];
}
Copy the code

Effect:

It is probably introduced here, more complex gameplay is waiting for you to slowly dig ~~


The project address

The address of the project: https://github.com/CainRun/iOS-10-Characteristic/tree/master/4.UIViewPropertyAnimator


The last