UIScrollView - (bounces = NO) 似乎覆盖 (pagingEnabled = YES)
我有一个带有分页功能的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的选择是编写一个 UIScrollView 子类并手动实现所需的行为。您应该能够从将
pagingEnabled
和bounces
都设置为YES
开始,然后用您的覆盖-setContentOffset:
自己的剪辑边缘的方法。Your best bet would be to write an
UIScrollView
subclass and implement the desired behavior manually. You should be able to start withpagingEnabled
andbounces
both set toYES
and then overwrite-setContentOffset:
with your own method that clips the edges.