首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >制作NSArray IBInspectable

制作NSArray IBInspectable
EN

Stack Overflow用户
提问于 2015-04-17 21:39:15
回答 1查看 3.5K关注 0票数 10

是否有一种方法可以使用NSArray IBInspectable来定义Storyboard自定义视图中的多个值?

我知道NSArray没有任何关于将存储在其中的对象类型的信息,所以它可能是一个问题。但是有一些使用注释之类的解决方案吗?

我想做的是通过童话板设置一个NSArray of UIColors,并在Storyboard可视化期间在视图中绘制一个渐变层。

我开始创建两个属性,名为startColor和endColor。这很好,但我想这样做更通用。

这是我的drawRect方法

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    
    gradientLayer.frame = self.bounds;
    
    gradientLayer.colors = [NSArray arrayWithObjects:(id)[self.startColor CGColor], (id)[self.endColor CGColor], nil];
    
    [self.layer addSublayer:gradientLayer];
}

和我想做这样的事情:

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    
    gradientLayer.frame = self.bounds;
    
    // colorsArray should be an IBInspectable property in order to be setted on Storyboard Attributes Inspector
    gradientLayer.colors = self.colorsArray;
    
    [self.layer addSublayer:gradientLayer];
}
EN

回答 1

Stack Overflow用户

发布于 2015-10-22 05:23:18

Xcode不支持7.1版本的可检查数组。

如果你选择一个最大数量的渐变停止,你可以为渐变层起奶酪。下面是我的测试,其中我硬编码了六站:

顺便说一句,您不应该在drawRect:中添加子视图或子层。在这个阶段,系统并不期望对层层次结构进行更新。你应该用layoutSubviews来做。

下面是我用来创建屏幕快照的代码。

GradientView.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface GradientView : UIView

@property(nonatomic, readonly, strong) CAGradientLayer *layer;

@property (nonatomic) IBInspectable CGPoint startPoint;
@property (nonatomic) IBInspectable CGPoint endPoint;

@property (nonatomic, strong) IBInspectable UIColor *color0;
@property (nonatomic) IBInspectable CGFloat location0;

@property (nonatomic, strong) IBInspectable UIColor *color1;
@property (nonatomic) IBInspectable CGFloat location1;

@property (nonatomic, strong) IBInspectable UIColor *color2;
@property (nonatomic) IBInspectable CGFloat location2;

@property (nonatomic, strong) IBInspectable UIColor *color3;
@property (nonatomic) IBInspectable CGFloat location3;

@property (nonatomic, strong) IBInspectable UIColor *color4;
@property (nonatomic) IBInspectable CGFloat location4;

@property (nonatomic, strong) IBInspectable UIColor *color5;
@property (nonatomic) IBInspectable CGFloat location5;

@end

GradientView.m

代码语言:javascript
复制
#import "GradientView.h"

#define MaxStops 6

typedef struct {
    CGColorRef color;
    CGFloat location;
} GradientStop;

@implementation GradientView

@dynamic layer;

- (void)setStartPoint:(CGPoint)startPoint {
    self.layer.startPoint = startPoint;
}

- (CGPoint)startPoint {
    return self.layer.startPoint;
}

- (void)setEndPoint:(CGPoint)endPoint {
    self.layer.endPoint = endPoint;
}

- (CGPoint)endPoint {
    return self.layer.endPoint;
}

#define DefineSetters(i) \
    - (void)setColor##i:(UIColor *)color##i { \
        _color##i = color##i; \
        [self setNeedsLayout]; \
    } \
 \
    - (void)setLocation##i:(CGFloat)location##i { \
        _location##i = location##i; \
        [self setNeedsLayout]; \
    }

DefineSetters(0)
DefineSetters(1)
DefineSetters(2)
DefineSetters(3)
DefineSetters(4)
DefineSetters(5)

+ (Class)layerClass {
    return [CAGradientLayer class];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self updateGradient];
}

static BOOL isValidStop(const GradientStop *stop) {
    return stop->color != nil && stop->location >= 0 && stop->location <= 1;
}

static int compareStops(const void *a, const void *b) {
    const GradientStop *stop0 = a;
    const GradientStop *stop1 = b;

    if (isValidStop(stop0) && isValidStop(stop1)) {
        if (stop0->location < stop1->location) {
            return -1;
        } else if (stop0->location > stop1->location) {
            return 1;
        } else {
            return 0;
        }
    } else if (isValidStop(stop0)) {
        return -1;
    } else if (isValidStop(stop1)) {
        return 1;
    } else {
        return 0;
    }
}

static size_t countOfValidStops(const GradientStop *stops, size_t maxStops) {
    for (size_t i = 0; i < maxStops; ++i) {
        if (!isValidStop(&stops[i])) {
            return i;
        }
    }
    return maxStops;
}

- (void)updateGradient {
    GradientStop stops[MaxStops];
    [self setStop:stops+0 color:self.color0 location:self.location0];
    [self setStop:stops+1 color:self.color1 location:self.location1];
    [self setStop:stops+2 color:self.color2 location:self.location2];
    [self setStop:stops+3 color:self.color3 location:self.location3];
    [self setStop:stops+4 color:self.color4 location:self.location4];
    [self setStop:stops+5 color:self.color5 location:self.location5];
    qsort(stops, MaxStops, sizeof *stops, compareStops);

    size_t count = countOfValidStops(stops, MaxStops);
    NSMutableArray *colors = [NSMutableArray arrayWithCapacity:count];
    NSMutableArray *locations = [NSMutableArray arrayWithCapacity:count];
    [self populateColors:colors locations:locations fromStops:stops count:count];
    self.layer.colors = colors;
    self.layer.locations = locations;
}

- (void)setStop:(GradientStop *)stop color:(UIColor *)color location:(CGFloat)location {
    stop->color = color.CGColor;
    stop->location = location;
}

- (void)populateColors:(NSMutableArray *)colors locations:(NSMutableArray *)locations fromStops:(const GradientStop *)stops count:(size_t)count {
    for (size_t i = 0; i < count; ++i) {
        [colors addObject:(__bridge id)stops[i].color];
        [locations addObject:@(stops[i].location)];
    }
}

@end
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29709818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档