これが私が思いついたものです。scrollViewShouldScrollToTop:
重要なのは、メソッドをオーバーライドすることです。また、デフォルトの動作を防ぐために NO を返します。
注意すべきことの 1 つscrollViewShouldScrollToTop:
は、スクロール ビューのコンテンツが既にスクロール ビューの上部にある場合は呼び出されないことです。以下のコードのトリックを参照してください。
@interface ViewController ()
<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width,
self.view.bounds.size.height * 2);
// if the scrollView contentOffset is at the top
// (0, 0) it won't call scrollViewShouldScrollToTop
self.scrollView.contentOffset = CGPointMake(0, 1);
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
// call your custom method and return YES
[self scrollToBottom:scrollView];
return NO;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// be sure that when you are at the top
// the contentOffset.y = 1
if (scrollView.contentOffset.y == 0)
{
scrollView.contentOffset = CGPointMake(0, 1);
}
}
- (void)scrollToBottom:(UIScrollView *)scrollView
{
// do whatever you want in your custom method.
// here it scrolls to the bottom
CGRect visibleRect = CGRectMake(0, scrollView.contentSize.height - 5, 1, 1);
[scrollView scrollRectToVisible:visibleRect animated:YES];
}
@end