UIView:画出两倍大小

时间:2012-11-28 08:14:00

标签: iphone cocoa-touch uiview

我有一个动态生成图形上下文的UIView。它的正常尺寸是100x100,但它可以放大到200x200,所以我希望它的尺寸​​实际上是200x200,以便显示得很好而且不会模糊,但显示它。

如何在drawRect中绘制200x200,显示大小为100或大小为200?如果我以100x100显示它,则传递给drawRect的rect小于我需要绘制的区域。

我的代码:

 - (void) drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // prevent the drawings to be flipped
    CGContextTranslateCTM(ctx, 0, rect.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    CGContextDrawImage(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height), [UIImage imageNamed:@"actionBg.png"].CGImage);

    // generate the overlay
    if ([self isActive] == NO && self.fullDelay != 0) { // TODO: remove fullDelay check!
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
        CGContextRef overlayCtx = UIGraphicsGetCurrentContext();

        int segmentSize = (rect.size.height / [self fullDelay]);

        for (int i=0; i<[self fullDelay]; i++) {
            float alpha = 0.9 - (([self fullDelay] * 0.1) - (i * 0.1));
            [[UIColor colorWithRed:120.0/255.0 green:14.0/255.0 blue:14.0/255.0 alpha:alpha] setFill];

            if (currentDelay > i) {
                CGRect r = CGRectMake(0, i * segmentSize, rect.size.width, segmentSize);
                CGContextFillRect(overlayCtx, r);
            }
            [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.3] setFill];
            CGRect line = CGRectMake(0, (i * segmentSize) + segmentSize - 1 , rect.size.width, 1);
            CGContextFillRect(overlayCtx, line);
        }

        UIImage *overlay = UIGraphicsGetImageFromCurrentImageContext();
        UIImage *overlayMasked = [TDUtilities maskImage:overlay withMask:[UIImage imageNamed:@"actionMask.png"]];

        // prevent the drawings to be flipped
        CGContextTranslateCTM(ctx, 0, rect.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);

        // put the overlay
        CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
        CGContextDrawImage(ctx, rect, overlayMasked.CGImage);
        CGContextSetBlendMode(ctx, kCGBlendModeNormal);

        UIGraphicsEndImageContext();
    }

    // prevent the drawings to be flipped
    CGContextTranslateCTM(ctx, 0, rect.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);


    // draw the delay symbol
    CGContextDrawImage(ctx, rect, [UIImage imageNamed:@"delaySymbol.png"].CGImage);

    CGContextSetAlpha(ctx, 0.8);
    // draw the symbol
    NSString *imgName = [K_ACTION_IMAGES objectAtIndex:[self actionType]];
    CGContextDrawImage(ctx, rect, [UIImage imageNamed:imgName].CGImage);

    CGContextSetAlpha(ctx, 1);

    // draw the delay number
    imgName = [NSString stringWithFormat:@"%d.png", [self fullDelay]];
    CGContextDrawImage(ctx, rect, [UIImage imageNamed:imgName].CGImage);

    // draw the priority number
    imgName = [NSString stringWithFormat:@"%d.png", [self actionType]];
    CGContextDrawImage(ctx, CGRectMake(32, 0, rect.size.width, rect.size.height), [UIImage imageNamed:imgName].CGImage);
}

1 个答案:

答案 0 :(得分:0)

您是否在设置框架

后调用了setNeedsDisplay方法