ios常用動畫

iosbg 9年前發布 | 1K 次閱讀 Objective-C IOS

使用block

使用前需引入QuartzCore.framework, 并在相關文件中加入 #import"QuartzCore/QuartzCore.h"

定義

         shakeFeedbackOverlay為UIImageView

設置

      self.shakeFeedbackOverlay.alpha= 0.0;

      self.shakeFeedbackOverlay.layer.cornerRadius= 10.0;

      self.shakeFeedbackOverlay.layer masksToBounds = YES;


1、圖像左右抖動

      CABasicAnimation* shake =[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

     shake.fromValue = [NSNumbernumberWithFloat:-M_PI/32];

     shake.toValue = [NSNumbernumberWithFloat:+M_PI/32];

     shake.duration = 0.1;

     shake.autoreverses = YES;//是否重復

     shake.repeatCount = 4;

     [self.shakeFeedbackOverlay.layeraddAnimation:shake forKey:@"shakeAnimation"];

     self.shakeFeedbackOverlay.alpha= 1.0;

    [UIView animateWithDuration:2.0delay:0.0 options:UIViewAnimationOptionCurveEaseIn |UIViewAnimationOptionAllowUserInteraction animations:^{self.shakeFeedbackOverlay.alpha = 0.0; //透明度變0則消失 } completion:nil];

 

2、圖像順時針旋轉

    CABasicAnimation* shake = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];

     shake.fromValue = [NSNumbernumberWithFloat:0];

     shake.toValue = [NSNumbernumberWithFloat:2*M_PI];

     shake.duration = 0.8;shake.autoreverses = NO;

     shake.repeatCount = 1;

     [self.shakeFeedbackOverlay.layeraddAnimation:shake forKey:@"shakeAnimation"];

     self.shakeFeedbackOverlay.alpha= 1.0;

     [UIViewanimateWithDuration:10.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn |UIViewAnimationOptionAllowUserInteraction animations:^{self.shakeFeedbackOverlay.alpha = 0.0; } completion:nil];

 

3、圖像關鍵幀動畫

     CAKeyframeAnimation*animation = [CAKeyframeAnimation animation];

     CGMutablePathRef aPath =CGPathCreateMutable();

     CGPathMoveToPoint(aPath,nil, 20, 20);

     CGPathAddCurveToPoint(aPath,nil, 160, 30, 220, 220, 240, 420);

     animation.path = aPath;

     animation.autoreverses =YES;

     animation.duration = 2;

     animation.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

     animation.rotationMode =@"auto";

     [ballView.layeraddAnimation:animation forKey:@"position"];

4、組合動畫 CAAnimationGroup

     CABasicAnimation *flip =[CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];

    flip.toValue = [NSNumbernumberWithDouble:-M_PI];

    CABasicAnimation *scale=[CABasicAnimation animationWithKeyPath:@"transform.scale"];

    scale.toValue =[NSNumbernumberWithDouble:12];

     scale.duration = 1.5;

     scale.autoreverses = YES;

     CAAnimationGroup *group =[CAAnimationGroup animation];

     group.animations = [NSArrayarrayWithObjects:flip, scale, nil];

    group.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

     group.duration = 3;

     group.fillMode =kCAFillModeForwards;

    group.removedOnCompletion = NO;

     [ballView.layeraddAnimation:group forKey:@"position"];

 5、指定時間內旋轉圖片

 //啟動定時器旋轉光圈

 - (void)startRotate

 

{

    self.rotateTimer = [NSTimerscheduledTimerWithTimeInterval:0.02

           target:self

           selector:@selector(rotateGraduation)

           userInfo:nil

          repeats:YES];

}

 

//關閉定時器

 

- (void)stopTimer

 

{

 

    if ([self.rotateTimerisValid])

 

{

 

        [self.rotateTimerinvalidate];

        self.rotateTimer= nil;

 

    }

 

}

//旋轉動畫

- (void)rotateGraduation

{    self.timeCount--;

    if (self.timeCount == 0)

    {

         [selfstopTimer];

         //doSomeThing //旋轉完畢 可以干點別的

         self.timeCount= 25;

    }    else

    {

         //計算角度 旋轉

         staticCGFloat radian = 150 * (M_2_PI / 360);

         CGAffineTransformtransformTmp = self.lightImageView.transform;

         transformTmp= CGAffineTransformRotate(transformTmp, radian);

         self.lightImageView.transform= transformTmp;

    };

}

 調用方法

 self.timeCount = 25; //動畫執行25次

 [self startRotate];

 

1

2

3

4

5

最后那個

static CGFloat radian = 150 * (M_2_PI / 360);

 

應該改為

static CGFloat radian = 150 * (M_PI * 2 / 360);

//圖片的淡入淡出

// 淡入顯示新View

//   imgagee.alpha = 0.0f;

//   [UIView beginAnimations:@"fadeIn" context:nil];

     [self.view addSubview:imgagee];

//   [UIView setAnimationDuration:4.0];

//   imgagee.alpha = 1.0f;

//   [UIView commitAnimations];

   

   // 淡出移除當前View

   imgagee.alpha= 1.0f;

   [UIView beginAnimations:@"fadeIn" context:nil];

   [UIView setAnimationDuration:3];

   imgagee .alpha = 0.0f;

[UIView commitAnimations];

 

 

//視圖抖動動畫

// 視圖抖動動畫

+ (void)shakeView:(UIView *)viewduration:(CGFloat)fDuration

{

    if (view && (fDuration>= 0.1f))

    {

        CABasicAnimation*shake = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];

        //設置抖動幅度

        shake.fromValue= [NSNumber numberWithFloat:-0.3];

        shake.toValue= [NSNumber numberWithFloat:+0.3];

        shake.duration= 0.1f;

        shake.repeatCount= fDuration/4/0.1f;

        shake.autoreverses= YES;

        [view.layeraddAnimation:shake forKey:@"shakeView"];

    }else{}

}

 本文由用戶 iosbg 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!