Wednesday, June 5, 2013

UIScrollView scrollRectToVisible example in Objective C (iOS).

UIScrollView scrollRectToVisible

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

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

Parameters of [UIScrollView scrollRectToVisible]
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]
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 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].
[UIView animateWithDuration:3
                 animations:^{ [scrollView scrollRectToVisible:frame animated:NO]; }

UIScrollView scrollRectToVisible 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 example article.