浅析UIView动画初步

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

// ViewController.m
#import "ViewController.h"
#import <pop/POP.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *redView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

// 停止按钮
- (IBAction)stop {
    [self stopTransformAnimation:self.redView];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self startTransformAnimation:self.redView];
//    [self popBasicAnimation1:self.redView];
//    [self POPSpringAnimation1:self.redView];
    [self POPSpringAnimation2:self.redView];
//    [self POPSpringAnimation3:self.redView];
//    [self popBasicAnimation2:self.redView];
}

- (void)POPSpringAnimation3:(UIView *)view {
    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];
    anSpring.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];
    anSpring.beginTime = CACurrentMediaTime() + 3.0f;
    anSpring.springBounciness = 10.0f;
    [view pop_addAnimation:anSpring forKey:@"size"];
}

- (void)POPSpringAnimation2:(UIView *)view {
    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anSpring.toValue = @(view.center.y + 150);
    anSpring.beginTime = CACurrentMediaTime() + 1.0f;
    anSpring.springBounciness = 10.0f;
    anSpring.springSpeed = 10.0f;
//    __weak typeof(self) weakSelf = self;
    [anSpring setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        view.backgroundColor = [UIColor purpleColor];
    }];
    [view pop_addAnimation:anSpring forKey:@"position"];
}

// POPSpringAnimation动画
// springBounciness:4.0    //[0-20] 弹力 越大则震动幅度越大
// springSpeed     :12.0   //[0-20] 速度 越大则动画结束越快
// dynamicsTension :0      //拉力  接下来这三个都跟物理力学模拟相关 数值调整起来也很费时 没事不建议使用哈
// dynamicsFriction:0      //摩擦 同上
// dynamicsMass    :0      //质量 同上
// CACurrentMediaTime() 获取的是当前时间
- (void)POPSpringAnimation1:(UIView *)view {
    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    anSpring.toValue = @(view.center.x + 150);
    anSpring.beginTime = CACurrentMediaTime() + 1.0f;
    anSpring.springBounciness = 10.0f;
    [view pop_addAnimation:anSpring forKey:@"position"];
}

- (void)popBasicAnimation2:(UIView *)view {
    POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    anim.fromValue = @(1.0);
    anim.toValue = @(0.0);
    anim.beginTime = CACurrentMediaTime() + 3.0f;
    [view pop_addAnimation:anim forKey:@"fade"];
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {

    }];
}

// POPBasicAnimation动画
- (void)popBasicAnimation1:(UIView *)view {
    POPBasicAnimation *anBasic = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    anBasic.toValue = @(view.center.x + 100); // 自己位置基础上再偏移100
    anBasic.duration = 0.4; // 动画时间间隔
    anBasic.beginTime = CACurrentMediaTime() + 1.0f;
//    [view pop_addAnimation:anBasic forKey:@"position"]; 好像两个都可以,效果一样
    [view.layer pop_addAnimation:anBasic forKey:@"position"];
    [anBasic setCompletionBlock:^(POPAnimation *anim, BOOL finished) {

    }];
}

// 开始旋转
- (void)startTransformAnimation:(UIView*)view {
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 0.5;
//    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

// 停止旋转
- (void)stopTransformAnimation:(UIView*)view {
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 0.5;
//    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

@end


  • 效果图片:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容