UIScrollView with left and right arrows in IOS

2019-08-19 07:54发布

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

6条回答
干净又极端
2楼-- · 2019-08-19 07:56

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];
}
查看更多
淡お忘
3楼-- · 2019-08-19 07:56

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];
    }
查看更多
神经病院院长
4楼-- · 2019-08-19 08:03

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];
}
查看更多
你好瞎i
5楼-- · 2019-08-19 08:06

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

}
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-08-19 08:12

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

查看更多
Juvenile、少年°
7楼-- · 2019-08-19 08:13
-(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];
          }
      }
查看更多
登录 后发表回答