gpt4 book ai didi

ios - swift : Button not responding to tap

转载 作者:行者123 更新时间:2023-11-30 14:13:24 26 4
gpt4 key购买 nike

嘿,我是编码新手,我在 View Controller 上放置了一个按钮(拖放)。该按钮对触摸或任何其他操作没有响应,并且显示为“静态”。我累了,筋疲力尽。有人帮忙吗?非常感谢!

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var lbl1: UILabel!
@IBOutlet weak var lblMadeToday: UILabel!

@IBOutlet weak var Cents: UILabel!
var timer = NSTimer()
var hourlyWage : CGFloat = CGFloat (hourlyPrice)
var timeTo1 : CGFloat = 0
var time : Float = 0
var earnt : CGFloat = 0
var madeToday : CGFloat = 0

@IBAction func btnClick(sender: AnyObject) {

}

func tick() {
earnt = CGFloat (time)/timeTo1
lbl1.text = NSString(format: "%.2f", earnt) as String


if (earnt >= 1) {
time = 0
madeToday++
lblMadeToday.text = madeToday.description
}
time++
}


override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("tick"), userInfo: nil, repeats: true)
timeTo1 = CGFloat (360000)/hourlyWage;

println("view Loaded :)")
NSNotificationCenter.defaultCenter().addObserver(self, selector:"doYourStuff", name:
UIApplicationWillEnterForegroundNotification, object: nil)
}


func doYourStuff(){
var nowHour = NSCalendar.currentCalendar().component(.CalendarUnitHour, fromDate: NSDate())
var nowMinute = NSCalendar.currentCalendar().component(.CalendarUnitMinute, fromDate: NSDate())
var nowSecond = NSCalendar.currentCalendar().component(.CalendarUnitSecond, fromDate: NSDate())

var time1 = (nowHour - globalHour) * 60
var time2 = (nowMinute - globalMinute)
var time3 : CGFloat = (CGFloat(nowSecond)-CGFloat(globalSecond))/60
var timeInMinutesGone : CGFloat = CGFloat(time1) + CGFloat(time2) + CGFloat(time3)
println (timeInMinutesGone)
madeToday = (timeInMinutesGone / 60) * (CGFloat(hourlyWage))
var ef : Int = Int(madeToday)
lblMadeToday.text = String(ef)
}

@IBAction func btnPress(sender: AnyObject) {
println("hello")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

您的 NSTimer 每 0.01 秒在 UI 线程中执行一次。这可能对您的应用程序有害。如果您在后台线程中执行 NSTimer,您的按钮很可能会按预期工作。

将 NSTimer 添加到后台的一种方法是:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in

let timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("tick"), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
NSRunLoop.currentRunLoop().run()
})

关于ios - swift : Button not responding to tap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502437/

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