美文网首页
简单动画

简单动画

作者: 伪装v5 | 来源:发表于2018-07-26 16:15 被阅读14次

-(void)removeAnima

{

    NSArray *arr = [self.layer.sublayers copy];

    for(CALayer*layerinarr) {

        [layerremoveFromSuperlayer];

    }

}

- (void)setAnimaType:(NSInteger)idx

{

    [self removeAnima];

    switch(idx) {

        case0:// 无动画

            break;

        case1:

            [selfanimaCross];

            break;

        case2:

            [selfanimaWindow];

            break;

        case3:

            [selfanimaFan];

            break;

        case4:

            [selfanimaDissolve];

            break;

        case5:

            [selfanimaRightIn];

            break;

        case6:

            [selfanimaCircle];

            break;

        case7:

            [selfanimaBoard];

            break;

        case8:

            [selfanimaSplit];

            break;

        default:

            break;

    }

}

// 扇形展开动画

- (void)animaFan

{

    CABasicAnimation *animation;

    animation = [CABasicAnimationanimationWithKeyPath:@"strokeEnd"];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.duration=2;

    animation.fromValue= [NSNumbernumberWithFloat:1.0];

    animation.toValue= [NSNumbernumberWithFloat:0.0];

    animation.repeatCount=CGFLOAT_MAX;

    CAShapeLayer *layer = [CAShapeLayer layer];

    layer.lineWidth=100;

    UIBezierPath *path = [UIBezierPath bezierPath];

    path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:50 startAngle:M_PI_2 endAngle:M_PI*3/2 clockwise:YES];

    layer.path= path.CGPath;

    layer.fillColor = [UIColor clearColor].CGColor;

    layer.strokeColor = [UIColor whiteColor].CGColor;

    [layeraddAnimation:animationforKey:@""];

    [self.layeraddSublayer:layer];

    CAShapeLayer *layer2 = [CAShapeLayer layer];

    layer2.lineWidth=100;

    UIBezierPath *path2 = [UIBezierPath bezierPath];

    path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:50 startAngle:M_PI_2 endAngle:M_PI*3/2 clockwise:NO];

    layer2.path= path2.CGPath;

    layer2.fillColor = [UIColor clearColor].CGColor;

    layer2.strokeColor = [UIColor whiteColor].CGColor;

    [layer2addAnimation:animationforKey:@""];

    [self.layeraddSublayer:layer2];

}

// 轮子展开动画

- (void)animaCircle

{

    CABasicAnimation *animation;

    animation = [CABasicAnimationanimationWithKeyPath:@"strokeEnd"];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.duration=2;

    animation.fromValue= [NSNumbernumberWithFloat:1.0];

    animation.toValue= [NSNumbernumberWithFloat:0.0];

    animation.repeatCount=CGFLOAT_MAX;

    CAShapeLayer *layer = [CAShapeLayer layer];

    layer.lineWidth=200;

    UIBezierPath *path = [UIBezierPath bezierPath];

    path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:100 startAngle:M_PI*3/2 endAngle:-M_PI_2 clockwise:NO];

    layer.path= path.CGPath;

    layer.fillColor = [UIColor clearColor].CGColor;

    layer.strokeColor = [UIColor whiteColor].CGColor;

    [layeraddAnimation:animationforKey:@""];

    [self.layeraddSublayer:layer];

}

// 百叶窗

- (void)animaWindow

{

    CABasicAnimation *animation;

    animation = [CABasicAnimationanimationWithKeyPath:@"strokeEnd"];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.duration=2;

    animation.fromValue= [NSNumbernumberWithFloat:1.0];

    animation.toValue= [NSNumbernumberWithFloat:0.0];

    animation.repeatCount=CGFLOAT_MAX;

    CGFloat h = self.frame.size.height;

    NSIntegercount =7;

    for(inti =0; i < count; i++) {

        CAShapeLayer *layer = [CAShapeLayer layer];

        layer.lineWidth = self.frame.size.width;

        UIBezierPath *path = [UIBezierPath bezierPath];

        [pathmoveToPoint:CGPointMake(self.frame.size.width/2, h/count*i)];

        [pathaddLineToPoint:CGPointMake(self.frame.size.width/2, h/count*(i+1))];

        layer.path= path.CGPath;

        layer.fillColor = [UIColor clearColor].CGColor;

        layer.strokeColor = [UIColor whiteColor].CGColor;

        [layeraddAnimation:animationforKey:@""];

        [self.layeraddSublayer:layer];

    }

}

