UIScrollView - (bounces = NO)似乎覆盖(pagingEnabled = YES) [英] UIScrollView - (bounces = NO) seems to override (pagingEnabled = YES)

查看:531
本文介绍了UIScrollView - (bounces = NO)似乎覆盖(pagingEnabled = YES)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

换句话说,何时:

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

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

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?

编辑:
解决方案:

@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


推荐答案

最好的办法是写一个 UIScrollView 子类并手动实现所需的行为。你应该可以从 pagingEnabled 跳出开始,两者都设置为 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.

这篇关于UIScrollView - (bounces = NO)似乎覆盖(pagingEnabled = YES)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