我有一个名为Circle
的类(UIView
的子类),它定义为IBDesignable
,其某些属性定义为IBInspectable
。另外,我正在尝试在Circle类上设置一些标准属性,这些属性与我在可检查属性上所做的一样,在设计时可以看到它们的更改。对于在Circle
类中定义的可检查属性,例如strokeThickness
,如果通过IB对其进行了更改,则所有操作均正常,并且我在视图/ xib中立即看到了更改。
当我说xib时,我的意思是我已经创建MyXib.xib文件并将其拖到UIView
上,并将自定义类设置为XibOwnerClass
。另外,在加载该xib
时,如果更改Circle
类的某些可检查属性,则在设计时会看到更改。
我该如何使用:
我在情节提要板上有一个视图控制器,然后拖动UIView并将其类更改为XibOwnerClass
。将XibOwnerClass
想象为具有一个Circle
的类。现在,在这里,如果我在xib
文件中设置了圆圈为红色描边,那么我在XibOWnerClass中进一步添加的所有圆圈都将为红色描边,这很好。但问题是,我想为每个圆圈设置startAngle
和endAngle
。
现在即使这有效,如果我重写prepareForInterfaceBuilder
方法并执行以下操作:
_circle.startAngle = 0.0f;
_circle.endAngle = 360.0f;
_anotherCircle.startAngle = 0.0f;
_circle.endAngle = 270.0f;
但是,如果我单击xib文件,则在设计时看不到更改,而是在运行时看到。那么,如何通过这种设置在设计时看到这些变化呢?
这是Circle类的drawRect:方法,但是我猜想它甚至不需要,除了它显示了这些非检查属性的用法:
在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];
}
如果仍然遇到相同的问题,请确保在需要时打开“自动刷新视图”(在:编辑器>自动刷新视图中)或通过“刷新所有视图”手动更新视图(在:编辑器>刷新所有视图中)(需要时)在IB中进行更新)。