如何在 xcode 中在选定和未选定状态之间交叉淡入淡出按钮?
我有两个按钮图像:一张用于正常状态,一张用于选定状态。 当我从选定状态更改回正常状态时,我希望在几秒钟内以交叉淡入淡出效果发生这种情况。
到目前为止,我正在做的是使用两个 UIView 动画:第一个动画在选定状态下从 alpha 1.0 变为 alpha 0.5。然后我切换到正常状态并执行第二个 UIView 动画,从 alpha 0.5 到 alpha 1.0。
我对视觉效果(从选定图像突然过渡到正常图像)不满意。我还读到不应再使用 UIView。那么这里正确的方法是什么?代码示例也非常有用。
I have two images for a button: one for the normal state, and one for the selected state.
When I change from the selected state back to the normal state, I would like this to happen with a crossfade effect over a couple of seconds.
What I am doing so far is using two UIView animations: the first one goes from an alpha of 1.0 to an alpha of .5 in the selected state. Then I switch to the normal state and perform a second UIView animation going from an alpha of .5 to an alpha of 1.0.
I am not thrilled with the visual effect (abrupt transition from selected to normal image). I also read that UIView should no longer be used. So what is the right approach here? A code sample would be very useful too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我来说,这有效:
但是,当我将相同的
UIImage
设置为UIControlStateHighlighted
和UIControlStateSelected
时,这仅才有效三个以下电话(留下一个电话对我来说不起作用):For me this worked:
But this only worked when I set the same
UIImage
toUIControlStateHighlighted
andUIControlStateSelected
with those three following calls (leaving one call did not work for me):下面很简单,4秒内就会从选中状态转变为未选中状态。唯一的问题是,当按钮同时移动时,过渡效果不佳。
The following is quite simple and will transition from the selected to not selected state in 4 seconds. The only problem is that the transition doesn't work well when the button is also moving at the same time.
我终于找到了我需要的东西:我没有将图像分配给按钮的选定和非选定状态,而是保持按钮透明,并向该按钮添加两个视图,最初的 alpha 值为 1.0 和 0.0。
当选择按钮并输入选择器中指定的方法时,我使用动画在这两个视图之间进行转换,如下所示:
如果按钮在转换期间移动,则此方法也适用。我希望这可以帮助其他人将来面临同样的问题!
I finally found what I needed: instead of assigning images to the selected and non-selected states of my button, I keep my button transparent and add instead two views to that button, initially with an alpha of 1.0 and 0.0.
When the button is selected and I enter the method specified in the selector, I use an animation to transition between these two views as follows:
This approach also works if the button is moving during the transition. I hope this helps others facing the same question in the future!