0

我有一个用 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中各种好的问题和答案的帮助下,我实现了像这样的缩放和平移:

- (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 & Pan 都不受 UIImageView 的限制,当缩小或平移到边界之外时,图像超出了下一个视图,一切都出错了。我仍在弄清楚这一点,但不知道我应该从哪里开始。

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

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

4

1 回答 1

2

如果您从此处使用 MWPhotoBrowser ,除了您期望的功能外,它还将负责下载远程图像。

于 2012-12-04T10:39:03.257 回答