渐变的效果

视图渐变的效果 通过渐变layer(CAGradientLayer)加载到bgview的mask上实现 ,中间视图要作为bgview的子视图 这样子试图才能达到渐变的效果

- (void)viewDidLoad {  
    [super viewDidLoad];  
    // Do any additional setup after loading the view.  

    UIView *gradinetView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];  
    gradinetView.backgroundColor = [UIColor redColor];  
    [self.view addSubview:gradinetView];  

    CAGradientLayer *gradinetLayer = [CAGradientLayer layer];  
    NSArray *colors = @[(id)[UIColor colorWithWhite:0 alpha:0].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:0.5].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:1].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:0.5].CGColor,  
                        (id)[UIColor colorWithWhite:0 alpha:0].CGColor,  
                        ];  
    [gradinetLayer setColors:colors];  
    [gradinetLayer setStartPoint:CGPointMake(0, 0)];  
    [gradinetLayer setEndPoint:CGPointMake(0, 1)];  
    [gradinetLayer setFrame:gradinetView.bounds];  
    [gradinetView.layer setMask:gradinetLayer];  


    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:gradinetLayer.bounds];  
    sv.backgroundColor = [UIColor whiteColor];  
    [sv setContentInset:UIEdgeInsetsMake(150, 0, 0, 0)];  
    [gradinetView addSubview:sv];  

    NSString *text = @"新华社北京1月10日电中共中央政治局常务委员会1月10日全天召开会议,听取全国人大常委会、国务院、全国政协、最高人民法院、最高人民检察院党组工作汇报,听取中央书记处工作报告。中共中央总书记习近平主持会议并发表重要讲话。会议强调,办好中国的事情,关键在党。中国特色社会主义最本质的特征是中国共产党领导,中国特色社会主义制度的最大优势是中国共产党领导。坚持党的领导首先是坚持党中央集中统一领导。坚持党中央集中统一领导,是全面从严治党、严肃党内政治生活、加强党内监督的重要目的,也是一条根本的政治规矩。要深入学习贯彻党的十八届六中全会精神,严格执行《关于新形势下党内政治生活的若干准则》、《中国共产党党内监督条例》,牢固树立政治意识、大局意识、核心意识、看齐意识,坚持以党的旗帜为旗帜、以党的方向为方向、以党的意志为意志,切实把党中央重大决策部署落实到各项工作中去。会议认为,过去的一年,全国人大常委会、国务院、全国政协、最高人民法院、最高人民检察院党组坚持党中央集中统一领导,维护党中央权威,落实全面从严治党责任,规范党内政治生活制度,强化政治领导责任,扎实开展“两学一做”学习教育。坚持围绕中心、服务大局,在贯彻落实党中央重大决策部署上凝神聚焦发力,为推进党和国家各项事业发展、实现“十三五”良好开局作出了积极贡献";  
    CGRect rect = [text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil];  
    CGFloat height = rect.size.height;  
    sv.contentSize = CGSizeMake(0, height);  

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sv.bounds.size.width, height)];  
    label.textColor = [UIColor darkGrayColor];  
    label.numberOfLines = 0;  
    label.textAlignment = NSTextAlignmentLeft;  
    label.text =text;  
    label.font = [UIFont systemFontOfSize:18];  
    [sv addSubview:label];  
}