0
votes

I am trying to develop a new custom UIView (to allow for horizontal date selection). I want to do all the UI design in XIB files.

The custom UI view contains a scrollview and then two 'week' views. The idea is that as the scrolling occurs, I will move the two 'week' views in place and reconfigure them to the right dates to create an 'infinite' scroll for date selections.

I can load the UIView, which then loads scrollview and week views (all designed in a XIB).

My DatePickerView class, derived from the UIView class does an addSubview of the scroll view (which contains the two week views). The scroll view is 320 wide and the contentSize is set to 640 wide. UserInteraction is enabled. Horizonal Scrolling is enabled.

This all works and displays on the screen. The week views each contain 7 buttons. I can press them and they get the touch. However, the scrollview does not seem to want to scroll.

I set my custom view to be a UIScrollViewDelegate. No calls occur to scrollViewDidScroll.

For each of the week views, I have a 'container' view and then the buttons. I added the following to the container view (again derived from a UIView).

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    DDLogInfo(@"Began.  Next Responder: %@", self.nextResponder);
    [self.nextResponder touchesBegan:touches withEvent:event];
}

(and comparable ones for the other touch events, touchesMoved, touchesEnded, touchesCancelled),

I print out the nextResponder, which is the UIScrollView, so I know that I am sending the touch to the view, but I never see the scrollview want to scroll.

Is my method of passing the touchEvents up the responder chain correct?
Is there anything else I need to configure to get the scrolling to work?

Any help is appreciated.

Charlie

2

2 Answers

1
votes

If I understand correctly, you want infinite scroll with just three pages of scroll view. I achieved it with similar effects in my calendar view project. You can checkout from here DPCalendar

In a nutshell, I created a view like

@interface DPCalendarMonthlyView : UIScrollView

And initial it like this

self.showsHorizontalScrollIndicator = NO;
self.clipsToBounds = YES;
self.contentInset = UIEdgeInsetsZero;
self.pagingEnabled = YES;
self.delegate = self;

I create three views like this

[self.pagingViews addObject:[self singleMonthViewInFrame:self.bounds]];
[self.pagingViews addObject:[self singleMonthViewInFrame:CGRectMake(self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)]];
[self.pagingViews addObject:[self singleMonthViewInFrame:CGRectMake(self.bounds.size.width * 2, 0, self.bounds.size.width, self.bounds.size.height)]];

Then I set the content size and also scroll it to the middle

[self setContentSize:CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height)];
[self scrollRectToVisible:((UIView *)[self.pagingViews objectAtIndex:1]).frame animated:NO];

In the scrollview delegate function, i need to do

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender
{
    //If scroll right
    if(self.contentOffset.x > self.frame.size.width)
    {
        //do something if scroll right
    } else if(self.contentOffset.x < self.frame.size.width)
    {
        //do something else if scroll left
    } else {
        return;
    }
    //scroll back to the middle
    [self scrollRectToVisible:((UICollectionView *)[self.pagingViews objectAtIndex:1]).frame animated:NO];
}

Hopefully it is useful to you.

0
votes

For those that follow down this path, I figured this out and it was a silly error. I forgot to turn off AutoLayout. I keep forgetting that Apple put autoLayout as an enable/disable option under the 'document'-level of a NIB (so I forget to look there).

Turned it off and it works as designed. Looks like autoLayout was causing the views to be rearranged to not need to be scrolled, or something equivalent.