UIScrollView仅在底部禁用垂直反弹
我一直在寻找一种方法来禁用UIScrollView垂直反弹,但只能在底部。 我仍然需要在顶部反弹。
找不到任何东西。 这可能吗?
提前致谢!
使用UIScrollViewDelegate方法scrollViewDidScroll
检查滚动视图的内容偏移量,或多或less检查用户是否尝试滚动滚动视图的底部界限以外。 然后,只需使用此设置将滚动设置回滚动视图的末尾。 它发生得如此之快,以至于你甚至不能说滚动视图反弹或超出了界限。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) { [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, scrollView.contentSize.height - scrollView.frame.size.height)]; } }
要在Swift顶部禁用垂直反弹:
func scrollViewDidScroll(scrollView: UIScrollView) { if scrollView.contentOffset.y < 0 { scrollView.contentOffset.y = 0 } }
基于0x7fffffff的答案,我创build了一个类:
VerticalBounceControl.h
#import <UIKit/UIKit.h> @interface VerticalBounceControl : NSObject @property (nonatomic, assign) BOOL bounce; - (id) initWithScrollView:(UIScrollView*)scrollView bounceAtTop:(BOOL)bounceAtTop bounceAtBottom:(BOOL)bounceAtBottom; @end
VerticalBounceControl.m
#import "VerticalBounceControl.h" @interface VerticalBounceControl () @property (nonatomic, retain) UIScrollView* scrollView; @property (nonatomic, assign) BOOL bounceAtTop; @property (nonatomic, assign) BOOL bounceAtBottom; @end @implementation VerticalBounceControl - (id) initWithScrollView:(UIScrollView*)scrollView bounceAtTop:(BOOL)bounceAtTop bounceAtBottom:(BOOL)bounceAtBottom { self = [super init]; if(self) { self.bounce = YES; self.scrollView = scrollView; self.bounceAtTop = bounceAtTop; self.bounceAtBottom = bounceAtBottom; [self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL]; } return self; } - (void) dealloc { [self.scrollView removeObserver:self forKeyPath:@"contentOffset"]; self.scrollView = nil; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"contentOffset"]) { if (self.bounce && ((self.scrollView.contentOffset.y<=0 && self.bounceAtTop) || (self.scrollView.contentOffset.y>=self.scrollView.contentSize.height-1 && self.bounceAtBottom))) { self.scrollView.bounces = YES; } else { self.scrollView.bounces = NO; } } } @end
可以在没有调用者作为UIScrollView
的委托的情况下使用,如下所示:
VerticalBounceControl* bounceControl = [[VerticalBounceControl alloc] initWithScrollView:anyScrollView bounceAtTop:YES bounceAtBottom:NO];
这样,你可以有UIScrollView
的这种反弹行为,你不想改变他们的委托(如UIWebView
的)。
再次感谢解决scheme@ 0x7fffffff!
我添加了fabsf()
到0x7fffffff♦的解决scheme,以迎合负(向下)滚动:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (fabsf(scrollView.contentOffset.y) >= scrollView.contentSize.height - scrollView.frame.size.height) { [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, scrollView.contentSize.height - scrollView.frame.size.height)]; } }
Swift 3:
这只能在底部禁用垂直反弹:
func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.y > scrollView.contentSize.height - scrollView.bounds.height { scrollView.contentOffset.y = scrollView.contentSize.height - scrollView.bounds.height } }