IOS 動畫

動畫 Animation

CALayer是動畫產(chǎn)生的地方樊卓,當將動畫添加到Layer層的時候,不能直接修改layer的屬性

  • CoreAnimation 有兩個Layer層次結構 - 模型層樹(model layer tree)和表示層樹(presentation layer tree)
- (instancetype)presentationLayer; - 表示層
- (instancetype)modelLayer; - 模型層
CAAnimation常用的有:
  • CABasicAnimation

CABasicAnimation的常用屬性:

  • duration 動畫的持續(xù)時間
  • beginTime 指定動畫開始的時間阀趴,從開始延遲幾秒的話,設置為CACurrentMediaTime() + 秒數(shù)的方式
  • repeatCount 動畫的持續(xù)次數(shù)
  • repeatDuration 設置動畫的時間苍匆,在該時間內(nèi)動畫一直執(zhí)行刘急,不計次數(shù)
  • autoreverses 動畫結束時是否執(zhí)行逆動畫
  • fillMode 動畫在開始和結束時的動畫,默認值是KCAFillModeRemoved
    • kCAFillModeForwards 動畫開始之后layer的狀態(tài)將保持在動畫的最后一幀浸踩,而removedOnCompletion的默認屬性值為YES,所以為了使動畫結束之后layer保持結束狀態(tài)叔汁,應將removedOnCompletion設置為NO
    • kCAFillModeBackwards 將會立即執(zhí)行動畫的第一幀,無論是否設置beginTime屬性
    • kCAFillModeBoth kCAFillModeForwards和kCAFillModeBackwards的組合狀態(tài)
    • kCAFillModeRemoved 動畫將在設置的beginTime開始執(zhí)行(若有設置beginTime屬性检碗,則動畫立刻執(zhí)行)据块,而動畫執(zhí)行完成后將會將layer的狀態(tài)恢復原狀
  • timingFunction 設置動畫的時間變化
  • fromValue 所要改變屬性的起始值
  • toValue 所要改變屬性的結束時的值
  • byValue 所要改變屬性相同起始值的改變量
    • 注:若將removedOnCompletion設置為false狀態(tài),則可能會引起循環(huán)引用的問題
    • 原因:由于CAAnimation的delegate使用的Strong類型(強引用類型)
一般創(chuàng)建動畫的時候折剃,會使用到KeyPath的屬性

備注:KeyPath的值

  • positon - 移動位置(CGPoint)
  • opacitu - 透明度(0-1)
  • bounds - 變大與位置(CGRect)
  • bounds.size - 由小變大(CGSize)
  • backgroundColor - 背景顏色(CGColor)
  • cornerRadius - 漸變圓角(任意數(shù)值)
  • borderWidth - 改變邊框border的大小(圖形周圍邊框另假,border默認為黑色) (任意數(shù)值)
  • contens - 改變layer內(nèi)容(圖片) 若想要達到改變內(nèi)容的動畫效果,則要先在運行之前定義好layer的contents (CGImage)
  • transform.scale - 縮放怕犁,放大(0.0-1.0)
  • transform.rotation.x - 旋轉動畫(翻轉边篮,沿著X軸)(M_PI * n)
  • transform.rotation.y - 旋轉動畫(沿著Y軸)(M_PI * n)
  • transform.rotation.z - 旋轉動畫(沿著Z軸)(M_PI * n)
  • transform.transiation.x - 橫向移動(沿著X軸)(任意數(shù)值)
  • transform.transiation.y - 縱向移動(沿著Y軸)(任意數(shù)值)

旋轉動畫

