在缩放后在UIScrollView中启用分页

时间:2012-12-05 09:31:39

标签: ios uiscrollview xcode4.3 paging zooming

我有一个UIScrollView可以加载三个不同的页面。
当我放大页面并缩小到原始大小时,应用程序停止让我在页面之间滚动,就好像分页已禁用。当缩小到原始大小时,我该怎么做才能重新启用分页(Scale == 1)?
这是我的代码

- (void)viewDidLoad
{
    [ScView setMaximumZoomScale : 2.0f];
    [ScView setMinimumZoomScale : 1.0f];

    ScView.contentSize = CGSizeMake(1024*3, 1.0);
    ScView.pagingEnabled = YES;

    ScView.clipsToBounds = YES;
    ScView.delegate = self;

    ScView.showsHorizontalScrollIndicator = NO;
    ScView.showsVerticalScrollIndicator = NO;

    [super viewDidLoad]; 

    [self returnImages];
}

-(void)returnImages{
    for (pageNumber = 1; pageNumber <= 3; pageNumber++) {
        imagen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",pageNumber]]];
        imagen.frame = CGRectMake((pageNumber-1)*1024, 0, 1024, 768);

        [ScView addSubview:imagen];
    }
}

//
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
        return ScView;
   //   return [imagen initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",pageNumber]]];
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)myScrollView withView:(UIView *)view
{ 
    NSLog(@"Scroll Will Begin");
    ScView.scrollEnabled = YES;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)myScrollView withView:(UIView *)view atScale:(float)scale 
{ 
    if(scale == 1)
    {
        ScView.scrollEnabled = YES;
        ScView.pagingEnabled = YES;
        [self returnImages];
        NSLog(@"Scrolol will end");
        //ScView.maximumZoomScale = 2.0f;
       // [super viewDidLoad];

        [self returnImages];
    }
}

任何想法都将受到高度赞赏..

2 个答案:

答案 0 :(得分:19)

要获得正确的分页和缩放,您必须将每个页面的UIScrollView嵌入到父UIScrollView中。这种组合将允许您同时使用分页和内部滚动。

以下是具有父滚动视图和三个嵌入式可缩放页面的UIViewController示例。

#define VIEW_FOR_ZOOM_TAG (1)

@implementation SVViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    mainScrollView.pagingEnabled = YES;
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.showsVerticalScrollIndicator = NO;

    CGRect innerScrollFrame = mainScrollView.bounds;

    for (NSInteger i = 0; i < 3; i++) {
        UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"page%d", i + 1]]];
        imageForZooming.tag = VIEW_FOR_ZOOM_TAG;

        UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
        pageScrollView.minimumZoomScale = 1.0f;
        pageScrollView.maximumZoomScale = 2.0f;
        pageScrollView.zoomScale = 1.0f;
        pageScrollView.contentSize = imageForZooming.bounds.size;
        pageScrollView.delegate = self;
        pageScrollView.showsHorizontalScrollIndicator = NO;
        pageScrollView.showsVerticalScrollIndicator = NO;
        [pageScrollView addSubview:imageForZooming];

        [mainScrollView addSubview:pageScrollView];

        if (i < 2) {
            innerScrollFrame.origin.x += innerScrollFrame.size.width;
        }
    }

    mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height);

    [self.view addSubview:mainScrollView];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return NO;
}

@end

答案 1 :(得分:2)

我按照@NikNarmo的解决方案,写了一个小的快速xcode项目来演示缩放和分页功能。

希望能帮助任何想要完成同样任务的人。

有些代码来自UIScrollView教程:入门http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift

和一些来自初学者的UIScrollView指南http://www.appcoda.com/uiscrollview-introduction/

使用Xcode 7.0和Swift 2.0

  override func viewDidLoad() {

    super.viewDidLoad()

    mainScrollView = UIScrollView(frame: self.view.bounds)
    mainScrollView.pagingEnabled = true
    mainScrollView.showsHorizontalScrollIndicator = false
    mainScrollView.showsVerticalScrollIndicator = false

    pageScrollViews = [UIScrollView?](count: photos.count, repeatedValue: nil)

    let innerScrollFrame = mainScrollView.bounds

    mainScrollView.contentSize =
        CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width,
            mainScrollView.bounds.size.height)

    mainScrollView.backgroundColor = UIColor.redColor()
    mainScrollView.delegate = self
    self.view.addSubview(mainScrollView)

    configScrollView()
    addPageControlOnScrollView()
}

并且魔法是在func scrollViewWillEndDragging中,当contentSize等于mainScrollViewContentSize时,如果是mainScrollViewController,则执行分页,否则什么也不做。

  func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    let targetOffset = targetContentOffset.memory.x
    let zoomRatio = scrollView.contentSize.height / mainScrollViewContentSize.height

    if zoomRatio == 1 {
        // mainScrollViewController
        let mainScrollViewWidthPerPage = mainScrollViewContentSize.width / CGFloat(pageControl.numberOfPages)

        let currentPage = targetOffset / (mainScrollViewWidthPerPage * zoomRatio)
        pageControl.currentPage = Int(currentPage)
        loadVisiblePages()

    }
    else {
        // pageScorllViewController
    }
}

这是项目代码https://github.com/Charles-Hsu/ScrollViewDemo