实现类似地球转动的效果

看到微信的启动图,联想到了怎样让地球转动起来这个动效,随后自己就亲身实现了一下,效果如下:

动图.gif

具体实现代码请参考:
https://github.com/Mortime/MM_EarthRotationAnimation.git
核心代码如下:

  1. 实现圆环的动态运转
    #pragma mark -- 实现旋转
    -(void)rotationpAnimationImageView:(UIImageView *)image{
    CABasicAnimation *animation = [ CABasicAnimation
    animationWithKeyPath: @"transform" ];
    // 设置类型
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    // 让旋转围绕Z轴
    animation.toValue = [ NSValue valueWithCATransform3D:
    CATransform3DMakeRotation(M_PI , 0.0, 0.0, 1.0) ];
    animation.duration = 5;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    // 添加一个像素的透明图片,去除边缘锯齿
    CGRect imageRrect = CGRectMake(0, 0,imageView.frame.size.width, imageView.frame.size.height);
    UIGraphicsBeginImageContext(imageRrect.size);
    [imageView.image drawInRect:CGRectMake(1,1,imageView.frame.size.width-2,imageView.frame.size.height-2)];
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [imageView.layer addAnimation:animation forKey:nil];
    }
  2. 实现效果图中小人的动态跑步
    - (void)animationImageView:(NSString *)imageName count:(NSInteger)count{
    // 判断动画是否在执行
    if ([imgView isAnimating]) return;
    NSMutableArray *arrayM = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
    NSString *imageName = [NSString stringWithFormat:@"%@%d.png", name, i+1];
    // 设置全路径
    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    [arrayM addObject:image];
    }
    // 设置动画数据
    imgView.animationImages = arrayM;
    imgView.animationRepeatCount = 0;
    imgView.animationDuration = imgView.animationImages.count * 0.05;
    // 动画开始
    [imgView startAnimating];
    }

你可能感兴趣的:(实现类似地球转动的效果)