0
votes

I am using a UIScrollView for showing UIButtons horizontally scrolled. 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? 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. 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)

1

1 Answers

0
votes

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. You may have to tinker with this a bit, but it should work.