2012-08-03 2 views
0

난 그냥 .. 나는 다음과 같은 코드를 작성했지만 그것은 선을 표시하지의 형태의 ViewController의 내 같은 관점에서 서로 다른 두 점 사이의 선을 그어야 할 .. .. 내가이 문제를 해결하는 데 도움이 바랍니다아이폰에서 같은보기에있는 두 개의 다른 점 사이에 선을 그리는 방법은 무엇입니까?

- (void)drawRect:(CGRect)rect 
{ 
[brushPattern setStroke]; 
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
// CGPoint *point=mytouch 
[myPath moveToPoint:[mytouch locationInView:self.view]]; 
CGPoint pos = [mytouch locationInView: self.view]; 
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y); 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
[myPath addLineToPoint:[mytouch locationInView:self.view]]; 
[self.view setNeedsDisplay]; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
CGPoint pos = [mytouch locationInView: self.view]; 
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y); 
} 
+0

안녕하세요 ... 저는이 문제를 인터넷 검색으로 해결했습니다. 고맙습니다 .. :) –

답변

0
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch* touch = [touches anyObject]; 

    currentPoint = [touch locationInView:self.image]; 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    if (!(last.x == 0 && last.y == 0)) { 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextSetLineWidth(context, 2.0); 
    CGContextMoveToPoint(context, currentPoint.x,currentPoint.y); 
    CGContextAddLineToPoint(context, last.x, last.y); 
    CGContextStrokePath(context); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt); 
    [image setNeedsDisplay]; 
    CGContextFlush(UIGraphicsGetCurrentContext()); 
    self.image.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    } 

    last = currentPoint; 
    NSLog(@"the current point is %f,%f",currentPoint.x,currentPoint.y); 
    NSLog(@"the current point is %f,%f",last.x,last.y); 
} 
+2

해결책을 설명해 주시겠습니까? –

+0

나는이 모든 메소드가 구현되어 있고 UIView 유형의 선을 그리고 나서 현재 뷰에서 UIView 클래스의 프레임을 LineDraw * ldVw와 같은 객체로 지정하여 선을 그립니다. ldVw = [[LineDraw alloc ] initWithFrame : CGRectMake (50, 50, 387, 600)]; –

관련 문제