- Published on
UIScrollView Reference
This article covers only scrolling-related content.
Key UIScrollView Properties
-
tracking: Returns YES when the user has touched but has not yet started dragging. -
dragging: Returns YES when the user has been scrolling for a short time or distance. -
decelerating: Returns YES when the user has lifted their finger but the scroll view is still coasting.
UIScrollView Delegate Methods
-
- (void)scrollViewDidScroll:(UIScrollView *)scrollView: Called whenever thecontentOffsetchanges by any means (user dragging, deceleration, or programmatic changes). Use this to monitorcontentOffsetchanges and adjust other views in response. -
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView: Called when the user begins dragging the scroll view. May be called after a short delay or distance. -
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset: Called when the user lifts their finger.velocityrepresents the scroll speed, with positive/negative values indicating direction (up/down).targetContentOffsetis the position where scrolling will come to rest. Notably,targetContentOffsetis a pointer — you can modify the deceleration destination, which is very useful for implementing certain effects. Access or modify it viatargetContentOffset->y. -
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate: Called after the user finishes dragging. Ifdecelerateis YES, there will be a deceleration phase after the drag ends. Note: afterdidEndDragging, if there is a deceleration phase,scrollView.draggingis not immediately set to NO — it stays YES until deceleration ends. The actual semantic of thedraggingproperty is therefore closer toscrolling. -
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView: Called just before the deceleration animation begins. -
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView: Called when the deceleration animation ends. Special case: if the user drags the scroll view again before a deceleration animation has finished,didEndDeceleratingwill not be called, and at that point bothdragginganddeceleratingare YES. If the new drag has velocity,willBeginDeceleratingwill be called again, followed bydidEndDecelerating. If the new drag has no velocity,willBeginDeceleratingwill not be called, but thedidEndDeceleratingleft over from the previous deceleration will fire. -
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView: Called when a programmatic scroll animation ends (viasetContentOffset(_:animated:)orscrollRectVisible(_:animated:)). This method is called if and only if an animation was involved.
User Scroll Sequence
//User begins touching and dragging
OCDemo[82268:532649] -[ViewController scrollViewWillBeginDragging:]
//Scrolling in progress, called multiple times
OCDemo[82268:532649] -[ViewController scrollViewDidScroll:]
OCDemo[82268:532649] -[ViewController scrollViewDidScroll:]
//User is about to stop dragging
OCDemo[82268:532649] -[ViewController scrollViewWillEndDragging:withVelocity:targetContentOffset:]
//User has stopped dragging
OCDemo[82268:532649] -[ViewController scrollViewDidEndDragging:willDecelerate:]
//Deceleration begins
OCDemo[82268:532649] -[ViewController scrollViewWillBeginDecelerating:]
//Scrolling in progress during deceleration, called multiple times
OCDemo[82268:532649] -[ViewController scrollViewDidScroll:]
OCDemo[82268:532649] -[ViewController scrollViewDidScroll:]
//Deceleration ends
OCDemo[82268:532649] -[ViewController scrollViewDidEndDecelerating:]
User Touches Again During Deceleration
//User begins touching and dragging
OCDemo[82268:532649] -[ViewController scrollViewWillBeginDragging:]
//Scrolling in progress, called multiple times
OCDemo[82268:532649] -[ViewController scrollViewDidScroll:]
OCDemo[82268:532649] -[ViewController scrollViewDidScroll:]
//User is about to stop dragging
OCDemo[82268:532649] -[ViewController scrollViewWillEndDragging:withVelocity:targetContentOffset:]
//User has stopped dragging
OCDemo[82268:532649] -[ViewController scrollViewDidEndDragging:willDecelerate:]
//Deceleration begins
OCDemo[82268:532649] -[ViewController scrollViewWillBeginDecelerating:]
//Scrolling in progress during deceleration, called multiple times
OCDemo[82268:532649] -[ViewController scrollViewDidScroll:]
OCDemo[82268:532649] -[ViewController scrollViewDidScroll:]
//User touches and drags again during deceleration
OCDemo[82406:539968] -[ViewController scrollViewWillBeginDragging:]
//User is about to stop dragging
OCDemo[82406:539968] -[ViewController scrollViewWillEndDragging:withVelocity:targetContentOffset:]
//User has stopped dragging
OCDemo[82406:539968] -[ViewController scrollViewDidEndDragging:willDecelerate:]
//Deceleration ends
OCDemo[82406:539968] -[ViewController scrollViewDidEndDecelerating:]
Miscellaneous
Detecting the user's scroll direction:
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView {
self.lastOffsetY = scrollView.contentOffset.y;
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (scrollView.contentOffset.y > self.lastOffsetY) {
//Scrolling up
}
}