在UIScrollView中缩放照片



我有一个UIScrollView,它唯一的错误是显示来自相机或从照片库中选择的单个照片标记。

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

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

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

其中我可以舒适地缩放垂直和水平照片

首先,这是我的自定义PhotoView(主要控制UIScrollView和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;
}

我的问题是,我的代码最初可以很好地处理垂直照片——无论照片放大到多大或多小,也就是

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

它总是反弹回myScrollView的边缘。

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

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

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

然后在缩放和滚动时,它的"反弹边"也应该与myScrollView的边相同,就像垂直边一样。

恐怕我糟糕的英语不能很清楚地表达自己,如果你不理解我的意思,只需用IOS设备拍一张垂直和水平的照片,然后在照片应用程序中尝试缩放和拖动它们,看看它们最初是如何显示的,以及它们是如何反弹的。

所以现在,我不知道如何获得我想要的效果,事实上,我对UIScrollView不太清楚——尽管我已经读了好几次了。

我试图更改我的ScrollView的contentSize以调整垂直和水平照片,但我还没有成功,

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

但现在这个照片视图同时存在垂直和水平照片的问题,比如照片完全从myScrollView中滚动出来,变得不可见,但根本无法反弹,

或者上方的"反弹边缘"在myScrollView的真实上边缘下方一定距离,照片总是在那里反弹,这总是在上方留下空白。

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

contentSize应该始终等于我正在显示的照片,还是固定为myScrollView,

当照片放大或缩小时,contentSize是否也发生了变化?

我想我应该小心处理contentOffset和contentInset,但如何处理呢?

希望有人能帮我!

非常感谢!

我已经解决了这个问题,只需要处理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);
    }
}

最新更新