ios常用的几个动画代码

 

 

#import "MianViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface MianViewController ()
{
    UIImageView *shakeFeedbackOverlay;
    NSTimer *rotateTimer;
    int timeCount;
}

@end

@implementation MianViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"View翻转";
    
//    shakeFeedbackOverlay.alpha = 0.0;
//    shakeFeedbackOverlay.layer.cornerRadius = 10.0; //设置圆角半径
    

    shakeFeedbackOverlay = [[UIImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 320)];
    shakeFeedbackOverlay.backgroundColor = [UIColor redColor];
    [self.view addSubview:shakeFeedbackOverlay];
    
    /**左右摇动**/
    /*
    CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    shake.fromValue = [NSNumber numberWithFloat:-M_PI/32];
    shake.toValue = [NSNumber numberWithFloat:+M_PI/32];
    shake.duration = 0.1;
    shake.autoreverses = YES; //是否重复
    shake.repeatCount = 20;
    [shakeFeedbackOverlay.layer addAnimation:shake forKey:@"shakeAnimation"];
    shakeFeedbackOverlay.alpha = 1.0;
    [UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:^{
        shakeFeedbackOverlay.alpha = 0.0; //透明度变0则消失
    } completion:nil];
     */
    
    
    /**顺时针旋转**/
    /*
    CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    shake.fromValue = [NSNumber numberWithFloat:0];
    shake.toValue = [NSNumber numberWithFloat:2*M_PI];
    shake.duration = 0.8;
    shake.autoreverses = NO;
    shake.repeatCount = 99999;
    [shakeFeedbackOverlay.layer addAnimation:shake forKey:@"shakeAnimation"];
    shakeFeedbackOverlay.alpha = 1.0;
    [UIView animateWithDuration:10.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:^{
        shakeFeedbackOverlay.alpha = 0.0;
    } completion:nil];
     */
    
    /*
    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";
    [shakeFeedbackOverlay.layer addAnimation:animation forKey:@"position"];
     */
    
    /**组合动画**/
    /*
    CABasicAnimation *flip = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    flip.toValue = [NSNumber numberWithDouble:-M_PI];
    CABasicAnimation *scale= [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scale.toValue = [NSNumber numberWithDouble:12];
    scale.duration = 1.5;
    scale.autoreverses = YES;
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray arrayWithObjects:flip, scale, nil];
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    group.duration = 3;
    group.fillMode = kCAFillModeForwards;
    group.removedOnCompletion = NO;
    [shakeFeedbackOverlay.layer addAnimation:group forKey:@"position"];
     */
    
    
    /*指定时间内旋转图片*/
    timeCount = 100; //动画执行100次
    [self startRotate];
    
    
}

//启动定时器旋转光圈

- (void)startRotate
{
    rotateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                        target:self
                                                      selector:@selector(rotateGraduation)
                                                      userInfo:nil
                                                       repeats:YES];    
}

//关闭定时器
- (void)stopTimer
{
    if ([rotateTimer isValid])
    {
        [rotateTimer invalidate];
        rotateTimer = nil;
    }
}

//旋转动画
- (void)rotateGraduation
{
    timeCount--;
    if (timeCount == 0)
    {
        [self stopTimer];
        // doSomeThing //旋转完毕 可以干点别的
        timeCount = 100;
    }
    else
    {
        //计算角度 旋转
        static CGFloat radian = 150 * (M_PI * 2 / 360);
        CGAffineTransform transformTmp = shakeFeedbackOverlay.transform;
        transformTmp = CGAffineTransformRotate(transformTmp, radian);
        shakeFeedbackOverlay.transform = transformTmp;
        
    };
    
}

 

原文地址:https://www.cnblogs.com/joesen/p/3270721.html