IOS中带有左右箭头的UIScrollView

时间:2015-03-31 07:23:59

标签: ios objective-c iphone uiscrollview

我有一个UIScrollView,带有左右按钮的图像集。我已经使用此代码

实现了左侧按钮动作以移动图像左侧
 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];
    }

它的工作完美

同样我实现右键操作功能不正常我正在做错的代码在下面

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 个答案:

答案 0 :(得分:1)

它应该与左边相反,检查:如果它大于,并从当前x位置减去scrollview宽度

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

答案 1 :(得分:1)

MM MM ..我建议你以另一种方式这样做。创建两个按钮和单独的操作方法rightArrowNavigation,leftArrowNavigation。在我的代码currentPage中添加一个全局int值,在viewDidLoad中赋值0.参见下面的

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

答案 2 :(得分:1)

对于右键,为什么要检查contentOffset.y?它应该是contentOffset.x,是否也启用了分页?

使用它,我认为它会解决

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

答案 3 :(得分:0)

左侧功能是正确的,而对于右侧功能,假设您要创建水平滚动条,则应使用x原点和宽度。您的右侧条件和代码应基于x原点,宽度,基本上所有那些负责水平滚动的参数

答案 4 :(得分:0)

启用了scrollview分页。如果您不想启用分页,请尝试更多数学。

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

}

答案 5 :(得分: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];
          }
      }