0

我有一个带有动态生成的图形上下文的 UIView。它的正常大小是 100x100,但它可以放大到 200x200,所以我希望它的大小实际上是 200x200,以便看起来很好并且不会模糊,但是它显示出来。

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

我的代码:

 - (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);
}
4

1 回答 1

0

设置框架后,您是否为查看调用 setNeedsDisplay 方法

于 2012-11-28T09:51:30.370 回答