2014-12-11 3 views
0

내 코드 (ipad 앱)를 터치하여 이미지의 크기 조정을 처리하고 싶습니다. xcode 핸들 포인트 좌표와 약간 혼동합니다. 프레임의 원점 (frame.origin)이 왼쪽 위 (어딘가에서 읽었을 때)에 있고, 터치하는 위치가 화면을 터치하는 손가락의 좌표이고 양의 방향이 남아 있다고 생각하여 코드를 작성했습니다. (x) 및 위로 (y). 어딘가에서 내가 잘못하면 내 코드가 제대로 작동하지 않는 이유가 설명됩니다. 나는 사용자가 바닥이나 실제로, 나는 확실하지 않다 kResizeThumbSize (내 이미지의 상단을 만지지 경우에만 이미지가 위/아래 방향으로 크기를 조정할 것으로 예상xcode 크기 조정 및 위치 좌표

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGFloat kResizeThumbSize = 45.0f; 
    touchStart = [[touches anyObject] locationInView:self.view]; 
    CGFloat xO = _ImageView.frame.origin.x; 
    CGFloat yO = _ImageView.frame.origin.y; 
    CGFloat width = _ImageView.frame.size.width; 
    CGFloat height = _ImageView.frame.size.height; 
    isResizingUD = (xO - kResizeThumbSize < touchStart.x < xO + width + kResizeThumbSize && 
       (yO - kResizeThumbSize < touchStart.y < yO + kResizeThumbSize || 
       yO - height - kResizeThumbSize < touchStart.y < yO - height + kResizeThumbSize)); 
    isResizingLR = (yO + kResizeThumbSize < touchStart.x < xO + width - kResizeThumbSize && 
       (xO - kResizeThumbSize < touchStart.x < xO + kResizeThumbSize || 
       xO - width - kResizeThumbSize < touchStart.x < xO - width + kResizeThumbSize)); 
} 

//moving the image 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; 
    CGPoint previous = [[touches anyObject] previousLocationInView:self.view]; 

    CGFloat deltaWidth = touchPoint.x - previous.x; 
    CGFloat deltaHeight = touchPoint.y - previous.y; 

    // get the frame values so we can calculate changes below 
    CGFloat x = _ImageView.frame.origin.x; 
    CGFloat y = _ImageView.frame.origin.y; 
    CGFloat width = _ImageView.frame.size.width; 
    CGFloat height = _ImageView.frame.size.height; 

    if (isResizingLR) { 
     _ImageView.frame = CGRectMake(x, y, width+deltaWidth, height); 
    } else if (isResizingUD) { 
     _ImageView.frame = CGRectMake(x, y, width, height+deltaHeight); 
    } else { 
     // not dragging from a corner -- move the view 
     _ImageView.center = CGPointMake(self.view.center.x + touchPoint.x - touchStart.x, 
            self.view.center.y + touchPoint.y - touchStart.y); 
    } 

} 

: 다음은 내 코드입니다 사용자가 kResizeThumbSize 내에서 이미지의 왼쪽 또는 오른쪽 테두리를 터치하면 오른쪽/왼쪽 방향으로 표시됩니다. 안타깝게도 이미지의 크기를 오른쪽/왼쪽 방향으로 만 조정할 수 있으며 손가락을 대면 크기가 조정됩니다. 도움이 될 것입니다.

+0

UIScrollview 클래스는 자동으로 확대/축소를 처리합니다. 아마 이미 당신이 원하는대로 할 것입니다. 그렇지 않다면 항상 하위 클래스를 만들거나 대리자 메서드를 사용할 수 있습니다. –

+0

빠른 답변 주셔서 감사합니다. 내 질문에 대한 링크를 이해할 수 없습니다. 뷰를 확대/축소하려고하는 것이 아니라 사용자가 적절한 테두리를 만질 때 너비 또는 높이를 개별적으로 늘려 개체의 크기를 조정하려고합니다. 또한 카메라 뷰 (증강 현실)에 삽입 된 객체입니다. 나는 여기에 줌을 원하지 않는다. – jjchristophe

+0

오, 죄송합니다. 나는 당신이하고있는 것을 오해했습니다. UIKit (UIViews 등)의 좌표계는 왼쪽 위를 원점으로합니다. 오른쪽으로 이동하면 X가 증가합니다. 아래로 내려 가면 Y가 증가합니다. 나는 당신이 뒤집힌 것 같아요. 당신은 아마 UIKit 대 CG 좌표에 대해 읽었을 것입니다 : http://stackoverflow.com/questions/2695262/how-to-compensate-the-flipped-coordinate-system-of-core-graphics-for-easy-drawin?rq= 1 –

답변

0

고정!

이 문제에 대한 도움이 필요한 분은 내 코드를 게시합니다.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGFloat kResizeThumbSize = 10.0f; 
    touchStart = [[touches anyObject] locationInView:self.view]; 
    CGFloat xO = _ImageView.frame.origin.x; 
    CGFloat yO = _ImageView.frame.origin.y; 
    CGFloat width = _ImageView.frame.size.width; 
    CGFloat height = _ImageView.frame.size.height; 
    isResizingUD = (xO - kResizeThumbSize < touchStart.x && 
       touchStart.x < xO + width + kResizeThumbSize && 
       yO + height - kResizeThumbSize < touchStart.y); 
    isResizingLR = (yO + kResizeThumbSize < touchStart.y && 
       touchStart.y< yO + height - kResizeThumbSize && 
       xO + width - kResizeThumbSize < touchStart.x); 
    isMoving = (xO+kResizeThumbSize< touchStart.x && 
      touchStart.x < xO + width - kResizeThumbSize && 
      yO + kResizeThumbSize < touchStart.y && 
      touchStart.y< yO + height - kResizeThumbSize); 

} 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; 
    CGPoint previous = [[touches anyObject] previousLocationInView:self.view]; 

    CGFloat deltaWidth = touchPoint.x - previous.x; 
    CGFloat deltaHeight = touchPoint.y - previous.y; 

    // get the frame values so we can calculate changes below 
    CGFloat x = _ImageView.frame.origin.x; 
    CGFloat y = _ImageView.frame.origin.y; 
    CGFloat width = _ImageView.frame.size.width; 
    CGFloat height = _ImageView.frame.size.height; 

    if (isResizingUD) { 
     _ImageView.frame = CGRectMake(x, y, width, height+deltaHeight); 
    } else if (isResizingLR) { 
     _ImageView.frame = CGRectMake(x, y, width+deltaWidth, height); 
    } else if (isMoving) { 
     // not dragging from a corner -- move the view 
     _ImageView.frame = CGRectMake(x+deltaWidth, y+deltaHeight, width, height); 
    } 

} 
관련 문제