自定义 UIScrollView 动画

发布于 2024-12-20 13:40:40 字数 544 浏览 1 评论 0原文

我正在使用 UIScrollView 和一个看起来很像 UIPickerView 的按钮列表。我刚刚实现了摇动来随机播放,在检测到摇动时,我使用以下命令将 UIScrollView 的内容偏移设置为 randomShuffle 的位置。

[Singlescroll setContentOffset:CGPointMake(jumpX, jumpY+randShuffle*sepValue) animated:YES];

我不想仅仅将内容偏移移动到与按钮匹配的随机位置,而是想实现一个随机播放动画,其中视图几乎像老虎机一样“旋转”,然后最终到达 randomShuffle 按钮位置。

我尝试通过简单地将偏移设置为 UIScrollView 顶部的动画,然后在返回 randomShuffle 位置(一个接一个)之前再次向下返回,但这不起作用,它只是直接转到 randomShuffle 位置。我意识到我没有尝试使用计时器,因此动画没有延迟,但如果可能的话,我想避免运行计时器。

有没有内置动画可以处理这个问题?如果没有,请您建议我在使用计时器之前如何解决这个问题?谢谢。

I am using a UIScrollView with a list of buttons made to look much like a UIPickerView. I have just implemented shake to shuffle, where upon detection of a shake I set the content offset of the UIScrollView to the position of the randomShuffle using the following.

[Singlescroll setContentOffset:CGPointMake(jumpX, jumpY+randShuffle*sepValue) animated:YES];

Instead of just moving the content offset to the random position that matches a button I would like to implement a shuffle animation where the view almost 'spins' like a slot machine and then ends up on the randomShuffle button position.

I tried to do this by simply animating the offset to the top of the UIScrollView then back down again before going back to the randomShuffle position (one after another), however this didn't work and it just went straight to the randomShuffle position. I realise that I didn't try this with a timer so the animations were not delayed however I would like to avoid running a timer if possible.

Is there any inbuilt animations that can handle this? and if not please can you suggest how I might approach this before using timers? thank you.

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

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

发布评论

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

评论(1

雨巷深深 2024-12-27 13:40:40

您是否尝试将其放入以下内容中而不是仅使用动画:是:

[UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    [Singlescroll setContentOffset:maximumOffsetPoint]; //spin all the way up?
}completion:^(BOOL finished){
    if (finished)
        //kick off another animation, to spin it back down:
        [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            [Singlescroll setContentOffset:CGPointMake(jumpX, jumpY+randShuffle*sepValue)];
        }completion:nil];
    }
}];

我不知道这是否能回答您的问题,但完成块非常方便,可能就是您所需要的。在完成块内,第二个动画只会在第一个动画结束时调用。

另外,UIView 上有几种 animateWithDuration: 方法,但根据我的经验,这个方法对于滚动视图的 contentOffset 和 ZoomToRect 等动画属性更可靠。

Instead of just using animated:YES, did you try putting it into something like:

[UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    [Singlescroll setContentOffset:maximumOffsetPoint]; //spin all the way up?
}completion:^(BOOL finished){
    if (finished)
        //kick off another animation, to spin it back down:
        [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            [Singlescroll setContentOffset:CGPointMake(jumpX, jumpY+randShuffle*sepValue)];
        }completion:nil];
    }
}];

I don't know if this answers your question, but completion blocks are super handy and might be what you need. Within the completion block, the second animation will only get called at the end of the first animation.

Also, there are several animateWithDuration: methods on UIView, but in my experience this one is more reliable for animating properties such as contentOffset and zoomToRect of scroll views.

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