像硬币一样旋转图像

6

我有一张图片,想要让它像硬币一样在表面上旋转。我尝试过使用旋转变换,但它不会像那样旋转。如何实现这样的动画效果?

enter image description here

代码:

- (void)viewDidLoad {
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
lbl_facebook.font=[UIFont fontWithName:GZFont size:12.0f];
txtPassword.font=[UIFont fontWithName:GZFont size:15.0f];
txtUsername.font=[UIFont fontWithName:GZFont size:15.0f];

CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = @"flip";
transition.subtype = @"fromRight";
transition.duration = 0.3;
transition.repeatCount = 2;
[_Image.layer addAnimation:transition forKey:@"transition"];
}

nd:

#import "LoginViewController.h"

#import "RegistrationViewController.h"
#import "ForgetPasswordViewController.h"
#import "ForgetPasswordController.h"
#import "SearchServiceProviderViewController.h"
#import <QuartzCore/QuartzCore.h>

1
尝试类似这样的代码:https://dev59.com/gWw15IYBdhLWcg3wkMnz - Radu Dan
1
使用 CATransform3DRotate - Himanshu Joshi
2个回答

19
这将制作出一个漂亮的、类似硬币的翻转效果:
CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = @"flip";
transition.subtype = @"fromRight";
transition.duration = 0.3;
transition.repeatCount = 2;

并将过渡动画添加到您的视图层中:

[_yourView.layer addAnimation:transition forKey:@"transition"];

看它如何运行:

输入图像描述


为什么它不起作用?你是否已经导入了Quartzcore Framework? - GenieWanted
也许您可以发布您的代码?否则我们怎么能帮助您呢?帮助我们帮助您。 - Rafał Sroka
1
@vivek,不要在viewDidLoad中运行动画。那时视图还没有显示出来,框架也没有设置好,所以无法正常工作。如果想看到效果,请至少在viewDidAppear方法中运行它。 - Rafał Sroka

3

有一个技巧可以让硬币像这样旋转,就是拍摄不同角度的硬币图像,就像旋转图像一样。然后将所有这些图像添加到数组中,并使用该数组启动图像的动画... 这将给你更好的效果... 就像视频制作一样简单。

例如:

NSArray *animationArray = [NSArray arrayWithObjects:
                          [UIImage imageNamed:@"images.jpg"],
                          [UIImage imageNamed:@"images1.jpg"],
                          [UIImage imageNamed:@"images5.jpg"],
                          [UIImage imageNamed:@"index3.jpg"],
                          nil];
UIImageView *animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
animationView.backgroundColor      = [UIColor purpleColor];
animationView.animationImages      = animationArray;
animationView.animationDuration    = 1.5;
animationView.animationRepeatCount = 0;
[animationView startAnimating]; 

1
@Elhoej 请检查我的编辑答案...如果有帮助,请点赞 :) - vivek bhoraniya
使用这种方法,我该如何设置动画为旋转状态?你能给我展示完整的代码示例吗?抱歉,我无法让它正常工作 :( - Elhoej
你需要多张模拟硬币旋转的图片...然后通过将所有这些图像序列传递给ImageView来启动动画...@Elhoej - vivek bhoraniya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接