简体   繁体   English

可移动UIView中的UIScrollView

[英]UIScrollView inside a movable UIView

I'm attempting to recreate the animation done in the Google Maps app that occurs when you select a location on the map. 我正在尝试重新创建在Google Maps应用中完成的动画,该动画是您在地图上选择位置时发生的。 After selecting a location a UIView pokes up from the bottom of the screen. 选择一个位置后,UIView从屏幕底部弹出。 You can pull that up to reveal a UIScrollView with additional content. 您可以将其拉出以显示带有其他内容的UIScrollView。 I'm curious how they make it so you can only scroll the content inside the UIScrollView when the parent view is at the "top". 我很好奇他们是如何做到的,因此只有当父视图位于“顶部”时,才可以滚动UIScrollView内部的内容。 I understand that you could setScrollEnabled:, but they somehow do it on the fly so that when sliding your finger up the parent view "docks", and then the inner content scrolls, and when you are scrolling the content down, it stops once it reaches the top of the content and starts pulling the header down with it. 我知道您可以使用setScrollEnabled :,但是它们会以某种方式即时进行操作,这样当您将手指滑到父视图“码头”上时,内部内容便会滚动,而当您向下滚动内容时,它会立即停止到达内容的顶部,然后开始拉下标题。

Any ideas? 有任何想法吗?

拉起时在底部

I solved this problem by doing something like this: Create an animation that moves the scroll view's parent from the half-visible position to the top.... 我通过执行以下操作解决了此问题:创建一个动画,将滚动视图的父级从半可见位置移动到顶部。

- (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;
        }
    }];
}

Trigger the animation based on change in scroll view's content offset relative to the top... 根据滚动视图相对于顶部的内容偏移量的变化触发动画。

-(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];
    }
}

Edit : I changed the expand animation after rechecking my old code. 编辑 :重新检查我的旧代码后,我更改了扩展动画。 It needs to be a little more complex due to feedback on the content offset while changing the frame. 由于在更改框架时对内容偏移量的反馈,因此它需要稍微复杂一些。 The edit doesn't change the size during the animation, just the origin. 编辑不会在动画过程中更改大小,仅更改原点。 It changes the size before or after the animation and blocks the delegate messages temporarily. 它在动画之前或之后更改大小,并暂时阻止委托消息。 Also note the scroll view's autoresize mask should be set to fill the container view. 还要注意,滚动视图的自动调整大小蒙版应设置为填充容器视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM