2017-01-06 4 views
0

enter image description here Je dessine une couche d'onde par substitution procédé drawInContext:(CGContextRef)ctx, étant donné que la ligne d'onde est sinusoïdale, je couper en segments par calculer la valeur de l'onde et les rejoindre dans CGMutablePathRefCALayer drawRect anticrénelage n'a pas travaillé

Voici ce que je l'ai fait dans -(void)drawInContext:(CGContextRef)ctx

//join style 
CGContextSetLineJoin(ctx, kCGLineJoinRound); 
CGContextSetLineCap(ctx, kCGLineCapRound); 

CGContextSetAllowsAntialiasing(ctx, true); 
CGContextSetShouldAntialias(ctx, true); 

CGContextSetFillColorWithColor(ctx, [UIColor themeColor].CGColor); 
CGContextSetStrokeColorWithColor(ctx, [UIColor themeColor].CGColor); 

CGContextSaveGState(ctx); 

CGMutablePathRef mutablePath = CGPathCreateMutable(); 

CGPathMoveToPoint(mutablePath, NULL, 0, 0); 
CGPathAddLineToPoint(mutablePath, NULL, 0, [self getSinPoint:0 WaveHeight:self.waveHeight]); 

//Calculate the Sin funciton value to draw lines and join them into path 
for (float i = 0; i <= self.bounds.size.width; i+= 1) { 
    NSInteger pointY = [self getSinPoint:i WaveHeight:self.waveHeight]; 
    CGPathAddLineToPoint(mutablePath, NULL, i, pointY); 
} 

CGPathAddLineToPoint(mutablePath, NULL, self.bounds.size.width, 0); 

CGPathCloseSubpath(mutablePath); 

[[UIColor themeColor]set]; 


CGContextAddPath(ctx, mutablePath); 

CGContextFillPath(ctx); 

CGContextRestoreGState(ctx); 

Répondre

1

enter image description here

Une solution utilise BezierPath pour dessiner la vague, 1.couper la ligne dans un segment tel que 20 2.l'indice de ligne actuel est un événement, obtenir le point de contrôle sous le centre du segment actuel 3.l'indice de ligne actuel est impair, puis placer le point de contrôle au centre de la ligne

... 
for (NSInteger i = 0; i <= self.waveCount; i++) { 

    CGPoint beginPoint = CGPointMake(i * self.waveWidth, pointY); 
    CGPoint endPoint = CGPointMake((i + 1) * self.waveWidth, pointY); 
    if (i%2 == 0) { 

     CGPoint controlPoint = [self getCenterControlPointUp:beginPoint End:endPoint]; 
     CGPathAddQuadCurveToPoint(mutablePath, NULL, controlPoint.x, controlPoint.y, endPoint.x, endPoint.y); 
    }else{ 
     CGPoint controlPoint = [self getCenterControlPointDown:beginPoint End:endPoint]; 
     CGPathAddQuadCurveToPoint(mutablePath, NULL, controlPoint.x, controlPoint.y, endPoint.x, endPoint.y); 
    } 
} 
... 
méthode

pour obtenir le point de contrôle d'une ligne de segment

//Below 
-(CGPoint)getCenterControlPointDown:(CGPoint)begin End:(CGPoint)end{ 
    return CGPointMake((begin.x + end.x)/2, (begin.y + end.y)/2 + self.waveHeight * 2); 
} 

//Above 
-(CGPoint)getCenterControlPointUp:(CGPoint)begin End:(CGPoint)end{ 
    return CGPointMake((begin.x + end.x)/2, (begin.y + end.y)/2 - self.waveHeight * 2); 
} 
+0

Peut-être en utilisant la méthode de péché pour calculer couper la ligne dans le boîtier trop de lignes bord flou, mais je ne comprends pas pourquoi il n'est pas encore assez lisse même si je dessine chaque ligne à 0,5 dp. Je peux l'accepter en basse performance, mais pas en dents de scie. – pf0214