I have a slider element that I want to detect a touchesEnded action on.
I'm trying to add the touchesEnded action programmatically:
[self.slider touchesEnded:(NSSet *] touches withEvent:event];
The problem is I have no idea where to get the touches variable from (I'm assuming the event param is the function I wish to call after the touch has ended).
Create a subclass for UISlider. Implement
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
method in the sub-class. Delegate the touch event to your view.
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.
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.
I'm working on iPad app. My UIViewController contains only a custom UIView with a size of 500wx500h.
I implemented the touches methods both in the UIViewController and the custom UIView in order to call the UIViewController touches methods when we touch all around the custom UIView and call the custom UIView touches methods when we touch inside it.
UIViewController touchesMoved :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
NSLog(#"TEST"); // Added during edition
if (!CGRectContainsPoint(self.drawingView.frame, point)) {
NSLog(#"UIVIEWCONTROLLER");
}
}
Custom UIView touches Moved :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"CUSTOM VIEW");
}
When I'm touching the custom UIView moving my finger, CUSTOM VIEW is logged until I removed the finger on the screen. On the other hand, when I'm touching outside of the custom UIView, the UIVIEWCONTROLLER is called only one time in the touchesMoved method.
Am I missing something wrong ?
EDIT : I added a log in my UIViewController touchesMoved method. When I touch inside the custom view, it logs the TEST during all the touching phase. But when I touch outside, I get the same behaviour.
add exclusiveTouch property to your view to disable multi touches
your view.exclusiveTouch=YES;
There is some likeliness that your self.view is intercepting touches and handling them, so they will not make it through to the view controller. You could try doing either of 2 things (or both) and see if it works:
self.view.exclusiveTouch = NO;
self.view.userInteractionEnabled = NO;
I would also try to call [super touchesMoved: touches withEvent: event];
Hope it helps.
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