2010-03-05 14 views
2

Quel est le problème avec ce code?UIVisez les coins arrondis Question

-(void) drawRect:(CGRect) rect { 
     CGContextRef c = UIGraphicsGetCurrentContext(); 
     if (c != nil) { 
      CGContextSetFillColorWithColor(c, self.cornerColor.CGColor); 
      [self drawRoundedCornersInRect:self.bounds inContext:c]; 
      CGContextFillPath(c); 
     } 
    } 

    -(void) drawCornerInContext:(CGContextRef)c cornerX:(int) x cornerY:(int) y 
      arcEndX:(int) endX arcEndY:(int) endY { 
     CGContextMoveToPoint(c, x, endY); 
     CGContextAddArcToPoint(c, x, y, endX, y, radius); 
     CGContextAddLineToPoint(c, x, y); 
     CGContextAddLineToPoint(c, x, endY); 
    } 

    -(void) drawRoundedCornersInRect:(CGRect) rect inContext:(CGContextRef) c { 
    int x_left = rect.origin.x; 
    int x_left_center = rect.origin.x + radius; 
    int x_right_center = rect.origin.x + rect.size.width - radius; 
    int x_right = rect.origin.x + rect.size.width; 
    int y_top = rect.origin.y; 
    int y_top_center = rect.origin.y + radius; 
    int y_bottom_center = rect.origin.y + rect.size.height - radius; 
    int y_bottom = rect.origin.y + rect.size.height; 

     if (roundUpperLeft) { 
      [self drawCornerInContext:c cornerX: x_left cornerY: y_top 
        arcEndX: x_left_center arcEndY: y_top_center]; 
     } 

     if (roundUpperRight) { 
      [self drawCornerInContext:c cornerX: x_right cornerY: y_top 
        arcEndX: x_right_center arcEndY: y_top_center]; 
     } 

     if (roundLowerRight) { 
      [self drawCornerInContext:c cornerX: x_right cornerY: y_bottom 
        arcEndX: x_right_center arcEndY: y_bottom_center]; 
     } 

     if (roundLowerLeft) { 
      [self drawCornerInContext:c cornerX: x_left cornerY: y_bottom 
        arcEndX: x_left_center arcEndY: y_bottom_center]; 
     } 
    } 

Aucune erreur, aucun avertissement ... mais les coins arrondis ne fonctionnent pas du tout. J'ai trouvé ce code here.

Répondre

4

Vous devez commencer un chemin et fermez-le lorsque vous avez terminé. Votre deuxième appel à CGContextAddLineToPoint salit les choses je pense. Voici un extrait qui fonctionne. Étudiez-le et améliorez-le pour soutenir vos cas multiples (il semble que vous voulez pouvoir arrondir seulement quelques coins, et pas nécessairement tous ...)

void addRoundedRect(CGContextRef ctx, CGRect rect, float cornerRadius) { 
    if (cornerRadius <= 2.0) { 
     CGContextAddRect(ctx, rect); 
    } else { 
     float x_left = rect.origin.x; 
     float x_left_center = x_left + cornerRadius; 
     float x_right_center = x_left + rect.size.width - cornerRadius; 
     float x_right = x_left + rect.size.width; 
     float y_top = rect.origin.y; 
     float y_top_center = y_top + cornerRadius; 
     float y_bottom_center = y_top + rect.size.height - cornerRadius; 
     float y_bottom = y_top + rect.size.height; 
     /* Begin path */ 
     CGContextBeginPath(ctx); 
     CGContextMoveToPoint(ctx, x_left, y_top_center); 
     /* First corner */ 
     CGContextAddArcToPoint(ctx, x_left, y_top, x_left_center, y_top, cornerRadius); 
     CGContextAddLineToPoint(ctx, x_right_center, y_top); 
     /* Second corner */ 
     CGContextAddArcToPoint(ctx, x_right, y_top, x_right, y_top_center, cornerRadius); 
     CGContextAddLineToPoint(ctx, x_right, y_bottom_center); 
     /* Third corner */ 
     CGContextAddArcToPoint(ctx, x_right, y_bottom, x_right_center, y_bottom, cornerRadius); 
     CGContextAddLineToPoint(ctx, x_left_center, y_bottom); 
     /* Fourth corner */ 
     CGContextAddArcToPoint(ctx, x_left, y_bottom, x_left, y_bottom_center, cornerRadius); 
     CGContextAddLineToPoint(ctx, x_left, y_top_center); 
     /* Done */ 
     CGContextClosePath(ctx); 
    } 
} 
+0

Merci pour le partager! :) – looneygrc

+0

c'est trop grand, nous pouvons utiliser seulement UILayer corner-rayon propriété comme mentionné dans ce lien http://cocoabugs.blogspot.com/2010/08/add-rounded-corners-and-border-to.html –

2

J'utilise ceci: [view.layer setCornerRadius:7];. Il définit le rayon de coin à l'aide du support de UIView.

+0

Je n'étais pas clair je suppose. Je veux spécifier quel coin sera arrondi. Avec CALayer cornerRadius je ne peux pas faire ça. – looneygrc

+0

réglage coin rayon et la bordure pour voir l'échantillon http://cocoabugs.blogspot.com/2010/08/add-rounded-corners-and-border-to.html –