2014-03-27 3 views
1

어이 얘들 아, UIImageView와 같은 객체를 만들 수 있는지 궁금 해서요. draggable. 나는 UITouch 알고 있는데, 사용자가 그 또는 그녀가 화면을 터치 위치로 화면을 가로 질러 객체 점프를 할 수 있도록 터치는어떻게 객체를 드래그 가능하게 만드시겠습니까?

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *drag=[[event allTouches] anyObject]; 
    player.center=[drag locationInView:self.view]; 
} 

을 즉 그러나이 방법으로 가능하지만 사용자가 갖고 싶어 객체를 화면에서 수동으로 드래그하십시오. 당신이 당신의 사용자가 & 드래그 앤 드롭 할 경우

코드를 설명하고 내가 더 구체적인 또는 명확하게하기 위해이 있으면 알려 주시기 바랍니다 ...

답변

1

... 비슷한 질문을 검색하려고 :

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

    UITouch *aTouch = [touches anyObject]; 
    CGPoint location = [aTouch locationInView:self.view]; 

    if(CGRectContainsPoint(self.playerView.frame,location)) { 
     [UIView beginAnimations:@"Dragging A DraggableView" context:nil]; 
     self.playerView.center = location; 
     [UIView commitAnimations]; 
    } 

} 

이 당신에게 부드러운 애니메이션을 제공해야합니다.

+0

Thnx 이것은 트릭을 수행했습니다 ... – vink007

+0

제발 해답을 표시해주세요 :-) 해피 코딩. – CW0007007

1

, 먼저 터치가 이미지 뷰 내부에 있는지 확인해야 직사각형 프레임. 사용자 환경을 개선하기 위해 이미지 뷰 센터에서 터치 오프셋을 감지 할 수 있습니다. 이렇게하기에 좋은 곳은 the touchesBegan:withEvent:입니다.

CGPoint touchOffset; //insert this inside your interface private iVars 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *drag=[[event allTouches] anyObject]; 
    CGPoint touchLocation = [drag locationInView:self.view]; 
    touchOffset = CGPointMake(player.center.x-touchLocation.x,player.center.y-touchLocation.y) 
    if(CGRectContainsPoint(player.frame,touchLocation) 
     player.center = CGPointMake(touchLocation.x+touchOffset.x,touchLocation.y+touchOffset.y); 
} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *drag=[[event allTouches] anyObject]; 
    CGPoint touchLocation = [drag locationInView:self.view]; 
    if(CGRectContainsPoint(player.frame,touchLocation) 
     player.center = CGPointMake(touchLocation.x+touchOffset.x,touchLocation.y+touchOffset.y); 
} 

추신 : 그 후,이처럼 이미지 뷰의 위치를 ​​변경해야 - 다음 시간 당신은이 작업을 수행 할 수 있습니다

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

    UITouch* aTouch = [touches anyObject]; 
    UIView* aView = [aTouch view]; 
    if (aView != self.view) { 
     [self.view bringSubviewToFront:aView]; 
    } 
} 

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

    UITouch* aTouch = [touches anyObject]; 
    UIView* aView = [aTouch view]; 
    if (aView != self.view) { 
     aView.center = [aTouch locationInView:self.view]; 

    } 
} 
관련 문제