2012-02-24 10 views
0

이미지 뷰를 드래그하려고합니다. 그렇게하는 데 약간의 성공을 거두었지만, 내가 원하는만큼 행동하지는 않습니다. 나는 그것이 이미지 내부를 만져서 끌면 움직여야한다. 화면의 아무 곳이나 눌러서 드래그해도 움직입니다. 이 같은 코드를 기록한이미지 뷰 드래그

는 :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     //retrieve touch point 
     CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]]; 
     startLocation = pt; 

    } 

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

    CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]]; 

    CGRect frame = [[self.view.subviews objectAtIndex:0]frame]; 
    frame.origin.x += pt.x - startLocation.x; 
    frame.origin.y += pt.y - startLocation.y; 
    [[self.view.subviews objectAtIndex:0] setFrame: frame]; 

}

+0

왜'subviews'에 액세스? 이미지 뷰의 인스턴스를 선언 할 수 있습니다. 그리고 CGRectContainsPoint를 사용하여 이미지 뷰를 터치했는지 확인합니다. – Ilanchezhian

+0

터치 할 때 터치 포인트가 이미지 뷰에 있는지 확인할 수 있습니다. – Bonny

+0

이미지 뷰의 프레임이 전체 화면을 덮도록 설정되어 있습니까? –

답변

2

locationInView 메소드의 리턴 값도 프레임에 대하여 점이다. 먼저보기 프레임에 있는지 확인하십시오. 이 같은

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGRect targetFrame = [self.view.subviews objectAtIndex:0].frame; 
    //retrieve touch point 
    CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]]; 
    //check if the point in the view frame  
    if (pt.x < 0 || pt.x > targetFrame.size.width || pt.y < 0 || pt.y > targetFrame.size.height) 
    { 
     isInTargetFrame = NO; 
    } 
    else 
    { 
     isInTargetFrame = YES; 
     startLocation = pt; 
    } 
} 

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
     if(!isInTargetFrame) 
     { 
      return; 
     } 
     //move your view here... 
} 
+0

자사의 working.some 수정 : 프레임은 속성이 아니므로 메시지로 보내주십시오. 조건에 따라 프레임을 targetFrame으로 교체하십시오. 감사합니다. – condinya

0

시도 뭔가 :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //retrieve touch point 
    startLocation = [[ touches anyObject] locationInView:self.view]; 
// Now here check to make sure that start location is within the frame of 
// your subview [self.view.subviews objectAtIndex:0] 
// if it is you need to have a property like dragging = YES 
// Then in touches ended you set dragging = NO 

} 

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

CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]]; 

CGRect frame = [[self.view.subviews objectAtIndex:0]frame]; 
frame.origin.x += pt.x - startLocation.x; 
frame.origin.y += pt.y - startLocation.y; 
[[self.view.subviews objectAtIndex:0] setFrame: frame];