iOS開發(fā)--實現(xiàn)倆張圖連貫的動畫(從右到左)

最近做的項目有一個需求,就是讓地球在圓圈里面從右到左運動,而且在運動的過程中不能出現(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];
}

寫的不對的地方請多多指教。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,698評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,274評論 5 13
  • 概覽 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復雜,今天將帶大家一窺iOS動畫全貌。在這里你...
    Yiart閱讀 3,971評論 3 34
  • 為什么要裝SublimeREPL? 我打算用Sublime寫Python代碼,Sublime的語法高亮挺好看的,比...
    mychen閱讀 5,374評論 0 3
  • 藝術學院的最大的股東韓錄少爺在這個學院成天欺負自己覺得看不慣的人,甚至把他們趕出了藝術學院。 當天是他們軍訓的...
    Miss梔閱讀 402評論 3 1

友情鏈接更多精彩內容