小技巧ios

使一个UIView有透明梯度的从中间到左边和右边的效果。

OC

小技巧ios_第1张图片

CAGradientLayerlayer *gradientLayer=[CAGradientLayerlayer];

gradientLayer.frame=baseView.bounds;gradientLayer.startPoint=CGPointMake(0.5,0.0);gradientLayer.endPoint=CGPointMake(0.5,1.0);gradientLayer.locations=@[@(0.0),@(0.2),@(1.0)];

gradientLayer.colors=@[(id)[UIColorcolorWithWhite:1.0alpha:0.9].CGColor,(id)[UIColorcolorWithWhite:1.0alpha:0.3].CGColor,(id)[UIColorcolorWithWhite:1.0alpha:0.0].CGColor];

[baseView.layer addSublayer:gradientLayer]; 


swift版本

let mask=CAGradientLayer()

mask.startPoint=CGPointMake(0.0,0.5)

mask.endPoint=CGPointMake(1.0,0.5)

let whiteColor=UIColor.whiteColor()

mask.colors=[whiteColor.colorWithAlphaComponent(0.0).CGColor,whiteColor.colorWithAlphaComponent(1.0),whiteColor.colorWithAlphaComponent(1.0).CGColor]

mask.locations=[NSNumber(double:0.0),NSNumber(double:0.2),NSNumber(double:1.0)]mask.frame=label.bounds

label.layer.mask=mask

你可能感兴趣的:(小技巧ios)