Tôi đang cố triển khai phương thức scrollViewWillBeginDragging. Khi phương pháp này được gọi là tôi kiểm tra xem người dùng đã chọn một trong các nút nằm trong scrollview có ở trạng thái: đã chọn hay không. Nếu không, tôi sẽ hiển thị UIAlert để thông báo cho người dùng.Kiểm tra hướng cuộn trong UIScrollView
Vấn đề của tôi ở đây là tôi chỉ muốn gọi nút chọn (NextQuestion) phương pháp nếu người dùng cuộn từ phải sang trái (kéo xem tiếp theo từ bên phải). Nhưng nếu chúng đang di chuyển từ Trái sang phải thì tôi muốn nó cuộn như bình thường.
Hiện tại, phương pháp kiểm tra được gọi là bất kể hướng người dùng cuộn vào. Làm cách nào tôi chỉ có thể gọi một phương thức khi họ cuộn từ Phải sang trái?
Dưới đây là làm thế nào tôi đã hiện thực hiện nó:
- (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];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}
// update the scroll view to the appropriate page
frame = scrollView.frame;
frame.origin.x = (frame.size.width * page) + frame.size.width;
frame.origin.y = 0;
[self.scrollView scrollRectToVisible:frame animated:YES];
}
// If question has not been answered show a UIAlert with instructions.
else
{
UIAlertView *alertNotAnswered = [[UIAlertView alloc] initWithTitle:@"Question Not Answered"
message:@"You must answer this question to continue the questionnaire."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alertNotAnswered show];
}
}
Mã cho Giải pháp 1:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"%f",scrollView.contentOffset.x);
if (scrollView.contentOffset.x < lastOffset) // has scrolled left..
{
lastOffset = scrollView.contentOffset.x;
[self NextQuestion:scrollView];
}
}
Hãy nhìn vào câu trả lời này: http://stackoverflow.com/a/4073028/742298 –