10、簡(jiǎn)單動(dòng)畫

一、UIView基礎(chǔ)動(dòng)畫

UIKit 直接將動(dòng)畫繼承到UIView類中,當(dāng)內(nèi)部的一些屬性發(fā)生改變時(shí),UIView將為這些改變提供動(dòng)畫支持。執(zhí)行動(dòng)畫的工作由UIView類自動(dòng)完成,但仍希望在執(zhí)行動(dòng)畫時(shí)通知視圖,為此需要將改變屬性的代碼放在[UIVIew beginAnimations:nil context:nil]與[UIView commitAnimations]之間。

1、UIView動(dòng)畫

//UIView動(dòng)畫(UIKit動(dòng)畫)
-(void)viewAnimation{
  //早期的動(dòng)畫塊  在begin和commit之間寫需要改變的視圖
    [UIView beginAnimations:@"test1" context:nil];
    //設(shè)置動(dòng)畫時(shí)長(zhǎng)
    [UIView setAnimationDuration:1.0];
    //設(shè)置動(dòng)畫是否反轉(zhuǎn)  動(dòng)畫執(zhí)行完畢,自動(dòng)原路返回原狀態(tài)
    [UIView setAnimationRepeatAutoreverses:YES];
    //設(shè)置動(dòng)畫重復(fù)次數(shù)
    [UIView setAnimationRepeatCount:5];
    //在兩者之間實(shí)現(xiàn)具體代碼
    self.testView.backgroundColor=[UIColor yellowColor];
    //改變大小
    CGRect rect=self.testView.bounds;
    rect.size=CGSizeMake(120, 120);
    self.testView.bounds=rect;
    
    //改變位置
    CGRect frame=self.testView.frame;
    frame.origin=CGPointMake(CGRectGetWidth(self.view.frame)-120, CGRectGetHeight(self.view.frame)-120);
    self.testView.frame=frame;
    //提交動(dòng)畫
    [UIView commitAnimations];
    
}

2、UIView層block動(dòng)畫

//UIView層block動(dòng)畫
-(void)blockViewAnimation{
    //第一個(gè)參數(shù):動(dòng)畫的時(shí)長(zhǎng)
   // 第二個(gè)參數(shù):將要添加動(dòng)畫的代碼在block中實(shí)現(xiàn),相當(dāng)于將動(dòng)畫的begin和commit之間的代碼寫在block中
   // 先將動(dòng)畫的設(shè)置配置完整,在改變屬性值
    [UIView animateWithDuration:1.0 animations:^{
        //設(shè)置動(dòng)畫是否反轉(zhuǎn)  動(dòng)畫執(zhí)行完畢,自動(dòng)原路返回原狀態(tài)
        [UIView setAnimationRepeatAutoreverses:YES];
        //設(shè)置動(dòng)畫重復(fù)次數(shù)
        [UIView setAnimationRepeatCount:5];
        //要改變的屬性
        self.testView.backgroundColor=[UIColor redColor];
    
        //改變大小
        CGRect rect=self.testView.bounds;
        rect.size=CGSizeMake(120, 120);
        self.testView.bounds=rect;
        
        //改變位置
        CGRect frame=self.testView.frame;
        frame.origin=CGPointMake(CGRectGetWidth(self.view.frame)-120, CGRectGetHeight(self.view.frame)-120);
        self.testView.frame=frame;
    }];
    
    
    //completion:動(dòng)畫執(zhí)行結(jié)束會(huì)執(zhí)行的block
    [UIView animateWithDuration:1.0 animations:^{
        [UIView setAnimationRepeatAutoreverses:YES];
        //設(shè)置動(dòng)畫重復(fù)次數(shù)
       [UIView setAnimationRepeatCount:5];
        //要改變的屬性
       self.testView.backgroundColor=[UIColor redColor];
    
    } completion:^(BOOL finished) {
        //當(dāng)動(dòng)畫執(zhí)行完成,我們不需要該界面,可以在此處將動(dòng)畫作用的界面隱藏或移除掉
        [self.testView removeFromSuperview];
        NSLog(@"動(dòng)畫執(zhí)行完畢");
    }];
    
    
    //delay:延時(shí)執(zhí)行  多少秒之后在執(zhí)行該動(dòng)畫
    //options:動(dòng)畫一些效果的設(shè)置,例如是否重復(fù)等
    [UIView animateWithDuration:1.0 delay:3.0 options:(UIViewAnimationOptionRepeat) animations:^{
        self.testView.backgroundColor=[UIColor orangeColor];
    } completion:^(BOOL finished) {
        NSLog(@"執(zhí)行完畢");
    }];
    
    
    //過(guò)渡動(dòng)畫   當(dāng)視圖進(jìn)行切換或者視圖位置發(fā)生改變的時(shí)候,可以考慮使用該動(dòng)畫
//    [UIView transitionWithView:self.testView duration:1.5 options:(UIViewAnimationOptionTransitionFlipFromLeft) animations:^{
//        
//         self.testView.backgroundColor=[UIColor orangeColor];
//    } completion:^(BOOL finished) {
//         NSLog(@"執(zhí)行完畢");
//    }];
   
}

