【问题标题】:Zoom in and out on photos inside uiscrollview在 uiscrollview 中放大和缩小照片
【发布时间】:2018-04-09 03:30:09
【问题描述】:

我想在 uiscrollview 中使用带缩放功能的 imageview

【问题讨论】:

  • 把你尝试过的代码放在你的问题中并简单解释一下

标签: ios objective-c uiscrollview uiimageview pinchzoom


【解决方案1】:

我创建了一个简单的 Github 项目,这可能就是您所追求的。在这里查看: https://github.com/twomedia/TMImageZoom

我通过创建一个处理手势状态变化的类来实现这一点。然后我在窗口上创建一个 UIImageView 并将其定位/调整为原始图像视图,从而创建用户缩放 imageView 的效果。这是上面链接中的一个小sn-p:

-(void) gestureStateChanged:(id)gesture withZoomImageView:(UIImageView*)imageView {

// Insure user is passing correct UIPinchGestureRecognizer class.
if (![gesture isKindOfClass:[UIPinchGestureRecognizer class]]) {
    NSLog(@"(TMImageZoom): Must be using a UIPinchGestureRecognizer, currently you're using a: %@",[gesture class]);
    return;
}

UIPinchGestureRecognizer *theGesture = gesture;

// Prevent animation issues if currently animating reset.
if (isAnimatingReset) {
    return;
}

// Reset zoom if state = UIGestureRecognizerStateEnded
if (theGesture.state == UIGestureRecognizerStateEnded || theGesture.state == UIGestureRecognizerStateCancelled || theGesture.state == UIGestureRecognizerStateFailed) {
    [self resetImageZoom];
}

// Ignore other views trying to start zoom if already zooming with another view
if (isHandlingGesture && hostImageView != imageView) {
    NSLog(@"(TMImageZoom): 'gestureStateChanged:' ignored since this imageView isnt being tracked");
    return;
}

// Start handling gestures if state = UIGestureRecognizerStateBegan and not already handling gestures.
if (!isHandlingGesture && theGesture.state == UIGestureRecognizerStateBegan) {
    isHandlingGesture = YES;

    // Set Host ImageView
    hostImageView = imageView;
    imageView.hidden = YES;

    // Convert local point to window coordinates
    CGPoint point = [imageView convertPoint:imageView.frame.origin toView:nil];
    startingRect = CGRectMake(point.x, point.y, imageView.frame.size.width, imageView.frame.size.height);

    // Post Notification
    [[NSNotificationCenter defaultCenter] postNotificationName:TMImageZoom_Started_Zoom_Notification object:nil];

    // Get current window and set starting vars
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    firstCenterPoint = [theGesture locationInView:currentWindow];

    // Init zoom ImageView
    currentImageView = [[UIImageView alloc] initWithImage:imageView.image];
    currentImageView.contentMode = UIViewContentModeScaleAspectFill;
    [currentImageView setFrame:startingRect];
    [currentWindow addSubview:currentImageView];
}

// Reset if user removes a finger (Since center calculation would cause image to jump to finger as center. Maybe this could be improved later)
if (theGesture.numberOfTouches < 2) {
    [self resetImageZoom];
    return;
}

// Update scale & center
if (theGesture.state == UIGestureRecognizerStateChanged) {
    NSLog(@"gesture.scale = %f", theGesture.scale);

    // Calculate new image scale.
    CGFloat currentScale = currentImageView.frame.size.width / startingRect.size.width;
    CGFloat newScale = currentScale * theGesture.scale;
    [currentImageView setFrame:CGRectMake(currentImageView.frame.origin.x, currentImageView.frame.origin.y, startingRect.size.width*newScale, startingRect.size.height*newScale)];

    // Calculate new center
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    int centerXDif = firstCenterPoint.x-[theGesture locationInView:currentWindow].x;
    int centerYDif = firstCenterPoint.y-[theGesture locationInView:currentWindow].y;
    currentImageView.center = CGPointMake((startingRect.origin.x+(startingRect.size.width/2))-centerXDif, (startingRect.origin.y+(startingRect.size.height/2))-centerYDif);

    // Reset gesture scale
    theGesture.scale = 1;
}
}

【讨论】:

  • 如果我想在用户抬起手指后停止图像缩小怎么办。截至目前,如果用户抬起手指,图像就会缩小
  • 如果我将此手势添加到具有多个图像视图的滚动视图,那么它会缩放最后一个图像。即使我将此手势添加到滚动视图内的所有 UIImageviews,这也会缩放滚动视图中的最后一个图像.
【解决方案2】:

如果您在此之前没有这样做过。

https://github.com/NYTimes/NYTPhotoViewer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    相关资源
    最近更新 更多