我UIScrollView只允许水平滚动,我想知道用户滚动的方向(左,右).我做的是继承UIScrollView和覆盖touchesMoved方法:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
float now = [touch locationInView:self].x;
float before = [touch previousLocationInView:self].x;
NSLog(@"%f %f", before, now);
if (now > before){
right = NO;
NSLog(@"LEFT");
}
else{
right = YES;
NSLog(@"RIGHT");
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我移动时,这种方法有时根本不会被调用.你怎么看?
我正在尝试实现scrollViewWillBeginDragging方法. 当调用此方法时,我检查用户是否已选择滚动视图中的一个按钮处于状态:selected.如果没有,那么我显示UIAlert以通知用户.
我的问题是,如果用户从右向左滚动(从右侧拉下一个视图),我只想调用按钮选择(NextQuestion)方法.但是,如果他们从左向右滚动,那么我希望它像往常一样滚动.
目前无论用户在哪个方向滚动,都会调用检查器方法.如何在从右向左滚动时调用方法?
以下是我目前的实现方式:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self NextQuestion:scrollView];
}
-(IBAction)NextQuestion:(id)sender
{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth) / pageWidth) + 1;
NSInteger npage = 0;
CGRect frame = scrollView.frame;
// Check to see if the question has been answered. Call method from another class.
if([QuestionControl CheckQuestionWasAnswered:page])
{
pageNumber++;
NSLog(@"Proceed");
if(([loadedQuestionnaire count] - 1) != page)
{
[self loadScrollViewWithPage:page - 1]; …Run Code Online (Sandbox Code Playgroud)