Rotate and translate UIImageView off and on screen

2019-08-01 04:36发布

问题:

How can I rotate and translate a UIImageView off one side of the screen, then its coming back from the other side.. Lets say I have a Wheel that I want to rotate and translate from the middle then off the screen to the left, then its "coming back" from the right side and back the middle..

I used following code to rotate and translate it OFF the screen;

        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: -M_PI * 2.0 /* full rotation*/ * 2 * 1 ];
        rotationAnimation.duration = 1;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = 1.0; 
        rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        rotationAnimation.delegate = self;

        CABasicAnimation* translationAnimation;
        translationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        translationAnimation.toValue = [NSNumber numberWithFloat:-700];
        translationAnimation.duration = 1;
        translationAnimation.cumulative = YES;
        translationAnimation.repeatCount = 1.0; 
        translationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        translationAnimation.removedOnCompletion = NO;
        translationAnimation.fillMode = kCAFillModeForwards;

Dunno if this is the right way to go, so please help!

回答1:

try This, it will Work for you

  CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: -M_PI * 2.0 /* full rotation*/ * 2 * 1 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1.0; 
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    rotationAnimation.delegate = self;

    CABasicAnimation* translationAnimation;
    translationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    translationAnimation.toValue = [NSNumber numberWithFloat:-700];
    translationAnimation.duration = 1;
    translationAnimation.cumulative = YES;
    translationAnimation.repeatCount = 1.0; 
    translationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    translationAnimation.removedOnCompletion = NO;
    translationAnimation.fillMode = kCAFillModeForwards;



    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray arrayWithObjects:rotationAnimation,translationAnimation, nil];
    group.delegate = self;
    group.removedOnCompletion = NO;
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    [[self.imageView.layer addAnimation:group forKey:@"randt"];