2016-09-25 7 views
1

CGPointsを含むNSArrayがあり、このクラスから返すパスを描画します。問題は[bezierPath closePath]がこのクラスのパスを閉じないということです。何故ですか?私はこのクラスが私に与えるカーブを使用して配列の最初のポイントにエンドポイントを接続し、このクラスを使用してパスを完全に閉じた/接続して維持する必要があります。 [bezierPath closePath]とは別に何をすべきですか?これは私のdrawrectメソッドでこれを使うと何もしないからです。どんな助けもありがとうございます。なぜ `[bezierPath closePath]`メソッドがパスを閉じないのですか?

UIBezierPath(SmoothPath)クラスのコード:

UIBezierPath+SmoothPath.h: 
#import <UIKit/UIKit.h> 
@interface UIBezierPath (SmoothPath) 
+ (UIBezierPath*)smoothPathFromArray:(NSArray*)arr; 
@end 

そして

UIBezierPath+SmoothPath.m: 

    #import "UIBezierPath+SmoothPath.h" 

    @implementation UIBezierPath (SmoothPath) 

    + (UIBezierPath*)smoothPathFromArray:(NSArray*)arr{ 
     if ([arr count] > 0){ 
      UIBezierPath *bezierPath = [UIBezierPath bezierPath]; 

      NSMutableArray *pts = [arr mutableCopy]; 
      int i = 0; 
      for (; i < pts.count - 4 ; i+= 3){ 
       CGPoint temp = CGPointMake(([pts[i+2] CGPointValue].x + [pts[i+4] CGPointValue].x)/2.0, 
              ([pts[i+2] CGPointValue].y + [pts[i+4] CGPointValue].y)/2.0); 
       pts[i+3] = [NSValue valueWithCGPoint:temp]; 

       [bezierPath moveToPoint:[pts[i] CGPointValue]]; 
       [bezierPath addCurveToPoint:temp controlPoint1:[pts[i+1] CGPointValue] controlPoint2:[pts[i+2] CGPointValue]]; 
      } 

      switch (pts.count - i) { 
       case 4: 
        [bezierPath moveToPoint:[pts[i] CGPointValue]]; 
        [bezierPath addCurveToPoint:[pts[i+3] CGPointValue] controlPoint1:[pts[i+1] CGPointValue] controlPoint2:[pts[i+2] CGPointValue]]; 
        break; 
       case 3: 
        [bezierPath moveToPoint:[pts[i] CGPointValue]]; 
        [bezierPath addCurveToPoint:[pts[i+2] CGPointValue] controlPoint1:[pts[i] CGPointValue] controlPoint2:[pts[i+1] CGPointValue]]; 
        break; 
       case 2: 
        [bezierPath moveToPoint:[pts[i] CGPointValue]]; 
        [bezierPath addLineToPoint:[pts[i+1] CGPointValue]]; 
        break; 
       case 1: 
        [bezierPath addLineToPoint:[pts[i] CGPointValue]]; 
        break; 

       default: 
      } 
     [bezierpath closePath]; 
     return bezierPath; 
    } 
    return nil; 
} 
@end 

答えて

2

あなたはパス(moveToPoint)を動かし続けます。これは不連続なパスを作るので、それを閉じるだけで最後のセクションの大きなイニングに戻ります。カーブまたはラインをパスに追加すると、パスの現在のポイントがそのカーブまたはラインの最後に移動します。パスの始点を設定するときは、最初にmoveToPointを使用してください。

+0

ありがとう、私はこの[bezierPath moveToPoint:[pts [0] CGPointValue]] 'を実行し、この行をforループから取り出し、' [path closePath] 'を使用してパスを閉じました終点と始点(配列の最後のインデックスと最初のインデックス)の間の部分は滑らかではありませんが、終点と始点のジョイントでも滑らかに見えるようにするにはどうすればいいですか? ? –

+0

他の点でやっているように曲線を計算する必要があります。パスを閉じると常に直線になります。カーブを最初まで計算し、それを追加してからパスを閉じます。 – jrturton

関連する問題