drawRect没有调整大小并运行屏幕3.5英寸屏幕

时间:2014-05-07 19:50:45

标签: ios objective-c uiview drawrect

我正在使用绘制矩形来绘制角半径的矩形。代码完美无缺。在4英寸屏幕上查看时,您会看到矩形。在3.5英寸屏幕上查看时,矩形不会调整大小并从屏幕底部移开。有什么指针吗?

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect rrect = CGRectMake(20, 30, 280, 515);
    CGFloat radius = 7;

    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);

    // Start at 1
    CGContextMoveToPoint(context, minx, midy);
    // Add an arc through 2 to 3
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    // Add an arc through 4 to 5
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    // Add an arc through 6 to 7
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    // Add an arc through 8 to 9
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);

    // Close the path
    CGContextClosePath(context);

    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);

    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);

}

1 个答案:

答案 0 :(得分:1)

CGRect rrect = CGRectMake(20, 30, 280, 515);

这就是问题所在。您绘制的高度为515的矩形从偏移量20开始,而您的屏幕不是那么长。尝试使用视图的框架来绘制而不是硬编码的值

CGRect rrect = CGRectMake(20, 30, self.frame.size.width - 20*2, self.frame.size.height - 30*2);