6. 动画

作者: 写代码的向日葵 | 来源:发表于2023-03-25 23:43 被阅读0次

一. 时钟(CATransform3D)

#import "ViewController.h"

#define width MIN(self.imageView.bounds.size.width, self.imageView.bounds.size.height) * 0.5

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) CALayer *secl;
@property (nonatomic, strong) CALayer *minL;
@property (nonatomic, strong) CALayer *houL;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];
    [self addSecl];
    [self animation];
}


-(void)addSecl{
    [self.imageView.layer addSublayer:self.houL];
    [self.imageView.layer addSublayer:self.minL];
    [self.imageView.layer addSublayer:self.secl];
}

-(void)animation{
    [self timeChanged];
    [NSTimer scheduledTimerWithTimeInterval: 1 target:self selector:@selector(timeChanged) userInfo:nil repeats:YES];
}


-(void)timeChanged{
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSDate * date = [NSDate date];
    NSDateComponents * components = [calender components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:date];
    NSInteger sec = components.second;
    NSInteger min = components.minute;
    NSInteger hour = components.hour;
    CGFloat secA = sec * M_PI / 180.0 * 6;
    self.secl.transform = CATransform3DMakeRotation(secA, 0, 0, 1);
    
    CGFloat minA = min * M_PI / 180.0 * 6;
    self.minL.transform = CATransform3DMakeRotation(minA, 0, 0, 1);
    
    
    CGFloat houLA = hour * M_PI / 180.0 * 30 +  minA / 12;
    self.houL.transform = CATransform3DMakeRotation(houLA, 0, 0, 1);
}



- (UIImageView *)imageView{
    if(!_imageView){
        _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"钟表"]];
        _imageView.center = self.view.center;
    }
    return _imageView;
}


- (CALayer *)secl{
    if(!_secl){
        _secl = [CALayer layer];
        _secl.bounds = CGRectMake(0, 0, 1, width - 15);
        _secl.backgroundColor = [UIColor redColor].CGColor;
        _secl.position =  CGPointMake(width, width);
        _secl.anchorPoint = CGPointMake(0.5, 1);
    }
    return _secl;
}



- (CALayer *)minL{
    if(!_minL){
        _minL = [CALayer layer];
        _minL.bounds = CGRectMake(0, 0, 3, width - 20);
        _minL.backgroundColor = [UIColor blackColor].CGColor;
        _minL.position = CGPointMake(width, width);
        _minL.anchorPoint = CGPointMake(0.5, 1);
        _minL.cornerRadius = 1.5;
    }
    return _minL;
}

- (CALayer *)houL{
    if(!_houL){
        _houL = [CALayer layer];
        _houL.bounds = CGRectMake(0, 0, 3, width - 40);
        _houL.backgroundColor = [UIColor blackColor].CGColor;
        _houL.position = CGPointMake(width, width);
        _houL.anchorPoint = CGPointMake(0.5, 1);
        _houL.cornerRadius = 1.5;
    }
    return _houL;
}

代码地址: https://github.com/zqdeveloper/ios_sample/tree/master/ClockView

二 . 心跳效果(CABasicAnimation)


#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];
}

- (UIImageView *)imageView{
    if(!_imageView){
        _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"心"]];
        _imageView.center = self.view.center;
    }
    return _imageView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"transform.scale";
    anim.toValue = @0;
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    anim.repeatCount = MAXFLOAT;
    anim.duration = 1;
    anim.autoreverses = YES;
    [self.imageView.layer addAnimation:anim forKey:nil];
}

-(void)closeAimation{
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.view.layer.position = CGPointMake(10, 10);
    [CATransaction commit];
}
@end

代码地址: https://github.com/zqdeveloper/ios_sample/tree/master/heartbeat

三. 图片抖动(CAKeyframeAnimation)


#import "ViewController.h"
#define angle2Rad(angle) (M_PI*angle/180.0)

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];
}


- (UIImageView *)imageView{
    if(!_imageView){
        _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon"]];
        _imageView.center = self.view.center;
    }
    return _imageView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"transform.rotation";
    anim.values = @[@angle2Rad(-10),@angle2Rad(10)];
    anim.repeatCount = MAXFLOAT;
    anim.autoreverses = YES;
    anim.removedOnCompletion = NO;
    anim.duration = 0.2;
}

-(void)pathAnimation{
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"position";
    anim.duration = 4;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(self.imageView.bounds.size.width,self.imageView.bounds.size.height)];
    [path addLineToPoint:CGPointMake(self.view.bounds.size.width-self.imageView.bounds.size.width, self.imageView.bounds.size.height)];
    [path addLineToPoint:CGPointMake(self.view.bounds.size.width-self.imageView.bounds.size.width, self.view.bounds.size.height-self.imageView.bounds.size.height)];
    [path addLineToPoint:CGPointMake(self.imageView.bounds.size.width, self.view.bounds.size.height-self.imageView.bounds.size.height)];
    [path addLineToPoint:CGPointMake(self.imageView.bounds.size.width,self.imageView.bounds.size.height)];
    anim.path = path.CGPath;
    anim.repeatCount = MAXFLOAT;
    anim.removedOnCompletion = NO;
    anim.fillMode  = kCAFillModeForwards;
    [self.imageView.layer addAnimation:anim forKey:nil];
}
@end

代码地址:https://github.com/zqdeveloper/ios_sample/tree/master/PictureJitter

四. 转场动画(CATransition)


#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];
}


- (UIImageView *)imageView{
    if(!_imageView){
        _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
        _imageView.center = self.view.center;
    }
    return _imageView;
}
static int _index = 1;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView transitionWithView:self.imageView duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
        _index++;
        if(_index>=4){
            _index = 1;
        }
        NSString *imaageName = [NSString stringWithFormat:@"%d",_index];
        UIImage *img = [UIImage imageNamed:imaageName];
        self.imageView.image = img;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)transition{
    _index++;
    if(_index>=4){
        _index = 1;
    }
    NSString *imaageName = [NSString stringWithFormat:@"%d",_index];
    UIImage *img = [UIImage imageNamed:imaageName];
    self.imageView.image = img;
    
    CATransition  *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.type = @"cube";
    //transition.startProgress = 0;
    //transition.endProgress = 0.5;
    [self.imageView.layer addAnimation:transition forKey:nil];
}

@end

代码地址: https://github.com/zqdeveloper/ios_sample/tree/master/TransitionAnimation

五.动画组(CAAnimationGroup)


#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIView *redView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.redView];
}

- (UIView *)redView{
    if(!_redView){
        _redView = [[UIView alloc]initWithFrame:CGRectMake(40, 64, 200, 200)];
        _redView.backgroundColor = [UIColor redColor];
    }
    return _redView;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CABasicAnimation *anim1 = [CABasicAnimation animation];
    anim1.keyPath = @"position.y";
    anim1.toValue = @600;
    
    CABasicAnimation *anim2 = [CABasicAnimation animation];
    anim2.keyPath = @"transform.scale";
    anim2.toValue = @0.5;
    
    CAAnimationGroup * anim = [CAAnimationGroup animation];
    anim.animations = @[anim1,anim2];
    anim.duration = 2;
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    [self.redView.layer addAnimation:anim forKey:nil];
}

@end

代码地址: https://github.com/zqdeveloper/ios_sample/tree/master/AnimationGroup

六. 关闭隐式动画

[CATransaction begin];
[CATransaction setDisableActions:YES];
self.view.layer.position = CGPointMake(10, 10);
[CATransaction commit];

相关文章

网友评论

      本文标题:6. 动画

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