类似开门的动画-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
原创粉丝点击