ios动画专题首页投稿(暂停使用,暂停投稿)

iOS开发--实现俩张图连贯的动画(从右到左)

2016-09-22  本文已影响609人  云淡风轻的成长

最近做的项目有一个需求,就是让地球在圆圈里面从右到左运动,而且在运动的过程中不能出现空白,就是一直平滑的运动。
刚开始可把我给愁死了,首先竟然傻傻的想到用关键帧动画,结果搞了一下午也没搞出来。第二天就换了一种思路,但是会出现卡顿的情况。后来经过大BOSS的指导,一会儿的功夫就搞出来了。请允许我小小的汗颜一下。
接下来言归正传,先看看效果图:

animation.gif

用到的图片是这样的:

image_earth_5.png

原理大概是这样的:这是俩张相同的图片结合起来的,每个UIImageView的大小是圆环的二倍。第一个UIImageView的起始位置是圆环的最左面,第二个UIImageView的起始位置是第一个UIImageView的最右边。每张图片的动画距离是UIImageView的宽度。当第一张UIImageView移动结束,第二个UIImageView移动到一半,此时第一个UIImageView重新开始运动,就不会出现图片突然出现,感觉像是跳了一下的感觉。大概就是这个意思,读者能看明白吧。下面就是代码啦!

@property(nonatomic, strong) UIView *circleView;
@property(nonatomic, strong) UIImageView *frontEarthImg;
@property(nonatomic, strong) UIImageView *behindEarthImg;
@property(nonatomic, strong) UIButton *bottomBtn ;
-(void) setupUI {
    _circleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RYScreenWidth * 0.6 , RYScreenWidth * 0.6)];
    _circleView.center = self.view.center;
    _circleView.backgroundColor = UIColorFromRGB(0x4ba0ef);
    _circleView.clipsToBounds = YES;
    _circleView.layer.masksToBounds = YES;
    _circleView.layer.cornerRadius = _circleView.frame.size.width /2;
    [self.view addSubview:_circleView];
    UIImage *image = [UIImage imageNamed:@"image_earth.png"];
    CGFloat earthWidth = RYScreenWidth * 0.6 * 2;
    CGFloat earthHeight = earthWidth * image.size.height / image.size.width ;
    CGFloat imgY = (earthWidth /2 - earthHeight)/2;
    _frontEarthImg = [[UIImageView alloc ] initWithFrame:CGRectMake(0, imgY, earthWidth, earthHeight)];
    _frontEarthImg.image = image;
    [_circleView addSubview:_frontEarthImg];
    _behindEarthImg = [[UIImageView alloc ] initWithFrame:CGRectMake(earthWidth, imgY, earthWidth, earthHeight)];
    _behindEarthImg.image = image;
    [_circleView addSubview:_behindEarthImg];
    _bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _bottomBtn.backgroundColor = UIColorFromRGB(0x4ba0ef);
    _bottomBtn.frame = CGRectMake(RYScreenWidth * 0.3, RYScreenHeight * 0.7 , RYScreenWidth * 0.4 , 40);
    [_bottomBtn setTitle:@"开始动画" forState:UIControlStateNormal];
    [_bottomBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_bottomBtn addTarget:self action:@selector(bottomBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_bottomBtn];
}
-(void) bottomBtnClick {
    if ([_bottomBtn.titleLabel.text isEqualToString:@"开始动画"]) {
        [_bottomBtn setTitle:@"停止动画" forState:UIControlStateNormal];
        [self startAnimation];
    } else if ([_bottomBtn.titleLabel.text isEqualToString:@"停止动画"]){
        [_bottomBtn setTitle:@"开始动画" forState:UIControlStateNormal];
        [self stopAnimation];
    }
}
-(void)startAnimation {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
    animation.repeatCount = MAXFLOAT;
    animation.fillMode= kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.duration = 8.0;
    animation.toValue = [NSNumber numberWithFloat:-1 * _frontEarthImg.frame.size.width];
    [_frontEarthImg.layer addAnimation:animation forKey:nil];
    animation.toValue = [NSNumber numberWithFloat:0];
    [_behindEarthImg.layer addAnimation:animation forKey:nil];
}
-(void)stopAnimation {
    [_frontEarthImg.layer removeAllAnimations];
    [_behindEarthImg.layer removeAllAnimations];
}

写的不对的地方请多多指教。

上一篇下一篇

猜你喜欢

热点阅读