访问drawRect:方法中定义为IBDENABL的类的属性



我的类称为CircleUIView的子类),并且将其定义为IBDesignable,其某些属性定义为IBInspectable。另外,我要设置的圆形类别上有一些标准属性,并在设计时间上与可检查属性相同。如果在Circle类中定义的可检查属性,例如strokeThickness,则如果它们通过IB进行了更改,则一切都可以,我会立即在视图/XIB上看到更改。

当我说XIB时,我的意思是我已经创建了myxib.xib文件并在其上拖动UIView,并将自定义类设置为XibOwnerClass。另外,当我加载xib时,如果更改Circle类的某些可检查属性,我会看到在设计时间发生更改。

我该如何使用:

我在情节提要上有一个视图控制器,然后我将Uiview拖动并将其类更改为XibOwnerClass。想象一下XibOwnerClass是一个具有Circle的类。现在在这里,如果我在xib文件中设置了圆圈的红色中风,那么我在Xibownerclass中进一步添加的所有圆圈都会带有红色冲程,这很好。但事实是,我想设置startAngleendAngle每个圈子。

现在,如果我覆盖prepareForInterfaceBuilder方法并执行这样的操作:

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

但是,如果我单击我的XIB文件,我看不到设计时间的更改,而是在运行时看到。那么,如何使用此设置在设计时间看到这些更改?

这是drawRect:circle类的方法,但我想这甚至不需要,除了它显示了那些不可接受的属性的用法:

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 as:

中定义
@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];
}

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

最新更新