CALayer *doorLayer = [CALayer layer];
doorLayer.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds)/2, 300);
doorLayer.position = CGPointMake(0, 150);
doorLayer.anchorPoint = CGPointMake(0, 0.5);
doorLayer.contents = (__bridge id)[UIImage imageNamed: @"Door"].CGImage;
[self.view.layer addSublayer:doorLayer];
//apply perspective transform
CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = -1.0 / 500.0;
doorLayer.sublayerTransform = perspective;
//apply swinging animation
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.rotation.y";
animation.toValue = @(-M_PI_2);
animation.duration = 2.0;
animation.repeatDuration = INFINITY;
animation.autoreverses = YES;
[doorLayer addAnimation:animation forKey:nil];
CALayer *rightDoorLayer = [CALayer layer];
rightDoorLayer.frame = CGRectMake(0, 0, 200, 300);
rightDoorLayer.position = CGPointMake(CGRectGetWidth([UIScreen mainScreen].bounds), 150);
rightDoorLayer.anchorPoint = CGPointMake(1, 0.5);
rightDoorLayer.contents = (__bridge id)[UIImage imageNamed: @"Door"].CGImage;
[self.view.layer addSublayer:rightDoorLayer];
//apply perspective transform
rightDoorLayer.sublayerTransform = perspective;
//apply swinging animation
CABasicAnimation *rightAnimation = [CABasicAnimation animation];
rightAnimation.keyPath = @"transform.rotation.y";
rightAnimation.toValue = @(M_PI_2);
rightAnimation.duration = 2.0;
rightAnimation.repeatDuration = INFINITY;
rightAnimation.autoreverses = YES;
[rightDoorLayer addAnimation:rightAnimation forKey:nil];
参考
添加手势效果
//
// ViewController.m
// RotationDoor
//
// Created by apple on 17/8/18.
// Copyright © 2017年 Wang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) CALayer *doorLayer;
@property (nonatomic, strong) CALayer *rightDoorLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CALayer *doorLayer = [CALayer layer];
doorLayer.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds)/2, 300);
doorLayer.position = CGPointMake(0, 150);
doorLayer.anchorPoint = CGPointMake(0, 0.5);
doorLayer.contents = (__bridge id)[UIImage imageNamed: @"Door"].CGImage;
[self.view.layer addSublayer:doorLayer];
self.doorLayer = doorLayer;
//apply perspective transform
CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = -1.0 / 500.0;
doorLayer.sublayerTransform = perspective;
doorLayer.speed = 0.f;
//apply swinging animation
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.rotation.y";
animation.toValue = @(-M_PI_2);
animation.duration = 1.0;
// animation.repeatDuration = INFINITY;
animation.autoreverses = YES;
[doorLayer addAnimation:animation forKey:nil];
CALayer *rightDoorLayer = [CALayer layer];
rightDoorLayer.frame = CGRectMake(0, 0, 200, 300);
rightDoorLayer.position = CGPointMake(CGRectGetWidth([UIScreen mainScreen].bounds), 150);
rightDoorLayer.anchorPoint = CGPointMake(1, 0.5);
rightDoorLayer.contents = (__bridge id)[UIImage imageNamed: @"Door"].CGImage;
[self.view.layer addSublayer:rightDoorLayer];
self.rightDoorLayer = rightDoorLayer;
//apply perspective transform
rightDoorLayer.sublayerTransform = perspective;
rightDoorLayer.speed = 0.f;
//apply swinging animation
CABasicAnimation *rightAnimation = [CABasicAnimation animation];
rightAnimation.keyPath = @"transform.rotation.y";
rightAnimation.toValue = @(M_PI_2);
rightAnimation.duration = 1.0;
// rightAnimation.repeatDuration = INFINITY;
rightAnimation.autoreverses = YES;
[rightDoorLayer addAnimation:rightAnimation forKey:nil];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
[pan addTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
//get horizontal component of pan gesture
CGFloat x = [pan translationInView:self.view].x;
//convert from points to animation duration //using a reasonable scale factor
x /= 300.0f;
//update timeOffset and clamp result
CFTimeInterval timeOffset = self.doorLayer.timeOffset;
timeOffset = MIN(0.999, MAX(0.0, timeOffset - x));
self.doorLayer.timeOffset = timeOffset;
self.rightDoorLayer.timeOffset = timeOffset;
//reset pan gesture
[pan setTranslation:CGPointZero inView:self.view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end