xcode - Swift - How to make one action if 2 buttons are pressed? -
in main.storyboard, have gun button , shield button. both buttons clicked @ same time (not same time know mean).
how make 1 action after both buttons clicked? in case if gun , shield clicked want make label defended. pretty asking how in view controller...
something should trick. connect both buttons each ibaction, , make sure onbuttondown: trigger control event .touchdowninside, , onbuttonup: trigger both .touchupinside , .touchdragexit.
class viewcontroller : uiviewcontroller { var buttonspressed: [uibutton] = [] // triggered .touchdowninside @ibaction func onbuttondown( sender: uibutton ) { self.buttonspressed.append( sender ) if self.buttonspressed.count == 2 { // both buttons pressed } } // triggered .touchupinside , .touchdragexit @ibaction func onbuttonup( sender: uibutton ) { in 0..<self.buttonspressed.count { if self.buttonspressed[i] == sender { self.buttonspressed.removeatindex( ) break } } } } update: add break statement in loop prevent crashing
Comments
Post a Comment