使用图像倒计时

发布于 2024-11-10 06:09:43 字数 760 浏览 1 评论 0 原文

我目前有一个应用程序,它将显示分配给用户执行特定操作的时间。目前很简单,使用 UILabel 来显示剩余时间。

我想对界面进行一些修饰,并显示图形倒计时,这对用户来说更有吸引力。

我不确定如何实现这一点,但我的理论如下: 我知道我倒数的最大值是 999。所以我可以创建 3 个 UIImageView 来表示每个数字。 当前,计时器每秒更新剩余倒计时值,这又将 UILabel 设置为该值。理论上,我可以将此字符串拆分为其组成数字(或循环字符串字符),并根据传入的数字将“数字”图像加载到相应的 UIImageView 中。

我的问题:

  1. 我提到的方法会吗是 有效吗?

  2. 考虑到这是否是最好的方法 情况如何?

  3. 如何为图像变化设置动画 超过?理想情况下我想要什么 从中滑入的新图像 图像视图的顶部,而 正在替换的图像滑出 从底部。这还算吗 可能吗?

只是为了澄清,我并不是在寻找有人为我编写代码,只是在寻找有关我的实际实现的一些指示。

非常感谢您花时间阅读这篇文章。

编辑,看来这里的帖子 如何对更改 UIImageView 中的图像? 帮助我对实际图像进行动画处理。但我仍然希望获得有关我提议的实施的任何建议。

I currently have a application which will display the time allocated to a user to perform a certain action. Currently it is simple, using a UILabel to display the time remaining.

I would like to add a bit of polish to the interface, and display a graphical countdown, which would be more compelling to the user.

I am not sure how to accomplish this, but my theory is as follows:
I know the maximum value that I count down from is 999. So I could create 3 UIImageViews to represent each digit.
A timer currently updates the remaining countdown value every second, which in turn sets the UILabel to the value. I could in theory, split this string into its component digits (or loop over the strings characters), and load a 'digit' image into the corresponding UIImageView based on the digit passed in.

My questions:

  1. Would the approach I mention be
    efficient?

  2. Is it the best approach given the
    situation?

  3. How could I animate the image change
    over? Ideally what I would like its
    the new image to slide in from the
    top of the image view, while the
    image it is replacing slides out
    from the bottom. Is this even
    possible?

Just to clarify, I am not looking for someone to write the code for me, just looking for a few pointers around my actual implementation.

Many thanks for taking the time to read this post.

EDIT, it appears that the post here How to animate the change of image in an UIImageView? helps me with animating the actual images. But I would still like to gain any suggestions on my proposed implementation.

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

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

发布评论

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

评论(2

牛↙奶布丁 2024-11-17 06:09:43
  1. 高效的?我认为你的方法是正确的。每 1000 毫秒动画 3 个相对较小的图像视图对于处理器来说并不是很多工作。但效率/性能问题始终必须凭经验来回答(我认为)。
  2. 最好的? - 如果它能工作、执行、不崩溃等,您还需要什么?
    我能想到的唯一其他方法是提供背景图像,并仍然对数字使用动态 UILabel。

在这一点上,我会说去做吧,看看你会得到什么。
祝你好运。

  1. Efficient? I think your approach is sound. Animating 3 relatively small imageviews every 1000 milliseconds is not a lot of work for the processor. But the efficient/performance question always has to be answered empirically (imo).
  2. Best? - If it works, performs, doesn't crash, etc. what more do you need?
    The only other approach I can think of is to supply a background image, and still use a dynamic UILabel for the digit.

At this point, I'd say go for it, and see what you get.
Good luck.

暖心男生 2024-11-17 06:09:43

你的基本方法是正确的。需要记住的一些事情:

对于倒计时器,不要减少 NSTimer 中的剩余时间。相反,请始终使用“现在减去开始时间”。如果系统繁忙,NSTimer 可能不会按计划触发。阅读 NSTimer 了解它们是如何工作的。

对于这样的问题,我喜欢用诸如 digital0.png、digit1.png 等名称来命名我的文件。然后,您可以像这样加载它们:

NSString *imageName = [NSString stringWithFormat:@"digit%d.png", digit];
self.imageView.image = [UIImage imageNamed:imageName];

+imageNamed: 有自己的缓存,所以这是相当有效的。或者,您可以使用早期加载的 10 张图像的数组,并使用 objectAtIndex: 来获取正确的图像。我通常只是用 +imageNamed: 来做。

滑入新图像没有问题,但您不应该为此使用 UIImageView 。您可能应该使用两个 CALayers 创建一个自定义视图,并使用当前图像和下一个图像加载它们,并使用简单的动画从一个视图滑动到另一个视图。从核心动画编程指南,网上也有一些教程。这是一个学习Core Animation的优秀小项目。

Your basic approach is sound. Some things to keep in mind:

For a countdown timer, don't decrement the time left in an NSTimer. Instead, always use "now minus the start time." NSTimer may not fire on schedule if the system is busy. Read "Repeating Versus Non-Repeating Timers" in NSTimer to understand how they work.

For a problem like this, I like to name my files with names like digit0.png, digit1.png, etc. Then, you can load them like this:

NSString *imageName = [NSString stringWithFormat:@"digit%d.png", digit];
self.imageView.image = [UIImage imageNamed:imageName];

+imageNamed: has its own caching, so this is quite efficient. Alternately, you can use an array of 10 images that you load early on and use objectAtIndex: to get the right one. I just usually do it with +imageNamed:.

Sliding a new image in is no problem, but you shouldn't use UIImageView for that. You should probably make a custom view with two CALayers that you load with the current and next image, and use a simple animation to slide from one to the other. Start with the Core Animation Programming Guide, and there are several tutorials around the web as well. This is an excellent small project for learning Core Animation.

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