我有一个浮动按钮,显示在tableview的顶部,允许用户快速返回到顶部。由于某些原因,它适用于除iOS9之外的所有iOS版本。当用户开始向上滚动时,会出现该按钮。当我点击它时,它只是停止滚动的tableview并且不执行按钮的动作。如果tableview没有滚动,则按钮有效。如果有人有任何建议,请告诉我。谢谢!
-(void)viewDidLoad {
[super viewDidLoad];
self.scrollToTopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.scrollToTopButton.alpha = 0.0f;
self.scrollToTopButton.frame = CGRectMake(0, self.tableView.contentOffset.y, [UIScreen mainScreen].bounds.size.width, 37);
self.scrollToTopButton.titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
self.scrollToTopButton.titleLabel.textColor = [UIColor whiteColor];
self.scrollToTopButton.tintColor = [UIColor whiteColor];
[self.scrollToTopButton setTitle:@"Scroll To Top" forState:UIControlStateNormal];
self.scrollToTopButton.backgroundColor = golfGamutGreen;
[self.scrollToTopButton addTarget:self action:@selector(scrollToTop) forControlEvents:UIControlEventTouchDown];
[self.tableView addSubview:self.scrollToTopButton];
[self.tableView bringSubviewToFront:self.scrollToTopButton];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
lastContentOffset = scrollView.contentOffset.y;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[UIButton animateWithDuration:0.8 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self.scrollToTopButton setAlpha:0.0f];
} completion:^(BOOL finished) {
}];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.scrollToTopButton.frame = CGRectMake(0, self.tableView.contentOffset.y, [UIScreen mainScreen].bounds.size.width, 37);
[self.tableView bringSubviewToFront:self.scrollToTopButton];
if (lastContentOffset > scrollView.contentOffset.y && scrollView.contentOffset.y > 1000 && isScrollingToTop == NO) {
[UIButton animateWithDuration:0.8 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self.scrollToTopButton setAlpha:0.9f];
} completion:^(BOOL finished) {
}];
}
else {
[UIButton animateWithDuration:0.8 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self.scrollToTopButton setAlpha:0.0f];
} completion:^(BOOL finished) {
}];
}
if (scrollView.contentOffset.y == 0) {
[UIButton animateWithDuration:0.8 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self.scrollToTopButton setAlpha:0.0f];
} completion:^(BOOL finished) {
}];
isScrollingToTop = NO;
}
}
- (void)scrollToTop {
NSLog(@"Scroll To Top");
//[self.tableView scrollRectToVisible:CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height) animated:YES];
[self.tableView setContentOffset:CGPointMake(0, 0 - self.tableView.contentInset.top) animated:YES];
isScrollingToTop = YES;
}