你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS之 基本動畫原理

IOS之 基本動畫原理

編輯:IOS開發綜合

IOS動畫分為屬性動畫和過渡動畫。ios4.0之前

屬性動畫

內容和設置主要放在方括號中既:如下

[UIView beginAnimations:@move context:@aa];

中間部分設置動畫內容和屬性

[UIView commitAnimations];

詳見代碼如下

    [UIView beginAnimations:@move context:@aa];
    [UIView setAnimationDuration:1];//設置動畫時間
    CGPoint point = self.View1.center;//
    [UIView setAnimationDelegate:self];//設置代理
    [UIView setAnimationWillStartSelector:@selector(startAnimation:)];//動畫開始之前
    [UIView setAnimationDidStopSelector:@selector(stopAnition:)];//動畫結束之後
    
//    [UIView setAnimationRepeatAutoreverses:YES];
    self.View1.center = CGPointMake(point.x+1, point.y);
//    self.View1.backgroundColor = [UIColor orangeColor];
    [UIView commitAnimations];

一下是能夠設置屬性動畫的屬性:

 

The following properties of the UIView class are animatable:

@property frame
@property bounds
@property center
@property transform
@property alpha
@property backgroundColor
@property contentStretch

過渡動畫:

 

    [UIView beginAnimations:@move context:@text];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.View3 cache:YES];

    
    [UIView commitAnimations];

能夠設置的屬性:

 

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};

 

4.0之後修改為調用block

Animating Views with Block Objects

//屬性動畫

+ animateWithDuration:delay:options:animations:completion:
+ animateWithDuration:animations:completion:
+ animateWithDuration:animations:

//過渡動畫

+ transitionWithView:duration:options:animations:completion:
+ transitionFromView:toView:duration:options:completion:

//關鍵幀動畫
+ animateKeyframesWithDuration:delay:options:animations:completion:
+ addKeyframeWithRelativeStartTime:relativeDuration:animations:

//系統動畫
+ performSystemAnimation:onViews:options:animations:completion:

//未知,還為測試
+ animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:
+ performWithoutAnimation:

 

 

CAAnimation動畫

代碼如下:具體過程。

 

    //CABasicAnimation 動畫
    
    CABasicAnimation* baseAnimation = [CABasicAnimation animationWithKeyPath:@center];
    baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)];
    
    baseAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(400, 100)] ;
    baseAnimation.duration = 3;
    [self.View2.layer addAnimation:baseAnimation forKey:@baseAnimation];
    // CAKeyframeAnimation 動畫
    CAKeyframeAnimation* keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@position];
    keyAnimation.values =[NSArray arrayWithObjects:
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x-5, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
                          nil];
    keyAnimation.keyTimes = @[[NSNumber numberWithFloat:.1],
                              [NSNumber numberWithFloat:.2],
                              [NSNumber numberWithFloat:.3],
                              [NSNumber numberWithFloat:.4],
                              [NSNumber numberWithFloat:.5],
                              [NSNumber numberWithFloat:1]];//設置時間 ,百分比
    keyAnimation.calculationMode = kCAAnimationDiscrete;
    [self.View2.layer addAnimation:keyAnimation forKey:@position];
    //CATransition 動畫
     CATransition* transiton = [CATransition animation];
    transiton.startProgress = 0;
    transiton.endProgress = 1.0;
    transiton.duration = 3;
    transiton.type = kCATransitionReveal;//根據不同值顯示不同動畫
    transiton.subtype = kCATransitionFromRight;
    [self.view.layer addAnimation:transiton forKey:@transtion];


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved