为视图设置阴影以及填色

//

//  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


结果图:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,729评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,558评论 5 13
  • 你是一树一树的花开,是燕在梁间呢喃,你是爱,是暖,是希望,你是人间四月天。似一抹烟霞飘过,不留痕迹,却温暖了整个春...
    券商老阿姨阅读 4,785评论 1 11
  • 刚刚收到晖姐的微信,她要共享单车,却打不开。 想了不少办法,还是不行。 最后,灵光一炫——好像是可以遥控的。叫她报...
    benz77777阅读 1,621评论 0 0
  • 绘画之路从简单开始…
    小易Y阅读 1,427评论 0 4

友情链接更多精彩内容