【问题标题】:UIScrollView paging disabled after rotation旋转后禁用 UIScrollView 分页
【发布时间】:2014-10-03 21:33:46
【问题描述】:

以下是我使用 UIScrollView 使用 AutoLayout 启用分页的 App Tour 代码 (Masonry) 它在纵向中工作正常,但是当我旋转分页时被禁用,因此在横向中不起作用。然后它在肖像模式下也不起作用。有人可以帮忙看看这里出了什么问题吗?

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor clearColor];

    self.scrollView = [UIScrollView new];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.scrollEnabled = YES;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    UIImageView *esImageview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page1"]];
    esImageview1.contentMode = UIViewContentModeScaleAspectFit;
    esImageview1.backgroundColor = [UIColor clearColor];
    esImageview1.userInteractionEnabled = YES;
    [self.scrollView addSubview:esImageview1];

    [esImageview1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.scrollView);
        make.width.height.equalTo(self.view);
    }];

    UIImageView *esImageview2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page2"]];
    esImageview2.contentMode = UIViewContentModeScaleAspectFit;
    esImageview2.backgroundColor = [UIColor clearColor];
    esImageview2.userInteractionEnabled = YES;
    [self.scrollView addSubview:esImageview2];

    [esImageview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(esImageview1.mas_right);
        make.top.bottom.equalTo(self.scrollView);
        make.width.height.equalTo(self.view);
    }];

    UIImageView *esImageview3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page3"]];
    esImageview3.contentMode = UIViewContentModeScaleAspectFit;
    esImageview3.backgroundColor = [UIColor clearColor];
    esImageview3.userInteractionEnabled = YES;
    [self.scrollView addSubview:esImageview3];

    [esImageview3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(esImageview2.mas_right);
        make.top.bottom.equalTo(self.scrollView);
        make.width.height.equalTo(self.view);
    }];

    self.pageControl = [UIPageControl new];
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;
    self.pageControl.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.pageControl];

    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.equalTo(@40);
        make.top.equalTo(self.view).with.offset(40);
    }];

    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"App Tour";
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:16];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.equalTo(@40);
        make.top.equalTo(self.view).with.offset(20);
    }];

    UIButton *doneButton = [UIButton new];
    [doneButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:16]];
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [doneButton setTitle:@"Done" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    doneButton.backgroundColor = [UIColor clearColor];
    [self.view addSubview:doneButton];

    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.height.equalTo(@40);
        make.width.equalTo(@70);
        make.top.equalTo(self.view).with.offset(20);
    }];
}

- (void)doneButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height);
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

【问题讨论】:

    标签: ios uiscrollview scroll-paging


    【解决方案1】:

    我猜它被另一个视图或类似的东西掩盖了。尝试继承 UIScrollView 并实现以下方法来检查它是否完全响应您的触摸。

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"OMG TOUCHES WORK, NOW WHAT?");
    }
    

    您可以通过强制它在前面轻松检查它是否被掩盖。在包含 UIScrollView 的 UIViewController 中的回调中调用这行代码进行旋转。它应该可以解决问题。您可能必须为控制器子类化 UIView 并将此方法放在 didLayoutSubviews 方法中,不太确定,这取决于您的实现。

    [self.view bringSubViewToFront:self.scrollView];
    

    还要检查旋转前后的框架和边界,看看是否有什么不合适的地方

    【讨论】:

    • 我检查过没有任何东西掩盖了滚动视图。最重要的是,我每页都有图像。当我旋转滚动视图自动滚动到第一页然后停止分页并且永远不会回来时,这很奇怪。
    • Top imageview 是否有可能失去交互启用属性?我在旋转后添加了图像 - 请注意,我在顶部的滚动视图中有一个图像视图,这是预期的。
    • 据我所知,它不应该丢失已启用的属性。您是否尝试过子类化和实现 touches 方法。因为那会告诉你很多。我也喜欢一个非常好的新功能(如果您使用的是 xCode 6,那么您应该使用)新的“查看调试”功能。你可以在这里阅读更多关于它的信息,developer.apple.com/library/ios/documentation/DeveloperTools/…
    【解决方案2】:

    我只是想在 didRotateFromInterfaceOrientation 委托中重置 Scrollview 的内容大小。不知道为什么,因为当我旋转时,videDidLayoutSubview 已经在我已经重置内容大小的地方被调用了。

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      相关资源
      最近更新 更多