美文网首页
为控件设置渐变色

为控件设置渐变色

作者: 蚂蚁也疯狂 | 来源:发表于2017-01-10 17:46 被阅读21次

    //为颜色设置渐变效果

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 200, 80)];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    //设置开始和结束位置(设置渐变的方向)
    gradient.startPoint = CGPointMake(0, 1.0);
    gradient.endPoint = CGPointMake(0, 0);
    gradient.frame =CGRectMake(0,0,200,80);
    gradient.colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id) [UIColor whiteColor].CGColor,nil];

    [view.layer insertSublayer:gradient atIndex:0];
    [self.view addSubview:view];

    //为透明度设置渐变效果
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 200, 80)];

    UIColor *colorOne = [UIColor colorWithRed:(216/255.0) green:(0/255.0) blue:(18/255.0) alpha:1.0];
    UIColor *colorTwo = [UIColor colorWithRed:(216/255.0) green:(0/255.0) blue:(18/255.0) alpha:0.0];
    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];

    CAGradientLayer *gradient1 = [CAGradientLayer layer];
    //设置开始和结束位置(设置渐变的方向)
    gradient1.startPoint = CGPointMake(0, 1.0);
    gradient1.endPoint = CGPointMake(0, 0);
    gradient1.colors = colors;
    gradient1.frame = CGRectMake(0, 0, 200, 80);
    [view1.layer insertSublayer:gradient1 above:0];
    [self.view addSubview:view1];

    相关文章

      网友评论

          本文标题:为控件设置渐变色

          本文链接:https://www.haomeiwen.com/subject/bmxlbttx.html