关闭和在屏幕上旋转和翻译UIImageView

时间:2012-01-16 08:57:17

标签: iphone ios animation uiimageview cabasicanimation

如何在屏幕的一侧旋转和翻译UIImageView,然后从另一侧返回.. 让我说我有一个轮子,我想旋转并从中间翻转,然后从屏幕向左翻转,然后从右侧“返回”并返回中间..

我使用以下代码旋转并将其翻译出屏幕;

        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如果这是正确的方法,那么请帮助!

2 个答案:

答案 0 :(得分:4)

试试这个,它会为你工作

  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"];

答案 1 :(得分:0)

Swift 5.4.2(基于 Sanjeev Rao 的回复):

let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.toValue = NSNumber(floatLiteral: Double.pi * 2.0 /* full rotation*/ * 2 * 1)
rotationAnimation.duration = 1
rotationAnimation.isCumulative = true
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = CAMediaTimingFunction(name: .easeOut)
rotationAnimation.delegate = self // can be skipped
        
let translationAnimation = CABasicAnimation(keyPath: "transform.rotation.x")
translationAnimation.toValue = NSNumber(floatLiteral: -700)
translationAnimation.duration = 1
translationAnimation.isCumulative = true
translationAnimation.repeatCount = 1.0
translationAnimation.timingFunction = CAMediaTimingFunction(name: .easeOut)
translationAnimation.isRemovedOnCompletion = false
translationAnimation.fillMode = .forwards
        
let groupAnimation = CAAnimationGroup()
groupAnimation.animations = [rotationAnimation, translationAnimation] 
group.delegate = self // can be skipped
groupAnimation.isRemovedOnCompletion = false
groupAnimation.timingFunction = CAMediaTimingFunction(name: .easeOut)
        
self.imageView.layer.add(groupAnimation, forKey: "randt")