3、關(guān)鍵幀動(dòng)畫

//關(guān)鍵幀動(dòng)畫
-(void)keyFrameAnimation{
    
    [UIView animateKeyframesWithDuration:4.0 delay:0.0 options:(UIViewKeyframeAnimationOptionRepeat) animations:^{
       //添加動(dòng)畫所需要的幀數(shù)
        //第一個(gè)參數(shù):當(dāng)前幀相對(duì)的開(kāi)始時(shí)間,取值范圍為0~1  例如:如果相對(duì)開(kāi)始時(shí)間為0.5,那么他的總時(shí)長(zhǎng)為0.5*4=2s
        //第二個(gè)參數(shù):當(dāng)前幀相對(duì)的時(shí)長(zhǎng),取值范圍為0~1,例如:此處給值0.5,那么此幀的播放時(shí)長(zhǎng)為0.5*4=2s
        //第三個(gè)參數(shù)為:block,在block中實(shí)現(xiàn)動(dòng)畫所要做的操作
        //第一幀
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
            
            CGRect rect=self.testView.frame;
            rect.origin.x=CGRectGetWidth(self.view.frame)-rect.size.width;
            self.testView.frame=rect;
    
      }];
 
    //第二幀
    [UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
         CGRect rect=self.testView.frame;
        rect.origin.y=CGRectGetHeight(self.view.frame)-rect.size.height;
        self.testView.frame=rect;

    }];
 
    //第三幀
    [UIView addKeyframeWithRelativeStartTime:0.50 relativeDuration:0.25 animations:^{
        CGRect rect=self.testView.frame;
        rect.origin.x=0;
        self.testView.frame=rect;
    }];
    
    //第四幀
    [UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
          CGRect rect=self.testView.frame;
        rect.origin.y=0;
        self.testView.frame=rect;
    }];
        
    } completion:^(BOOL finished) {
        NSLog(@"執(zhí)行完畢");
    }];
    
}

4、spring動(dòng)畫

//spring動(dòng)畫
-(void)springAnimation{
    //usingSpringWithDamping:阻尼,取值范圍為0-1,值越小,動(dòng)畫效果越明顯,抖動(dòng)的次數(shù)多
    //initialSpringVelocity:初始速率,值越大,動(dòng)畫效果越明顯,彈得距離大
    //options:動(dòng)畫效果
    [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.1 initialSpringVelocity:20.0 options:(UIViewAnimationOptionCurveLinear) animations:^{
        [UIView setAnimationRepeatAutoreverses:YES];
        self.testView.center=self.view.center;
        
    } completion:^(BOOL finished) {
        self.testView.center=CGPointMake(50, 50);
    }];
    
    
}

