当前位置: 首页 > 知识库问答 >
问题:

UI的调度信号量

慕宏博
2023-03-14

我想让uiviews的alpha动画化,但我似乎做不到。它的行为怪异,并表示错误的运行UI更改不推荐在后台线程上运行,但我不知道如何使它运行在主线程上。谁能帮帮我吗?我相信它跳过了第一个uiview.animate块,并在没有任何动画的情况下执行第二个块中的内容。

函数animateSemaphore(){

circleRed.alpha = 0.2
circleOrange.alpha = 0.2
circleGreen.alpha = 1

let dispatchSemaphore = DispatchSemaphore(value: 0)
let dispatchQueue = DispatchQueue.global(qos: .background)

dispatchQueue.async {
    UIView.animate(withDuration: 0.5, delay: 5, options: .curveEaseInOut) {
        self.circleOrange.alpha = 1
        self.circleGreen.alpha = 0.2
    } completion: { (_) in
        print("1")
        dispatchSemaphore.signal()
    }
    
    dispatchSemaphore.wait()
    UIView.animate(withDuration: 0.5, delay: 3, options: .curveEaseInOut) {
        self.circleOrange.alpha = 0.2
        self.circleRed.alpha = 1
    } completion: { (_) in
        dispatchSemaphore.signal()
    }
    
    dispatchSemaphore.wait()
    UIView.animate(withDuration: 0.5, delay: 5, options: .curveEaseInOut) {
        self.circleOrange.alpha = 1
    } completion: { (_) in
        dispatchSemaphore.signal()
    }
    
    dispatchSemaphore.wait()
    UIView.animate(withDuration: 0.5, delay: 1, options: .curveEaseInOut) {
        self.circleOrange.alpha = 0.2
        self.circleRed.alpha = 0.2
        self.circleGreen.alpha = 1
    } completion: { (_) in
        self.animateSemaphore()
    }
}

}

共有1个答案

秦炜
2023-03-14

您需要在主线程中插入任何与UI/Animate相关的代码,而不是在后台队列中

func animateSemaphore() {
    circleRed.alpha = 0.2
    circleOrange.alpha = 0.2
    circleGreen.alpha = 1
        
    UIView.animate(withDuration: 0.5, delay: 5, options: .curveEaseInOut) {
        self.circleOrange.alpha = 1
        self.circleGreen.alpha = 0.2
    } completion: { (_) in
        UIView.animate(withDuration: 0.5, delay: 3, options: .curveEaseInOut) {
            self.circleOrange.alpha = 0.2
            self.circleRed.alpha = 1
        } completion: { (_) in
            UIView.animate(withDuration: 0.5, delay: 5, options: .curveEaseInOut) {
                self.circleOrange.alpha = 1
            } completion: { (_) in
                UIView.animate(withDuration: 0.5, delay: 1, options: .curveEaseInOut) {
                    self.circleOrange.alpha = 0.2
                    self.circleRed.alpha = 0.2
                    self.circleGreen.alpha = 1
                } completion: { (_) in 
                }
            }
        }
    }
 }

或者玩延迟而不是嵌套动画

func animateSemaphore() {

    circleRed.alpha = 0.2
    circleOrange.alpha = 0.2
    circleGreen.alpha = 1

    UIView.animate(withDuration: 0.5, delay: 5, options: .curveEaseInOut) {
        self.circleOrange.alpha = 1
        self.circleGreen.alpha = 0.2
    } completion: { (_) in
        print("1")
      
    }
    
   
    UIView.animate(withDuration: 0.5, delay: 8.5, options: .curveEaseInOut) {
        self.circleOrange.alpha = 0.2
        self.circleRed.alpha = 1
    } completion: { (_) in
        
    }
    
   
    UIView.animate(withDuration: 0.5, delay: 14, options: .curveEaseInOut) {
        self.circleOrange.alpha = 1
    } completion: { (_) in
       
    }
    
  
    UIView.animate(withDuration: 0.5, delay: 15.5, options: .curveEaseInOut) {
        self.circleOrange.alpha = 0.2
        self.circleRed.alpha = 0.2
        self.circleGreen.alpha = 1
    } completion: { (_) in
        
    }
}
 类似资料:
  • 问题内容: 我想获取连接到手机的另一台设备的蓝牙信号强度, 如何获得蓝牙信号强度? 我试图在Google上进行大量搜索,但未找到任何答案。 有人知道我该如何实施吗? 这是myActivity: 我的清单文件中也有蓝牙许可。 问题答案: 要获取信号,您可以检查蓝牙RSSI,可以阅读已连接设备的RSSI,或执行蓝牙发现以检查附近任何设备的RSSI。 基本上,蓝牙发现是向范围内的所有电台广播以进行响应。

  • 问题内容: 您好,有什么方法可以使Linux中的蓝牙设备获得接近的信号强度吗?或任何适用于nodejs,php或mono的好的库(我确实知道一些c ++或python,但宁愿不使用它们)如果工具不存在,但编写起来相当容易 谢谢 问题答案: 在Linux上,可以通过hcitool命令执行此操作。但是,必须连接才能获得设备的rssi。如果要从命令行实现此目的,请尝试: 如果要查看实现此目标的实际C代码

  • 我是一个新的Android和工作在这个聊天应用,在那里我想改变一个activity元素基于预定的数据,在预定的确切时间。它不一定要工作时,应用程序或电话关闭,但我想发送通知,以及与更改。有AlarmManager、JobScheduler、Handler、Timer和WorkManager,但我对它们之间的比较感到非常困惑。在这种情况下哪一个是最好的选择?

  • 问题内容: 我正在使用有角度的ui-bootstrap typeahead,并且希望将其用作选择许多选项的方法,因此启动selectMatch方法时,我需要获取选定的值,但是我找不到方法在我的控制器中 如果我观看选定的值,则每按一次键都会得到更改… 我知道方法selectMatch是在用户按Enter或单击列表时调用的方法,但我不知道如何对此进行回调… 谢谢 ! 问题答案: 现在有更好的方法。添加

  • 我有一个非常简单的DIV元素,我试图调整大小并水平拖动。它工作得很好,但是DIV元素的高度也会被jQueryUI改变。我不明白为什么。 有人有主意吗? JS代码: 超文本标记语言代码: CSS代码:

  • 首先想到的问题是,为什么我们需要信号量? 一个简单的答案,以保护多个进程共享的关键/共同区域。 假设多个进程正在使用相同的代码区域,如果所有人都想并行访问,那么结果是重叠的。 例如,多个用户仅使用一台打印机(通用/关键部分),例如个用户,同时给予个作业,如果所有作业并行启动,则一个用户输出与另一个用户输出重叠。 因此,我们需要使用信号量来保护这个信号,即当一个进程正在运行时锁定关键部分,并在完成时