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
Related
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.
Where the user clicks on the screen and the keyboard and text field show up. Then when they are done it disappears and the text is saved. But I don't want the box to be around it I just want the words that the user typed to appear. I am somewhat a noob to Xcode, and I have been trying to figure this out for many days now. If you guys have any input that would be great!
Thanks in Advance
First you have to create a UiTextField or a UILabel.
In Snapchat the keyboard hides whenever you tap on the screen. Objective-C provides a methode to detect if the screen was touched.
Use - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
With resignFirstResponder you hide the keyboard from your label.
you have to implement -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method, when method fired than
UITouch *touch = [touches anyObject];
float position = [touch locationInView:view];
and add UITextField at that point, and on return of UITextField create UILabel with the calculation of text size and add it to view at same position where UITextField was added and remove UITextField from superview.
Or for touch you also can use UITapGestureRecognizer
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.
I am trying to achieve a certain behavior in which an object executes a method when a tap passes into the bounds of the object, and then another when the tap leaves its bounds. Since this is a difficult scenario to put into words, I drew a picture:
Which type of ControlEvent is this, and if it doesn't exist, are there other accepted methods of getting this to work?
Thanks,
James
There isnt any control event for this that i know of. You will have to implement this yourself. On the view controller (superview to your button) place the following methods, something like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// Touches began, check the position of my touch. Is it outside a button?
// note that in a BOOL
...
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// was my first touch on a button? if not, see if my current touch is inside
// the button, note that
...
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// Touch is ending, is my touch outside the button and did i cross through earlier?
// If so, "drag through" event is successful
...
}
hope this helps
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.