簡體   English   中英

放大和縮小uiscrollview內部的照片

[英]Zoom in and out on photos inside uiscrollview

我想使用具有縮放功能的imageview在uiscrollview中包含多個圖像。我嘗試在scrollview中嘗試使用scrollview,但這無法解決我的問題,因為我想在照片之間滾動並縮放這些照片。

我創建了一個簡單的Github項目,這可能就是您想要的。 在這里查看: https : //github.com/twomedia/TMImageZoom

我通過創建一個處理手勢狀態更改的類來實現這一點。 然后,我在窗口上創建一個UIImageView並將其定位/調整為原始圖像視圖,從而創建用戶縮放imageView的效果。 這是上面鏈接中的一小段代碼:

-(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;
}
}

如果您尚未嘗試過,請嘗試此操作。

https://github.com/NYTimes/NYTPhotoViewer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM