我有類稱爲Circle
(的UIView
的子類),它被定義爲IBDesignable
,和它的一些屬性被定義爲IBInspectable
。此外,我正試圖設置Circle類的一些標準屬性,並在設計時間處看到與我對可檢查屬性所做的更改相同的更改。在Circle
類定義的可檢查屬性的情況下,如strokeThickness
,如果它們通過IB更改,一切正常,我立即看到我的view/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;
但儘管如此,如果我在我的廈門國際銀行文件點擊,我看不到在設計時的變化,但而不是運行時。那麼在設計時如何看到這些變化?
這裏的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定義爲:
@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.
我已經看到故事板的變化。我沒有看到xib文件中的更改。我在我的問題中描述過,但如果您認爲需要更多信息,請讓我知道。我目前無法上傳示例項目... – Whirlwind
另外我的用法是不要將此屬性定義爲可檢查的,並且可以通過IB在我的xib文件中進行編輯。我需要通過編程來定義每個Circle。例如,所有圓圈(它們都是XibOwnerClass的一部分)將具有相同的顏色和相同的strokeColor。但是,其中一些將有不同的開始和結束角度。如果這對你有意義。 – Whirlwind
@Whirlwind:請檢查我編輯的答案。 –