view渐变色,透明度渐变
生活随笔
收集整理的這篇文章主要介紹了
view渐变色,透明度渐变
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 功能描述
開發(fā)中經(jīng)常遇到這樣的需求:view2顯示在view1上面,透過view2可以漸漸的看到view1.效果如圖1所示:view1是一個imageView,view2是一個普通view。view1與view2完全重疊,view2從下到上由灰色到白色,由不透明到透明。圖2為只有view2的效果,圖3為只有view1的效果。
2 代碼如下
如果只想顏色漸變,而不需要透明度的漸變,可以將alpha設(shè)為1.0。
?
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.view.bounds]; 4 imgView.image = [UIImage imageNamed:@"sun"]; 5 [self.view addSubview:imgView]; 6 7 UIView *gradView = [[UIView alloc] initWithFrame:self.view.bounds]; 8 CAGradientLayer *layer = [self getGradientWithFrame:gradView.bounds]; 9 [gradView.layer insertSublayer:layer atIndex:0]; 10 [self.view addSubview:gradView]; 11 } 12 13 - (CAGradientLayer *)getGradientWithFrame:(CGRect) frame { 14 int colorOneV = 33; 15 int colorTwoV = 33; 16 int colorThreeV = 33; 17 // 設(shè)置顏色與不透明度alpha,如果只想漸變而不想透明,可以將alpha設(shè)為1.0。 18 UIColor *colorOne = [UIColor colorWithRed:(colorOneV/255.0) green:(colorOneV/255.0) blue:(colorOneV/255.0) alpha:0.0]; 19 UIColor *colorTwo = [UIColor colorWithRed:(colorTwoV/255.0) green:(colorTwoV/255.0) blue:(colorTwoV/255.0) alpha:0.5]; 20 UIColor *colorThree = [UIColor colorWithRed:(colorThreeV/255.0) green:(colorThreeV/255.0) blue:(colorThreeV/255.0) alpha:1.0]; 21 NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, colorThree.CGColor , nil]; 22 NSNumber *stopOne = [NSNumber numberWithFloat:0.0]; 23 NSNumber *stopTwo = [NSNumber numberWithFloat:0.5]; 24 NSNumber *stopThree = [NSNumber numberWithFloat:1.0]; 25 NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, stopThree, nil]; 26 // 漸變圖層 27 CAGradientLayer *layer = [CAGradientLayer layer]; 28 layer.colors = colors; 29 layer.locations = locations; 30 layer.frame = frame; 31 return layer; 32 }?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/mddblog/p/4705277.html
總結(jié)
以上是生活随笔為你收集整理的view渐变色,透明度渐变的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: innodb和myisam数据类型,即在
- 下一篇: DP专题