2012-07-20 32 views
0

我有一個UIView我在哪裏添加一個ID球。當我用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。

任何人都可以幫助我嗎? 謝謝!

回答

0

類的UIView(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:)的蘋果文檔列出了兩種方法,這將確定哪個視圖包含命中/點:

– hitTest:withEvent: 
– pointInside:withEvent: 

可能則hitTest將幫助最:它的描述寫着「返回的最遠的後裔接收者在包含指定點的視圖層次結構中(包括它本身)「,所以它甚至根據需要轉換座標。如果你檢查[self hitTest:[touch.locationInView self] withEvent:event](或類似的,這裏沒有代碼完成; - )=在你的touchesMoved:方法中,你應該能夠檢測手指何時結束任何其他的圈子。

希望這會有所幫助,nobi

+0

太好了!你幫助我很多!作品!現在我必須做其他功能來創建更多的線條。謝謝!! – murb83 2012-07-20 11:40:57

+0

哦,我使用hitTest: [self hitTest:touchLocation withEvent:event] ^^ – murb83 2012-07-20 11:43:15