在UIScrollView中缩放照片

Wan*_*ang 0 photo zoom uiscrollview uiimageview bounce

我有一个UIScrollView,它唯一的缺点是从相机显示单个照片令牌或从照片库中选择.

但是有一个问题 - 照片的方向,

我的意思是,"垂直"照片(高度>宽度的照片)与"水平"照片(宽度>高度).

而我想要的就像在系统照片应用程序中显示一张照片,

我可以舒适地缩放垂直和地平线照片

首先,这是我的自定义PhotoView的代码(主要控制UIScrolView和UIImageView)

// .h
@interface PhotoView : UIView
<UIScrollViewDelegate>{
    UIScrollView *myScrollView;
    UIImageView *photoView;
}
...
@end
// .m
- (void)refreshPhoto:(UIImage *)aPhoto {
    if (aPhoto) {
        CGSize photoSize = aPhoto.size;
        CGSize scrollSize = self.myScrollView.frame.size;
        if (photoSize.height > photoSize.width) { // vertical photo
            self.photoView.frame = CGRectMake(0, 0, scrollSize.width, scrollSize.height);
        }
        else { // horizontal photo, initially it should be zoomed out to fit the width of myScrollView
            CGFloat factor = photoSize.width / scrollSize.width;
            self.photoView.frame = CGRectMake(0, 0, scrollSize.width, photoSize.height / factor);
            self.photoView.center = CGPointMake(scrollSize.width / 2, scrollSize.height / 2);
        }
    }
    else  // no photo
        self.photoView.frame = self.myScrollView.frame;
    self.photoView.image = aPhoto;
    //self.myScrollView.zoomScale = 1.0;
    //self.myScrollView.contentSize = self.photoView.frame.size;
}
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"photoViewBg.png"]];
        self.myScrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(30, 35, 260, 360)] autorelease];
        self.myScrollView.delegate = self;
        self.myScrollView.maximumZoomScale = 2.5;
        self.myScrollView.minimumZoomScale = 1.0;
        self.myScrollView.backgroundColor = [UIColor clearColor];
        self.photoView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
        self.myScrollView.contentSize = self.imageView.frame.size;
        [self.myScrollView addSubview:self.photoView];
        [self addSubview:self.imageView];
    }
    return self;
}
#pragma mark - Scroll View Delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.photoView;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,我的代码最初在垂直照片上工作得很好 - 无论照片被放大到多大或多小,也就是说,

当我缩放然后将照片拖动到设备屏幕中的myScrollView框架((30,35,260,360))时,

它总是反弹回myScrollView的边缘.

但是当我开始处理水平照片时,事情变得混乱.

从我的代码你可以看到我最初想要的水平照片是,因为水平的宽度是>他们的高度,但myScrollView的宽度是<高度,

所以最初,水平的应该在myScrollView的中间,宽度相同但高度较小,在上侧和下侧留下两个相等的空白,

然后被缩小和滚动时,它的"反弹力"也应该是一样myScrollView的边缘,就像垂直的.

我担心我可怜的英语不能很清楚地表达自己,如果你没有得到我,只需用你的IOS设备拍摄垂直和水平照片,然后尝试在Photo应用程序中缩放和拖动它们,看看它们是怎样的最初显示以及它们如何反弹.

所以现在,我不知道如何获得我想要的效果,事实上我对UIScrollView并不是那么清楚 - 虽然我已经阅读过几次文件了.

我试图改变myScrollView的contentSize以调整垂直和水平照片,但我还没有,

现在我总是将contentSize设置为与myScrollView的大小相同,因为我认为myScrollView的边缘始终是任何缩放照片的正确"反弹边缘".

但是现在这个photoView对于垂直和水平照片都存在问题,例如照片完全滚出myScrollView并变得不可见但根本没有反弹,

或者上面的"反弹边缘"在myScrollView的真实上边缘下方有一段距离,照片总是在那里反弹,总是在上方留空.

我不确定我是否正确理解UIScrollView的工作原理.

如果contentSize总是等于我正在显示或固定到myScrollView的照片,

当照片放大或缩小时,contentSize是否也改变了或从未改变过?

我想我应该仔细照顾contenOffset和contentInset,但是怎么样?

希望有人能给我一个帮助!

非常感谢!

Wan*_*ang 7

我已经解决了这个问题,只需要处理contentSize及其位置(contentInset和contentOffset)

我设法使用以下代码解决了这个问题:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    CGSize nowSize = view.frame.size;
    if (nowSize.height >= self.myScrollView.frame.size.height)
        self.myScrollView.contentInset = UIEdgeInsetsZero;
    else {
        CGFloat delta = self.myScrollView.frame.size.height/2 - view.frame.size.height/2;
        self.myScrollView.contentInset = UIEdgeInsetsMake(delta, 0, delta, 0);
    }
}
Run Code Online (Sandbox Code Playgroud)