美文网首页
为视图设置阴影以及填色

为视图设置阴影以及填色

作者: endymion33 | 来源:发表于2018-02-04 11:15 被阅读0次

    //

    //  ENDHypView.m

    //  ENDHyp

    //

    //  Created by 周长鑫 on 2018/2/2.

    //  Copyright © 2018年 endymion. All rights reserved.

    //

    #import "ENDHypView.h"

    @implementation ENDHypView

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    - (void)drawRect:(CGRect)rect {

        // Drawing code

        //获取上下文

        CGContextRef currentRef = UIGraphicsGetCurrentContext();

        //确定同心圆的圆心

        CGPointcenter;

        center.x = self.bounds.origin.x + self.bounds.size.width / 2.0;

        center.y = self.bounds.origin.y + self.bounds.size.height / 2.0;

        //设定最大半径

        float maxRadius = hypot(self.bounds.size.width, self.bounds.size.height) / 2.0;

        //设定圆圈颜色

        CGContextSetStrokeColorWithColor(currentRef, [[UIColor lightGrayColor] CGColor]);

        //设定线宽

        CGContextSetLineWidth(currentRef, 10.0);

        //画同心圆

        for(floatcurrentRadius = maxRadius; currentRadius >0; currentRadius -=20) {

            CGContextAddArc(currentRef, center.x, center.y, currentRadius,0,M_PI*2,YES);

            CGContextStrokePath(currentRef);

        }

        CGContextMoveToPoint(currentRef, center.x,100);

        CGContextAddLineToPoint(currentRef, center.x-150,600);

        CGContextAddLineToPoint(currentRef, center.x+150,600);

        //啊啊啊啊啊!!我开始就是忘了这行

        CGContextClosePath(currentRef);

        //开始填色

        CGContextSaveGState(currentRef);

        CGContextClip(currentRef);

        CGFloatlocations[2]  = {0.0,1.0};

        CGFloatcomponents[8] = {1.0,0.0,0.0,1.0,// 起始颜色为红色

            1.0,1.0,0.0,1.0};// 起始颜色为黄色

        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

        CGGradientRefgradient =CGGradientCreateWithColorComponents(colorSpaceRef, components, locations,2);

        CGContextDrawLinearGradient(currentRef, gradient,CGPointMake(center.x,100),CGPointMake(center.x,600),0);

        CGContextRestoreGState(currentRef);

        //填色结束

        //做阴影

        CGContextSaveGState(currentRef);

        CGContextSetShadow(currentRef, CGSizeMake(4, 7), 3);

        UIImage*image = [UIImageimageNamed:@"logo.png"];

        [imagedrawInRect:CGRectMake(100,100,self.bounds.size.width-200,self.bounds.size.height-200)];

        CGContextRestoreGState(currentRef);

        //做阴影结束

    }

    -(instancetype)initWithFrame:(CGRect)frame

    {

        if(self= [superinitWithFrame: frame]){

            [self setBackgroundColor:[UIColor clearColor]];

        }

        return self;

    }

    @end

    结果图:

    相关文章

      网友评论

          本文标题:为视图设置阴影以及填色

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