【问题标题】:Accessing properties of class defined as IBDesignable in drawRect: method访问在 drawRect: 方法中定义为 IBDesignable 的类的属性
【发布时间】:2017-05-10 19:07:11
【问题描述】:

我有一个名为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.

【问题讨论】:

    标签: ios objective-c xcode uiview interface-builder


    【解决方案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 中更新时)。

    【讨论】:

    • 我已经看到了故事板的变化。我没有看到 xib 文件的变化。我在我的问题中描述了这一点,但如果您认为需要更多信息,请告诉我。我目前无法上传示例项目...
    • 另外,我的预期用途是不将此属性定义为可检查的,并且可通过我的 xib 文件中的 IB 进行编辑。我需要按 Circle 以编程方式定义这些。例如,所有圆圈(属于 XibOwnerClass 的一部分)将具有相同的颜色和相同的 strokeColor。但是,其中一些将具有不同的开始和结束角度。如果这对你有意义。
    • 我犯了一个最愚蠢的错误 - 我试图在错误的圆实例上设置这些 startAngle 和 endAngle 属性。因此,无论我使用可检查属性还是通过代码设置所有内容,都无法正常工作。我想我需要喝杯咖啡什么的 :) 无论如何,非常感谢您的努力和时间,因为当您说它适合您时,我知道该看什么。
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 2020-03-13
    • 2023-02-04
    • 2011-07-27
    • 1970-01-01
    • 2016-12-11
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多