UIScrollView - (bounces = NO) 似乎覆盖 (pagingEnabled = YES)

发布于 2024-08-04 17:46:00 字数 1489 浏览 10 评论 0原文

我有一个带有分页功能的 UIScrollView (因此带有 UIPageControl 并在页面之间左右拖动/轻拂的典型模型),并且我的工作正常。奇怪的是,当我想摆脱弹跳(这样就看不到左右两侧UI后面的黑色)时,突然分页不再起作用。

换句话说,当:

scrollView.pagingEnabled = YES;
scrollView.bounces = YES;

一切工作正常,除了我不喜欢页面(0)和页面(length-1)处的弹跳。但是当我这样做时:

scrollView.pagingEnabled = YES;
scrollView.bounces = NO;

它停止在每个页面上卡入到位,而是将所有页面一起视为一个长页面。因此,似乎出于某种原因,分页依赖于弹跳,只要我能以某种方式阻止弹跳,就可以了。那么,有没有其他方法可以摆脱它呢?或者我做错了什么?

编辑: 解决方案:

@interface PagingScrollView : UIScrollView
@end

@implementation PagingScrollView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.pagingEnabled = YES;
        self.bounces = YES;
    }
    return self;
}

- (void)setContentOffset:(CGPoint)offset
{
    CGRect frame = [self frame];
    CGSize contentSize = [self contentSize];
    CGPoint contentOffset = [self contentOffset];

    // Clamp the offset.
    if (offset.x <= 0)
        offset.x = 0;
    else if (offset.x > contentSize.width - frame.size.width)
        offset.x = contentSize.width - frame.size.width;

    if (offset.y <= 0)
        offset.y = 0;
    else if (offset.y > contentSize.height - frame.size.height)
        offset.y = contentSize.height - frame.size.height;

    // Update only if necessary 
    if (offset.x != contentOffset.x || offset.y != contentOffset.y)
    {
        [super setContentOffset:offset];
    }
}

@end

I have a UIScrollView with paging in it (so the typical model with a UIPageControl and dragging/flicking left and right between pages), and I've got that working fine. The weird thing is that when I wanted to get rid of bouncing (so that you can't see black behind the UI on the left and right sides), suddenly paging no longer works.

In other words, when:

scrollView.pagingEnabled = YES;
scrollView.bounces = YES;

Everything works fine, except I don't like the bouncing at page(0) and page(length-1). But when I do this:

scrollView.pagingEnabled = YES;
scrollView.bounces = NO;

It stops snapping into place at each page, instead treating all the pages together as one long page. So it seems that for some reason paging is dependent upon bouncing, which is fine as long as I can somehow stop the bouncing. So, is there another way to get rid of it? Or is there something I'm doing wrong?

EDIT:
The solution:

@interface PagingScrollView : UIScrollView
@end

@implementation PagingScrollView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.pagingEnabled = YES;
        self.bounces = YES;
    }
    return self;
}

- (void)setContentOffset:(CGPoint)offset
{
    CGRect frame = [self frame];
    CGSize contentSize = [self contentSize];
    CGPoint contentOffset = [self contentOffset];

    // Clamp the offset.
    if (offset.x <= 0)
        offset.x = 0;
    else if (offset.x > contentSize.width - frame.size.width)
        offset.x = contentSize.width - frame.size.width;

    if (offset.y <= 0)
        offset.y = 0;
    else if (offset.y > contentSize.height - frame.size.height)
        offset.y = contentSize.height - frame.size.height;

    // Update only if necessary 
    if (offset.x != contentOffset.x || offset.y != contentOffset.y)
    {
        [super setContentOffset:offset];
    }
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2024-08-11 17:46:00

最好的选择是编写一个 UIScrollView 子类并手动实现所需的行为。您应该能够从将 pagingEnabledbounces 都设置为 YES 开始,然后用您的覆盖 -setContentOffset:自己的剪辑边缘的方法。

Your best bet would be to write an UIScrollView subclass and implement the desired behavior manually. You should be able to start with pagingEnabled and bounces both set to YES and then overwrite -setContentOffset: with your own method that clips the edges.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文