使用图像序列制作开关动画
我想知道如何解决我遇到的问题。我有一个图像序列,基本上可以对开关进行动画处理。
我想在 iOS 中实现此功能。你会如何建议我去做呢?也许使用 UIButton?或者更确切地说是 UISwitch?
I would like to know how to solve a problem I'm having. I have an image sequence that basically animates a switch.
I'd like to implement this in iOS. How would you advise me to go for that? Maybe using UIButton? Or rather UISwitch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于一个相当简单的解决方案(点击开关,无法滑动),我建议您使用堆叠的两个 UIView。
-1- 在底部,使用 UIImageView 来运行图像动画。
-2- 在顶部,使用
UIControl
来捕获 touché-up-inside 事件。或者,您可以使用
UITapGestureRecognizer
代替 -2-。仅当生成的控件不用于 iOS <> 时才应考虑后一个选项。 3.2.一旦您的点击识别器(
UIControl
或UITapGestureRecognizer
)收到点击事件,您只需播放通过UIImageView
提供的动画即可。有关使用 UIImageView 的动画的更多详细信息,请参阅其类参考,特别是诸如
animationImages
(图像数组)之类的内容。由于 UIImageView 不为您提供反向动画功能,因此您必须在动画回到开始之前以相反的顺序分配图像(即在用户打开之后,现在切换回关闭)。
对于更复杂的解决方案(点击开关,可以滑动),您可以将
UISwipeGesture Recognizer
添加到自定义控件,并使用该UIGestureRecognizer
的状态来选择当前可见的UIImageView
的图像。For a rather simple solution (tap switches, no swiping possible), I would advise you to use two UIViews stacked.
-1- At the bottom, use a
UIImageView
which allows you to run image animations.-2- At the top, use a
UIControl
which will capture the touché-up-inside events.Alternatively, you could use a
UITapGestureRecognizer
as a replacement for -2-. The latter option should only be considered if the resulting control will not be used for iOS < 3.2.As soon as your tap-recognizer (
UIControl
orUITapGestureRecognizer
) receives the tap-event, you simply play the animation provided through yourUIImageView
.For more details on animations using UIImageView see its class reference, specifically on things like
animationImages
(an array of images).As UIImageView does not provide you with a reverse animation feature, you will have to assign the images in the reverse order before animating back to the start (that is after a user switched it on and now switches back to off).
For a more complex solution (tap switches, swiping is possible), you may add a
UISwipeGesture Recognizer
to your custom control and use the status of thatUIGestureRecognizer
to select the currently visible image of yourUIImageView
.iOS 5 允许您制作动画 UIImage。您可以将动画 UIImage 直接放入 UIButton 中,并在按下按钮时为其设置动画。 动画 UIImage
iOS 5 allows you to make animated UIImages. You can put the animated UIImage right into the UIButton and animate it on button presses. Animated UIImage