End a game if a button is not tapped - Swift - ios

So in this game, i have a button pop up for a short amount of time. If the button disappears before the user clicks it, then they lose the game. How can i make it so to check if the button has appeared and they have not clicked it during its duration of appearance they lose the game. I already have all my necessary functions for when the game is lost. All i need help with is how to read if a user did not click a button during its time of appearance.
#IBAction func tapButton(_ sender: Any) {
if colorImage.image == UIImage(named: "\(self.currentColorNumber).png") {
// give the user a point
}
So i am able to give them a point if they tap the button. How do i end the game if they do not tap the button?

Wow hold on just a minute of algorithmization. Why dont you detect the user didn't tap the button on the button disappearance? If the user cliecked the button, the action happens, some class bool gets true and something happens... that's fine as it is. If the user didn't, you listen to the point the button disappears and handle the custom action there. I don't see anything hard about that.

Related

Swift - how to hide unknown button?

I have 4 buttons. Each of them represent 1 answer for question in trivia game. In different screens i have different questions and buttons with different answers. I have a mode, when when player can make a mistake, this is enabled by pressing the fifth button. So, when this mode is enabled i show alert and i need to hide button with wrong question which was pressed. Everything that i know about - that it is member of array with buttons with wrong questions. How can I tell the alert which button to hide on completion ?
Attach them all to an IBAction that takes a UIButton and then hide the one that was pushed by adding the line
sender.hidden = true
or disable it by adding
sender.enabled = false
Do you mean you wish to know if the button has been pressed? Well if you connect that UIButton to that IBAction, that IBAction will be fired each time the button is pressed. You could insert and NSLog("button press") line inside the IBAction to see that it is pressed.

How do I toggle hidden of a label while a button is pressed?

I am trying to figure out how to only display a label while a button is pressed in OS. I know how to operate the touch events but I am not sure how to incorporate the UILongPressGestureRecognizer into this.
The UIButton class, as well as lots of other UIControl subclasses can have numerous actions hooked up to them.
When we are hooking up an action from interface builder to our source code file, if we open the "Event" drop down, we're presented with a long list of options:
In almost every scenario, we're hooking our actions only up to "Touch Up Inside". This allows the user to consider whether or not they want to really press the button. If they drag their finger off the button before letting go, the action doesn't fire, because the "up touch" gesture happened outside the bounds of the object.
But here, we want to actually hook our button's "touch down" event up. This is when we'll display the label.
Let's go ahead and create a "touch down" event and a "touch up inside" event:
Swift
#IBAction func buttonTouchDown(sender: UIButton) {
self.myLabel.hidden = false
}
#IBAction func buttonTouchEnded(sender: UIButton) {
self.myLabel.hidden = true
}
Objective-C
- (IBAction)buttonTouchDown:(UIButton *)sender {
self.myLabel.hidden = NO;
}
- (IBAction)buttonTouchEnded:(UIButton *)sender {
self.myLabel.hidden = YES;
}
So far, buttonTouchEnded is set up completely normally, and buttonTouchDown was set up by selecting "touch down" from the "Event" list.
We can always verify what our control is hooked up to by right clicking it in the interface builder:
But this menu is useful for more than simply checking what we've already hooked up. From here, we can hook up any of the other actions to our existing #IBAction methods simply by clicking in the circle and dragging to the existing method.
So we obviously want the label to disappear if we stop pressing the button, a normal touch up like you'd hook up any other button. The only question remaining is, what exact behavior do you want?
If you want the label to disappear only when the finger is lifted, no matter where the finger goes, then we must also hook up "touch up outside".
If you want the label to disappear when the user drags their finger off the button, then we should hook up the "touch drag exit" action.
We also probably want to hook up the "touch cancel" action, which would occur if some sort of system event (perhaps an incoming phone call) cancels the touch.
This Stack Overflow answer elaborates on the differences between the action options we have, so you can craft the behavior exactly how you need it.
Anyway, once we decide which actions we want to hook up to which methods, bring up that right click menu and click-drag from the circles to the methods:
The easiest thing to do would be to add an action to the touchDown event and a separate action to touchUpInside and touchUpOutside.
Show the label on the touchDown action and hide it on the touchUpInside / touchUpOutside action. (and for completeness, on touchCancel, as suggested by nhgrif in his very thorough answer.)
A long press gesture recognizer won't work in this situation. You could create a custom gesture recognizer that triggered one event on touch and another event on release, and use that. It's actually not that hard to do.
EDIT
I just uploaded a demo project to GitHub called "MorphingButton" (link) that I created for another question here on Stack Overflow.
That project now shows a label on touching the app button and hides the label when you release the button.
The project is a hybrid Swift/Objective-C project that shows how to do the button morphing and label showing/hiding in both languages. It has a tab bar with a Swift tab and an Objective-C tab.

Fire and cancel touch events manually

I have a question about programming my own third-party keyboard for iOS8. My keyboard already looks pretty good, but some functionalities are missing. If you look at the standard keyboard on an iPhone you can press any button and if you swipe your finger to another button, the touch event of the first button gets cancelled and your second button is "active". So e.g. if I press the button "E" and swipe my finger to "R" and release my finger, the letter "R" is the selected one. But I don't know how to implement this.
Now in my app when I press a button and swipe my finger around, this button never gets "released". Seems like I'm stuck on that button as long is I have my finger put on the display.
I think I need these touch events:
TouchUpInside: when the user taps a button, and releases it inside the buttons' frame, this event gets fired (that's when I want to write a letter)
TouchDragInside: That's the event when I already have my finger on the display and swipe my finger "inside" the buttons' frame.
TouchDragOutside: Same as above, just swiping outside the buttons' frame.
But here's the problem: TouchDragInside just get's fired for the button I tap. So when TouchDragOutside gets fired, I have to "release" the button and make the button active where my finger is at the moment.
I hope you understand my question, if you need some further information or some details just let me know.
You could consider not using UIControl at all.
Just use a transparent overlay on top of all the keys so that you can manually deal with which key the message should be dispatched towards.

swipe event for page?

i have got a page in QML and i have implemented a new logic (function back()) for back button on this page. now I want, that this logic would also run after swiping the page from the screen. What is the best way to do it? onPeekEnded event is not suitable for this situation, because it fires also when the swiping was canceled. Thanks.
paneProperties: NavigationPaneProperties {
backButton: ActionItem {
onTriggered: {
back();
}
}
}
When you push a new page to the screen using a NavigationPane, a onPopTransitionEnded() signal is fired after the page is dismissed, either by the back button or because the user swiped it away. You can add a slot for that signal to your controller, and put your logic there.
There is no need to implement manual back button functionality here. You get all of that for free.

How can I post a manufactured event to UIButton

I'm writing a simple iOS 6.1 game. The game involves pressing buttons (OK, it's a tictactoe board, with the cells being UIButtons). The allows the player to choose whether to go first, or whether the computer should go first. If the player tells the computer to go first, I want to set some values, and then fire off the UIButton just as if the user had pressed it.
How can I post an event, or otherwise simulate the action of the button being pressed, to let the rest of the framework do the work of handling the button press?
If there is a better design approach, where I don't have to pretend that the computer has pressed a button, I'm open to hearing about that, instead.
Thank you
Your button will be connected to an action method, typically in your view controller. Just call that method yourself, passing the button as the sender.
Your method will be something like:
-(IBAction)buttonPressed:(UIButton*)sender
{
// Respond to your button press...
}
You'd call it as follows:
[self buttonPressed:self.whicheverButtonYouLike];
You'd need the buttons defined as outlets for this to work.

Resources