2013-05-30 5 views
1

스크롤 버튼을 사용하여 UIScrollView을 만들고 싶습니다. 사용자가 왼쪽 화살표 버튼을 누르면 스크롤이 제대로 스크롤되어야합니다.스크롤 버튼이있는 UIScrollView

문제는 : 버튼을 3 번 빨리 누르면 스크롤이 제대로 스크롤되지 않습니다 (많은 전화가 scrollRectToVisible이므로). 다음 애니메이션 전에 현재 애니메이션을 멈출 수 있습니까?

P. 내가 [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]; 
    } 
} 
+0

'setContentOffset : animated : YES' 메서드를 사용해 볼 수 있습니다. – danypata

+0

나는 (주석 코드에서 볼 수있다) 시도했지만, 더 악화되다. –

+0

당신은 항상'contentOffset' 속성을 애니메이션으로 만들고'UIViewAnimationOptionBeginFromCurrentState' 옵션을 사용할 수있다. – Mar0ux

답변

1

당신이 할 수있는 항상 contentOffset 속성에 애니메이션을 적용하고 UIViewAnimationOptionBeginFromCurrentState 옵션을 사용하십시오. 두 번째 애니메이션이 시작되면 바로 첫 번째 애니메이션이 끝나고 현재 상태 옵션을 사용하여 두 번째 애니메이션이 첫 번째 애니메이션이 시작된 지점부터 시작됩니다.

0

몇 가지 제안 :

1) 당신은 정말 그 스크롤하는 동안 버튼의 사용자 망치를 원하는가

? 그렇다면 UI 디자인을 다시 디자인해야 할 수도 있습니다.

2) 동작 방법에서 UI를 혼란스럽게하는 경우 코드가있는 블록을 기본 대기열로 보내어 다른 UI 작업을 게시하는 것이 가장 좋습니다. 버튼 hiliting이 더 좋아 보입니다.

3) 특정 경우에 조치 방법에서 버튼을 사용 중지 한 다음 스크롤이 중지되었을 때 다시 사용 가능하게 설정할 수 있습니다.