2012-02-08 26 views
0

我已經存儲我的NSPoint像這樣的數組。我想給該數組輸入到另一個類來繪製相同的操作。如何繪製特定的數組值。繪製線使用NSpoint像素與陣列

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self]; 

    CGPathAddLineToPoint(self.currentLine.linePath, NULL, currentPoint.x, currentPoint.y); 
    [self setNeedsDisplay]; 

    [_array addObject:[NSValue valueWithCGPoint: currentPoint]]; 

} 

這是我的數組值 控制檯輸出

"NSPoint: {745, 575}", 
"NSPoint: {730, 584}", 
"NSPoint: {717, 588}", 
"NSPoint: {701, 590}", 
"NSPoint: {678, 590}", 
"NSPoint: {642, 590}", 
"NSPoint: {590, 590}", 
"NSPoint: {520, 590}", 
"NSPoint: {465, 589}", 
"NSPoint: {438, 587}", 
"NSPoint: {415, 587}", 
"NSPoint: {403, 582}" 


     - (void)drawRect:(CGRect)rect 
     { 
// CGContextSetAlpha(context, self.currentLine.opacity);  
//  CGContextSetStrokeColorWithColor(context, self.currentLine.lineColor.CGColor); 
//  CGContextSetLineWidth(context, self.currentLine.lineWidth); 
//  CGContextSetLineCap(context, kCGLineCapRound); 
//  CGContextSetLineJoin(context, kCGLineJoinRound); 
//  CGContextBeginPath(context); 
//  CGContextAddPath(context, self.currentLine.linePath); 
//  CGContextStrokePath(context); 
??????????? 
     } 

這個代碼將使用觸摸輸入線drawing.how來繪製出用戶交互 線時的視圖將衝擊片雷管。這子類的UIView。我可以繪製使用mouse.my需要是我有CGPoint值數組我想要傳遞該數組到這個類輸入的繪圖line.how我可以通過這

+0

call this viewWllAppear – 2012-02-08 09:55:48

+0

@InderKumarRathore call this?哪一個我必須打電話? – 2012-02-08 10:04:15

+0

在'viewWillAppear'調用'drawRect' – 2012-02-08 10:14:17

回答

1

您需要獲得積分值出陣列並從中創建一條路徑,然後對路徑進行描邊。

- (void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    // Set up your conext here, line colour, thickness, endcaps etc 
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor); 
    CGContextSetLineWidth(ctx, 2.0); 
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); 

    //Create a path 
    CGMutablePathRef pathRef = CGPathCreateMutable(); 

    // assume your array is called pointsArray and is a property 
    // Move to the starting point 
    CGPoint pt = [[self.pointsArray objectAtIndex:0] CGPointValue]; 
    CGPathMoveToPoint(pathRef, NULL, pt.x, pt.y); 

    // Add the other points to the path 
    CFIndex arrayCount = [self.pointsArray count]; 

    for (CFIndex i = 1; i < arrayCount; i++) { 
     pt = [[self.pointsArray objectAtIndex:i] CGPointValue]; 
     CGPathAddLineToPoint(pathRef, NULL, pt.x, pt.y); 
    } 

    // Now you have a path, stroke it 
    CGContextAddPath(ctx, pathRef); 
    CGContextStrokePath(ctx); 

    // Release your pathRef 
    CGPathRelease(pathRef); 
} 

您可以下載一個小Example Xcode project,顯示這個工作。

0

在你想要通過當前類陣列

{ 
    NSArray *mArray; 
} 
@property (nonatomic, retain) NSArray *mArray; 

上午

@synthesize mArray; 

然後寫這篇文章,您已經創建A的對象

A *a = [[A alloc] init]]; 
//this code to set the array in your A class 
a.mArray = yourArrayOfPoints; 
+0

我有所有的步驟。我想輸入drawRect像這樣的方法CGPathAddLineToPoint(<#CGMutablePathRef path#>,<#constst CGAffineTransform * m#>,<#CGFloat x#>,<#CGFloat y#>); – 2012-02-08 11:46:03

+0

雖然這對繪圖沒有幫助,但是呢? – Abizern 2012-02-08 12:00:16

0
CGContextMoveToPoint(context, 0.0, 0.0); 
    for (NSValue* value in array) 
    { 
     CGPoint p =[value CGPointValue]; 
     CGContextAddLineToPoint(context,p.x,p.y); 

    } 

把它放在 - (空)drawInContext:(CGContextRef)上下文方法

嘗試這可能對你有所幫助......

+0

我得到這樣的警告UIView可能不會響應'drawContext'..... – 2012-02-09 04:02:54

+0

嘗試drawInContext:not drawContext:或者您可以使用 - (void)drawRect:(CGRect)rect – Hector 2012-02-09 04:55:00