[英]UIScrollView inside a movable UIView
我正在嘗試重新創建在Google Maps應用中完成的動畫,該動畫是您在地圖上選擇位置時發生的。 選擇一個位置后,UIView從屏幕底部彈出。 您可以將其拉出以顯示帶有其他內容的UIScrollView。 我很好奇他們是如何做到的,因此只有當父視圖位於“頂部”時,才可以滾動UIScrollView內部的內容。 我知道您可以使用setScrollEnabled :,但是它們會以某種方式即時進行操作,這樣當您將手指滑到父視圖“碼頭”上時,內部內容便會滾動,而當您向下滾動內容時,它會立即停止到達內容的頂部,然后開始拉下標題。
有任何想法嗎?
我通過執行以下操作解決了此問題:創建一個動畫,將滾動視圖的父級從半可見位置移動到頂部。
- (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];
}
}
編輯 :重新檢查我的舊代碼后,我更改了擴展動畫。 由於在更改框架時對內容偏移量的反饋,因此它需要稍微復雜一些。 編輯不會在動畫過程中更改大小,僅更改原點。 它在動畫之前或之后更改大小,並暫時阻止委托消息。 還要注意,滾動視圖的自動調整大小蒙版應設置為填充容器視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.