在view上添加渐变的透明度的颜色

  /**透明度渐变 */

    UIImageView *imag = [[UIImageView alloc]initWithFrame:CGRectMake(10, 50, 300,500)];

    imag.image = [UIImage imageNamed:@"1"];

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];

    UIColor *color1 = [[UIColor blackColor]colorWithAlphaComponent:0.0] ;

    UIColor *color2 = [[UIColor blackColor]colorWithAlphaComponent:0.1] ;

    UIColor *color3 = [[UIColor blackColor]colorWithAlphaComponent:1] ;

 

    CAGradientLayer *layer = [CAGradientLayer layer];

    NSArray *colorAry = @[(__bridge id)color1.CGColor,

                          (__bridge id)color2.CGColor,

                          (__bridge id)color3.CGColor];

    layer.colors = colorAry;

    layer.startPoint = CGPointMake(0, 0);;

    layer.endPoint = CGPointMake(0, 1);

    layer.locations  = @[@(0.25),@(0.5),@(0.75)];

    layer.frame = view.bounds;

    [view.layer addSublayer:layer];

    CGContextRef context = UIGraphicsGetCurrentContext();

    UIGraphicsPushContext(context);

    [imag addSubview:view];

    [self.view addSubview:imag];

posted @ 2016-09-14 13:06  半窗疏影  阅读(473)  评论(0编辑  收藏  举报