CASpringAnimation的使用

CASpringAnimation的使用

 

效果

 

源码

https://github.com/YouXianMing/Animations

//
//  CASpringAnimationController.m
//  Animations
//
//  Created by YouXianMing on 16/1/19.
//  Copyright  2016年 YouXianMing. All rights reserved.
//

#import "CASpringAnimationController.h"
#import "RangeValueView.h"
#import "WxHxD.h"
#import "UIView+SetRect.h"

@interface CASpringAnimationController ()

@property (nonatomic, strong) UIButton       *showView;

@property (nonatomic, strong) RangeValueView *stiffnessView;
@property (nonatomic, strong) RangeValueView *dampingView;
@property (nonatomic, strong) RangeValueView *massView;
@property (nonatomic, strong) RangeValueView *initialVelocityView;

@end

@implementation CASpringAnimationController

- (void)setup {

    [super setup];

    [self initRangeViews];

    [self initButton];
}

- (void)initButton {

    CGFloat gap = Height - 60 - 40*4 - 64;

    CGFloat width                    = 50.f;
    self.showView                    = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, width)];
    self.showView.center             = CGPointMake(self.contentView.middleX, 64 + gap / 2.f);
    self.showView.backgroundColor    = [UIColor cyanColor];
    self.showView.layer.cornerRadius = width / 2.f;
    self.showView.x                  = Width / 2.f - 50;
    [self.showView addTarget:self action:@selector(doAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.showView];
}

- (void)doAnimation {

    CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"position.x"];
    springAnimation.stiffness          = self.stiffnessView.currentValue;
    springAnimation.mass               = self.massView.currentValue;
    springAnimation.damping            = self.dampingView.currentValue;
    springAnimation.initialVelocity    = self.initialVelocityView.currentValue;
    springAnimation.duration           = springAnimation.settlingDuration;

    springAnimation.fromValue    = @(Width / 2.f - 50);
    springAnimation.toValue      = @(Width / 2.f + 50);
    self.showView.layer.position = CGPointMake(Width / 2.f + 50, self.showView.layer.position.y);

    [self.showView.layer addAnimation:springAnimation forKey:nil];
}

- (void)initRangeViews {

    self.stiffnessView = [RangeValueView rangeValueViewWithFrame:CGRectMake(10, Height - 60, Width - 20, 0)
                                                            name:@"硬度  Stiffness"
                                                        minValue:10.f
                                                        maxValue:200.f
                                                    defaultValue:100.f];
    [self.contentView addSubview:self.stiffnessView];

    self.dampingView = [RangeValueView rangeValueViewWithFrame:CGRectMake(10, Height - 60 - 40, Width - 20, 0)
                                                          name:@"阻尼  Damping"
                                                      minValue:0.1f
                                                      maxValue:10.f
                                                  defaultValue:5.f];
    [self.contentView addSubview:self.dampingView];

    self.massView = [RangeValueView rangeValueViewWithFrame:CGRectMake(10, Height - 60 - 40*2, Width - 20, 0)
                                                       name:@"质量  Mass"
                                                   minValue:0.1
                                                   maxValue:20.f
                                               defaultValue:1.f];
    [self.contentView addSubview:self.massView];

    self.initialVelocityView = [RangeValueView rangeValueViewWithFrame:CGRectMake(10, Height - 60 - 40*3, Width - 20, 0)
                                                                  name:@"速度  Velocity"
                                                              minValue:-20.f
                                                              maxValue:20.f
                                                          defaultValue:0.f];
    [self.contentView addSubview:self.initialVelocityView];
}

@end

细节

时间: 2024-10-27 16:38:52

CASpringAnimation的使用的相关文章

[翻译] RBBAnimation,让你使用关键帧动画更便利

RBBAnimation RBBAnimation is a subclass of CAKeyframeAnimation that allows you to declare your animations using blocks instead of writing out all the individual key-frames. This gives you greater flexibility when specifying your animations while keep

iOS的CoreAnimation开发框架中的Layer层动画制作解析_IOS

CAAnimation动画体系的介绍CAAnimation是CoreAnimation框架中执行动画对象的基类,下面有一张图,是我手画的,不太美观,但是可以将与CAAnimation相关的几个动画类的关系表达清楚: 从上图中可以看到,从CAAnimation中继承出三个子类,分别是用于创建属性动画的CAPropertyAnimation,创建转场动画的CATransition和创建组合动画的CAAnimationGroup. 我们就先从根类开始探讨. 1.CAAnimation属性和方法 CAA

iOS开发CoreAnimation解读之四——Layer层动画内容

iOS开发CoreAnimation解读之四--Layer层动画内容 一.引言         通过前几篇博客的介绍,我们可以了解到layer层可以设置许多与控件UI相关的属性,并且对于iOS开发,UIView层的属性是会映射到CALayer的,因此,可以通过UIKit和CoreAnimation两个框架来设置控件的UI相关属性,当属性发生变化时,我们可以使其展示一个动画效果. 二.CAAnimation动画体系的介绍         CAAnimation是CoreAnimation框架中执行

[翻译] JNWSpringAnimation

JNWSpringAnimation https://github.com/jwilling/JNWSpringAnimation   JNWSpringAnimation is a subclass of CAKeyframeAnimation that adds support for creating damped harmonic animations. JNWSpringAnimation 是 CAKeyframeAnimation 的子类,它支持创建阻尼动画效果.   Getting