我使用 UISlider 创建了一个视频持续时间线。效果很好;我可以获得 slider 值更改。然后,我尝试向其添加视觉效果(淡入和淡出),因此当用户点击屏幕时, slider 淡入,如果没有其他交互则淡出。
为了实现淡入淡出效果,我在 0-1 之间调整了 alpha。但是,ios 显然不接受 alpha 0.5 以下元素的用户交互。因此,我无法使用普通的 animateWithDuration
实现这一点,因此我尝试添加一个计数器来计算秒数。
(更新: 很明显,我想要实现的是,我有一个全屏视频,我想放置一个 slider 来显示视频时间(当前时间 + 持续时间)。我想将 slider 最初设置为不可见,当用户点击屏幕时, slider 变得可见。然后,用户在 slider 上进行交互。但是,我想让 slider 在上次点击后 5 秒后淡出.)
我试过的...
var durationSlider: UISlider!
var timer = NSTimer()
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
durationSlider.minimumValue = 0
durationSlider.maximumValue = 100
durationSlider.continuous = true
durationSlider.alpha = 0
durationSlider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
self.view.addSubview(durationSlider)
let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
tapGestureRecognizer.numberOfTapsRequired = 1
self.player.view.addGestureRecognizer(tapGestureRecognizer)
}
func scheduledTimerWithTimeInterval(){
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounting"), userInfo: nil, repeats: true)
}
func updateCounting(){
counter = counter + 1
}
到这里为止,一切正常,如果我只使用淡入甚至不使用计数器,它就可以工作。
func handleTapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
UIView.animateWithDuration(0.2, delay: 0.2, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
self.durationSlider.alpha = 1.0
print("B")
}, completion: nil)
}
这里开始问题
如果我尝试在完成中使用淡出,它会直接进入完成闭包内,所以它甚至不会给我一秒钟的时间来与 slider (alpha < 5)进行交互。
UIView.animateWithDuration(0.2, delay: 0.4, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
self.durationSlider.alpha = 1.0
print("B")
}, completion: {completed in
UIView.animateWithDuration(2.0, delay: 10.0, options: [], animations: {
print("A")
self.durationSlider.alpha = 0
}, completion: nil)
})
当然,也尝试将它们分成两个单独的函数,但这会更 swift 地改变打印日志(因此 alpha):
UIView.animateWithDuration(0.2, delay: 0.4, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
self.durationSlider.alpha = 1.0
print("B")
}, completion: nil)
UIView.animateWithDuration(2.0, delay: 10.0, options: [], animations: {
print("A")
self.durationSlider.alpha = 0
}, completion: nil)
最后,我突然想到设置计时器的想法,所以我尝试了类似的方法,但也没有成功。它启动计数器并且它正在上升,但我认为这个逻辑在这里不起作用,因为它永远不会进入那个 if 条件。
func handleTapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
counter = 0
scheduledTimerWithTimeInterval()
UIView.animateWithDuration(0.2, delay: 0.2, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
self.durationSlider.alpha = 1.0
print("B")
}, completion: nil)
if counter > 6 {
UIView.animateWithDuration(2.0, delay: 5.0, options: [], animations: {
print("A")
self.durationSlider.alpha = 0
}, completion: nil)
}
实现我想要实现的目标的最佳方式是什么?通过在 UISlider 上启用用户交互来调整淡入和淡出的正确方法是什么?
最佳答案
这些简单的步骤就可以做到:
当用户点击以显示 slider 时,设置一个五秒计时器(并以相当快的速度设置 slider 的淡入动画,例如 0.3 秒)。 [一个不错的变体可能是淡入 slider 并在动画的完成处理程序中启动计时器。]
在 slider 的 Value Changed 操作中,使计时器无效/取消并创建一个新的五秒计时器。 (如果用户从不更改 slider ,则不会发生此步骤。但这没关系;已经有一个计时器在运行。)
不管怎样,您有一个计时器在运行,它最终会触发(从您执行淡入后五秒,或者从用户上次更改 slider 后五秒)。当计时器触发时,淡出 slider 。全部完成!
关于ios - 启用用户交互的淡入淡出 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34454630/