2012-11-28 38 views
0

我有一個帶動態生成圖形上下文的UIView。它的正常尺寸是100x100,但它可以放大到200x200,所以我希望它的尺寸​​實際上是200x200,以便顯示很好而且不會模糊。UIView:以兩倍的大小繪製

如何在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); 
} 
+1

你是如何縮放起來後查看?你正在改變視圖的「框架」或「邊界」嗎?或者你是否設置了視圖的「變換」? –

+0

我改變框架 – pistacchio

+0

你有沒有嘗試將視圖的'contentMode'設置爲'UIViewContentModeRedraw'?你可以在代碼或xib中完成它。這是否給你你想要的結果?部分是 –

回答

0

你打電話setNeedsDisplay方法爲您設置它的框架

+0

的問題,但這並未解決問題 – pistacchio