#pragma mark - 旋轉動畫 -
-(void)setRotateView
{
    //1.創(chuàng)建一個View圖層
    UIView *animationView = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 70, 70)];
    animationView.backgroundColor = [UIColor orangeColor];
    
    //將AnimationView添加到self.view上
    [self.view addSubview:animationView];
    //2.創(chuàng)建動畫圖層
    /**
     3.設置keyPath的常用屬性:
     transform.rotation.x  圍繞x軸旋轉
     transform.rotation.y  圍繞y軸旋轉
     transform.rotation.z  圍繞z軸旋轉
     */
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    
    //4.設置beginTime指定動畫開始的時間,從開始延遲幾秒的話奏甫,設置為CACurrentMediaTime() + 秒數(shù)的方式
    basic.beginTime = 0.0;
    
    //5.設置toValue所要改變屬性的結束時的值
    basic.toValue = [NSValue valueWithCGPoint:CGPointMake(2*M_PI, 0)];
    
    //6.設置duration動畫的持續(xù)時間
    basic.duration = 1.5;
    
    //7.設置repeatCount動畫的持續(xù)次數(shù)
    basic.repeatCount = INFINITY;
    
    /**
     timingFunction   設置動畫的時間變化
     kCAMediaTimingFunctionLinear   在整個動畫時間內(nèi)動畫都是以相同速度來改變 - 勻速運動
     kCAMediaTimingFunctionEaseIn   動畫開始慢苟耻,之后加速                 - 加速運動
     kCAMediaTimingFunctionEaseOut  動畫開始快,之后減速                 - 減速運動
     kCAMediaTimingFunctionEaseInEaseOut  動畫在開始和結束都慢扶檐,中間時間段內(nèi)速度較快
     kCAMediaTimingFunctionDefault  默認動畫
     */
    //[basic setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    
    //8.將動畫添加到圖層
    [animationView.layer addAnimation:basic forKey:@"basic"];
 
}

移動動畫

#pragma mark - 移動動畫 -
-(void)SetMoveAnimationView
{
    //1.創(chuàng)建一個View圖層
    UIView *animationView = [[UIView alloc]initWithFrame:CGRectMake(10, 120, 70, 70)];
    animationView.backgroundColor = [UIColor orangeColor];
    
    //將AnimationView添加到self.view上
    [self.view addSubview:animationView];
    //2.創(chuàng)建動畫圖層
    /**3.position    設置內(nèi)容(eg:UIImage)*/
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"position"];
    
    //4.fromValue所要改變屬性的起始值
    basic.fromValue = [NSValue valueWithCGPoint:CGPointMake(40, 240)];
    
    //5.設置toValue所要改變屬性的結束時的值
    basic.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 240)];
    
    //6.設置duration動畫的持續(xù)時間
    basic.duration = 2;
    
    //7.設置repeatCount動畫的持續(xù)次數(shù)
    basic.repeatCount = INFINITY;
    //8.autoreverses動畫結束時是否執(zhí)行逆動畫
    basic.autoreverses = YES;
    //10.將動畫添加到圖層
    [animationView.layer addAnimation:basic forKey:@"basic"];
}

背景顏色變化動畫

#pragma mark - 背景顏色變化動畫 -
-(void)SetBackgroundColorChangeAnimationView
{
    //1.創(chuàng)建一個View圖層
    UIView *animationView = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 70, 70)];
    animationView.backgroundColor = [UIColor orangeColor];
    
    //將AnimationView添加到self.view上
    [self.view addSubview:animationView];
    //2.創(chuàng)建動畫圖層
    //backgroundColor       設置背景顏色的變化
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    
    //4.fromValue所要改變屬性的起始值
    basic.fromValue = (__bridge id _Nullable)([UIColor orangeColor].CGColor);
    
    //5.設置toValue所要改變屬性的結束時的值
    basic.toValue = (__bridge id _Nullable)([UIColor lightGrayColor].CGColor);
    
    //6.設置duration動畫的持續(xù)時間
    basic.duration = 2;
    
    //7.設置repeatCount動畫的持續(xù)次數(shù)
    basic.repeatCount = INFINITY;
    //8.autoreverses動畫結束時是否執(zhí)行逆動畫
    basic.autoreverses = YES;
    
    //10.將動畫添加到圖層
    [animationView.layer addAnimation:basic forKey:@"basic"];
    
}

內(nèi)容改變動畫

#pragma mark - 內(nèi)容改變動畫 -
-(void)SetContantChangeAnimationView
{
    //1.創(chuàng)建一個View圖層
    UIImageView *animationView = [[UIImageView alloc]initWithFrame:CGRectMake(200, 50, 70, 70)];
    animationView.image = [UIImage imageNamed:@"from"];
    
    [self.view addSubview:animationView];
    //2.創(chuàng)建動畫圖層 contents
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"contents"];
    
    //5.設置toValue所要改變屬性的結束時的值
    basic.toValue = (__bridge id _Nullable)([UIImage imageNamed:@"to"].CGImage);
    
    //6.設置duration動畫的持續(xù)時間
    basic.duration = 1.5;
    
    //7.設置repeatCount動畫的持續(xù)次數(shù)
    basic.repeatCount = INFINITY;
    //8.autoreverses動畫結束時是否執(zhí)行逆動畫
    basic.autoreverses = YES;
    //10.將動畫添加到圖層
    [animationView.layer addAnimation:basic forKey:@"basic"];
}

