【问题标题】:Passing parameters to drawRect将参数传递给drawRect
【发布时间】:2013-06-05 15:45:28
【问题描述】:

我有几个 UIView 类都绘制相同的东西,但颜色和 alpha 设置不同。我尝试传递参数,但无法弄清楚如何将 drawRect 部分放在需要的位置。

我是这样画的:

CGRect positionFrame = CGRectMake(widthCoordinate,heightCoordinate,20.9f,16.5f);
DrawHexBlue *hex = [[DrawHexBlue alloc] initWithFrame:positionFrame];
[self.imageView addSubview:hex];

我的 DrawHexBlue 类是这样的:

- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self)
{
    [self setOpaque:YES];
    [self setBackgroundColor:[UIColor clearColor]];
}
return self;

}

- (void)drawRect:(CGRect)rect{    
UIBezierPath *aPath = [UIBezierPath bezierPath];
[[UIColor whiteColor] setStroke];
[[UIColor blueColor] setFill];

// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(7, 0)];

// Draw the lines.
[aPath addLineToPoint:CGPointMake(16.2, 0)];
[aPath addLineToPoint:CGPointMake(20.9, 8.7)];
[aPath addLineToPoint:CGPointMake(16.2, 16.5)];
[aPath addLineToPoint:CGPointMake(6.7, 16.5)];
[aPath addLineToPoint:CGPointMake(2.1, 8.7)];
[aPath closePath];

[aPath setLineWidth:1.0f];
[aPath fillWithBlendMode:kCGBlendModeNormal alpha:0.75];
[aPath stroke];
}

我为我需要的每种新颜色和新的 alpha 值创建一个新类。当然必须有一种更好的方法来使用一个类并且只需更改参数/值...?

【问题讨论】:

    标签: ios uiview drawrect


    【解决方案1】:

    创建一个UIView 子类并添加属性:

    @interface YourView : UIView
    
    @property (nonatomic, strong) UIColor *strokeColor;
    @property (nonatomic, strong) UIColor *fillColor;
    
    @end
    

    那么在drawRect:你就可以访问这个属性了:

    - (void)drawRect:(CGRect)rect{    
        UIBezierPath *aPath = [UIBezierPath bezierPath];
        [self.strokeColor setStroke];
        [self.fillColor setFill];
    

    而且你可以在创建的时候在视图上设置:

    YourView *hex = [[YourView alloc] initWithFrame:positionFrame];
    hex.strokeColor = [UIColor whiteColor];
    hex.fillColor = [UIColor blueColor];
    

    【讨论】:

      【解决方案2】:

      是的。

      使用属性

      进行绘图的视图应该在其头文件中有这些代码行:

      @property (nonatomic, strong) UIColor *colorToDraw;
      
      - (id)initWithFrame:(CGRect)frame color:(UIColor *)color;
      

      你的init 方法应该是这样的:

      - (id)initWithFrame:(CGRect)frame color:(UIColor *)color
      {
          self = [super initWithFrame:frame];
          if (self)
          {
              [self setOpaque:YES];
              [self setBackgroundColor:[UIColor clearColor]];
              self.colorToDraw = color;
          }
          return self;
      }
      

      带有drawRect 的:

      - (void)drawRect:(CGRect)rect{    
          UIBezierPath *aPath = [UIBezierPath bezierPath];
          [[UIColor whiteColor] setStroke];
          [self.colorToDraw setFill];
      
          // Set the starting point of the shape.
          [aPath moveToPoint:CGPointMake(7, 0)];
      
      // Draw the lines.
          [aPath addLineToPoint:CGPointMake(16.2, 0)];
          [aPath addLineToPoint:CGPointMake(20.9, 8.7)];
          [aPath addLineToPoint:CGPointMake(16.2, 16.5)];
          [aPath addLineToPoint:CGPointMake(6.7, 16.5)];
          [aPath addLineToPoint:CGPointMake(2.1, 8.7)];
          [aPath closePath];
          
          [aPath setLineWidth:1.0f];
          [aPath fillWithBlendMode:kCGBlendModeNormal alpha:0.75];
          [aPath stroke];
      }
      

      现在,你可以这样画了:

      CGRect positionFrame = CGRectMake(widthCoordinate,heightCoordinate,20.9f,16.5f);   
      DrawHexBlue *hex = [[DrawHexBlue alloc] initWithFrame:positionFrame color:[UIColor orangeColor]]; //Or whatever.
      [self.imageView addSubview:hex];
      

      不过,您可能希望将子类的名称更改为不表示它会绘制蓝色的名称。

      【讨论】:

        【解决方案3】:

        创建属性来存储颜色和 alpha 数据。

        @interface DrawHex : UIView
        // ...
        @property (nonatomic) CGFloat pathAlpha;
        @property (nonatomic, strong) UIColor *strokeColor;
        @property (nonatomic, strong) UIColor *fillColor;    
        @end
        

        在你的这个类的init方法中为它们设置一些默认值,例如:

        self.fillColor = [UIColor blueColor];
        self.strokeColor = [UIColor whiteColor];
        self.pathAlpha = 0.75;
        

        然后在drawRect 中使用属性而不是硬编码值:

        [self.strokeColor setStroke];
        [self.fillColor setFill];
        //...
        [aPath fillWithBlendMode:kCGBlendModeNormal alpha:self.pathAlpha];
        

        然后在您的视图控制器中您可以更改它们:

        DrawHex *hex = [[DrawHex alloc] initWithFrame:positionFrame];
        hex.fillColor = [UIColor redColor];
        // you get the idea
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-09-27
          • 1970-01-01
          • 1970-01-01
          • 2013-01-27
          • 2013-11-19
          • 2011-01-06
          • 2013-07-19
          相关资源
          最近更新 更多