autoreverses(CAMedia Timing)

QQ20170818-105123.gif
    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];

参考

添加手势效果

QQ20170818-153934.gif
//
//  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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天听到了一个故事,提到了一只玩具熊——帕丁顿熊。刚一听到这个名字的时候,我脑海中似乎闪过那么一点点印象——可能是...
    中立生物阅读 3,738评论 0 0
  • 大概每个人的童年时期,都有一片dream land——没有条框的束缚,不需要严密的逻辑,只管天马行空、上天入地。 ...
    英会阅读 8,791评论 1 8
  • 当风吹过不留一丝痕迹的时候 我就开始唱歌 伴着哭泣 并不是在哀求什么 也没有什么远离使我悲痛 我只是单纯的想要落泪...
    無魚阅读 3,051评论 4 1
  • 亲爱的女神,我谈到了要在心理上和父母分离,我建议你要成为自己喜欢的女神,而不是成为别人希望你成为的样子。那么今天,...
    清水微甜的日常阅读 1,800评论 0 0