ccTouchesMoved and selector:#selector(ButtonTapped:) - iphone

Ill try and keep this as simple as possible.
I have a guy which you drag around the screen
I have a shoot button which fires bullets
To drag the guy around the screen im using ccTouchesMoved, basically get the users finger movement and set the guy to that place. Done.
I have a button which is a CCMenuItemImage and if it is tapped then is called a selector to run a method. The method simply fires some bullets. Done.
Now my problem is i cant have these two things work at the same time. I would like the player to be able to drag around the guy and shoot at the same time. Im assuming the button is not being tapped becuase the user is touching and dragging around the player.
How can i fix this? Still be able to drag around with one finger and press the shoot button with the other and they both work together?

You might want to implement your fire button using a sprite. You can handle ccTouchesBegan and ccTouchesEnded to check for a press within the bounds of your button, to swap it with a pressed state (on begin) or to implement the fire action (on end).
In your ccTouchesMoved handler, you can iterate over the set of touches and determine if the touch that moved is the one over the button, or the touch that should change the character position.

Related

Adding specific touch event to UIButton iPhone

I am trying to add a touch to UIButton.
I want it to be when I click on button, until I release the finger, it won't click on it.
I Have played with many touch events, Touch down, Touch up inside etc but none is working like that. This is a very common touch event. How can I add such a thing in iPhone UIButton?
Touch up inside will only execute when you remove your finger from the button
Touch down event will work as soon as you touch the button.
Sample Project:
https://www.dropbox.com/s/hm82u553ktyszbd/ST-16385963.zip
Touch up inside is the event you should use here. If it doesn't work, that means you haven't implemented it correctly.
Could you post your code? It sounds like you want the Touch Up Inside functionality of not firing until the finger is raised.

How to cancel UITapGestureRecognizer?

I noticed that the single tap happens when the touch dragging is performed quickly.
I want to cancel the single tap if the user moves the touch.
How can I do this?
I noticed that the single tap happens when the touch dragging is performed quickly.
My suggestion is to do nothing. Assuming you have everything set up properly then what you are going to end up doing is creating an app specific behavior for dragging simply to accommodate your personal preference. This could confuse your customers and possibly prevent your app from getting approved.
If the problem you are experience cannot be duplicated in other apps then I would guess that you are doing something incorrectly with respect to the responder chain, and you should focus on fixing that first.
I'm going to assume that you don't want to cancel the tap gesture itself (once the tap gesture has fired, I don't think you can cancel the gesture, it's already done!), but rather the code fired from it.
What you could do is create a timer, and delay the action created by the tap gesture by a second or two. You then have that time interval to cancel the action.

Cocoa Touch - Handling touches that get interrupted or lost

I have a couple places in my app where UI elements reset themselves when a touch ends on them. For example, hiding a dashed outline, sliding a view back to the default position.
The problem is that on occasion the app loses track of the touches. One example is if I slide the view upward and cause a UIAlertView to show, the view doesn't slide back, because the reset code is in touches ended. The touch ends during the time the UIAlertView is active, and the view doesn't reset. The same example works for hiding the dashed outlines.
My question is, where/how can I handle the reseting of these custom UI elements so that when a touch ends without being noticed it will still reset. TouchesEnded isn't always doing it for me.
Have you try touchesCancelled? I think it is called when the touch get interrupted.
EDIT: If this doesn't work then maybe you can manually add code that cancel the touch when you initiate the alert.

Dragging a touch along a row of UIButtons

I have a row of say 10 UIButtons along the bottom of the iPhone screen. The user can TouchUpInside each of these. I'd also like to allow the user to drag their finger along the row, deselecting the previous button that was touched when the finger is over the next. This kind of behaviour is seen on iPhone's keyboard.
The obvious thing to try is UIControlEventDragOutside or UIControlEventDragExit to trigger the appropriate actions however these only work when the touch event is sufficiently far away from the button. As my buttons must sit flush against each other this is not good enough for me.
Suggestions?

Where's the difference between UIControlEventTouchDragOutside and UIControlEventTouchDragExit?

Both appear to have the exact same effect. They come both when the finger is far enough away from the control. The "bounds" is not really the criteria for UIControlEventTouchDragExit. It gets fired only if it's far away enough...
I came here looking for the same thing and the answer from eOgas did not seem accurate. I did my own test case and here are my results for those who want a detailed answer without having to test it for themselves:
UIControlEventTouchDragExit
gets called only once as the user leaves the control they pressed. Once the user breaks the 'magical boundary'* outside a UIButton (for instance) this event gets called once. If, while still dragging, the user drags back into the control and back out again, this event gets called once again. The reverse can apply to UIControlEventTouchDragEnter.
UIControlEventTouchDragOutside
gets called after UIControlEventTouchDragExit and gets called repeatedly every time the user drags their finger while still holding down the original touch that was used to enter the control. For those familiar with the touchesMoved method of UIView, it works similarly. The reverse can apply to UIControlEventTouchDragInside however this can obviously get called without having to leave the control first.
To understand or remember better, you can compare these events to a person leaving (and coming to) their house wherein they only exit the house once but then proceed to move outside repeatedly. Also, a person only enters their house once but then proceeds to move inside repeatedly.
*the extra space around a UIControl object that takes into consideration the user's potential for imprecise touches.
UIControlEventTouchDragOutside
An event where a finger is dragged just outside the bounds of the control.
UIControlEventTouchDragExit
An event where a finger is dragged from within a control to outside its bounds.
It sounds like with UIControlEventTouchDragOutside is fired when the user touches just outside the bounds, regardless of whether or not the finger was ever within the bounds. UIControlEventTouchDragExit is only fired when the finger is dragged from within the bounds to outside the bounds.
So, UIControlEventTouchDragOutside would be used when resizing a control (an edge tap, then drag), whereas UIControlEventTouchDragExit would be used to move the control around (tap inside and drag).
.touchDragOutside is continous, while .touchDragExit is not.
.touchDragOutside gets called on every drag movement that happens outside bounds (similar to DragGesture.onChanged(_:)), while
.touchDragExit gets called once as the drag leaves the bounds.
They both (!) get called only when the drag is initiated within the view.
In addition, they are only fired when the finger is "far away" (~70 pt) from the control. They won't get fired as soon as the finger leaves the bounds (same goes .touchUpOutside). They seem to be designed for the users to deliberately cancel ongoing touches.