Recipe: Scroll Snapping

Recipe 10-4 focuses user attention at the center of the screen. Why not ensure that the central object moves to the most optimal position? Accomplish this by implementing a layout method that snaps to specific boundaries. Recipe 10-5 shows how.

The targetContentOffsetForProposedContentOffset: method, which is called during scrolling, specifies where the scroll would naturally stop. It iterates through all the onscreen objects, finds the one closest to the view’s horizontal center, and adjusts the offset so that the object’s center coincides with the view’s.

Recipe 10-5 Customizing the Target Content Offset

- (CGPoint)targetContentOffsetForProposedContentOffset:         (CGPoint)proposedContentOffset ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.