【问题标题】:iOS drawRect with custom height具有自定义高度的iOS drawRect
【发布时间】:2013-09-02 18:53:53
【问题描述】:

我在 viewController 上放置了一个视图,我想在这个视图中绘制一个矩形,但具有自定义高度。我根据 viewController 中的参数确定高度。 举个例子。如果我的参数是 50,我希望矩形的高度为 UIView 的 50%。

2 个问题:

  • 如何将高度传递给自定义drawRect?

  • 如何让矩形放置在 UIView 的底部?

我已经使用 Interface Builder 放置了视图,并在 UIView 的子类中实现了 drawRect,并将其用作 UIView 的自定义类。

所以在我的自定义类中:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor * lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];

    CGRect paperRect =  self.bounds;

    drawLinearGradient(context, paperRect, lightGrayColor.CGColor, [UIColor clearColor].CGColor);


}

void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };

    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];

    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);

    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

    CGContextSaveGState(context);
    CGContextAddRect(context, rect);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

这在我的视图中绘制了一个漂亮的渐变矩形,但它填充了整个 UIView。 这是因为 self.bounds。

我在自定义类中添加了一个属性 *height,但我不知道如何从我的 viewController 中填充它。所以我希望它从 UIView 的底部开始,并使其与我确定的一样高(实际高度的 %)。

有人知道我是怎么做到的吗?

【问题讨论】:

  • 为什么不添加一个子视图(当前视图的 50%),然后填充它?

标签: ios height drawrect


【解决方案1】:

我认为你可以计算 50% 的屏幕高度,然后从全屏高度中减去新的视图高度

youParameter = 50; // Lets assume this is your parametere

int heightOfView   = ([UIScreen mainScreen].bounds.size.height * yourParameter) / 100;

// For placing the view to the bottom;

CGRect newFrame;
frame.origin.y = [UIScreen mainScreen].bounds.size.height - heightOfView;
frame.origin.x = 0;
frame.size.width = [UIScreen mainScreen].bounds.size.width; // assuming it will take full width
frame.size.height = heightOfView;

youViewToChange.frame = newFrame; // normally do this or

要将值传递给drawRect,您可以这样做:

[self drawRect:newFrame];

【讨论】:

  • Hmm.. 当我把它放在我的 UIViewController 中时,这会使视图也变小,但我希望视图保持全高,但在(或顶部)绘制一个矩形这种观点。当我将它放在 drawRect 方法中时,矩形具有新的高度,但是我没有那里的参数...
【解决方案2】:

您是否在界面构建器的身份检查器中设置了自定义视图类?

您可以从 viewController 设置 height 属性:

((MyViewClass *) self.myView).height = myCalculatedValue;

然后实现drawRect:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor * lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];

    CGRect paperRect =  self.bounds;
    paperRect.origin.y = paperRect.size.height - self.height;
    paperRect.size.height = self.height;
    //assuming your height property is of CGFloat type

    drawLinearGradient(context, paperRect, lightGrayColor.CGColor, [UIColor clearColor].CGColor);
}

这将绘制从底部上方的(高度)点到底部的渐变

【讨论】:

  • 太棒了!我已经在inspector中设置了自定义视图类,但是我不知道(MyViewClass *)添加...非常感谢!!!
  • 不客气!如果 IBOutlet 的类型设置为 MyViewClass,则不需要类型转换。
  • 啊....这是我的错误!!!我将它设置为 UIView,而不是 CustomView...doh 请给这个加分!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
相关资源
最近更新 更多