轉換圓角動畫

#pragma mark - 圓角變化動畫 -
-(void)SetRadiusView
{
    //1.創(chuàng)建一個View圖層
    UIView *animationView = [[UIView alloc]initWithFrame:CGRectMake(10, 310, 70, 70)];
    animationView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:animationView];
    animationView.layer.masksToBounds = YES;
    //2.創(chuàng)建動畫圖層 contents
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    
    //5.設置toValue所要改變屬性的結束時的值
    basic.toValue = [NSValue valueWithCGPoint:CGPointMake(35, 0)];
    
    //6.設置duration動畫的持續(xù)時間
    basic.duration = 2;
    
    //7.設置repeatCount動畫的持續(xù)次數(shù)
    basic.repeatCount = INFINITY;
    //8.autoreverses動畫結束時是否執(zhí)行逆動畫
    basic.autoreverses = YES;
    //10.將動畫添加到圖層
    [animationView.layer addAnimation:basic forKey:@"basic"];
}

按比例縮放動畫

#pragma mark - 按比例縮放動畫 -
-(void)SetZoomAnimationView
{
    //1.創(chuàng)建一個View圖層
    UIView *animationView = [[UIView alloc]initWithFrame:CGRectMake(10, 400, 70, 70)];
    animationView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:animationView];
    //2.創(chuàng)建動畫圖層 contents
    /**
     transform.scale 按比例縮放 - 0.8
     transform.scale.x  縮放寬的比例
     transform.scale.y  縮放高的比例
     */
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basic.fromValue = [NSValue valueWithCGPoint:CGPointMake(0.3, 0)];
    //5.設置toValue所要改變屬性的結束時的值
    basic.toValue = [NSValue valueWithCGPoint:CGPointMake(1.3, 0)];
    
    //6.設置duration動畫的持續(xù)時間
    basic.duration = 2;
    
    //7.設置repeatCount動畫的持續(xù)次數(shù)
    basic.repeatCount = INFINITY;
    //8.autoreverses動畫結束時是否執(zhí)行逆動畫
    basic.autoreverses = YES;
    //10.將動畫添加到圖層
    [animationView.layer addAnimation:basic forKey:@"basic"];
}

按指定大小進行縮放動畫

#pragma mark - 指定大小縮放 -
-(void)SetAssginZoomView
{
    //1.創(chuàng)建一個View圖層
    UIView *animationView = [[UIView alloc]initWithFrame:CGRectMake(120, 400, 70, 70)];
    animationView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:animationView];
    //2.創(chuàng)建動畫圖層 contents
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"bounds"];
    //5.設置toValue所要改變屬性的結束時的值
    basic.toValue = [NSValue valueWithCGRect:CGRectMake(800, 500, 90, 30)];
    
    //6.設置duration動畫的持續(xù)時間
    basic.duration = 2;
    
    //7.設置repeatCount動畫的持續(xù)次數(shù)
    basic.repeatCount = INFINITY;
    //8.autoreverses動畫結束時是否執(zhí)行逆動畫
    basic.autoreverses = YES;
    //10.將動畫添加到圖層
    [animationView.layer addAnimation:basic forKey:@"basic"];
}

設置漸變動畫(透明)

#pragma mark - 透明動畫 -
-(void)SetAlphaAnimationView
{
    //1.創(chuàng)建一個View圖層
    UIView *animationView = [[UIView alloc]initWithFrame:CGRectMake(300, 50, 70, 70)];
    animationView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:animationView];
    //2.創(chuàng)建動畫圖層 contents
    CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"opacity"];
    basic.fromValue= [NSValue valueWithCGPoint:CGPointMake(0.3, 0)];
    //5.設置toValue所要改變屬性的結束時的值
    basic.toValue = [NSValue valueWithCGPoint:CGPointMake(1, 0)];
    
    //6.設置duration動畫的持續(xù)時間
    basic.duration = 0.6;
    
    //7.設置repeatCount動畫的持續(xù)次數(shù)
    basic.repeatCount = INFINITY;
    //8.autoreverses動畫結束時是否執(zhí)行逆動畫
    basic.autoreverses = YES;
    //10.將動畫添加到圖層
    [animationView.layer addAnimation:basic forKey:@"basic"];
}

  • CAKeyframeAnimation(關鍵幀動畫)

