為視圖設(shè)置陰影以及填色

//

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


? ? //設(shè)定最大半徑

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

? ? //設(shè)定圓圈顏色

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

? ? //設(shè)定線寬

? ? 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);

? ? //填色結(jié)束


? ? //做陰影

? ? 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);

? ? //做陰影結(jié)束

}

-(instancetype)initWithFrame:(CGRect)frame

{

? ? if(self= [superinitWithFrame: frame]){

? ? ? ? [self setBackgroundColor:[UIColor clearColor]];

? ? }

? ? return self;

}

@end


結(jié)果圖:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,688評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,260評論 5 13
  • 你是一樹一樹的花開,是燕在梁間呢喃,你是愛,是暖,是希望,你是人間四月天。似一抹煙霞飄過,不留痕跡,卻溫暖了整個春...
    券商老阿姨閱讀 1,172評論 1 11
  • 剛剛收到暉姐的微信,她要共享單車,卻打不開。 想了不少辦法,還是不行。 最后,靈光一炫——好像是可以遙控的。叫她報...
    benz77777閱讀 248評論 0 0
  • 繪畫之路從簡單開始…
    小易Y閱讀 201評論 0 4

友情鏈接更多精彩內(nèi)容