// 劈裂效果

- (void)animaSplit

{

    CABasicAnimation *animation;

    animation = [CABasicAnimationanimationWithKeyPath:@"strokeEnd"];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.duration=2;

    animation.fromValue= [NSNumbernumberWithFloat:1.0];

    animation.toValue= [NSNumbernumberWithFloat:0.0];

    animation.repeatCount=CGFLOAT_MAX;

    CAShapeLayer *layer = [CAShapeLayer layer];

    layer.lineWidth = self.frame.size.height;

    UIBezierPath *path = [UIBezierPath bezierPath];

    [pathmoveToPoint:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];

    [pathaddLineToPoint:CGPointMake(0, self.frame.size.height/2)];

    layer.path= path.CGPath;

    layer.fillColor = [UIColor clearColor].CGColor;

    layer.strokeColor = [UIColor whiteColor].CGColor;

    [layeraddAnimation:animationforKey:@""];

    [self.layeraddSublayer:layer];

    CAShapeLayer *layer2 = [CAShapeLayer layer];

    layer2.lineWidth = self.frame.size.height;

    UIBezierPath *path2 = [UIBezierPath bezierPath];

    [path2moveToPoint:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];

    [path2addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height/2)];

    layer2.path= path2.CGPath;

    layer2.fillColor = [UIColor clearColor].CGColor;

    layer2.strokeColor = [UIColor whiteColor].CGColor;

    [layer2addAnimation:animationforKey:@""];

    [self.layeraddSublayer:layer2];

}

// 十字扩展 效果

- (void)animaCross

{

    CGFloat w = self.frame.size.width;

    CGFloat h = self.frame.size.height;

    CABasicAnimation *animation;

    animation = [CABasicAnimationanimationWithKeyPath:@"bounds"];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.duration=2;

    animation.fromValue= [NSValuevalueWithCGRect:CGRectMake(0,h/4,w,h/2)];

    animation.toValue= [NSValuevalueWithCGRect:CGRectMake(w/2,h/2,w,0)];

    animation.repeatCount=CGFLOAT_MAX;

    CAShapeLayer *layer = [CAShapeLayer layer];

    layer.frame=CGRectMake(0,h/4,w,h/2);

    layer.backgroundColor = [UIColor whiteColor].CGColor;

    [layeraddAnimation:animationforKey:@""];

    [self.layeraddSublayer:layer];

    CABasicAnimation*animation2;

    animation2 = [CABasicAnimationanimationWithKeyPath:@"bounds"];

    animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation2.duration=2;

    animation2.fromValue= [NSValuevalueWithCGRect:CGRectMake(w/4,0,w/2,h)];

    animation2.toValue= [NSValuevalueWithCGRect:CGRectMake(w/2,h/2,0,h)];

    animation2.repeatCount=CGFLOAT_MAX;

    CAShapeLayer *layer2 = [CAShapeLayer layer];

    layer2.frame=CGRectMake(w/4,0,w/2,h);

    layer2.backgroundColor = [UIColor whiteColor].CGColor;

    [layer2addAnimation:animation2forKey:@""];

    [self.layeraddSublayer:layer2];

}

// 右边切入 效果

- (void)animaRightIn

{

    CGFloat w = self.frame.size.width;

    CGFloat h = self.frame.size.height;

    CABasicAnimation *animation;

    animation = [CABasicAnimationanimationWithKeyPath:@"position"];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.duration=2;

    animation.fromValue= [NSValuevalueWithCGRect:CGRectMake(w*3/2,h/2,w,h)];

    animation.toValue= [NSValuevalueWithCGRect:CGRectMake(w/2,h/2,w,h)];

    animation.repeatCount=CGFLOAT_MAX;

    CAShapeLayer *layerBack = [CAShapeLayer layer];

    layerBack.frame=CGRectMake(0,0,w,h);

    layerBack.backgroundColor = [UIColor whiteColor].CGColor;

    [self.layeraddSublayer:layerBack];

    CAShapeLayer *layer = [CAShapeLayer layer];

    layer.backgroundColor = [UIColor redColor].CGColor;

    layer.contents = (__bridge id)[UIImage imageNamed:@"anim_back"].CGImage;

    layer.contentsGravity = kCAGravityResizeAspectFill;

    layer.frame=CGRectMake(0,0,w,h);

    [layeraddAnimation:animationforKey:@""];

    [self.layeraddSublayer:layer];

}

