스크롤 버튼을 사용하여 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];
}
}
'setContentOffset : animated : YES' 메서드를 사용해 볼 수 있습니다. – danypata
나는 (주석 코드에서 볼 수있다) 시도했지만, 더 악화되다. –
당신은 항상'contentOffset' 속성을 애니메이션으로 만들고'UIViewAnimationOptionBeginFromCurrentState' 옵션을 사용할 수있다. – Mar0ux