iphone sdk: how to distinguish between touch and drag - ios

i am using 2 pods.
MMDrawerController 0.5.1& WYPopoverController 0.1.7
now i want to make a WYPopover on my MMView
some pics:
this is how the MMController looks like (Playground)
now i want to touch it anywhere and make it look like:
the error: if i want to open the left view from the 'MMController' sometimes i get this:
but it should look like (left MMControllerView)
i am using the 2 methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// show the popoverController
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[popController dismissPopoverAnimated:NO];
}
my question: why the touchesMoved isn't called continuous and if there is a better way?

I don't know anything about the "pods" you mention (CocoaPods?) However, the most straightforward way to distinguish between taps and drags is to use a pair of gesture recognizers, a tap gesture recognizer and a pan gesture recognizer, and set up the tap gesture recognizer so the pan gesture must fail before the tap is triggered (There is a "wait until another gesture recognizer fails" mechanism built into gesture recognizers.)
I suggest reading up on UIGestureRecognizer, and the specific classes UITapGestureRecognizer and UIPanGestureRecognizer

You can dismiss popover when your left menu is going to open,
i.e. you can do that in delegate method of your slide menu:
- menuWillOpen: or something like this.

Related

Passing touches from subview of UIButton back to UIButton

Suppose I have a UIView named Cake. Cake has a gesture recognizer.
Now, suppose I have a UIButton named Bob.
I add Cake as a subview to Bob:
[Bob addSubview: Cake];
Now, Bob, the UIButton, no longer responds the control event touch up inside.
I want Cake to be able to handle the touch while Bob simultaneously handles the touch as well. Currently, Cake can handle the touch, but Bob lazily does nothing.
Things I have tried:
Setting cancelsTouchesInView of Cake's gesture recognizer to NO
Implementing the UIGestureRecognizerdelegate for Cake's gesture recognizer and always returning YES for the shouldRecognizeSimultaneouslyWithGestureRecognizer method
Subclassing UIGestureRecognizer and calling [self.view.nextResponder touchesSomething:touches withEvent:event]; in each of the touchesSomething (touchesBegan, touchesEnded, etc.) methods (I've also confirmed that the next responder IS IN FACT the UIButton that is supposed to handle the control events)
Not using a gesture recognizer and instead just using the touchesSomething methods in the UIView (Cake) + passing through the touchesSomething calls to all of super, self.superview, self.nextResponder and more.
Does anyone know a good way to make this work?
My suggestion is the following:
Make sure, that you set the UserInteraction of the subview to false! Now the action of the button should be called correctly.
Now add the event parameter to the action:
- (IBAction)pressButton:(UIButton *)sender forEvent:(UIEvent *)event
Inside the action check if the press was inside the subview
- (IBAction)pressButton:(UIButton *)sender forEvent:(UIEvent *)event
// get location
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
// check position
if (CGRectContainsPoint(self.subview.frame, location) {
// call selector like the gesture recognizer here
}
}
What I'm trying to make, Cake, is a view that can be placed as a subview into any button without additional setup - its a decorative view of sorts. The gesture recognizer of Cake is there to make a small animation
Then you're going about this all wrong. Take away the gesture recognizer of Cake; you don't need it. You're trying to get Cake to respond to Bob being pressed. But that's easy; Bob's a button! The button already tells you everything that's happening — it's being highlighted etc. So all you need is a UIButton subclass that tells Cake when to do its animation.

UIView drag with finger and release like Instagram back swipe

I have a UIViewController which displays content. Inside of it, I have an overlaying UIView named contentView which has a frame that equals self.view.frame. When my user "swipes", I have a UIView named newView which smoothly slides into contentView's old position. The user can swipe left or right and newView will come in from frame.origin.x = -320 (UISwipe direction right), or frame.origin.x = 320 (UISwipe direction left).
Anyway.. If you view any picture/post on Instagram, you'll notice that you can hold your finger on the view and the animation is guided by your finger. If you dont pass a certain x-position on the screen with the view and release, the view will simply pop back into place. I want this type of animation with the swapping of my two UIViews which, dynamically, swipe until the end of an array is reached.
I understand that maybe what Im looking for is these delegation methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
But Im currently using UISwipeGestureRecognizer. What would this implementation look like? And no I'm not asking you to write my code for me, I just need a general idea of how this can be accomplished via UISwipeGestureRecognizer or touchEvents. Thanks!

Swipe then hold - touchesBegan/touchesMoved/touchesEnded

I'm developing an app for iOS, and I want the user to be able to select some stuff on the screen, and then at the end of the selection be able to hold the finger for a second, to trigger some other event.
I have already created a lot of actions, for when the user marks stuff by swiping around. For this i have been using:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//Stuff here
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//Stuff here
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//Stuff here
}
I don't care if these functions are used, or if it done by UIGestureRecognizer, but i simply can't find a efficient and simple way to implement this.
Any suggestions?
You could initiate a NSTimer when a touch began, if the finger is within a target area, and if the touch ended function is called before the timer has fired r some condition is met within the touch moved function then cancel the timer, otherwise let it fire.
You'll probably need to use a timer as well as a pan gesture recognizer. When the finger reaches a target area, start the timer. If the finger moves out of the area, invalidate the timer. If the timer fires, it's a long press.

iOS Passing Interactions

I have two custom views as subviews on a UIView. I'd like to take every interaction that happens on either of the two subviews and does the same thing to the other subview. This includes all possible interactions like zooming, panning, rotating, etc. What is the easiest/cleanest way to implement that?
If you are only interested in recognizing specific gestures such as zooming, panning, long presses, I would recommend using the UIGestureRecognizer subclasses, which allow you to listen for specific types of gestures. Add them all to one view, and whenever one of them is triggered, perform the actions on both of them (such as increasing the scale of your view or moving the content).
Another similar option is to implement the touch handling methods of UIView and respond to each tough event directly, mimicking the actions on both views. Those methods are:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//called when a touch or touches begin
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//called when a touch or touches move
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//called when a touch or touches end
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
//called when a touch or touches are interrupted in some way, such as a phone call
}
You can read more about those methods in the UIResponder reference here.
OTOH, if you want to actually synthesize the touch events, you will have to do some hacking. Here is a tutorial on how to extend the functionality of the UITouch class so that you can simulate a touch programmatically. This is not a solution you should use if you plan to submit this app to the App Store, since it uses private APIs.

Tap in & out in UITapGestureRecognizer

In UITapGestureRecognizer, when user tap in & then out do we get two different events for this?
I have put UITapUITapGestureRecognizer on one of my view & then when user tap in I need to change the color of the view & when user tap out (i.e. remove his finger from the point) the color should be changed back to original color. I am able to change the color on tap in but not on tap out.
Any advise?
You shouldn't (can't?) use gesture recognizers for that, since they are built to handle raw touch events and parse them into gestures. The "tap in" event isn't a gesture by itself though.
Use these :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

Resources