使用UIScrollView和UIImageView实现简单的照片库,如iPhone的“照片”

时间:2012-12-04 10:36:47

标签: objective-c uiscrollview uiimageview uigesturerecognizer

我有一个用UIImageView填充的UIScrollView。我想让每个UIImageView能够变焦和放大。平移,然后轻弹到下一页,就像iPhone的照片应用程序一样。我到处搜索了,到目前为止我的是这个:

NSArray *pages = [self loadImages];

_pageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSUInteger pageCount = [pages count]; // Adjustable
for (int i = 0; i < pageCount; i++) {

    CGFloat x = i * self.view.frame.size.width;

    // Page Settings
    UIImageView *pageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];

    pageView.backgroundColor = [UIColor greenColor];
    pageView.image = [pages objectAtIndex:i];
    pageView.userInteractionEnabled = YES;
    pageView.tag = i;

    UIScrollView *pageScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    pageScroll.maximumZoomScale = 2.0f;
    pageScroll.minimumZoomScale = 1.0f;
    pageScroll.zoomScale = 1.0f;
    pageScroll.bouncesZoom = YES;
    pageScroll.contentSize = CGSizeMake(pageView.frame.size.width, pageView.frame.size.height);
    pageScroll.pagingEnabled = NO;
    pageScroll.delegate = self;
    [pageScroll addSubview:pageView];

    // Gesture Reocgnisers
    UIPinchGestureRecognizer *pinchGr = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchImage:)];
    pinchGr.delegate = self;
    [pageView addGestureRecognizer:pinchGr];

    UIPanGestureRecognizer *panGr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panImage:)];
    [pageView addGestureRecognizer:panGr];

    [_pageScrollView addSubview:pageView];

}
_pageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * pageCount, self.view.frame.size.height);

_pageScrollView.pagingEnabled = YES;

_pageScrollView.delegate = self;

[self.view addSubview:_pageScrollView];  

它们只适用于分页(水平滚动)。但是,我是如何为每个UIImageView实现UIGestures的。在各种好问题的帮助下,在stackoverflow中,我已经实现了Zoom&amp;像这样泛:

- (void) pinchImage:(UIPinchGestureRecognizer*)pgr {

  [self adjustAnchorPointForGestureRecognizer:pgr];

  if ([pgr state] == UIGestureRecognizerStateBegan || [pgr state] == UIGestureRecognizerStateChanged) {

    CGFloat currentScale = pgr.view.frame.size.width / pgr.view.bounds.size.width;
    CGFloat newScale = currentScale * pgr.scale;

    if (newScale < 1.01f) {
        newScale = 1.01f;
    }
    if (newScale > 3.0f) {
        newScale = 3.0f;
    }

    CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
    pgr.view.transform = transform;
    pgr.scale = 1;
  }
}

- (void) panImage:(UIPanGestureRecognizer*)panGr {

  if ([panGr state] == UIGestureRecognizerStateBegan || [panGr state] == UIGestureRecognizerStateChanged) {

    CGPoint p = [panGr translationInView:self.view];
    NSLog(@"[PAMPHLET PAGE]: Point: %@", NSStringFromCGPoint(p));
    CGPoint movedPoint = CGPointMake(panGr.view.center.x + p.x, panGr.view.center.y + p.y);

    if (panGr.view.frame.size.width > panGr.view.center.x + p.x &&
        panGr.view.frame.size.height > panGr.view.center.y + p.y) {
        panGr.view.center = movedPoint;
        NSLog(@"Current: %f / %f", panGr.view.center.x, panGr.view.center.y);
    }

    [panGr setTranslation:CGPointZero inView:self.view];
  }
}

这几乎可以使用,但有几个问题。 Zoom&amp; Pan不限于UIImageView的比例,当缩小或平移到边界外时,图像超出下一个视图,一切都出错了。我还在弄清楚这个,但不知道我应该从哪里开始。

我如何'限制'这些手势的有效区域,或者我应该尝试其他方法?这甚至是实现这一目标的正确方法吗?我只是想像照片应用程序那样制作简单的照片库......

请,任何帮助将不胜感激。
谢谢。

1 个答案:

答案 0 :(得分:2)

如果您使用here中的MWPhotoBrowser,除了您期望的功能外,它还会负责下载远程图像。