常用屬性有:

  • values 當path=nil的時候才起作用
  • keyTimes 一個包含若干NSNumber(浮點型值在0.0~1.0之間)對象值得數(shù)組凶杖,用來區(qū)分動畫的分割時機
  • calculationMode 計算模式 主要針對的是每一幀的內(nèi)容來為一個坐標點的情況,也就是對anchorPoint和Position進行的動畫
    • kCAAnimationLinear 默認值 自定義控制動畫的時間款筑,可以設置keyTimes,表示當關鍵幀為坐標點的時候智蝠,關鍵幀之間之間直線相連進行插針計算 線性的
    • kCAAnimationDiscrete 離散的 不進行插值計算 所有關鍵幀之間逐個兒進行顯示
    • kCAAnimationPaced 節(jié)奏動畫,自動計算動畫的運動時間奈梳,使得動畫勻速運行 keyTimes和timingFunctions無效
    • kCAAnimationCubic 對關鍵幀為坐標點的關鍵幀進行圓滑曲線相連后進行插值計算 若為曲線形狀杈湾,可通過tensionValues,continuityValues,biasValues來進行調(diào)整自定義,其主要目的是使得運行軌跡變得圓滑攘须,而曲線動畫則要設置timingFunctions
    • kCAAnimationCubicPaced 在系統(tǒng)時間內(nèi)運行的距離相同漆撞,keyTimes和timingFunctions無效
  • rotationMode 旋轉樣式
    • kCAAnimationRotateAuto 根據(jù)路徑自動旋轉
    • kCAAnimationRotateAutoReverse 根據(jù)路徑自動翻轉
創(chuàng)建一個例子來實現(xiàn)一下
#pragma mark - CAKeyframeAnimation 核心動畫里面的幀動畫 -
-(void)setCAKeyframeAnimationView
{
    //1.創(chuàng)建View
    UIView * keyFrameAnimation = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
    keyFrameAnimation.center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0-50);
    //2.創(chuàng)建貝塞爾曲線
    UIBezierPath * bezierPath = [UIBezierPath bezierPathWithOvalInRect:keyFrameAnimation.frame];
    //3.再創(chuàng)建一個存放動畫的View
    UIView * animationView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 70, 80)];
    animationView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:animationView];
    //2.創(chuàng)建幀動畫
    CAKeyframeAnimation * keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyframe.duration = 5;
    keyframe.path = bezierPath.CGPath;
    /**
     calculationMode的屬性:
     kCAAnimationLinear  默認值 自定義控制動畫的時間,可以設置keyTimes,表示當關鍵幀為坐標點的時候,關鍵幀之間之間直線相連進行插針計算  線性的
     kCAAnimationDiscrete 離散的 不進行插值計算 所有關鍵幀之間逐個兒進行顯示
     kCAAnimationPaced  節(jié)奏動畫浮驳,自動計算動畫的運動時間悍汛,使得動畫勻速運行 keyTimes和timingFunctions無效
     kCAAnimationCubic  對關鍵幀為坐標點的關鍵幀進行圓滑曲線相連后進行插值計算 若為曲線形狀,可通過tensionValues,continuityValues,biasValues來進行調(diào)整自定義至会,其主要目的是使得運行軌跡變得圓滑离咐,而曲線動畫則要設置timingFunctions
     kCAAnimationCubicPaced  在系統(tǒng)時間內(nèi)運行的距離相同,keyTimes和timingFunctions無效
     */
    //calculationMode   計算模式 主要針對的是每一幀的內(nèi)容來為一個坐標點的情況奉件,也就是對anchorPoint和Position進行的動畫
    keyframe.calculationMode = kCAAnimationPaced;
    keyframe.repeatCount = INFINITY;
    //設置旋轉樣式
    /**
     kCAAnimationRotateAuto  根據(jù)路徑自動旋轉
     kCAAnimationRotateAutoReverse  根據(jù)路徑自動翻轉
     */
    keyframe.rotationMode = kCAAnimationRotateAutoReverse;
    [animationView.layer addAnimation:keyframe forKey:@"keyframe"];
    
    //創(chuàng)建CAShapeLayer
    CAShapeLayer * shape = [[CAShapeLayer alloc]init];
    shape.strokeColor = [UIColor lightGrayColor].CGColor;
    shape.fillColor = [UIColor clearColor].CGColor;
    shape.lineWidth = 0.5;
    shape.lineJoin = kCALineJoinRound;
    shape.lineCap = kCALineCapRound;
    shape.path = bezierPath.CGPath;
    [self.view.layer addSublayer:shape];
}

  • CATransition(轉場動畫)

