最近做的項目有一個需求,就是讓地球在圓圈里面從右到左運動,而且在運動的過程中不能出現(xiàn)空白,就是一直平滑的運動。
剛開始可把我給愁死了,首先竟然傻傻的想到用關鍵幀動畫,結果搞了一下午也沒搞出來。第二天就換了一種思路,但是會出現(xiàn)卡頓的情況。后來經過大BOSS的指導,一會兒的功夫就搞出來了。請允許我小小的汗顏一下。
接下來言歸正傳,先看看效果圖:

animation.gif
用到的圖片是這樣的:

image_earth_5.png
原理大概是這樣的:這是倆張相同的圖片結合起來的,每個UIImageView的大小是圓環(huán)的二倍。第一個UIImageView的起始位置是圓環(huán)的最左面,第二個UIImageView的起始位置是第一個UIImageView的最右邊。每張圖片的動畫距離是UIImageView的寬度。當?shù)谝粡圲IImageView移動結束,第二個UIImageView移動到一半,此時第一個UIImageView重新開始運動,就不會出現(xiàn)圖片突然出現(xiàn),感覺像是跳了一下的感覺。大概就是這個意思,讀者能看明白吧。下面就是代碼啦!
@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];
}
寫的不對的地方請多多指教。