UIView Animaton 重置为开始

发布于 2024-09-28 08:59:01 字数 547 浏览 3 评论 0原文

我正在尝试运行动画,以便滚动视图的 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];

有什么想法吗?

I'm attempting to run an animation so that the scrollview's contentoffset gets continually scrolling down.

However, after each repeat, it will animate from the original position and it is not progressing.

[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];

any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2024-10-05 08:59:01

您为什么尝试手动滚动视图?不会用这个方法吗

- (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];

Why are you attempting to manually scroll the view? Can't you use the method

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

?

Regardless, I believe the reason your animation isn't working as you expect is because you're setting a repeat count instead of actually re-rendering your animation each time. If you want to work around this problem, you could animate again in the animation callback by creating a method wrapping the animation and passing parameters via the context pointer.

- (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]
}

Then in your code, just call

[self animateContentOffsetByAmount:50 numberRemaining:100];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文