【发布时间】:2020-09-21 05:51:40
【问题描述】:
我有一个大问题,我在手动单击按钮时创建了一个串行 UIbutton,我更改了背景颜色和填充颜色,并取消单击正常状态的颜色更改。
但是当我的程序选择一个随机按钮时,顺序很好,但是按钮没有实时改变,看不到选择了什么按钮(没有背景颜色,没有填充颜色)我不明白为什么,我在其他问题上看到了 setNeedsDisplay、setEnable、...的可能性。但不适用于我的...
你解决了这个刷新问题吗?我如何刷新按钮(button.setNeedsDisplay() 不起作用)
查看我的程序,当我的 mp3 结束播放时,我使用 soundEnd 布尔值,当我的按钮处于正常状态时,我使用 nextSound 布尔值。睡眠用于慢循环并等待 mp3 播放结束,如果不是多选则不起作用......我找到了这个解决方案,但不适用于背景和填充颜色
谢谢!
//
// ViewController.swift
// test
//
// Created by Thierry on 03/06/2020.
// Copyright © 2020 Thierry All rights reserved.
//
import UIKit
let squareSize = 100
let borderSpace = 20
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let zone3 = UIView(frame: CGRect(x: 10, y: 10, width: view.frame.width-20, height: view.frame.height-20))
zone3.backgroundColor = .red
view.addSubview(squareDraw(contentView: zone3))
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
for randomInt in 1..<13 {
print(randomInt)
let subview = self.view.viewWithTag(randomInt)
let button = subview as! UIButton
button.sendActions(for: .touchDown)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
button.sendActions(for: .touchUpInside)
}
sleep(3)
}
}
}
func squareDraw(contentView : UIView) ->UIView {
let widthScreen: Int = Int(contentView.bounds.width)
let heightScreen: Int = Int(contentView.bounds.height)
let scaleFactorX = (widthScreen*100/((3 * squareSize) + (4 * borderSpace)))
let scaleFactorY = (heightScreen*100/((4 * squareSize) + (5 * borderSpace)))
let squareSizeScaleX = (squareSize * scaleFactorX)/100
let squareSizeScaleY = (squareSize * scaleFactorY)/100
let borderSpaceScaleX = (borderSpace * scaleFactorX)/100
let borderSpaceScaleY = (borderSpace * scaleFactorY)/100
let originY = 0
var Tag = 0
for yCoord in 1...4 {
for xCoord in 1...3 {
Tag = Tag + 1
let k = button(CGRect(x: xCoord * (squareSizeScaleX + borderSpaceScaleX) - squareSizeScaleX, y: originY + (yCoord * (squareSizeScaleY + borderSpaceScaleY) - squareSizeScaleY), width: squareSizeScaleX, height: squareSizeScaleY), tag: Tag)
contentView.addSubview(k)
}
}
return contentView
}
func button(_ rect: CGRect, tag: Int) ->UIView {
let myButton = UIButton(type: .system)
myButton.frame = CGRect(x:CGFloat(rect.minX), y:CGFloat(rect.minY), width:CGFloat(rect.width), height:CGFloat(rect.height))
myButton.tag = tag
myButton.backgroundColor = .white
myButton.layer.borderWidth = 5
myButton.layer.borderColor = UIColor.white.cgColor
myButton.addTarget(self, action: #selector(buttonSelected), for: .touchUpInside)
myButton.addTarget(self, action: #selector(buttonReleased), for: .touchDown )
return myButton
}
@objc func buttonSelected(sender: UIButton) {
let button = sender
if button.isSelected == true {
} else {
button.layer.borderWidth = 5
button.layer.borderColor = UIColor.white.cgColor
}
}
@objc func buttonReleased(sender: UIButton) {
let button = sender
if button.isEnabled == true {
button.layer.borderWidth = 5
button.layer.borderColor = UIColor.black.cgColor
} else {
}
}
}
【问题讨论】:
-
如果你在主线程上运行它,你可能会遇到很多问题。如果这是在后台线程上运行,则需要在主线程上执行任何 UI 更新。
-
@DonMag 感谢您的回复!我没有其他问题,只有颜色不会在 UIButton 上实时更改,所有更改都在循环结束时可见,我没有发现每次更改结果后如何刷新我的视图与 11 中的 randomInt 循环相同..
-
很难确切地知道你的代码在做什么或者是什么影响了它。你能把minimal reproducible example放在一起吗?
-
@DonMag 我在我的第一个问题代码中进行了更新,例如,如果按钮中没有 addTarget,它会在 xcode 新项目中自动运行,如果您对此问题有解决方案?谢谢!
-
OK - 你对
sleep(2)的使用是个大问题。这会“冻结”您的代码执行 2 秒。如果删除该行,则在视图加载 3 秒后,所有白色方块都会出现黑色边框。您是否尝试每 3 秒更改 一个 边框?
标签: ios xcode uibutton refresh ios12