-->

Programmatically Control UIScrollView

2020-08-04 12:55发布

问题:

Is there a method of programmatically 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 programmatically?

回答1:

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!



回答2:

You can scrolll the UIScrollView using

scrollView.contentOffset = CGPointMake(x, y);

If you want the scroll to be animated use UIview Animation

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
scrollView.contentOffset = CGPointMake(x, y);
[UIView commitAnimations];

To get the the content offset create a CGPoint to get the offset value:

CGPoint value;
value = scrollview.contentOffset;


回答3:

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];

         }
      }
   }


回答4:

Yep. Take a look at this posting and let me know if it helps: http://jason.agostoni.net/2011/02/12/ipad-resize-view-to-account-for-keyboard/

It is specifically for dealing with the keyboard but I explain in there how to get the scroll position and set it, etc. With the help of Apple's documentation, of course.