I'd like to implement multitouch, and I was hoping to get some sanity checks from the brilliant folks here. :)
From what I can tell, my strategy to detect and track multitouch is going to be to use the touchesBegan _Moved and _Ended methods and use the allTouches method of the event parameter to get visibility on all relevant touches at any particular time.
I was thinking I'd essentially use the previousLocationInView as a way of linking touches that come in with my new events with the currently active touches, i.e. if there is a touchBegan for one that is at x,y = 10,14, then I can use the previous location of a touch in the next message to know which one this new touch is tied to as a way of keeping track of one finger's continuous motion etc. Does this make sense? If it does make sense, is there a better way to do it? I cannot hold onto UITouch or UIEvent pointers as a way of identifying touches with previous touches, so I cannot go that route. All I can think to do is tie them together via their previouslocationInView value (and to know which are 'new' touches).
You might want to take a look at gesture recognizers. From Apple's docs,
You could implement the touch-event handling code to recognize and handle these gestures, but that code would be complex, possibly buggy, and take some time to write. Alternatively, you could simplify the interpretation and handling of common gestures by using one of the gesture recognizer classes introduced in iOS 3.2. To use a gesture recognizer, you instantiate it, attach it to the view receiving touches, configure it, and assign it an action selector and a target object. When the gesture recognizer recognizes its gesture, it sends an action message to the target, allowing the target to respond to the gesture.
See the article on Gesture Recognizers and specifically the section titled "Creating Custom Gesture Recognizers." You will need an Apple Developer Center account to access this.
Related
I'm trying to pass tap events to the superview but handle longpress events. I've added LongPressGestureRecognizer to the top view but the tap events aren't passed to the superview. I tried multiple approaches:
Overriding hitTest doesn't work since the longpress gesture recognizer handler doesn't get called
isUserInteractionEnabled - same as above
Overriding touchesBegan/Ended and calling them manually on the superview doesn't trigger the tap event
Handing complex tap interactions can be hard, and mixing different approaches can make it much much harder.
Generally, the best way to handle it is to have a single view that has multiple gesture recognisers on them. Implement the UIGestureRecognizerDelegate method gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) and gestureRecognizer(_:shouldRequireFailureOf:) to handle conflicts. When a touch event is recognised it can delegate the action to whatever other object needs to deal with it. Having different views all trying to deal with touches at the same time is not a good way to deal with the problem. Gestures are dependent on other gestures and cannot all be handled independently by different views.
I want to build a game that when I touch and drag, in the place of the touch it creates a line that goes in the same direction as the drag, and ends in the boundaries of the game (in this case my frame).
Any tips of how could I approach this problem?
Your search term in UIPanGestureRecognizer.
You'd create a pan gesture recognizer and attach it to your view. In the method that it calls, look for the state UIGestureRecognizerStateBegan. Record the start position, then respond to calls to your action method with the state UIGestureRecognizerStateChanged.
That should be enough to get you started. As Matt says, this is a fairly common use case, so you should be able to find sample code online if you can't work this out from the docs.
I'm new to developing iOS apps,
I've successfully implemented a Swipe Gesture Recognizer,
What I was wondering is if there is an easy to use recognizer like the swipe gesture. That would let you implement the homescreen page turning effect but just on a small view in the view controller?
If your unclear on what effect I mean, when you look at the iPhone's homescreen you can drag your finger and it responds instantly (unlike swipe) and also has some spring feeling to it, is this some effect I can use, or do I manually have to program this into the code if so is there a tutorial that explains this?
Thanks,
I hope my question makes sense.
Have a look at UIPanGestureRecognizer:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html
UIPanGestureRecognizer is a concrete subclass of UIGestureRecognizer
that looks for panning (dragging) gestures. The user must be pressing
one or more fingers on a view while they pan it. Clients implementing
the action method for this gesture recognizer can ask it for the
current translation and velocity of the gesture.
A panning gesture is continuous. It begins
(UIGestureRecognizerStateBegan) when the minimum number of fingers
allowed (minimumNumberOfTouches) has moved enough to be considered a
pan. It changes (UIGestureRecognizerStateChanged) when a finger moves
while at least the minimum number of fingers are pressed down. It ends
(UIGestureRecognizerStateEnded) when all fingers are lifted.
Clients of this class can, in their action methods, query the
UIPanGestureRecognizer object for the current translation of the
gesture (translationInView:) and the velocity of the translation
(velocityInView:). They can specify the view whose coordinate system
should be used for the translation and velocity values. Clients may
also reset the translation to a desired value.
Edit: The spring feeling part you would need to implement yourself. Since iOS 7 there is UIDynamics which contains different animators, for what you describe you may need UIGravityBehavior and maybe UICollisionBehaviour. Look at the WWDC 2013 videos for this topic, I think you will find some examples there.
I'm a bit new to programming, but am creating an app for iOS in which there are going to be up to four objects that need to be moved separate from each other. So I need to know how to create 4 different areas on the screen that the user can touch and swipe to drag each object.
Sorry if this question was asked before! I didn't find the answer, but I may have just been searching the wrong way.
Gesture recognizer:
You can verify the touch area in the selector.
Create 4 separate gesture recognizer on 4 objects.
And in each's handler, check for the touch location, verify it and respond accordingly.
I need to track multiple touch events in an iOS MonoTouch application. I have overridden the TouchesBegan, TouchesCancelled, TouchesMoved and TouchesEnded methods.
What I need to figure out now is how to iterate through the touches (there will be more than one) during each of those overrides and match them up. I want to know when a touch begins and do something with it versus a different touch event. The user may put a finger down at which time I will start a timer to do something if that finger stays down.
If during that time the user puts down another finger I will want to start a timer for that one that is different than the first one.
I am pretty sure I can figure out a way to store my timers and such. What I can't figure out it how to iterate through the touch events that the NSSet contains in each of the overrides and then how to uniquely identify them BETWEEN the overrides.
I am assuming that a TouchesBegan touch in the NSSet will match up with a TouchesMoved, TouchesCancelled or TouchesEnded touch in the NSSets that those overrides get as well.
Is that true? If so how to I get at each one and uniquely ID them to match them up?
Here is a good example of MonoGame's use of TouchesBegan etc: https://github.com/mono/MonoGame/blob/develop/MonoGame.Framework/iOS/iOSGameView_Touch.cs
UITouch also has a timestamp field you could use to differentiate touches. I think you should store them in a dictionary to get the functionality you mention.
Here is the class reference for UITouch: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITouch_Class/Reference/Reference.html