2010-02-10 6 views
1

내가 특정 프레임, 여기서는 버튼 영역을 입력하면 물건을 할 때 - (void) touchesMoved을 사용하고 있습니다.- (void) touchesMoved 중 한 번만 메서드를 호출하는 방법?

내 문제는 프레임에 들어가면 손가락으로 움직일 때 이 아니라이 아닌 것만을 처리하기를 원합니다.

프레임 안에있는 동안 내 메서드를 한 번만 호출 할 수있는 방법을 알고 있고 같은 touchMove에 다시 입력하면 다시 한 번 호출 할 수 있습니다.

감사합니다.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event touchesForView:self.view] anyObject]; 

    CGPoint location = [touch locationInView:touch.view]; 

    if(CGRectContainsPoint(p1.frame, location)) 
    { 
      //I only want the below to me called 
      // once while I am inside this frame 
     [self pP01]; 
     [p1 setHighlighted:YES]; 
    }else { 
     [p1 setHighlighted:NO]; 
    } 
} 

답변

1

일부 속성을 사용하여 특정 영역을 입력 할 때 코드가 이미 호출되었는지 확인할 수 있습니다. 그것은 (가 확실하지 무엇을) p1 객체의 강조 표시 상태처럼 보이는 적절한 수 있습니다 :

if(CGRectContainsPoint(p1.frame, location)) 
{ 
    if (!p1.isHighlighted){ // We entered the area but have not run highlighting code yet 
     //I only want the below to me called 
     // once while I am inside this frame 
     [self pP01]; 
     [p1 setHighlighted:YES]; 
    } 
}else { // We left the area - so we'll call highlighting code when we enter next time 
    [p1 setHighlighted:NO]; 
} 
1

Simply add a BOOL 당신이있는 touchesMoved 확인에서 다시 그 touchesEnded

+0

필요한 영역을 종료 할 때도이 bool 플래그를 재설정해야합니다. "동일한 touchMove에 다시 입력하면 다시 한 번 호출 할 수 있습니다."를 참조하십시오. 질문에 대한 요구 – Vladimir

+0

@Vladimir - 저에게 감사합니다. – willcodejavaforfood

1
if(CGRectContainsPoint([p1 frame],[touch locationInView:self.view])) { 
    NSLog (@"Touch Moved over p1"); 
    if (!p14.isHighlighted) { 
    [self action: p1]; 
    p1.highlighted = YES; 
    } 
    }else { 
    p1.highlighted = NO; 
    } 
0

있는 UIButton를 사용하여 시도 Interface Builder에서 'touch drag enter'연결을 사용하십시오.

관련 문제