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

为视图设置阴影以及填色

作者: 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

结果图:

相关文章

  • 为视图设置阴影以及填色

    // // ENDHypView.m // ENDHyp // // Created by 周长鑫 on 2018...

  • 给视图添加阴影效果同时设置圆角效果

    有时候我们需要同时设置视图的圆角效果和阴影效果,但是当为视图切割圆角后,设置的阴影效果很可能就显示不出来,其实这种...

  • swift shadow 阴影设置及注意事项

    swift shadow 设置阴影注意事项: 父类视图及自身属性masksToBounds 为false 关于s...

  • UIView圆角+阴影

    backView是要添加阴影的视图,思路是backView设置corner圆角,再添加一个带阴影的父视图,代码如下...

  • iOS设置阴影

    在通过这样的方式设置阴影时,必须把父视图的masksToBounds属性关掉,因为阴影设置的方式就是加offset...

  • 为UIView设置阴影效果

    在通过这样的方式设置阴影时,必须把父视图的masksToBounds属性关掉,因为阴影设置的方式就是加offset...

  • 000-CALayer阴影

    1、通过设置CALayer来给视图控件添加阴影 2、效果如下:

  • 说说都有哪些图标设计风格

    断点设计、双色搭配、高光设置、填色错位 渐变色使用、半透明装饰、统一的阴影 拟物图标icon手机应用3D仿物图标A...

  • iOS开发常用知识点一

    1、如果要设置视图的阴影效果,我们必须不能设置以下代码 view.layer.masksToBounds = YE...

  • iOS中给view设置阴影效果

    iOS开发中我们经常会遇到给指定视图设置其阴影效果 今天就来简单整理一下这里只是简单的给视图添加上阴影的效果 简单...

网友评论

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

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