gpt4 book ai didi

ios - 按下 UIButton 时,在 ImageView 中更改图像

转载 作者:行者123 更新时间:2023-11-28 15:24:19 25 4
gpt4 key购买 nike

我是 Swift 的新手,正在尝试制作按钮。我想在按下按钮时启动计时器并更改 ImageView 中的图像。

我的代码;

@IBOutlet weak var label: UILabel!

func start()
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController2.updateTime as (ViewController2) -> () -> ())), userInfo: nil, repeats: true)

}

func updateTimer () {
count += 1
let hours = Int(count) / 3600
let minutes = Int(count) / 60 % 60
let seconds = Int(count) % 60
label.text = String(format: "%02i:%02i:%02i",hours,minutes,seconds)
}

func reset()
{
timer.invalidate()
count = 0
label.text = "00:00:00"

}

@IBAction func buttonPressed(sender : UIButton)
{
if sender.currentTitle == " "
{
sender.setTitle("", for: UIControlState.normal)
reset()
}

else

{
sender.setTitle(" ", for: UIControlState.normal)
start()
}
}

@IBOutlet weak var image: UIImageView!
@IBAction func tick(sender: UIButton)
{

if checked
{
sender.setImage( UIImage(named:"Rectangle1.png"), for: [])

checked = false
}
else
{
sender.setImage(UIImage(named:"Rectangle2.png"), for: [])

checked = true
}
}

不幸的是,当我按下按钮时,改变了按钮的图片。如何更改 ImageView 中的图像?

最佳答案

您正在设置发件人图像..这是您的 UIButton..更改这部分代码

错误:

sender.setImage(UIImage(named:"Rectangle1.png"), for: [])
...
sender.setImage(UIImage(named:"Rectangle2.png"), for: [])

右:

image.image = UIImage(named:"Rectangle1.png")   
...
image.image = UIImage(named:"Rectangle2.png")

关于ios - 按下 UIButton 时,在 ImageView 中更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45487838/

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