[英]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.