带滚动按钮的UIScrollView问题

时间:2013-05-30 10:56:31

标签: iphone ios uiscrollview uiscrollviewdelegate

我想用滚动按钮创建UIScrollView。所以当用户按左箭头按钮时,滚动必须正确滚动。

问题是:当我单击按钮3次快速滚动无法正确滚动(因为多次调用scrollRectToVisible)。可能我可以在下一个动画之前停止当前动画吗?

P.S。如果我设置[self scrollScrollViewToIndex:index animated:NO]一切正常,但我需要动画

这是我的代码:

- (void)scrollScrollViewToIndex:(int)index animated:(BOOL)animated
{
    NSLog(@"scrolled to index: %d", index);
    CGFloat offsetX = CGRectGetWidth(_scrollMain.frame) * index;
    CGRect scrollRect = CGRectMake(offsetX, 0, CGRectGetWidth(_scrollMain.frame), CGRectGetHeight(_scrollMain.frame));    
    [_scrollMain scrollRectToVisible:scrollRect animated:animated];
//    [self.scrollMain setContentOffset:CGPointMake(offsetX, 0) animated:animated];
}

- (IBAction)leftArrowPressed:(id)sender
{
    int indexOfVoucher = [_arrayVouchers indexOfObject:_voucher];
    indexOfVoucher--;
    self.voucher = [_arrayVouchers objectAtIndex:indexOfVoucher];
    [self updateViewWithVoucherWithScrolling:YES];
}

- (IBAction)rightArrowPressed:(id)sender
{
    int indexOfVoucher = [_arrayVouchers indexOfObject:_voucher];
    indexOfVoucher++;
    self.voucher = [_arrayVouchers objectAtIndex:indexOfVoucher];
    [self updateViewWithVoucherWithScrolling:YES];
}

- (void)updateViewWithVoucherWithScrolling:(BOOL)withScrolling
{
    int indexOfVoucher = [_arrayVouchers indexOfObject:_voucher];
    _leftArrowButton.hidden = _rightArrowButton.hidden = NO;
    if (indexOfVoucher == 0)
    {
        _leftArrowButton.hidden = YES;
    }
    else if (indexOfVoucher == [_arrayVouchers count] - 1)
    {
        self.rightArrowButton.hidden = YES;
    }
    if (withScrolling)
    {
       [self scrollScrollViewToIndex:indexOfVoucher animated:YES]; 
    }
}

更新 根据Mar0ux的建议工作代码

- (void)scrollScrollViewToIndex:(int)index animated:(BOOL)animated
{
    NSLog(@"scrolled to index: %d", index);
    CGFloat offsetX = CGRectGetWidth(_scrollMain.frame) * index;

    if (animated)
    {
        [UIView animateWithDuration:0.5
                              delay:0.0
                            options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState //Multiple options
                         animations:^ {
                             //                         [self.scrollMain setContentOffset:CGPointMake(offsetX, 0) animated:NO];
                             CGRect scrollRect = CGRectMake(offsetX, 0, CGRectGetWidth(_scrollMain.frame), CGRectGetHeight(_scrollMain.frame));
                             [_scrollMain scrollRectToVisible:scrollRect animated:NO];
                         }
                         completion:^ (BOOL finished) {

                         }];
    }
    else
    {
        CGRect scrollRect = CGRectMake(offsetX, 0, CGRectGetWidth(_scrollMain.frame), CGRectGetHeight(_scrollMain.frame));
        [_scrollMain scrollRectToVisible:scrollRect animated:NO];
    }
}

2 个答案:

答案 0 :(得分:1)

您可以随时自行设置contentOffset属性的动画,并使用UIViewAnimationOptionBeginFromCurrentState选项。第二个动画开始后,第一个动画将结束,并且通过使用当前状态选项,第二个动画将从第一个动画开始的位置开始。

答案 1 :(得分:0)

一些建议:

1)你真的希望用户在滚动时敲击按钮吗?如果是这样,那么我建议您的UI设计可能需要重新设计。

2)当你在动作方法中扰乱UI时,最好通过将带有代码的块调度到主队列来发布其他UI动作 - 按钮hiliting看起来会更好。

3)在您的特定情况下,您可以在操作方法中禁用该按钮,然后在滚动停止时重新启用它。