【问题标题】:Gradient fill in CAGradientLayerCAGradientLayer中的渐变填充
【发布时间】:2020-01-20 14:45:14
【问题描述】:

我需要用径向填充填充CAGradientLayer 以获得像上面这样的输出

这是我尝试过的代码

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.colors = @[(id)[UIColor blueColor].CGColor, (id)[UIColor greenColor].CGColor];
//    gradientLayer.type = kCAGradientLayerRadial;

通过这段代码,我得到了下面的输出

如果有任何解决方案,请回答

提前致谢

【问题讨论】:

    标签: objective-c core-graphics


    【解决方案1】:

    这可能接近您想要的。这是一个自定义的IB_DESIGNABLE 视图,因此您可以在 IB / Storyboard 中看到它。


    SanjayView.h

    //
    //  SanjayView.h
    //
    //  Created by Don Mag on 1/21/20.
    //
    
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    IB_DESIGNABLE
    @interface SanjayView : UIView
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    SanjayView.m

    //
    //  SanjayView.m
    //
    //  Created by Don Mag on 1/21/20.
    //
    
    #import "SanjayView.h"
    
    @interface SanjayView ()
    @property (strong, nonatomic) CAGradientLayer *gLayer;
    @end
    
    @implementation SanjayView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self commonInit];
        }
        return self;
    }
    
    - (instancetype)initWithCoder:(NSCoder *)coder
    {
        self = [super initWithCoder:coder];
        if (self) {
            [self commonInit];
        }
        return self;
    }
    
    - (void)prepareForInterfaceBuilder
    {
        [super prepareForInterfaceBuilder];
        [self commonInit];
    }
    
    - (void)commonInit {
    
        self.backgroundColor = [UIColor clearColor];
    
        if (!_gLayer) {
            _gLayer = [CAGradientLayer new];
            [self.layer addSublayer:_gLayer];
        }
    
        _gLayer.type = kCAGradientLayerRadial;
    
        _gLayer.startPoint = CGPointMake(0.5, 0.5);
        _gLayer.endPoint = CGPointMake(1.0, 1.0);
    
        _gLayer.colors = @[
            (id)[UIColor whiteColor].CGColor,
            (id)[UIColor blueColor].CGColor,
            (id)[UIColor colorWithRed:0.0 green:0.5 blue:0.0 alpha:1.0].CGColor,
            (id)[UIColor greenColor].CGColor,
            (id)[UIColor clearColor].CGColor,
        ];
    
        _gLayer.locations = @[
                [NSNumber numberWithFloat:0.7],
                [NSNumber numberWithFloat:0.72],
                [NSNumber numberWithFloat:0.75],
                [NSNumber numberWithFloat:0.9],
                [NSNumber numberWithFloat:0.95],
        ];
    
    }
    
    - (void)layoutSubviews {
        [super layoutSubviews];
        _gLayer.frame = self.bounds;
    }
    
    @end
    

    IB成绩:

    注意:您看到的浅蓝色是 viewController 视图的背景。实际的SanjayView 有清晰的背景。

    玩弄颜色和位置,直到获得想要的结果。

    【讨论】:

      猜你喜欢
      • 2016-12-13
      • 1970-01-01
      • 2019-04-23
      • 2012-12-09
      • 2011-08-22
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多