2013-01-10 16 views
5

Çekirdek grafikleriyle, bir yolun içerisini konturlamak mümkün mü? Çizgi ağırlığının aksine, dışarıdan yarıya ve yanmış bir yolun yarısına doğru çekiliyor mu?UIView drawRect: Bir yolun içine inmek mümkün mü?

Görüntülemenin bir kısmı ekran kenarında ve parçada değilse, vuruşun görünür kalınlığını kontrol etmenin daha kolay olmasının nedeni. Ekran kenarındaki parça kesilir, ekran tamamen görünen görünüm kenarı daha kalın görünür (kontur görünürse her iki tarafın da olduğu gibi). Senden önce yoluna

enter image description here

cevap

10

Klip onu felç.

+0

teşekkürler! Yukarıdaki "cevabım" a ve yorumunuza bakar mısınız? Bu yöntem oldukça işe yaramıyor. İnme hiç çekmiyor. Yolun klibi nedir, yöntem adı nedir? – Mrwolfy

+0

Kapa çeneni. [CGContextClip() 'için belgeler (https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextClip) diyor ki : "Yeni kırpma yolunu belirledikten sonra, işlev bağlamın geçerli yolunu boş bir yola sıfırlar." Bu, CGContextDraw'ın neden hiçbir şey çizmediğini açıklar - siz inmeden önce tekrar içeriğe yol eklemeniz gerekir. –

+0

Bir ['CGPath'] kullanma (https://developer.apple.com/library/ios/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/tdef/CGPathRef) veya [ "UIBezierPath"] (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBezierPath_class/Reference/Reference.html) bu işlemi daha kolay hale getirebilir - yolu bir kez oluşturabilirsiniz, sonra iki kere. –

6

Bu, hiçbir inme çizer:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor); 
    CGContextSetLineWidth(context, 14); 
    CGRect rrect = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect), CGRectGetHeight(rect)); 
    CGFloat radius = 30; 
    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect); 
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect); 
    CGContextMoveToPoint(context, minx, midy); 
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); 
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); 
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); 
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); 
    CGContextClosePath(context); 
    CGContextClip(context); 
    CGContextDrawPath(context, kCGPathStroke); 
} 

DÜZENLEME: Bu (doğru cevaba göre) çalıştı budur:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor); 
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
    CGContextSetLineWidth(context, 14); 
    CGRect pathRect = CGRectMake(10, 10, rect.size.width -20, rect.size.height -20); 
    CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:pathRect cornerRadius:20].CGPath; 
    CGContextAddPath(context, path); 
    CGContextClip(context); 
    CGContextAddPath(context, path); 
    CGContextDrawPath(context, kCGPathEOFillStroke); 

} 
İlgili konular