CATransition的常用屬性有:

  • type - 轉場動畫的類型
    • kCATransitionFade 淡出效果 (支持方向)
    • kCATransitionMoveIn 將新視圖移動到舊視圖上 (支持方向)
    • kCATransitionPush 用新視圖來推出舊視圖 (支持方向)
    • kCATransitionReveal 將舊視圖移開后再顯示新視圖 (支持方向)
  • subtype - 轉場動畫將要去往的方向
    • kCATransitionFromRight 從右側轉場
    • kCATransitionFromLeft 從左側轉場
    • kCATransitionFromTop 從頂部轉場
    • kCATransitionFromBottom 從底部轉場
  • TimingFunction - 時間函數(shù)
  • repeatCount - 重復顯示次數(shù)
創(chuàng)建一個例子來實現(xiàn)上述屬性:
#pragma mark - CATransition 主要用于轉場動畫從一個場景以動畫的形式來過渡到另一個場景 -
-(void)setCATransitionView
{
    //1宵蛀、創(chuàng)建顯示效果名稱的label
    self.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
    self.textLabel.center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.width/2.0);
    self.textLabel.backgroundColor = [UIColor colorWithRed:88/255.0 green:161/255.0 blue:86/255.0 alpha:1];
    self.textLabel.textColor = [UIColor whiteColor];
    self.textLabel.font = [UIFont systemFontOfSize:16];
    self.textLabel.numberOfLines = 0;
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.textLabel];
    
    //創(chuàng)建按鈕
    self.clickButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 180, 40)];
    self.clickButton.center = CGPointMake(self.view.frame.size.width/2.0f, self.view.frame.size.height-240);
    self.clickButton.backgroundColor = [UIColor blackColor];
    [self.clickButton setTitle:@"AnimationChange" forState:UIControlStateNormal];
    self.clickButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.clickButton addTarget:self action:@selector(ClickButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.clickButton];
}
//MARK:點擊按鈕,開始改變轉場動畫效果
-(void)ClickButton
{
    //創(chuàng)建轉場動畫圖層
    CATransition * transition = [CATransition new];
    //設置代理事件
    transition.delegate = self;
    //設置時間
    transition.duration = 1.0;
    //設置時間函數(shù)
    /**
     kCAMediaTimingFunctionLinear
     kCAMediaTimingFunctionEaseIn
     kCAMediaTimingFunctionEaseOut
     kCAMediaTimingFunctionEaseInEaseOut
     kCAMediaTimingFunctionDefault 默認
     */
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    
    //設置轉場動畫類型 默認從第一個開始
    [transition setType:self.animationArray[_index]];
    
    //設置轉場動畫的方向
    /**
     kCATransitionFromRight  從右側轉場
     kCATransitionFromLeft   從左側轉場
     kCATransitionFromTop    從頂部轉場
     kCATransitionFromBottom 從底部轉場
     */
    transition.subtype = kCATransitionFromLeft;
    //設置重復次數(shù)
    transition.repeatCount = 1;
    //設置鍵值對
    [transition setValue:@"transitionAnim" forKey:@"anim"];
    //添加到視圖
    [self.textLabel.layer addAnimation:transition forKey:@"transition"];
    self.textLabel.text = [NSString stringWithFormat:@"This Animation was %@",self.animationArray[_index]];
}
#pragma mark - 實現(xiàn)CAAnimationDelegate方法 -
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    _index = (_index<self.animationArray.count-1)?_index+1:0;
    
    if(flag)
    {
        //創(chuàng)建轉場動畫
        CATransition * tranAnimation = [CATransition new];
        tranAnimation.delegate = self;
        tranAnimation.duration = 1.0;
        [tranAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
        tranAnimation.type = self.animationArray[_index];
        tranAnimation.subtype = kCATransitionFromLeft;
        tranAnimation.autoreverses = YES;
        tranAnimation.repeatCount = 1;
        [tranAnimation setValue:@"transitionAnim" forKey:@"anim"];
        [self.textLabel.layer addAnimation:tranAnimation forKey:@"transition"];
        self.textLabel.text = [NSString stringWithFormat:@"This Animation was %@",self.animationArray[_index]];
    }
}

  • CASpringAnimation(彈簧動畫)

