简体   繁体   English

如何维护UIScrollView,以便始终保持可见的subView?

[英]How to maintain UIScrollView so that a subView always remains visible?

I am using a UIScrollView for showing UIButtons horizontally scrolled. 我正在使用UIScrollView来显示水平滚动的UIButton。 Now when I select and highlight a button i shouldn't be able to scroll that button out of view. 现在,当我选择并突出显示一个按钮时,我应该无法将该按钮滚动到视线之外。 At time I am allowing only one button to be selected. 当时我只允许选择一个按钮。

After considering the question: How to stop a UIScrollView at a specific point? 考虑了以下问题后: 如何在特定点停止UIScrollView? and implemented the following: 并实现了以下内容:

-(void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    if(targetContentOffset->x==0){
        return;
    }else if(targetContentOffset->x>self.maxScrollPoint.x){
        targetContentOffset->x=self.maxScrollPoint.x;
    }else if (targetContentOffset->x<self.maxScrollPoint.x+self.componentView.frame.size.width){
        targetContentOffset->x=self.maxScrollPoint.x+self.componentView.frame.size.width;
    }
 }

Where maxScrollPoint is the origin of the UIButton in the Scrollview. 其中maxScrollPoint是Scrollview中UIButton的原点。 This implementation is allowing the user to scroll beyond the visible rect and then scrolling back to the origin of the button. 此实现允许用户滚动到可见矩形之外,然后滚动回到按钮的原点。

Is there anyway I can restrict the scrollview to stop scrolling beyond that point and also beyond the scrollview's max visible width(not contentsize) 无论如何,我可以限制滚动视图以停止滚动到该点以及滚动视图的最大可见宽度(不是contentsize)

You probably want something like this: 您可能想要这样的东西:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_selectedButton != nil)
    {
        CGRect oldFrame = _selectedButton.frame;
        oldFrame.origin = scrollView.contentOffset;
        _selectedButton.frame = oldFrame;
    }
}

The idea is that whenever the scroll view scrolls, the _selectedButton , if there is one, will place its origin at the left side of the scroll view's current frame. 这个想法是,每当滚动视图滚动时, _selectedButton (如果有的话)都将其原点放置在滚动视图当前帧的左侧。 You may have to tinker with this a bit, but it should work. 您可能需要对此进行一些修改,但是它应该可以工作。

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

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