我正在尝试运行动画,以便scrollview的contentoffset不断向下滚动。
然而,在每次重复之后,它将从原始位置动画,并且它没有进展。
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatCount: 100];
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.y += 50;
scrollView.contentOffset = contentOffset;
scrollView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
任何想法?
答案 0 :(得分:1)
为什么要尝试手动滚动视图?你不能使用方法
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
无论如何,我相信您的动画无法正常工作的原因是因为您设置了重复计数而不是每次都实际重新渲染动画。如果要解决此问题,可以通过创建包装动画并通过上下文指针传递参数的方法,在动画回调中再次设置动画。
- (void) animateContentOffsetByAmount:(CGFloat)amount numberRemaining:(int)num {
if (num == 0) return;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:
@selector(scrollAnimationStoppedWithID:finished:context:)];
//NSArray released in callback
NSArray* contextArray = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:amount],
[NSNumber numberwithInt:num], nil]
[UIView beginAnimations:nil context:contextArray];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.y += amount;
scrollView.contentOffset = contentOffset;
scrollView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
- (void) scrollAnimationStoppedWithID:(NSString*)id
finished:(NSNumber*)finished context:(void*)context {
NSArray* contextArray = (NSArray*)context;
CGFloat amount = [(NSNumber*)[contextArray objectAtIndex:0] floatValue];
int count = [(NSNumber*)[contextArray objectAtIndex:1] intValue];
count = count - 1;
[contextArray release];
[self animateContentOffsetByAmount:amount numberRemaining:count]
}
然后在你的代码中,只需调用
[self animateContentOffsetByAmount:50 numberRemaining:100];