将 UIImageView 旋转并平移到屏幕外或屏幕上

发布于 2024-12-27 13:54:49 字数 1294 浏览 1 评论 0原文

如何在屏幕的一侧旋转并平移 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;

不知道这是否是正确的方法,所以请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱没勇气 2025-01-03 13:54:49

试试这个,它会对你有用

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

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"];
赠意 2025-01-03 13:54:49

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")

Swift 5.4.2 (based on Sanjeev Rao reply):

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文