简体   繁体   English

Objective-C:ScrollView左右滚动

[英]objective-c: ScrollView scroll to left & right direction

How can i scroll my scrollview to a specific point in both left & right directions.I have tried with the following piece of code but it is entering into infinite loop. 我如何在左右两个方向上将我的滚动视图滚动到特定点。我尝试了以下代码,但是它正在进入无限循环。

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    if (xCoord > scrollView.contentOffset.x )
    {
        scrollView.contentOffset = CGPointMake(xCoord + 160, 0);
    }
    else if(xCoord < scrollView.contentOffset.x )
    {
        scrollView.contentOffset = CGPointMake(xCoord - 160, 0);
    }

    xCoord = scrollView.contentOffset.x;
}

I just wanted scroll my scrollview by 160inches in both directions whenever user drags the screen. 每当用户拖动屏幕时,我只想在两个方向上将我的滚动视图滚动160英寸。 In my case i have few labels on scrollview, when i scroll the scrollview it has to scroll specific to label width(160) . 就我而言,我在scrollview上的标签很少,当我滚动scrollview时,它必须滚动到特定于label width(160)的位置。

Try these, 试试这些

  1. Set your scrollview contentSize (should be higher than the frame ) 设置您的scrollview contentSize (应该大于frame

    self.scrollView.contentSize = CGSizeMake(320, 900); self.scrollView.contentSize = CGSizeMake(320,900);

  2. Uncheck the autoLayout checkbox in the xib 取消选中autoLayout复选框在厦门国际银行

  3. Assign the delegate and setScrollEnabled property to true. 将委托和setScrollEnabled属性分配为true。

  [self.scrollView setDelegate:self]; 
> [self.scrollView setScrollEnabled:YES];

Note: Only set your initial visible area as frame and total content size as contentSize . 注意:仅将初始可见区域设置为frame ,将总内容大小设置为contentSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM