类似开门的动画-iOS
来源:互联网 发布:安卓内存修改器源码 编辑:程序博客网 时间:2024/03/29 12:35
首先说一下CALayer的两个属性 position与anchorPoint
/* The position in the superlayer that the anchor point of the layer's * bounds rect is aligned to. Defaults to the zero point. Animatable. */@property CGPoint position;/* Defines the anchor point of the layer's bounds rect, as a point in * normalized layer coordinates - '(0, 0)' is the bottom left corner of * the bounds rect, '(1, 1)' is the top right corner. Defaults to * '(0.5, 0.5)', i.e. the center of the bounds rect. Animatable. */@property CGPoint anchorPoint;
有上面定义可知,position是锚点anchorPoint在superlayer中的位置。position点是相对superlayer的,anchorPoint点是相对layer的,两者是相对不同的坐标空间的一个重合点。
锚点的作用就是作为变换的支点,如旋转、平移、缩放等。
并且position是根据anchorPoint而定的,其变换为:
__.layer.position.x = __.layer.frame.origin.x + __.layer.anchorPoint.x * __.layer.bounds.size.width; __.layer.position.y = __.layer.frame.origin.y + __.layer.anchorPoint.y * __.layer.bounds.size.height;
这里就不说太多理论了,否则是说不完的。
下面说重点,类似开门关门的动画
直接上代码:
APPDelegate:
#import "AppDelegate.h"#import "ViewController.h"#import "SecondViewController.h"@interface AppDelegate (){ UIWindow * _leftWindow;}@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window.rootViewController = [[ViewController alloc] init]; _leftWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; _leftWindow.windowLevel = UIWindowLevelNormal - 1; _leftWindow.rootViewController = [[SecondViewController alloc] init]; [_leftWindow makeKeyAndVisible]; return YES;}ViewController:
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor redColor]; UIView * view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 200, 200)]; view1.backgroundColor = [UIColor whiteColor]; [self.view addSubview:view1]; UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; [btn setTitle:@"click" forState:UIControlStateNormal]; [self.view addSubview:btn]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];}- (void) btnClick { UIWindow * currentWindow = self.view.window; CGPoint oldAnchorPoint = currentWindow.layer.anchorPoint; currentWindow.layer.anchorPoint = CGPointMake(1, 0.5); CGPoint p = CGPointMake(currentWindow.layer.position.x + currentWindow.layer.bounds.size.width * (currentWindow.layer.anchorPoint.x - oldAnchorPoint.x), currentWindow.layer.position.y +currentWindow.layer.bounds.size.height * (currentWindow.layer.anchorPoint.y - oldAnchorPoint.y)); [currentWindow.layer setPosition:p]; if (currentWindow.frame.origin.x > 0) { [UIView animateWithDuration:1.0 delay:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{ currentWindow.layer.transform = CATransform3DIdentity; currentWindow.frame = [[UIScreen mainScreen] bounds]; } completion:^(BOOL finished) { }]; }else{ currentWindow.layer.transform = CATransform3DIdentity; CGFloat angle = -60 * M_PI / 180;//控制旋转角度 CATransform3D dh = currentWindow.layer.transform; dh.m34 = 1.0 / - 600;//这个是最重要的,控制旋转方向 dh = CATransform3DRotate(dh, angle, 0, 1, 0); [UIView animateWithDuration:1.0 delay:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{ currentWindow.layer.transform = dh; } completion:^(BOOL finished) { }]; }}
SecondViewController:
#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor purpleColor]; UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; [btn setTitle:@"click" forState:UIControlStateNormal]; [self.view addSubview:btn]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];}- (void) btnClick { UIWindow * currentWindow = [[UIApplication sharedApplication] keyWindow]; [UIView animateWithDuration:1.0 delay:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{ currentWindow.layer.transform = CATransform3DIdentity; currentWindow.frame = [[UIScreen mainScreen] bounds]; } completion:^(BOOL finished) { }]; }
源码传送:https://github.com/dzonel/ios--simple-menu.git
0 0
- 类似开门的动画-iOS
- 开门动画
- 微信开门动画的实现
- IOS 简单动画 用于加分 之类的 类似血瓶
- android实现类似微信的开门效果
- android-微信引导图类似开门的效果
- iOS动画进阶 - 实现类似美颜相机的相机启动动画
- 单侧开门动画
- android开门动画效果
- iOS 类似小红书闪动标签点动画
- Android UI设计之<二>自定义SwitchButton开关,实现类似IOS中UISwitch的动画效果
- 开门见iOS(1)
- 开门见iOS(2)
- 开机动画开门效果实现中遇到的隐式动画问题
- 类似google 首页的 png动画
- 类似余额宝数值增加的动画
- 类似QQ点赞的动画效果
- 类似加入购物车的抛物线动画
- Address already in use: JVM_Bind<null>:8080
- Android bootchart(二)
- 代码规范
- NSThread
- Mac OS X / IOS 内存管理机制简要说明
- 类似开门的动画-iOS
- OC基础知识 -- 类的扩展
- 【总结】10月训练赛第5场(8中)
- Apache配置虚拟目录和多主机头
- nginx负载均衡基于ip_hash的session粘帖
- 设计模式
- NSOperation
- ios About the Virtual Memory System
- [Servlet]微博应用程序(2):会话管理