我有一个按钮来控制计时器。我轻松,有规律,有困难。我在一个按钮内创建了三个开关。如何将每个按钮分配给特定开关?像func hard $将分配给switch(buttonStatehard)。
var buttonStatehard: UISwitch!
@IBAction func start(sender: AnyObject) {
//stop within 5 ms
buttonStatehard.setOn(false, animated: true)
switch(buttonStatehard){
case 0:
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
//buttonStatehard = 1;
break;
case 1:
timer.invalidate()
if(displayTime.text == stoptimer.text){
score++
scoring.text = String(score)
try = 0
attempt.text = String(try)
}
if(displayTime.text != stoptimer.text){
try++
attempt.text = String(try)
if(try > 2){
gameOver = UIView(frame: CGRectMake(0, 0, 0, 0))
gameOver.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
gameOver.opaque = false
self.view.addSubview(gameOver)
//UIView.animateWithDuration(0, animations:{
self.gameOver.frame.size = CGSizeMake(1000, 1000)
//})
gobutton = UIButton()
let image = "button.png"
gobutton.setImage(UIImage(named: image), forState: .Normal)
gobutton.frame = CGRectMake(135, 300, 95, 95)
gobutton.setTranslatesAutoresizingMaskIntoConstraints(false)
gobutton.addTarget(self, action: "pressed:" , forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(gobutton)
let horizontalConstraint1 = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
self.view.addConstraint(horizontalConstraint1)
let verticalConstraint2 = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
self.view.addConstraint(verticalConstraint2)
let widthConstraint = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 95)
gobutton.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 95)
gobutton.addConstraint(heightConstraint)
golabel = UILabel(frame: CGRectMake(30, 100, 350, 100))
golabel.setTranslatesAutoresizingMaskIntoConstraints(false)
golabel.text = "Game Over"
golabel.textColor = UIColor.whiteColor()
golabel.font = UIFont(name: "AppleSDGothicNeo-Bold" , size: 50)
self.view.addSubview(golabel)
let horizontalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 1)
self.view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -150)
self.view.addConstraint(verticalConstraint)
score = 0
scoring.text = String(score)
}
}
//buttonStatehard = 2;
break;
case 2:
timer.invalidate()
// buttonStatehard = 0;
displayTime.text = "0.00"
stoptimer.text = String(format: "%.2f", Double(Int(arc4random_uniform(5)+1)))
default: break;
}
if(scoring.text > best.text){
best.text = String(score)
defaults.setObject(self.best.text, forKey: "Score")
}
//land within 20 ms
switch(buttonStateez){
case 0:
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
buttonStatehard = 1;
break;
case 1:
timer.invalidate()
if(displayTime.text == stoptimer.text){
score++
scoring.text = String(score)
try = 0
attempt.text = String(try)
}
/* func puones(sender: UIButton!){
let a = NSString(string: displayTime.text!).doubleValue
let b = NSString(string: stoptimer.text!).doubleValue
if (a-b == 0.02 || b-a == 0.02){
score++
scoring.text = String(score)
try = 0
attempt.text = String(try)
}
}
let a = NSString(string: displayTime.text!).doubleValue
let b = NSString(string: stoptimer.text!).doubleValue
if(a-b == 0.01 || b-a == 0.01) {
score++
scoring.text = String(score)
try = 0
attempt.text = String(try)
}
*/
if(displayTime.text != stoptimer.text){
try++
attempt.text = String(try)
if(try > 2){
gameOver = UIView(frame: CGRectMake(0, 0, 0, 0))
gameOver.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
gameOver.opaque = false
self.view.addSubview(gameOver)
//UIView.animateWithDuration(0, animations:{
self.gameOver.frame.size = CGSizeMake(1000, 1000)
//})
gobutton = UIButton()
let image = "button.png"
gobutton.setImage(UIImage(named: image), forState: .Normal)
gobutton.frame = CGRectMake(135, 300, 95, 95)
gobutton.setTranslatesAutoresizingMaskIntoConstraints(false)
gobutton.addTarget(self, action: "pressed:" , forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(gobutton)
let horizontalConstraint1 = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
self.view.addConstraint(horizontalConstraint1)
let verticalConstraint2 = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
self.view.addConstraint(verticalConstraint2)
let widthConstraint = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 95)
gobutton.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 95)
gobutton.addConstraint(heightConstraint)
golabel = UILabel(frame: CGRectMake(30, 100, 350, 100))
golabel.setTranslatesAutoresizingMaskIntoConstraints(false)
golabel.text = "Game Over"
golabel.textColor = UIColor.whiteColor()
golabel.font = UIFont(name: "AppleSDGothicNeo-Bold" , size: 50)
self.view.addSubview(golabel)
let horizontalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 1)
self.view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -150)
self.view.addConstraint(verticalConstraint)
score = 0
scoring.text = String(score)
}
}
buttonStatehard = 2;
break;
case 2:
timer.invalidate()
buttonStatehard = 0;
displayTime.text = "0.00"
stoptimer.text = String(format: "%.2f", Double(Int(arc4random_uniform(5)+1)))
default: break;
}
if(scoring.text > best.text){
best.text = String(score)
defaults.setObject(self.best.text, forKey: "Score")
}
//stop within 10 ms
switch(buttonStatereg){
case 0:
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
buttonStatehard = 1;
break;
case 1:
timer.invalidate()
if(displayTime.text == stoptimer.text){
score++
scoring.text = String(score)
try = 0
attempt.text = String(try)
}
/* func puones(sender: UIButton!){
let a = NSString(string: displayTime.text!).doubleValue
let b = NSString(string: stoptimer.text!).doubleValue
if (a-b == 0.02 || b-a == 0.02){
score++
scoring.text = String(score)
try = 0
attempt.text = String(try)
}
}
let a = NSString(string: displayTime.text!).doubleValue
let b = NSString(string: stoptimer.text!).doubleValue
if(a-b == 0.01 || b-a == 0.01) {
score++
scoring.text = String(score)
try = 0
attempt.text = String(try)
}
*/
if(displayTime.text != stoptimer.text){
try++
attempt.text = String(try)
if(try > 2){
gameOver = UIView(frame: CGRectMake(0, 0, 0, 0))
gameOver.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
gameOver.opaque = false
self.view.addSubview(gameOver)
//UIView.animateWithDuration(0, animations:{
self.gameOver.frame.size = CGSizeMake(1000, 1000)
//})
gobutton = UIButton()
let image = "button.png"
gobutton.setImage(UIImage(named: image), forState: .Normal)
gobutton.frame = CGRectMake(135, 300, 95, 95)
gobutton.setTranslatesAutoresizingMaskIntoConstraints(false)
gobutton.addTarget(self, action: "pressed:" , forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(gobutton)
let horizontalConstraint1 = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
self.view.addConstraint(horizontalConstraint1)
let verticalConstraint2 = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
self.view.addConstraint(verticalConstraint2)
let widthConstraint = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 95)
gobutton.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: gobutton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 95)
gobutton.addConstraint(heightConstraint)
golabel = UILabel(frame: CGRectMake(30, 100, 350, 100))
golabel.setTranslatesAutoresizingMaskIntoConstraints(false)
golabel.text = "Game Over"
golabel.textColor = UIColor.whiteColor()
golabel.font = UIFont(name: "AppleSDGothicNeo-Bold" , size: 50)
self.view.addSubview(golabel)
let horizontalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 1)
self.view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -150)
self.view.addConstraint(verticalConstraint)
score = 0
scoring.text = String(score)
}
}
buttonStatehard = 2;
break;
case 2:
timer.invalidate()
buttonStatehard = 0;
displayTime.text = "0.00"
stoptimer.text = String(format: "%.2f", Double(Int(arc4random_uniform(5)+1)))
default: break;
}
if(scoring.text > best.text){
best.text = String(score)
defaults.setObject(self.best.text, forKey: "Score")
}
}
如何将每个按钮分配给特定开关?
func ez$ (sender: UIButton!){
}
func reg$ (sender: UIButton!){
}
func hard$ (sender: UIButton!){
buttonStatehard.setOn(true, animated:true)
//error EXC_BAD_INSTRUCTION
}
答案 0 :(得分:0)
我为你找到了这个,如果它不是你想要的,请评论。
@IBAction func buttonClicked(sender: UIButton) {
if mySwitch.on {
myTextField.text = "The Switch is Off"
println("Switch is on")
mySwitch.setOn(false, animated:true)
} else {
myTextField.text = "The Switch is On"
mySwitch.setOn(true, animated:true)
}
}