二、CoreAnimation動(dòng)畫
1、CoreAnimation動(dòng)畫位于iOS框架的Media層需要添加QuartzCore.Framework(系統(tǒng)自動(dòng)添加)CoreAnimation基本上是Layer Animation

  • CALayer負(fù)責(zé)繪制,提供UIView需要展示的內(nèi)容,不能交互。
  • UIView負(fù)責(zé)交互,顯示CALayer繪制的內(nèi)容。Layer有很多種,最常用和最基本的是CALayer,還有它的子類:
  • CAScrollerLayer簡(jiǎn)化顯示層的一部分
  • CATextLayer文本層
  • CAGradientLayer 可以方便的處理顏色漸變、CAShapeLayer僅僅限于沿著邊緣的動(dòng)畫效果,它實(shí)現(xiàn)不了填充效果
//layer層的動(dòng)畫
//basicAnimation
-(void)basicAnimation{
    //keyPath:想要改變layer的屬性或者是layer的屬性的屬性
    //keyPath如果書寫錯(cuò)誤,動(dòng)畫沒(méi)有效果
    CABasicAnimation* basicAni=[CABasicAnimation animationWithKeyPath:@"position.x"];
    //如果我們只設(shè)置toValue一個(gè)屬性,那就是默認(rèn)從當(dāng)前位置到toValue的位置
//    basicAni.toValue=@(CGRectGetWidth(self.view.frame)-50);
    //如果我們只設(shè)置byValue屬性,那么就是默認(rèn)從當(dāng)前位置加byValue的值
    basicAni.byValue=@50;
    //讓動(dòng)畫結(jié)束之后,不返回原位置
    //當(dāng)動(dòng)畫結(jié)束的時(shí)候,是否移除動(dòng)畫所產(chǎn)生的效果
    basicAni.removedOnCompletion=NO;
    //保持哪種狀態(tài)
    //kCAFillModeForwards:保持動(dòng)畫結(jié)束的狀態(tài)
    basicAni.fillMode=kCAFillModeForwards;
    //fromValue:必須和toValue或者byValue兩者中的一個(gè)結(jié)合使用
    basicAni.fromValue=@100;
    //設(shè)置動(dòng)畫時(shí)長(zhǎng)
    [basicAni setDuration:2.0];
    //將動(dòng)畫添加到layer層
    //forKey:動(dòng)畫標(biāo)記,移除動(dòng)畫時(shí)使用
    [self.testView.layer addAnimation:basicAni forKey:@"basicAni"];
    
}
//layer層的keyframeAnimation
-(void)layerKeyframeAnimation{
    
    //初始化
    CAKeyframeAnimation* keyFrameAni=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    //得到視圖原始的position
    CGPoint originPoint=self.testView.layer.position;
    NSValue* originValue=[NSValue valueWithCGPoint:originPoint];
    //一幀
    CGPoint firstPoint=CGPointMake(50, 50);
    NSValue* firstValue=[NSValue valueWithCGPoint:firstPoint];
    //二幀
    CGPoint secondPoint=CGPointMake(CGRectGetWidth(self.view.frame)-50, 50);
    NSValue* secondValue=[NSValue valueWithCGPoint:secondPoint];
    
    //將幀添加到layer上
    keyFrameAni.values=@[originValue,firstValue,secondValue];
    //將動(dòng)畫添加到layer上
    [self.testView.layer addAnimation:keyFrameAni forKey:@"keyFrameAni"];
    keyFrameAni.duration=10.0;
    keyFrameAni.removedOnCompletion=NO;
    keyFrameAni.fillMode=kCAFillModeForwards;
    
    
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,141評(píng)論 5 13
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,572評(píng)論 6 30
  • 在iOS實(shí)際開(kāi)發(fā)中常用的動(dòng)畫無(wú)非是以下四種:UIView動(dòng)畫,核心動(dòng)畫,幀動(dòng)畫,自定義轉(zhuǎn)場(chǎng)動(dòng)畫。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,159評(píng)論 1 23
  • 在前面的學(xué)習(xí)中,我們討論了CoreAnimation除了動(dòng)畫外可以做到的任何事情。但是動(dòng)畫是CoreAni...
    小貓仔閱讀 1,560評(píng)論 0 0
  • 臨的簡(jiǎn)友的一幅纏繞,自認(rèn)并沒(méi)有表達(dá)原畫的精髓,畫面過(guò)于平庸,不突出
    大酋閱讀 94評(píng)論 0 0