且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS-在UIScrollView上缩放UIImageView

更新时间:2022-11-02 10:03:26

示例代码:

- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer
{
    // two-finger tap zooms out
    float newScale = [scrlView zoomScale] / ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    [scrlView zoomToRect:zoomRect animated:YES];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    // the zoom rect is in the content view's coordinates.
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [scrlView frame].size.height / scale;
    zoomRect.size.width  = [scrlView frame].size.width  / scale;
    // choose an origin so as to get the right center.
    zoomRect.origin.x    = scrlView.center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = scrlView.center.y - (zoomRect.size.height / 2.0);
    return zoomRect;
}