访问在 drawRect: 方法中定义为 IBDesignable 的 class 的属性

Accessing properties of class defined as IBDesignable in drawRect: method

我将 class 称为 CircleUIView 的子 class),它被定义为 IBDesignable,它的一些属性定义为 IBInspectable。此外,我正在尝试设置 Circle class 上的一些标准属性,并在 设计时 查看更改,就像我对可检查属性所做的一样。对于在 Circle class 上定义的可检查属性,比如 strokeThickness,如果它们通过 IB 更改,一切正常,我会立即在我的视图/xib 上看到更改。

当我说 xib 时,我的意思是我已经创建了 MyXib.xib 文件并将 UIView 拖到上面,并将自定义 class 设置为 XibOwnerClass。此外,当我加载 xib 时,如果更改 Circle class 的一些可检查属性,我会在设计时看到更改。

我该如何使用它:

我在故事板上有一个视图控制器,然后我拖动一个 UIView 并将其 class 更改为 XibOwnerClass。将 XibOwnerClass 想象成具有一个 Circle 的 class。现在在这里,如果我在我的 xib 文件中设置了圆圈有红色描边,我在 XibOWnerClass 中进一步添加的所有圆圈都将有红色描边,这很好。但问题是,我想为每个圆设置 startAngleendAngle

现在,如果我覆盖 prepareForInterfaceBuilder 方法并执行如下操作,即使这样也能工作:

  _circle.startAngle = 0.0f;
  _circle.endAngle = 360.0f;

  _anotherCircle.startAngle = 0.0f;
  _circle.endAngle = 270.0f;

但是,如果我点击我的 xib 文件,我无法在设计时看到更改,而是在运行时看到更改。那么如何使用此设置在设计时查看这些更改?

这是 drawRect: 圆的方法 class,但我想这甚至不需要,除了它显示了那些不可检查的属性的用法:

在 Circle.m 文件中:

#import "Circle.h"
#define getRadians(angle) ((angle) / 180.0 * M_PI)
#define getDegrees(radians) ((radians) * (180.0 / M_PI))
@implementation Circle


- (void)drawRect:(CGRect)rect {
    // Drawing code



    CGPoint centerPoint = CGPointMake(rect.size.width/2.0f, rect.size.height / 2.0f);

    UIBezierPath *path = [UIBezierPath
                          bezierPathWithArcCenter: centerPoint
                          radius:(rect.size.height / 2.0f)
                          startAngle: getRadians(self.startAngle)
                          endAngle: getRadians(self.endAngle)
                          clockwise:YES];

    CAShapeLayer *shape = [CAShapeLayer new];

    shape.path = path.CGPath;

    shape.fillColor = self.mainColor.CGColor;

    shape.strokeColor = self.strokeColor.CGColor;

    shape.lineWidth = self.ringThicknes;

    [self.layer addSublayer:shape];
}

并且属性在 .h 中定义为:

@property(nonatomic) CGFloat startAngle;

@property(nonatomic) CGFloat endAngle;

//Some more IBInspectable properties go here, and those are properties that I set when i want to affect all circles.

我已经实施了相同的方法,下面的解决方案正在运行,请在您的 .h 文件中进行更改:

#ifndef IB_DESIGNABLE
#define IB_DESIGNABLE
#endif

#import <UIKit/UIKit.h>

IB_DESIGNABLE @interface Circle : UIView

@property(nonatomic)IBInspectable CGFloat startAngle;

@property(nonatomic)IBInspectable CGFloat endAngle;

@property(nonatomic)IBInspectable CGFloat ringThicknes;

@property(nonatomic)IBInspectable UIColor* mainColor;

@property(nonatomic)IBInspectable UIColor *strokeColor;

@end

并且在 .m 文件中

#import "Circle.h"
#define getRadians(angle) ((angle) / 180.0 * M_PI)
#define getDegrees(radians) ((radians) * (180.0 / M_PI))

@implementation Circle

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self drawCircle];
    }
    return self;
}

-(void)drawCircle {
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2.0f, self.frame.size.height / 2.0f);

    UIBezierPath *path = [UIBezierPath
                          bezierPathWithArcCenter: centerPoint
                          radius:(self.frame.size.height / 2.0f)
                          startAngle: getRadians(self.startAngle)
                          endAngle: getRadians(self.endAngle)
                          clockwise:YES];

    CAShapeLayer *shape = [CAShapeLayer new];

    shape.path = path.CGPath;

    shape.fillColor = self.mainColor.CGColor;

    shape.strokeColor = self.strokeColor.CGColor;

    shape.lineWidth = self.ringThicknes;

    [self.layer addSublayer:shape];

}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [self drawCircle];
}

如果您仍然遇到相同的问题,请确保打开 "Automatically Refresh Views"(在:编辑器 > 自动刷新视图中)或通过 "Refresh All Views" 手动更新视图(在:编辑器 > 刷新所有视图中)(当您需要在 IB 中更新时)。