首页 > 其他分享 >UIView翻转效果实现(转)

UIView翻转效果实现(转)

时间:2022-09-28 14:03:57浏览次数:58  
标签:动画 效果 parentView context forView YES 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

标签:动画,效果,parentView,context,forView,YES,UIView,翻转
From: https://blog.51cto.com/u_13760719/5719219

相关文章

  • 代码随想录第八天 |344.反转字符串 、541. 反转字符串II、剑指Offer 05.替换空格 、15
    第一题344.反转字符串编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组s的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使......
  • 17、OpenCV灰度翻转\增强\压缩\伽马变化
    基本思想:基本原理参考OpenCV手册,此处只记录一下如何使用;#include<opencv2/opencv.hpp>#include<iostream>#include<vector>usingnamespacestd;usingnamespacecv;intma......
  • css星空效果
    <divclass="box"> <divclass="stars"></div> </div>.box{ width:100%; height:700px; overflow:hidden; perspective:340px;/*设置元素被查看位置的......
  • imageIO图像流 验证码效果案例
    packagecn.images;importjavax.imageio.ImageIO;importjava.awt.*;importjava.awt.image.BufferedImage;importjava.io.File;importjava.util.Random;publicclassCa......
  • 支持动静态智能分离,华为云CDN使用简单,增速效果明显!
     随着5G时代的到来,用户对于网络联接的要求也越来越高,实时、随需、安全、无缝地联接到人与人、人与物、物与物等已成为用户对于网络联接的新要求。作为业内领先的云服务厂......
  • cesium customShader实现建筑泛光效果
    在cesium1.87版本及以上,可使customShader用实现建筑泛光效果,代码如下//Cesium3dtile泛光效果实现(需版本1.87以上,支持customShader)constviewer=newCesium.Viewer(......
  • 直播平台怎么搭建,实现js开光灯效果
    直播平台怎么搭建,实现js开光灯效果 <!DOCTYPEhtml><html><head>  <metacharset="UTF-8">  <metaname="viewport"content="width=device-width,initial-sca......
  • CSS0027: div 倒角效果
    1,<divid="test"></div>#test{display:inline-block;width:100px;height:100px;background:linear-gradient(135d......
  • 在 WPF 中实现融合效果
    1.融合效果融合效果是指对两个接近的元素进行高斯模糊后再提高对比度,使它们看上去“粘”在一起。在之前的一篇文章中,我使用Win2D实现了融合效果,效果如下:不过Win2D......
  • 2D 消隐效果
    在触发消隐的物体(比如玩家)挂下面的代码,以及boxcollider,rigidbody2d usingUnityEngine;publicclassColliderFader:MonoBehaviour{ItemFader[]m_Faders;......