Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UI按钮使UIScrollView反弹_Ios_Uiscrollview_Uibutton_Bounce - Fatal编程技术网

Ios UI按钮使UIScrollView反弹

Ios UI按钮使UIScrollView反弹,ios,uiscrollview,uibutton,bounce,Ios,Uiscrollview,Uibutton,Bounce,我有一个带分页的水平UIScrollView。有两个按钮。一个向左滚动滚动视图,另一个向右滚动 左边的代码是: - (IBAction)goLeftAction:(id)sender { CGFloat pageWidth = _theScrollView.frame.size.width; int page = floor((_theScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; if(page

我有一个带分页的水平
UIScrollView
。有两个按钮。一个向左滚动
滚动视图
,另一个向右滚动

左边的代码是:

- (IBAction)goLeftAction:(id)sender
{
    CGFloat pageWidth = _theScrollView.frame.size.width;
    int page = floor((_theScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    if(page>0){
        page -=1;
        [_theScrollView scrollRectToVisible:CGRectMake(_theScrollView.frame.size.width*page, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
    }
}
当我们在第一页(page=0)时,我想让按钮使
scrollView
显示一个反弹效果,然后向左推

请帮助找到实现这一目标的方法

编辑:

如果有人需要,这里有代码

首先,我添加到
goLeftAction:

[_theScrollView setPagingEnabled:NO];
[_theScrollView setScrollEnabled:NO];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(bounceScrollView) userInfo:nil repeats:NO];
接下来:

- (void)bounceScrollView
{
    [self.theScrollView scrollRectToVisible:CGRectMake(100, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(unbounceScrollView) userInfo:nil repeats:NO];
}
- (void)unbounceScrollView
{
    [self.theScrollView scrollRectToVisible:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
    [_theScrollView setPagingEnabled:YES];
    [_theScrollView setScrollEnabled:YES];
}

这是一个有趣的问题。你看过这样的电影吗?它垂直反弹,而不是水平反弹,但概念是一样的

  • 将PaginEnabled和scrollEnabled设置为NO
  • 使用scrollRectToVisible:animated:
  • 将PaginEnabled和scrollEnabled设置回YES
  • 还有一些可能有用的示例代码

    编辑:
    我在上面的编辑中对代码进行了修改,并设法使反弹朝着正确的方向运行。我不得不使用
    setContentOffset:animated:
    而不是
    scrollRectToVisible:animated:
    ,并且我还将计时器间隔增加到了0.3(在调用
    UnbouncScrollView
    之前,0.1的时间不足以使弹跳达到完整距离)

    我在高尔夫球场的代码::

    [self.scrollView setPagingEnabled:NO];
    [self.scrollView setScrollEnabled:NO];
    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(bounceScrollView) userInfo:nil repeats:NO];
    
    反弹方法:

    - (void)bounceScrollView
    {
        [self.scrollView setContentOffset:CGPointMake(-100, 0) animated:YES];
        [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(unbounceScrollView) userInfo:nil repeats:NO];
    }
    - (void)unbounceScrollView
    {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        [self.scrollView setPagingEnabled:YES];
        [self.scrollView setScrollEnabled:YES];
    }
    

    是的,当设置分页并滚动到否时,我得到了反弹。但它显示镜像反弹。它反弹到另一边:)谢谢@AlexanderZ请参见上面的编辑。这段代码应该让你的反弹朝着正确的方向运行。太棒了!非常感谢。