2016-12-26 167 views
0

我有類稱爲Circle(的UIView的子類),它被定義爲IBDesignable,和它的一些屬性被定義爲IBInspectable。此外,我正試圖設置Circle類的一些標準屬性,並在設計時間處看到與我對可檢查屬性所做的更改相同的更改。在Circle類定義的可檢查屬性的情況下,如strokeThickness,如果它們通過IB更改,一切正常,我立即看到我的view/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; 

但儘管如此,如果我在我的廈門國際銀行文件點擊,我看不到在設計時的變化,但而不是運行時。那麼在設計時如何看到這些變化?

這裏的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. 

回答

1

我已經實現了相同及以下解決方案正在工作,請在您的.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中更新時)。

+0

我已經看到故事板的變化。我沒有看到xib文件中的更改。我在我的問題中描述過,但如果您認爲需要更多信息,請讓我知道。我目前無法上傳示例項目... – Whirlwind

+0

另外我的用法是不要將此屬性定義爲可檢查的,並且可以通過IB在我的xib文件中進行編輯。我需要通過編程來定義每個Circle。例如,所有圓圈(它們都是XibOwnerClass的一部分)將具有相同的顏色和相同的strokeColor。但是,其中一些將有不同的開始和結束角度。如果這對你有意義。 – Whirlwind

+0

@Whirlwind:請檢查我編輯的答案。 –