网易云音乐每日推荐动画

我的原理:利用ios的CATransition完成动画的转场切换,添加一个定时器实现动画的连贯。

看下效果


网易云音乐每日推荐动画_第1张图片
recommended.gif

-(NSArray *)images{
   if (!_images) {
       _images = @[@"selected_icon",@"selected_icon",@"selected_icon"];
   }
   return _images;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.bgView = [[UIView alloc] init];
    self.bgView.frame = CGRectMake(90, 90, 80, 80);
    self.bgView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.bgView];
    
    //防止显示画面超出父视图
    [self.bgView setClipsToBounds:YES];
    
    self.imageView.frame = CGRectMake(0, 0, 80, 80);
    _imageView.backgroundColor = [UIColor grayColor];
    [self.bgView addSubview:_imageView];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(switchAnimation) userInfo:nil repeats:YES];
  
    
}
-(void)switchAnimation{

    if (self.allTime == 0) {
        
        _imageView.image = [UIImage imageNamed:self.images.firstObject];
        CATransition *transition = [CATransition animation];
        transition.type = @"oglFlip";
        transition.duration = 1.0;
        transition.subtype = kCATransitionFromRight;
        [_imageView.layer addAnimation:transition forKey:nil];
        
    }else if(self.allTime == self.images.count){
        
        _imageView.image = [UIImage imageNamed:@"test_icon"];
        CATransition *transition = [CATransition animation];
        transition.type = @"oglFlip";
        transition.duration = 1.0;
        transition.subtype = kCATransitionFromRight;
        [_imageView.layer addAnimation:transition forKey:nil];

        [self.timer invalidate];
        self.timer = nil;
        
    }else{
        
        _imageView.image = [UIImage imageNamed:self.images[self.allTime]];
        CATransition *transition = [CATransition animation];
        transition.type = @"push";
        transition.duration = 1.0;
        transition.subtype = kCATransitionFromRight;
        [_imageView.layer addAnimation:transition forKey:nil];
    }
    self.allTime ++;
}

相关代码:https://github.com/DavieDang/recommendedDemo2

你可能感兴趣的:(网易云音乐每日推荐动画)