[UIButton更新时不刷新

问题描述 投票:0回答:1

我有一个大问题,当我手动单击按钮时,创建了一个串行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 {
                    
                }
            }

 
        
  


}

我有一个大问题,当我手动单击按钮时,创建了一个串行UIbutton,更改了背景色和填充色,然后取消了正常状态下的颜色更改。但是当我的程序...

ios xcode uibutton refresh ios12
1个回答
0
投票

首先,您从不

© www.soinside.com 2019 - 2024. All rights reserved.