// 棋盘动画 效果

- (void)animaBoard

{

    CGFloat w = self.frame.size.width;

    CGFloat h = self.frame.size.height;

    CAKeyframeAnimation *animation;

    animation = [CAKeyframeAnimationanimationWithKeyPath:@"strokeEnd"];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.duration=2;

    animation.repeatCount=CGFLOAT_MAX;

    animation.values=@[@(1.0),@(1.0),@(0)];

    animation.keyTimes = @[@(0.0),@(0.5),@(1.0)]; // 数组递增 [0-1]

    CAKeyframeAnimation *animation1;

    animation1 = [CAKeyframeAnimationanimationWithKeyPath:@"strokeEnd"];

    animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation1.duration=2;

    animation1.values=@[@(1.0),@(0),@(0)];

    animation1.keyTimes=@[@(0.0),@(0.5),@(1.0)];// 数组递增 [0-1]

    animation1.repeatCount=CGFLOAT_MAX;

    CGFloatpointw = w/10;

    CGFloatpointh = h/10;

    for(inti =0; i <10; i++)

    {

        for(intj =0; j <=10; j++)

        {

            CGFloatpointX = pointw*i;

            if(j%2!=0) {

                pointX -= pointw/2;

            }

            CGFloatpointY = pointh * j;

            CAShapeLayer*layer = [CAShapeLayerlayer];

            layer.lineWidth= pointh;

            UIBezierPath*path = [UIBezierPathbezierPath];

            [pathmoveToPoint:CGPointMake(pointX+pointw, pointY)];

            [pathaddLineToPoint:CGPointMake(pointX, pointY)];

            layer.path= path.CGPath;

            layer.fillColor= [UIColorclearColor].CGColor;

            layer.strokeColor= [UIColorwhiteColor].CGColor;

            [layeraddAnimation:animationforKey:@""];

            //                            layer.opacity = 0;

            if(j%2!=0) {

                [layeraddAnimation:animation1forKey:@""];

                layer.opacity=1;

            }

            [self.layeraddSublayer:layer];

        }

    }

}

// 溶解 效果

- (void)animaDissolve

{

    CGFloat w = self.frame.size.width;

    CGFloat h = self.frame.size.height;

    CGFloatpointw = w/20;

    CGFloatpointh = h/20;

    for(inti =0; i <=20; i++)

    {

        for(intj =0; j <=20; j++)

        {

            CGFloatpointX = pointw*i;

            if(j%2!=0) {

                pointX -= pointw/2;

            }

            CGFloatpointY = pointh * j;

            CAShapeLayer*layer = [CAShapeLayerlayer];

            layer.lineWidth= pointh;

            UIBezierPath*path = [UIBezierPathbezierPath];

            [pathmoveToPoint:CGPointMake(pointX+pointw, pointY)];

            [pathaddLineToPoint:CGPointMake(pointX, pointY)];

            layer.path= path.CGPath;

            layer.fillColor= [UIColorclearColor].CGColor;

            layer.strokeColor= [UIColorwhiteColor].CGColor;

            CAKeyframeAnimation*animation = [selfkeyAnimaWithIndex:random()%19rate:0.05];

            [layeraddAnimation:animationforKey:@""];

            [self.layeraddSublayer:layer];

        }

    }

}

#pragma  mark  --  帧动画:闪灭

- (CAKeyframeAnimation*)keyAnimaWithIndex:(NSInteger)idx rate:(CGFloat)rate

{

    CAKeyframeAnimation *animation;

    animation = [CAKeyframeAnimationanimationWithKeyPath:@"opacity"];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.duration=2;

    animation.values = @[@(1.0),@(1.0),@(0),@(0)];

    animation.keyTimes=@[@(0.0),@(rate*idx),@(rate*idx+rate/10),@(1.0)];// 数组递增 [0-1]

    animation.repeatCount=CGFLOAT_MAX;

    animation.removedOnCompletion = NO;

    returnanimation;

}

相关文章

网友评论

      本文标题:简单动画

      本文链接:https://www.haomeiwen.com/subject/kdvbmftx.html