reusable - Programatically Control UIScrollView




swift programmatically (4)

Is there a method of programatically controlling a UIScrollView? As in, getting a value for its location, so how far along it is scrolled and things and change its position, scroll it programatically?

Hope you can help, thanks.


I don't know what either of @Alexandergre or @JAgostoni are talking about. Scrolling in UIScrollView is really easy.

I suggest you take a look at the documentation for UIScrollView: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html

To scroll a UIScrollView, simply use

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

Example:

[myScrollView scrollRectToVisible:CGRectMake(20, 20, 320, 480) animated:YES];

That will scroll a UIScrollView with animation to a 20 on the x axis, 20 on the y and 320x480 height and width.

If you want to get information of the view it self(for example what frame is visible) you should use methods and properties from UIView, as it's UIScrollView's parent. Check out the UIView documentation at: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html

To get the frame that is visible, use

myScrollView.frame;

Do not mix it up with the content size

myScrollView.contentSize;

But, as i said. Use the documentation!



Try this :-

 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView  willDecelerate:(BOOL)decelerate{
    //NSLog(@"%f %f", scrollView.contentOffset.y,  scrollView.contentSize.height);
 }
 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
     //NSLog(@"scrollViewDidEndDecelerating");
 }
 -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
     //NSLog(@"scrollViewDidEndScrollingAnimation");
 }
//Update the value of rol
 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
     if(scrollView==MainScrollVw){//Main Scroll View All Fns Start,,,
         int rol_y= MainScrollVw.contentOffset.y;
         //int rol_x= MainScrollVw.contentOffset.x;

         if(rol_y>0){
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.30];
            MainMenuVw.frame= CGRectMake(0 ,0 ,self.view.frame.size.width ,75);
            [UIView commitAnimations];

         }else{
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.30];
            MainMenuVw.frame= CGRectMake(0 ,0 ,self.view.frame.size.width ,150);
            [UIView commitAnimations];

         }
      }
   }