I have a view controller than contains a UIScrollView at the top half of the screen. On the bottom half I have a label. This is the detail view for a table, so once you select an item from the table, it loads this detail view. Some of the items you select have different variations so this is where the scroll view comes in. You can scroll left and right to see the variations.
I am trying to have the label BELOW the scroll view change its text when the scroll view scrolls. Here is what I am using to detect which page I am on:
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
This returns the "page number" of the scroll view. How can I tell the label to change it's text when the user scrolls to a different page? I have looked at a lot of examples including custom delegates, but I just cant seem to find a good solution.
As a side note, the titles to be displayed are in an array so objectAtIndex:page - 1
will be perfect once I can find a way to tell the label (and any others that might be displayed in the future) to update.
Why not give this a try:
#define ScrollObjWidth 320.0f // Note: Use what size you need!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView; {
float roundedValue = round(scrollView.contentOffset.x / ScrollObjWidth);
_label.text = [NSString stringWithFormat:@"%d", ((int)roundedValue - 1)];
}
Make sure that your UIViewController
is a UIScrollViewDelegate
, and that the UIScrollView
has it's delegate
property set to the UIViewController
.
Hope that Helps!