淺析UIView動(dòng)畫初步

// 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動(dòng)畫
// springBounciness:4.0    //[0-20] 彈力 越大則震動(dòng)幅度越大
// springSpeed     :12.0   //[0-20] 速度 越大則動(dòng)畫結(jié)束越快
// dynamicsTension :0      //拉力  接下來(lái)這三個(gè)都跟物理力學(xué)模擬相關(guān) 數(shù)值調(diào)整起來(lái)也很費(fèi)時(shí) 沒(méi)事不建議使用哈
// dynamicsFriction:0      //摩擦 同上
// dynamicsMass    :0      //質(zhì)量 同上
// CACurrentMediaTime() 獲取的是當(dāng)前時(shí)間
- (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動(dòng)畫
- (void)popBasicAnimation1:(UIView *)view {
    POPBasicAnimation *anBasic = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    anBasic.toValue = @(view.center.x + 100); // 自己位置基礎(chǔ)上再偏移100
    anBasic.duration = 0.4; // 動(dòng)畫時(shí)間間隔
    anBasic.beginTime = CACurrentMediaTime() + 1.0f;
//    [view pop_addAnimation:anBasic forKey:@"position"]; 好像兩個(gè)都可以,效果一樣
    [view.layer pop_addAnimation:anBasic forKey:@"position"];
    [anBasic setCompletionBlock:^(POPAnimation *anim, BOOL finished) {

    }];
}

// 開始旋轉(zhuǎn)
- (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"];
}

// 停止旋轉(zhuǎn)
- (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


  • 效果圖片:
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容