Ios 动画阻止其他UI元素响应
我正在尝试使用计时器每3秒更新一次背景色,并计算随机颜色值并将其分配给背景。我已经这样做了,但问题是当我在主线程上运行代码时,Ios 动画阻止其他UI元素响应,ios,swift,multithreading,animation,Ios,Swift,Multithreading,Animation,我正在尝试使用计时器每3秒更新一次背景色,并计算随机颜色值并将其分配给背景。我已经这样做了,但问题是当我在主线程上运行代码时,UITextField将不再响应 // // ViewBG.swift // E-Sign // // Created by shayan rahimian on 1/30/18. // Copyright © 2018 shayan rahimian. All rights reserved. // import Foundation import UIKit
UITextField
将不再响应
//
// ViewBG.swift
// E-Sign
//
// Created by shayan rahimian on 1/30/18.
// Copyright © 2018 shayan rahimian. All rights reserved.
//
import Foundation
import UIKit
extension UIViewController{
@objc func changeBG(){
//background style!
DispatchQueue.global(qos: .utility).async {
let red = Float((arc4random() % 256)) / 255.0
let green = Float((arc4random() % 256)) / 255.0
let blue = Float((arc4random() % 256)) / 255.0
let alpha = Float(1.0)
DispatchQueue.main.async {
UIView.animate(withDuration: 3.0, delay: 0.0, options:[.repeat, .autoreverse], animations: {
self.view.backgroundColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))}, completion:nil)
}
}
//end of background style
}
@objc func Blur(){
//Blur BackGround
let blur = UIBlurEffect(style: .light)
let blurview = UIVisualEffectView(effect: blur)
blurview.frame = self.view.frame
blurview.alpha = 0.7
self.view.insertSubview(blurview, at: 0)
//End of Blur
}
@objc func DoBG(){
DispatchQueue.main.async {
self.Blur()
_ = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(UIViewController.changeBG), userInfo: nil, repeats: true)
}
}
}
背景很好,但我的文本字段不会编辑!有没有办法改变背景线程中的背景颜色或任何解决方法
如有任何帮助,将不胜感激。请提供以下建议:
1) 删除DispatchQueue.global(qos:.utility).async
(对于不需要它的操作),只保留main.async
2) 在blurview.isUserInteractionEnabled=false上禁用交互,我不知道您的层次结构如何,但如果它取代了其他一些子视图,则可能有害
3) 你的计时器每3秒调用一次changeBG
,这样的函数会启动一个动画,并重复.autoreverse
:我想这是一个糟糕的机制(你可能会冻结ViewController
或使应用程序崩溃),因为你一直在添加循环的动画
4) 在
[.repeat、.autoreverse、.allowUserInteraction]中更改动画的选项
谢谢你的回复,1.谢谢你的建议我已经删除了它,但这不起作用。2.启用所有用户交互。3.有没有关于更改背景颜色的建议?@sci3nt15t是的,但首先尝试对UIView.animate
进行注释,看看它是否有助于解决您的主要问题。然后我们集中精力寻找更好的动画我已经评论了uiview.animated行,我只是保留了我的backgroundcolor行,我已经设置了计时器,每3秒钟更改一次我的背景色,现在一切都正常了。唯一的问题是动画@sci3nt15t酷,让我来修复这样的动画。只是一个moment@sci3nt15t发现问题:allowUserInteraction
。检查建议4。如果有帮助,请告诉我。