gpt4 book ai didi

ios - 动画阻止其他 UI 元素响应

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:04 25 4
gpt4 key购买 nike

我正在尝试使用计时器每 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

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 上的交互,我不知道你的层次结构如何,但如果它代替其他一些 subview 可能会有害。

3) 你的计时器每 3 秒调用一次 changeBG,这样的函数用 repeat, .autoreverse 启动动画:我想这是一个糟糕的机制(你可能会卡住ViewController 或使应用程序崩溃),因为您一直在添加循环播放的动画 forever .

4) 在 [.repeat, .autoreverse, .allowUserInteraction] 中更改动画选项

关于ios - 动画阻止其他 UI 元素响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48557223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com