CASpringAnimation的常用屬性有:

  • mass 質(zhì)量县貌,影響圖層運動時的彈簧慣性术陶,質(zhì)量越大,彈簧的拉伸和壓縮的幅度越大煤痕,動畫的速度變慢梧宫,并且波動幅度變大
  • stiffness 剛度系數(shù) = 勁度系數(shù)/彈性系數(shù) 當剛度系數(shù)越大的時候,形變所產(chǎn)生的力就越大杭攻,運動就越快
  • damping 阻尼系數(shù) 阻止彈簧伸縮的系數(shù)祟敛,阻尼系數(shù)越大疤坝,停止越快
  • initialVelocity 初始速率兆解,動畫的初始速度大小速率為正數(shù)時,速度方向與運動方向一致跑揉,當速率為負數(shù)的時候锅睛,速度方向和運動方向相反
  • settingDuration 結算時間,返回彈簧動畫到停止時的估算時間历谍,根據(jù)當前的動畫參數(shù)估算通常動畫的時間使用結算時間比較準確
創(chuàng)建一個例子來實現(xiàn)上述屬性
#pragma mark - CASpringAnimation 彈簧動畫-
-(void)setCASpringAnimationView
{
    //創(chuàng)建label
    self.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 50, 30)];
    self.textLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.textLabel];
    
    //創(chuàng)建彈簧動畫
    CASpringAnimation * spring = [CASpringAnimation animationWithKeyPath:@"position.x"];
    spring.damping = 5;
    spring.stiffness = 100;
    spring.mass = 1;
    spring.initialVelocity = 0;
    spring.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.textLabel.layer.position.x, 0)];
    spring.toValue = [NSValue valueWithCGPoint:CGPointMake(self.textLabel.layer.position.x+100, 0)];
    spring.autoreverses = YES;
    spring.repeatCount = INFINITY;
    [self.textLabel.layer addAnimation:spring forKey:spring.keyPath];
}

備注:

  • CABasicAnimation(基礎動畫)和CAKeyframeAnimation(幀動畫)的區(qū)別&聯(lián)系
  • CABasicAnimation可以看做是最多只有兩個關鍵幀的CAKeyframeAnimation
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末现拒,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子望侈,更是在濱河造成了極大的恐慌印蔬,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脱衙,死亡現(xiàn)場離奇詭異侥猬,居然都是意外死亡,警方通過查閱死者的電腦和手機捐韩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進店門退唠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人荤胁,你說我怎么就攤上這事瞧预。” “怎么了?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵垢油,是天一觀的道長盆驹。 經(jīng)常有香客問我,道長秸苗,這世上最難降的妖魔是什么召娜? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮惊楼,結果婚禮上玖瘸,老公的妹妹穿的比我還像新娘。我一直安慰自己檀咙,他們只是感情好雅倒,可當我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著弧可,像睡著了一般蔑匣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上棕诵,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天裁良,我揣著相機與錄音,去河邊找鬼校套。 笑死价脾,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的笛匙。 我是一名探鬼主播侨把,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼妹孙!你這毒婦竟也來了秋柄?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤蠢正,失蹤者是張志新(化名)和其女友劉穎骇笔,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嚣崭,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡笨触,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了有鹿。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片旭旭。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖葱跋,靈堂內(nèi)的尸體忽然破棺而出持寄,到底是詐尸還是另有隱情源梭,我是刑警寧澤,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布稍味,位于F島的核電站废麻,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏模庐。R本人自食惡果不足惜烛愧,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望掂碱。 院中可真熱鬧怜姿,春花似錦、人聲如沸疼燥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽醉者。三九已至但狭,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間撬即,已是汗流浹背立磁。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留剥槐,地道東北人唱歧。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像才沧,于是被迫代替她去往敵國和親迈喉。 傳聞我的和親對象是個殘疾皇子绍刮,可洞房花燭夜當晚...
    茶點故事閱讀 45,781評論 2 361

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