用缓动函数模时钟

1.模拟弹簧效果

- (void)keyFrameAnimation{

    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];

    keyFrameAnimation.keyPath              = @"position";

    keyFrameAnimation.duration             = 4.f;

    keyFrameAnimation.values = [YXEasing calculateFrameFromPoint:showView.center

                                                         toPoint:CGPointMake(200, 200)

                                                            func:BounceEaseInOut

                                                      frameCount:30 * 4];

    showView.center = CGPointMake(200, 200);//动画执行完还原

    [showView.layer addAnimation:keyFrameAnimation forKey:nil];

}

2.实现时钟效果

@property (nonatomic, strong) CALayer *secLayer; // 秒针layer

@property (nonatomic, strong) NSTimer *timer;    // 定时器


- (void)viewDidLoad {

    [super viewDidLoad];


    // 创建一个表盘

    UIView *showView            = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];

    showView.center             = self.view.center;

    showView.layer.borderWidth  = 1.f;

    showView.layer.cornerRadius = 150;

    showView.layer.borderColor  = [UIColor redColor].CGColor;

    [self.view addSubview:showView];

    

    // 创建出秒针layer

    self.secLayer                 = [CALayer layer];

    self.secLayer.anchorPoint     = CGPointMake(0, 0);//默认锚点为0.5,0.5

    self.secLayer.frame           = CGRectMake(150, 150, 1, 150);

    self.secLayer.backgroundColor = [UIColor blackColor].CGColor;

    [showView.layer addSublayer:self.secLayer];

    

    // 创建定时器

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f

                                                  target:self

                                                selector:@selector(timerEvent)

                                                userInfo:nil

                                                 repeats:YES];

}


- (void)timerEvent {

    

    static int i = 1;

    //度转成弧度

    CGFloat oldValue = DEGREES_TO_RADIANS((360 / 60.f) * i++);

    CGFloat newValue = DEGREES_TO_RADIANS((360 / 60.f) * i);

    

    // 创建关键帧动画

    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];

    keyFrameAnimation.keyPath              = @"transform.rotation.z";

    keyFrameAnimation.duration             = 0.5;

    keyFrameAnimation.values               = [YXEasing calculateFrameFromValue:oldValue

                                                                       toValue:newValue

                                                                          func:BounceEaseInOut 

                                                                    frameCount:0.5 * 30];

    

    self.secLayer.transform = CATransform3DMakeRotation(newValue, 0, 0, 1);

    [self.secLayer addAnimation:keyFrameAnimation forKey:nil];

}

缓动函数下载地址:http://download.csdn.net/detail/baitxaps/8890629

你可能感兴趣的:(用缓动函数模拟弹簧效果)