Multiple simultaneous UIViewAnimations on one UIView

Purpose :
At the same time, several animations are played on one UIView.
Problem :
From what I can say, this is impossible. Each animation must be performed sequentially and cannot overlap.

In essence, I want one UIView to animate vertically while animating horizontally. Since I'm using UIViewAnimationOptionRepeat , I can not insert any other animation in onCompletion: . Here I want to work, but not:

[UIView animateWithDuration:duration
                      delay:kANIMATION_DELAY
                    options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{

                        object.frame = CGRectMake(object.frame.origin.x + 40,
                                           object.frame.origin.y,
                                           object.frame.size.width,
                                           object.frame.size.height);

        [UIView animateWithDuration:duration
                              delay:kANIMATION_DELAY
                            options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{

                                object.frame = CGRectMake(object.frame.origin.x,
                                                   object.frame.origin.y - 40,
                                                   object.frame.size.width,
                                                   object.frame.size.height);

                             } completion:^(BOOL finished) {
        }];

                    } completion:^(BOOL finished) {
}];
+4
source share
1 answer

UIViewAnimation, CABasicAnimations. (x, y) . :

    UIView *view = views[i];

    // Add the horizontal animation
    CABasicAnimation *horizontal = [CABasicAnimation animationWithKeyPath:@"position.x"];

    horizontal.delegate = self;
    horizontal.fromValue = [NSNumber numberWithFloat:25.0];
    horizontal.toValue = [NSNumber numberWithFloat:50.0];
    horizontal.repeatCount = INFINITY;
    horizontal.duration = 6;
    horizontal.autoreverses = YES;
    horizontal.beginTime = 0; // ignore delay time for now
    horizontal.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    [view.layer addAnimation:horizontal forKey:@"horizontal_animation"];

    // Add the vertical animation
    CABasicAnimation *vertical = [CABasicAnimation animationWithKeyPath:@"position.y"];

    vertical.delegate = self;
    vertical.fromValue = [NSNumber numberWithFloat:100.0];
    vertical.toValue = [NSNumber numberWithFloat:400.0];
    vertical.repeatCount = INFINITY;
    vertical.duration = 2;
    vertical.autoreverses = YES;
    vertical.beginTime = 0; // ignore delay time for now
    vertical.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    [view.layer addAnimation:vertical forKey:@"vertical_animation"];

, .. . , , follow . ,

    [view.layer removeAllAnimations];

, ,

    [view.layer removeAnimationForKey:@"vertical_animation"];

, , /, :

    -(void)animationDidStart:(CAAnimation *)anim {
        // custom code here
    }

    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
        // custom code here
    }

. , - . !

+4

All Articles