温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UIView翻转效果如何实现

发布时间:2022-10-18 15:46:57 来源:亿速云 阅读:137 作者:iii 栏目:编程语言

今天小编给大家分享一下UIView翻转效果如何实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

新建一个view-based模板工程,在ViewController文件中添加下面的代码,即可实现翻转效果;
 
- (void)viewDidLoad {
    [super viewDidLoad];
 
//需要翻转的视图
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 200)];
parentView.backgroundColor = [UIColor yellowColor];
parentView.tag = 1000;
 
[self.view addSubview:parentView];
}
 
//需要在h头文件声明下面的动作响应函数
//在xib文件中添加一个button,其响应函数为下面的函数
//运行程序后,点击button就看到翻转效果
-(IBAction)ActionFanzhuan{
 
//获取当前画图的设备上下文
CGContextRef context = UIGraphicsGetCurrentContext();
 
//开始准备动画
[UIView beginAnimations:nil context:context];
 
//设置动画曲线,翻译不准,见苹果官方文档 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
 
//设置动画持续时间
[UIView setAnimationDuration:1.0];
 
//因为没给viewController类添加成员变量,所以用下面方法得到viewDidLoad添加的子视图
UIView *parentView = [self.view viewWithTag:1000];
 
//设置动画效果
[UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:parentView cache:YES];  //从上向下
// [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:parentView cache:YES];   //从下向上
// [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:parentView cache:YES];  //从左向右
// [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:parentView cache:YES];//从右向左
 
//设置动画委托
[UIView setAnimationDelegate:self];
 
//当动画执行结束,执行animationFinished方法
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
 
//提交动画
[UIView commitAnimations];
}
 
//动画效果执行完毕
- (void) animationFinished: (id) sender{
NSLog(@"animationFinished !");
}
 
 
 
运行程序,点击按钮,就能看到动画效果了
 
 
 
 
下面我自己在parentView上添加了两个子视图实现动画
 
- (void)viewDidLoad {
     [super viewDidLoad];
 
 
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 200)];
parentView.backgroundColor = [UIColor yellowColor];
parentView.tag = 1000;
 
 
UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
image1.backgroundColor = [UIColor redColor];
image1.tag = 1001;
 
 
UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
image2.backgroundColor = [UIColor blueColor];
image2.tag = 1002;
 
 
[parentView addSubview:image1];
[parentView addSubview:image2];
 
 
[self.view addSubview:parentView];
}
 
-(IBAction)ActionFanzhuan{
 
 
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];

 
 
UIView *parentView = [self.view viewWithTag:1000];
 
 
[UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:parentView cache:YES];
// [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:parentView cache:YES];
// [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:parentView cache:YES];
// [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:parentView cache:YES];
 
 
NSInteger purple = [[parentView subviews] indexOfObject:[parentView viewWithTag:1002]];
NSInteger maroon = [[parentView subviews] indexOfObject:[parentView viewWithTag:1001]];
[parentView exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon];

 
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];

[UIView commitAnimations];
 
}
 
- (void) animationFinished: (id) sender{
NSLog(@"animationFinished !");
}
 
 
 
另外:之前在viewDidLoad里面写实现动画的代码,但一致未实现动画效果,原来在viewDidLoad里面执行
CGContextRef context = UIGraphicsGetCurrentContext();
后context的指针为0

以上就是“UIView翻转效果如何实现”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI