1

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

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

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

2 回答 2

1

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

于 2013-05-30T12:09:10.383 回答
0

几点建议:

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

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

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

于 2013-05-30T12:10:30.570 回答