핀치 확대를 사용하여 바둑판 식으로 UIScrollView
을 올바르게 확대/축소하는 데 어려움이 있습니다. 문제는 핀치 줌이 발생하면 결과 뷰가 보통 같은 영역의 중앙에 위치하지 않는다는 것입니다.확대/축소하는 동안 UIScrollView의 중심점 찾기
세부 사항 : 앱은 500x500의 타일 이미지로 시작됩니다. 사용자가 확대하면 1000x1000으로 스냅되고 타일이 다시 그려집니다. 모든 확대/축소 효과에 대해, 나는 단지 UIScrollView
을시키는 것입니다. scrollViewDidEndZooming:withView:atScale:
이 호출되면 타일을 다시 그립니다 (많은 예제와 다른 질문에서 볼 수있는 것처럼).
내가 내려 내가 scrollViewDidEndZooming:withView:atScale:
에 도착하면 제대로 뷰의 중심을 계산하는 문제를 드릴 것 생각 (I 다시 그리기 후 나는 알려진 점 벌금에 중심을 수 있습니다.)
내가 현재 사용하고 무엇 :
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
// as an example, create the "target" content size
CGSize newZoomSize = CGSizeMake(1000, 1000);
// get the center point
CGPoint center = [scrollView contentOffset];
center.x += [scrollView frame].width/2;
center.y += [scrollView frame].height/2;
// since pinch zoom changes the contentSize of the scroll view, translate this point to
// the "target" size (from the current size)
center = [self translatePoint:center currentSize:[scrollView contentSize] newSize:newZoomSize];
// redraw...
}
/*
Translate the point from one size to another
*/
- (CGPoint)translatePoint:(CGPoint)origin currentSize:(CGSize)currentSize newSize:(CGSize)newSize {
// shortcut if they are equal
if(currentSize.width == newSize.width && currentSize.height == newSize.height){ return origin; }
// translate
origin.x = newSize.width * (origin.x/currentSize.width);
origin.y = newSize.height * (origin.y/currentSize.height);
return origin;
}
이 올바른 것입니까? 더 좋은 방법이 있습니까? 감사!
제스처 인식기를 사용할 수 없습니까? 일을 훨씬 쉽게 할 수 있지만 3.1 이전 버전에서는 사용할 수 없습니다. 따라서 3.1 또는 이전 버전을 타겟팅하지 않는 경우 UIGestureRecognizer를 사용하는 것이 좋습니다. 손가락 사이의 중심점을 얻을 수 있습니다. – jer
3.1 지원을 삭제할 수 있는지 확실하지 않지만 UIGestureRecognizer가 매우 멋져 보인다! 당분간, 3.1에서 이것을 발견 할 방법을 찾을 것이지만, UIGestureRecognizer가 갈 길이 될 것입니다. – Rob