私はこの問題を次のようにして解決しました:スクロールビューの親を半分見える位置から一番上に移動するアニメーションを作成します....
- (void)setScrollViewExpanded:(BOOL)expanded {
BOOL isExpanded = self.scrollViewContainer.frame.origin.y == 0.0;
if (isExpanded == expanded) return;
CGRect frame = self.scrollViewContainer.frame;
CGRect newFrame;
if (expanded) {
newFrame = CGRectMake(0, 0, frame.size.width, self.view.frame.size.height);
self.scrollView.delegate = nil;
self.scrollViewContainer.frame = CGRectMake(0, frame.origin.y, frame.size.width, self.view.bounds.size.height);
self.scrollView.delegate = self;
} else {
newFrame = CGRectMake(0, 300, frame.size.width, frame.size.height);
}
[UIView animateWithDuration:1 animations:^{
self.scrollViewContainer.frame = newFrame;
} completion:^(BOOL finished) {
if (!expanded) {
self.scrollView.delegate = nil;
self.scrollViewContainer.frame = CGRectMake(0, 300, self.scrollViewContainer.bounds.size.width, self.view.bounds.size.height-300);
self.scrollView.delegate = self;
}
}];
}
上部に対するスクロール ビューのコンテンツ オフセットの変化に基づいてアニメーションをトリガーします...
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y > 10.0) {
// 10 is a little threshold so we won't trigger this on a scroll view
// "bounce" at the top. alternatively, you can set scrollView.bounces = NO
[self setScrollViewExpanded:YES];
} else if (scrollView.contentOffset.y < 0.0) {
[self setScrollViewExpanded:NO];
}
}
編集:古いコードを再確認した後、展開アニメーションを変更しました。フレームの変更中にコンテンツ オフセットに関するフィードバックが発生するため、もう少し複雑にする必要があります。編集はアニメーション中にサイズを変更せず、原点のみを変更します。アニメーションの前後でサイズを変更し、デリゲート メッセージを一時的にブロックします。また、スクロール ビューの自動サイズ変更マスクは、コンテナー ビューを埋めるように設定する必要があることに注意してください。