Wednesday, June 5, 2013

UIScrollView scrollRectToVisible animated example in Objective C (iOS).

UIScrollView scrollRectToVisible animated

Scrolls a specific area of the content so that it is visible in the receiver.

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

A rectangle defining an area of the content view.
YES if the scrolling should be animated, NO if it should be immediate.

Discussion of [UIScrollView scrollRectToVisible animated]
This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing.

UIScrollView scrollRectToVisible animated example.
int page = sidePager.currentPage + 1;
CGRect frame = scroller.frame;
frame.origin.x = frame.size.width * page;

if (0 != UpAndDownPager.currentPage) {

     frame.origin.y = frame.size.height * (UpAndDownPager.currentPage + 1 );

scroller scrollRectToVisible:frame animated:YES];      
sidePager.currentPage = sidePager.currentPage + 1;  

Example of [UIScrollView scrollRectToVisible animated].
[UIView animateWithDuration:3
                 animations:^{ [scrollView scrollRectToVisible:frame animated:NO]; }

UIScrollView scrollRectToVisible animated example.
CGRect rectBottom = CGRectZero;
rectBottom.size = myUIScrollView.frame.size;
rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
rectBottom.origin.x = 0;

[myUIScrollView scrollRectToVisible:r animated:YES];

End of UIScrollView scrollRectToVisible animated example article.