1

I have a UIScrollView with set of images with left and right button. I have implement the left button action for moving the image left side using this code

 if ( scroll.contentOffset.x <= scroll.frame.size.width ) {
        CGRect frame;
        frame.origin.x = scroll.contentOffset.x + scroll.frame.size.width;
        frame.origin.y = 0;
        frame.size = scroll.frame.size;
         //NSLog(@"%f %f %f", frame.origin.y ,scroll.frame.size.width, frame.origin.x);
        [scroll scrollRectToVisible:frame animated:YES];
    }

its working perfectly

same i implement the functionality for right button action not working properly what i am doing wrong the code is below

if (  scroll.frame.size.width <= scroll.contentOffset.x ) {
        CGRect frame;
        frame.origin.x = scroll.contentOffset.x - scroll.frame.size.width;
        frame.origin.y = 0;
        frame.size = scroll.frame.size;

        [scroll scrollRectToVisible:frame animated:YES];
    }
Ajumal
  • 1,048
  • 11
  • 33
Ben10
  • 3,221
  • 2
  • 34
  • 61
  • You can refer this link http://stackoverflow.com/questions/2234875/programmatically-scroll-a-uiscrollview – iPhoneDev Mar 31 '15 at 07:28
  • I assume the scrollview is horizontal, horizontal means changing the x, and here you are setting the X zero and calculating Y and also you are checking the y position in IF – Raheel Sadiq Mar 31 '15 at 07:28
  • @RaheelSadiq yes I am using horizontal scroll i update my code.but when i click right button its fully scroll – Ben10 Mar 31 '15 at 07:37
  • in your code, code execute inside the if condition block ? – Dipen Chudasama Mar 31 '15 at 07:46

6 Answers6

2

it should be opposite of the left, check: if it is greater than, and subtracting the scrollview width from the current x position

if ( scroll.contentOffset.x >= scroll.frame.size.width ) {
    CGRect frame;
    frame.origin.x = scroll.contentOffset.x - scroll.frame.size.width;
    frame.origin.y = 0;
    frame.size = scroll.frame.size;
     //NSLog(@"%f %f %f", frame.origin.y ,scroll.frame.size.width, frame.origin.x);
    [scroll scrollRectToVisible:frame animated:YES];
}
Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54
1

MMm.. I would suggest you to do this in another way. Create two button and separate action method rightArrowNavigation, leftArrowNavigation . Add a global int value here in my code currentPage , and in viewDidLoad assign 0. See below

- (IBAction)rightArrowNavigation:(id)sender {

    CGRect bounds = self.scrollView.bounds;
    bounds.origin.x = CGRectGetWidth(bounds) * (currentPage + 1);
    [self.scrollView scrollRectToVisible:bounds animated:YES];
}

- (IBAction)leftArrowNavigation:(id)sender {

    CGRect bounds = self.scrollView.bounds;
    bounds.origin.x = CGRectGetWidth(bounds) * (currentPage - 1);
    [self.scrollView scrollRectToVisible:bounds animated:YES];
}
Ajumal
  • 1,048
  • 11
  • 33
1

For right button, why are you checking for contentOffset.y? It should be contentOffset.x, also is there paging enabled?

Use this, I think it will solve

if ( _scroll.contentOffset.x >= _scroll.frame.size.width/2 ) {
        CGRect frame;
        frame.origin.x = _scroll.contentOffset.x - _scroll.frame.size.width;
        frame.origin.y = 0;
        frame.size = _scroll.frame.size;
        //NSLog(@"%f %f %f", frame.origin.y ,scroll.frame.size.width, frame.origin.x);
        [_scroll scrollRectToVisible:frame animated:YES];
    }
Uttam Sinha
  • 722
  • 6
  • 9
0

Th left side functioanlity is correct whereas for the right side functionality assuming you want to create horizontal scroller, you should work with x origin and width. Your conditions and code for right side should be based on x origin, width, basically all those parameters which are responsible for horizontal scrolling

Sanjay Mohnani
  • 5,947
  • 30
  • 46
0

With scrollview paging enabled. If you don't want paging enabled try some more mathematics.

(IBAction)leftButtonClicked:(id)sender {

      if (self.iTemScrollView.contentOffset.x <
self.iTemScrollView.contentSize.width - self.iTemScrollView.frame.size.width){           
    [self.iTemScrollView setContentOffset:CGPointMake(self.iTemScrollView .contentOffset.x + self.iTemScrollView .frame.size.width, 0) animated:YES];
    }

}

(IBAction)rightButtonClicked:(id)sender {

    NSLog(@" rightbutton self.iTemScrollView.contentOffset.x %f",self.iTemScrollView.contentOffset.x);
    NSLog(@"rightbutton self.iTemScrollView.contentOffset.y %f",self.iTemScrollView.contentOffset.y);

    if (self.iTemScrollView.contentOffset.x>0){
    [self.iTemScrollView setContentOffset:CGPointMake(self.iTemScrollView .contentOffset.x - self.iTemScrollView .frame.size.width, 0) animated:YES];
    }

}
bummi
  • 27,123
  • 14
  • 62
  • 101
Chandramani
  • 871
  • 1
  • 12
  • 11
0
-(IBAction)leftScroll:(id)sender 
      {
  //page variable initialize to 0 in view did load method
      if(page!=0)
       {
         CGRect frame = scrollEvent.frame;
         frame.origin.x = frame.size.width * (--page);
         frame.origin.y = 0;
         [scrollEvent scrollRectToVisible:frame animated:YES];
       }
  }
- (IBAction)rightScroll:(id)sender 
     {

         if(arrData.count>page)
          {
           CGRect frame = scrollEvent.frame;
           frame.origin.x = frame.size.width * (++page);
           frame.origin.y = 0;
           [scrollEvent scrollRectToVisible:frame animated:YES];
          }
      }
Baljeet Singh
  • 453
  • 5
  • 15