带有滚动按钮的UIScrollView问题



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

问题是:当我点击按钮3次快速滚动不能正常滚动(因为scrollRectToVisible的许多调用)。也许我可以在下一个动画之前停止当前动画?

注:如果我设置[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];
    }
}

您可以始终自己动画contentOffset属性并使用UIViewAnimationOptionBeginFromCurrentState选项。一旦第二个动画开始,第一个动画将结束,并且通过使用当前状态选项,第二个动画将从第一个动画停止的地方开始。

几点建议:

1)你真的想让用户在滚动的时候敲打按钮吗?如果是这样的话,我建议你的UI设计可能需要重新设计。

2)当你在一个操作方法中扰乱UI时,最好通过将一个带有代码的块发送到主队列来发布其他UI操作-按钮执行将看起来更好。

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

最新更新