2011-07-17 3 views
1

는 내가보기에있는 UIButton를 만들고, 내가 아닌 전경에게있는 touchesMoved 만있는 UIButton을 제어 할 수 할touchesMoved에 한 가지보기 만 제어하게하는 방법은 무엇입니까?

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

나는 내가있는 UIButton하고있는 UIButton에 닿으면이 처럼하고 싶은 내 손가락으로 움직일 수 있습니다. 다른보기를 터치하고 손가락이 화면에서 움직이는 경우 UIButton은 아무 것도하지 않습니다. 즉, touchesMoved 함수는 UIButton의 역할 만 가지고 있다는 것을 의미하므로 어떻게 할 수 있습니까? 덕분에

답변

5

나는 사용자 정의보기 컨트롤러 하위 클래스에서 발생하는 코드를 가정하고 UIButton은 해당보기의 하위보기입니다.

클래스에 BOOL을 간단히 정의하여 NO으로 먼저 설정하십시오. 그런 다음 이벤트 처리 메소드에서 업데이트하십시오.

// .h 
BOOL buttonTouched; 

// .m 
// in the viewDidLoad 
buttonTouched = NO; 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    // test wether the button is touched 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchBegan = [touch locationInView:self.view]; 
    if(CGRectContainsPoint(theButton.frame, touchBegan) { 
     buttonTouched = YES; 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(buttonTouched) { 
     // do it here 
     UITouch *touch = [touches anyObject]; 
     CGPoint touchMoved = [touch locationInView:self.view]; 
     CGRect newFrame = CGRectMake(touchMoved.x, 
            touchMoved.y, 
            theButton.frame.width, 
            theButton.frame.height); 
     theButton.frame = newFrame; 
    } 
} 

// when the event ends, put the BOOL back to NO 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    buttonTouched = NO; 
} 
+0

@megamind : 얻었습니까? –

+0

감사합니다.하지만 여전히 작동하지 않습니다. 버튼을 지금 이동할 수 없습니다. 감사합니다. – matt

+0

if (CGRectContainsPoint (theButton.frame, touchBegan)) { buttonTouched = YES; } 여기 코드가 작동하지 않습니다 – matt

관련 문제