我正在尝试重新创建在地图上选择位置时在Google地图应用中完成的动画。选择一个位置后,UIView会从屏幕底部向上戳。您可以将其拉出来显示带有其他内容的UIScrollView。我很好奇他们是如何制作的,因此当父视图位于“顶部”时,您只能滚动UIScrollView内的内容。我知道你可以setScrollEnabled :,但是他们会以某种方式动态地执行它,这样当你的手指向上滑动父视图“停靠”,然后滚动内部内容时,当你滚动内容时,它会停止一次到达内容的顶部并开始用它拉下标题。
有什么想法吗?
答案 0 :(得分:0)
我通过这样的方式解决了这个问题:创建一个动画,将滚动视图的父级从半可见位置移动到顶部....
- (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];
}
}
编辑:我在重新检查旧代码后更改了展开动画。由于在更改帧时对内容偏移的反馈,它需要更复杂一些。编辑不会在动画期间更改大小,只会更改原点。它会在动画之前或之后更改大小,并暂时阻止委托消息。另请注意,应设置滚动视图的自动调整大小以填充容器视图。