最近做的項(xiàng)目有一個(gè)需求,就是讓地球在圓圈里面從右到左運(yùn)動(dòng)删豺,而且在運(yùn)動(dòng)的過程中不能出現(xiàn)空白共虑,就是一直平滑的運(yùn)動(dòng)。
剛開始可把我給愁死了呀页,首先竟然傻傻的想到用關(guān)鍵幀動(dòng)畫妈拌,結(jié)果搞了一下午也沒搞出來。第二天就換了一種思路蓬蝶,但是會(huì)出現(xiàn)卡頓的情況尘分。后來經(jīng)過大BOSS的指導(dǎo),一會(huì)兒的功夫就搞出來了丸氛。請(qǐng)?jiān)试S我小小的汗顏一下培愁。
接下來言歸正傳,先看看效果圖:
用到的圖片是這樣的:
原理大概是這樣的:這是倆張相同的圖片結(jié)合起來的缓窜,每個(gè)UIImageView的大小是圓環(huán)的二倍定续。第一個(gè)UIImageView的起始位置是圓環(huán)的最左面,第二個(gè)UIImageView的起始位置是第一個(gè)UIImageView的最右邊禾锤。每張圖片的動(dòng)畫距離是UIImageView的寬度私股。當(dāng)?shù)谝粡圲IImageView移動(dòng)結(jié)束,第二個(gè)UIImageView移動(dòng)到一半时肿,此時(shí)第一個(gè)UIImageView重新開始運(yùn)動(dòng)庇茫,就不會(huì)出現(xiàn)圖片突然出現(xiàn),感覺像是跳了一下的感覺螃成。大概就是這個(gè)意思旦签,讀者能看明白吧。下面就是代碼啦寸宏!
@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:@"開始動(dòng)畫" 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:@"開始動(dòng)畫"]) {
[_bottomBtn setTitle:@"停止動(dòng)畫" forState:UIControlStateNormal];
[self startAnimation];
} else if ([_bottomBtn.titleLabel.text isEqualToString:@"停止動(dòng)畫"]){
[_bottomBtn setTitle:@"開始動(dòng)畫" 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];
}
寫的不對(duì)的地方請(qǐng)多多指教宁炫。