UIButton 更新时不刷新
UIButton not refresh when updating
我有一个大问题,当我手动点击一个按钮时,我创建了一个串行UIbutton,我改变了背景颜色和填充颜色,并且取消点击颜色更改为正常状态。
但是当我的程序 select 一个随机按钮时,序列是好的,但是按钮不会实时改变并且看不到按钮是什么 selected(没有背景颜色,不填充颜色)我不明白为什么,我在其他问题上看到了 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 {
}
}
}
首先你从不想要使用sleep(2)
- 它会冻结你的程序 2 秒,并且不会发生其他任何事情。有 罕见 例外情况,但您绝对不希望出现这种情况。
你真正想做的事情还是有点迷惑。你的问题是 "soundEnd boolean value when my mp3 ending play"?
所以,这是一个示例 - 使用您的一些代码。
- 在
viewDidLoad()
- 在红色背景上绘制一个由 12 个白色按钮组成的网格
- 在
viewDidAppear()
- 开始播放声音
- 将第一个按钮的边框设置为黑色
- 当声音停止时
- 将按钮边框设置回白色
- 开始第二声
- 将第二个按钮的边框设置为黑色
这将循环播放您拥有的声音片段的数量:
class SoundTestViewController: UIViewController, AVAudioPlayerDelegate {
let squareSize = 100
let borderSpace = 20
var myPlayer: AVAudioPlayer = AVAudioPlayer()
// start at -1 so first call to "play next" will increment it to 0
var soundNumber: Int = -1
// replace with your mp3 file names
let mp3FileNames: [String] = [
"clip1",
"clip2",
"clip3",
"clip4",
]
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))
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playNextSoundFile()
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
player.stop()
if let subview = self.view.viewWithTag(soundNumber + 1) as? UIButton {
subview.layer.borderColor = UIColor.white.cgColor
}
playNextSoundFile()
}
func playNextSoundFile() {
soundNumber += 1
if soundNumber > mp3FileNames.count - 1 {
soundNumber = 0
}
guard let url = Bundle.main.url(forResource: mp3FileNames[soundNumber], withExtension: ".mp3") else {
// mp3 file not found in bundle
return
}
do {
if let subview = self.view.viewWithTag(soundNumber + 1) as? UIButton {
subview.layer.borderColor = UIColor.black.cgColor
}
let sound = try AVAudioPlayer(contentsOf: url)
self.myPlayer = sound
// set the audio player delegate to self
sound.delegate = self
sound.numberOfLoops = 0
sound.prepareToPlay()
sound.play()
} catch {
print("error initializing audio player")
}
}
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
return myButton
}
}
如果这不是您真正想要做的,也许它至少会让您朝着正确的方向前进。
我有一个大问题,当我手动点击一个按钮时,我创建了一个串行UIbutton,我改变了背景颜色和填充颜色,并且取消点击颜色更改为正常状态。
但是当我的程序 select 一个随机按钮时,序列是好的,但是按钮不会实时改变并且看不到按钮是什么 selected(没有背景颜色,不填充颜色)我不明白为什么,我在其他问题上看到了 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 {
}
}
}
首先你从不想要使用sleep(2)
- 它会冻结你的程序 2 秒,并且不会发生其他任何事情。有 罕见 例外情况,但您绝对不希望出现这种情况。
你真正想做的事情还是有点迷惑。你的问题是 "soundEnd boolean value when my mp3 ending play"?
所以,这是一个示例 - 使用您的一些代码。
- 在
viewDidLoad()
- 在红色背景上绘制一个由 12 个白色按钮组成的网格
- 在
viewDidAppear()
- 开始播放声音
- 将第一个按钮的边框设置为黑色
- 当声音停止时
- 将按钮边框设置回白色
- 开始第二声
- 将第二个按钮的边框设置为黑色
这将循环播放您拥有的声音片段的数量:
class SoundTestViewController: UIViewController, AVAudioPlayerDelegate {
let squareSize = 100
let borderSpace = 20
var myPlayer: AVAudioPlayer = AVAudioPlayer()
// start at -1 so first call to "play next" will increment it to 0
var soundNumber: Int = -1
// replace with your mp3 file names
let mp3FileNames: [String] = [
"clip1",
"clip2",
"clip3",
"clip4",
]
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))
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playNextSoundFile()
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
player.stop()
if let subview = self.view.viewWithTag(soundNumber + 1) as? UIButton {
subview.layer.borderColor = UIColor.white.cgColor
}
playNextSoundFile()
}
func playNextSoundFile() {
soundNumber += 1
if soundNumber > mp3FileNames.count - 1 {
soundNumber = 0
}
guard let url = Bundle.main.url(forResource: mp3FileNames[soundNumber], withExtension: ".mp3") else {
// mp3 file not found in bundle
return
}
do {
if let subview = self.view.viewWithTag(soundNumber + 1) as? UIButton {
subview.layer.borderColor = UIColor.black.cgColor
}
let sound = try AVAudioPlayer(contentsOf: url)
self.myPlayer = sound
// set the audio player delegate to self
sound.delegate = self
sound.numberOfLoops = 0
sound.prepareToPlay()
sound.play()
} catch {
print("error initializing audio player")
}
}
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
return myButton
}
}
如果这不是您真正想要做的,也许它至少会让您朝着正确的方向前进。