[英]How do I create one button, but make it have two functions?
我为秒表创建了两个单独的按钮,即开始和停止。 如何将其组合为一个按钮。 单击开始,计时器启动,然后功能应更改,因此单击时将停止,然后再次单击时将重置。
@IBAction func start(sender: AnyObject) {
if !timer.valid{
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
}
}
@IBAction func stop(sender: AnyObject) {
timer.invalidate()
}
您可以按照以下步骤进行操作。
@IBAction func startstop(sender: AnyObject) {
if !timer.valid{
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
}
else{
timer.invalidate()
}
}
否则,您可以将一个标志设为boolean
。
您可以根据条件检查是非题。
使用按钮选择和未选择状态,因为您可以为按钮分配不同状态的图像
[button setImage:[UIImage imageNamed:@"touch_selected.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"touch_selected.png"] forState:UIControlStateHighlighted];
在使用之后
@IBAction func buttonAction(sender: AnyObject) {
// use you if statement to select the condition for start and stop
if ([_button isSelected]){
// start function
} else {
//stop function
}
}
抱歉,代码是Obj c和swift的混合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.