2012-07-20 2 views
0

ID가있는 볼을 추가하는 위치에 UIView가 있습니다. ID 1로 볼을 만지면 점선이 그립니다.개체 감지, touchesmoved 및 UITouch

다른 볼에 손가락을 대고 움직이면이 볼을 감지하고 ID를 확인하는 방법을 모르겠습니다. 내가해야 할 일은 ID 2로 다음 공을 터치하고, 공을 끝내고, 공 1에서 공 2까지 머물러 서 2에서 손가락까지 새로운 것을 만듭니다. 3 등 ...

코드 :

#import "DrawView.h" 

@implementation DrawView 

- (id)init 
{ 
    self = [super initWithFrame:CGRectMake(0, 0, 1024, 768)]; 
    if (self) { 
     self.backgroundColor = [UIColor clearColor]; 
     self.opaque = YES; 

     checkPointCircle = [[CheckPointCircle alloc] initWithPosX:315 posY:138 andNumber:1]; 
     checkPointCircle.userInteractionEnabled = YES; 
     [self addSubview:checkPointCircle]; 

     checkPointCircle = [[CheckPointCircle alloc] initWithPosX:706 posY:138 andNumber:2]; 
     checkPointCircle.userInteractionEnabled = YES; 
     [self addSubview:checkPointCircle]; 

     checkPointCircle = [[CheckPointCircle alloc] initWithPosX:315 posY:526 andNumber:3]; 
     checkPointCircle.userInteractionEnabled = YES; 
     [self addSubview:checkPointCircle]; 

     checkPointCircle = [[CheckPointCircle alloc] initWithPosX:706 posY:526 andNumber:4]; 
     checkPointCircle.userInteractionEnabled = YES; 
     [self addSubview:checkPointCircle]; 

    } 
    return self; 
} 

- (Line *)drawLine { 
    return drawLine; 
} 

- (void)setDrawLine:(Line *)line 
{ 
    drawLine = line; 
} 

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

    CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view; 

    if (touch.view.class == checkPointCircle.class) { 
     if ([checkTouch getObjectID] == 1) { 
      startTouchPoint = CGPointMake([checkTouch center].x, [checkTouch center].y); 
      endTouchPoint = [touch locationInView:self]; 
     } 
    } 

    self.drawLine = [[Line alloc] initWithPoint:[touch locationInView:self]]; 
    [self setNeedsDisplay]; 
} 

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"Cancelado"); 
} 

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

    UITouch *touch = [touches anyObject]; 

    UITouch *secondaryTouch = (UITouch *)[[[event touchesForView:checkPointCircle] allObjects] objectAtIndex: 0]; 

    NSLog(@"Que toco: %@ ", secondaryTouch.view); 

    if (touch.view.class == checkPointCircle.class) { 
     CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view; 
     if ([checkTouch getObjectID] == 1) { 
      endTouchPoint = [touch locationInView:self]; 
     } 
    } 
    [self setNeedsDisplay]; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view; 

    if (touch.view.class == checkPointCircle.class) { 
     if ([checkTouch getObjectID] == 1) { 
      endTouchPoint = [touch locationInView:self]; 
     } 
    } 
    [self setNeedsDisplay]; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    [drawLine drawLineFrom:startTouchPoint to:endTouchPoint]; 
} 

@end 

ID를 얻기 위해 다른 공을 감지하는 방법.

아무도 도와 줄 수 있습니까? 감사합니다.

– hitTest:withEvent: 
– pointInside:withEvent: 

아마 그러나 hitTest 가장 도움이 될 것입니다 : 그것의 설명을 읽고 "의 가장 먼 후손을 돌려

답변

0

클래스의 UIView (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:)의 애플 문서보기 히트/지점을 포함하는 결정하는 두 가지 방법을 나열 수신자는 지정된 점을 포함하는 뷰 계층 구조 (자체 포함)에 있습니다. "따라서 필요에 따라 좌표를 변환합니다.

touchesMoved : 메소드에서 [self hitTest : [touch.locationInView self] withEvent : event] (또는 이와 유사하며 코드 완성 없음 -) =을 선택하면 손가락이 끝났을 때를 감지 할 수 있어야합니다 다른 서클 중 하나.

희망이 있습니다. nobody

+0

와우! 너 내가 많이 도와 줘! 공장! 이제는 더 많은 라인을 만들기 위해 다른 기능을 수행해야합니다. 고맙습니다!! – murb83

+0

아, 사용하고 있습니다. hitTest : [self hitTest : touchLocation withEvent : event] ^^ – murb83

관련 문제