gpt4 book ai didi

objective-c - 在 UIScrollView 中劫持 "swipe to next page"

转载 作者:行者123 更新时间:2023-11-29 11:12:10 26 4
gpt4 key购买 nike


我想防止在我的 UIScrollview 的第 3 页上滚动和“劫持滑动”手势来触发某事。别的。完成此操作后,我想响应式滚动。

这行不通。

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if(scrollView.contentOffset.x == self.view.frame.size.width * 2 ) {
// disable scrolling
scrollView.scrollEnabled = NO;
}
}


// hijack the next scrolling event
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

当 scrollEnabled = NO 时不调用此委托(delegate)

感谢帮助


编辑 EventHandler 未被调用 ;-(

- (void)viewDidLoad
{
[super viewDidLoad];

// Default background color
self.view.backgroundColor = [UIColor redColor];

// Create scroll view
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;


UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[scrollView addGestureRecognizer:recognizer];
[recognizer release];
[scrollView delaysContentTouches];

// Create subviews (pages)
NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++) {
// x pos
CGFloat yOrigin = i * self.view.frame.size.width;

// Create subview and add to scrollView
UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
pageView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];

[scrollView addSubview:pageView];
[pageView release];
}

// Set contentsize
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);


// Add scrollView to view and release
[self.view addSubview:scrollView];
[scrollView release];

}


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"swipe!!!!");
scrollView.scrollEnabled = YES;
}

最佳答案

如果禁用 ScrollView :

    scrollView.scrollEnabled = NO;

不调用委托(delegate)方法是不可避免的,因此您需要一种替代方法来处理劫持模式下的滑动。您可以尝试的一件事是使用 UISwipeGestureRecognizer:您可以将 UISwipeGestureRecognizer 关联到您查看和处理来自处理程序方法的滑动,而不是简单地禁用滚动:

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
recognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:recognizer];

handleSwipeFrom 中,您将重新启用滚动:

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
// do your hijack here
scrollView.scrollEnabled = YES;
}

关于objective-c - 在 UIScrollView 中劫持 "swipe to next page",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11036192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com