UIScrollView里面的UIScrollView
我有一个UIScrollView
与另一个UIScrollView
里面。 他们都水平滚动,并具有pagingEnabled = YES
。 假设我开始滚动内部滚动视图,并达到最右边的界限。 如果我继续滚动它,那么外面的scrollView开始移动。 我需要避免这一点。 内部观点应该以橡皮筋效应跳跃,外部应该留在原地。
希望这是明确的,但这里是一个草图:
我试着设置outerView.scrollEnabled = NO;
喜欢这个:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { outerView.scrollEnabled = NO; }
,它的工作原理是我需要的,如果滚动innerView。 OuterView不再滚动。 但是如果我想再次滚动outerView,我必须将scrollEnabled
设置回YES。 我试过在这里做:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { outerView.scrollEnabled = YES; }
,但我得到了同样的问题:达到innerView outerView的最右边界滚动,而不是innerView跳跃与橡皮筋效应。
任何build议如何解决问题?
UPDATE
此解决scheme始终工作:
@implementation InnerScrollViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate> - (void)viewDidLoad { UISwipeGestureRecognizer* swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)] autorelease]; swipeGesture.delegate = self; [self.view addGestureRecognizer:swipeGesture]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { _outerScrollView.scrollEnabled = NO; return YES; } - (void)handleSwipe:(UIGestureRecognizer*)gestureRecognizer { } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { _outerScrollView.scrollEnabled = NO; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { _outerScrollView.scrollEnabled = YES; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { _outerScrollView.scrollEnabled = YES; } @end
————————————————– ———————
老解答:不总是工作
这是我解决问题的方法:
@implementation InnerView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.delaysContentTouches = NO; } return self; } - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view { return NO; }
据我所知, self.delaysContentTouches = NO;
使所有事件立即被传递,并且- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
阻止响应者链传递这些事件。
通过以下方式修改您的方法:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if(scrollView == innerView) outerView.scrollEnabled = NO; else outerView.scrollEnabled = YES; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if(scrollView == innerView) { if(innerView.contentOffset.x + innerView.frame.size.width == innerView.contentSize.width) { outerView.scrollEnabled = NO; } else { outerView.scrollEnabled = YES; } } }
这适用于我:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { outerScrollView.scrollEnabled = NO; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [NSTimer scheduledTimerWithTimeInterval:0.3 target:[NSBlockOperation blockOperationWithBlock:^{ outerScrollView.scrollEnabled = YES; }] selector:@selector(main) userInfo:nil repeats:NO]; }
请记住在类声明中采用UIScrollViewDelegate,并将innerScrollView的委托设置为self(不要指派outerScrollView的委托)
我有一个类似的问题。 我find了解决scheme,但它只适用于innerScrollView.bounces = NO
。
@interface PageCtrl () @property (weak, nonatomic) IBOutlet UIScrollView *innerScrollView; @property(nonatomic) UIPanGestureRecognizer *panLockScroll; @end @implementation PageCtrl - (void)viewDidLoad { // ... self.innerScrollView.delegate = self; self.innerScrollView.bounces = NO; // disable bounces! self.panLockScroll = [[UIPanGestureRecognizer alloc]initWithTarget:self action:nil]; self.panLockScroll.delegate = self; [self.innerScrollView addGestureRecognizer:self.panLockScroll]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{ if( gestureRecognizer == self.panLockScroll && otherGestureRecognizer == self.innerScrollView.panGestureRecognizer ){ return YES; } return NO; } @end