美文网首页
autoreverses(CAMedia Timing)

autoreverses(CAMedia Timing)

作者: 淘代码者 | 来源:发表于2017-08-18 10:55 被阅读0次
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

相关文章

  • autoreverses(CAMedia Timing)

    参考 添加手势效果

  • timer

    Is it still annoying for the timing of the timing? This i...

  • animation-timing-function与三次贝塞尔曲

    animation-timing-function 和 transition-timing-function控制动...

  • animation

    animation-timing-function transition-timing-function抛物线动画...

  • Perl 脚本记录1

    Summary_Timing_Report.pl (适用于Innovus Timing report 处理)

  • Timing

    春节幼儿园开园后只要正常上下班,我都坚持早上七点起床做早餐,虽然早餐品种不多,重要的是早起规律的早餐,小朋友...

  • Timing

    (1) 先生是一個很相信命的人,聽說在與我確定關係之前各種地辦法把我的時...

  • timing

    爱情是讲timing的。 友情,也是讲timing的。 朋友一路走一路丢,数数到现在还常联系的已不多。 上大学的时...

  • Timing

    回家,打开冰箱, 发现9月初从大西北千辛万苦带回来的昂贵的百合好像开始胀包了~ 这是老赫妈妈说带给我家人的西北特产...

  • Timing

    Go!

网友评论

      本文标题:autoreverses(CAMedia Timing)

      本文链接:https://www.haomeiwen.com/subject/pnnorxtx.html