I've created a custom control which derives directly from a class UIView. Now I want to perform an action if a user tap on a particular part of my view. So I've overrided methods touchesBegan, touchesEnded and touchesCancelled. The problem is, that the method touchesEnded is never invoked, if I just tap on the display. The method touchesCancelled is called insted of it. touchesEnded is invoked only if I perform some gestures (swipe, move, ...).
Do I need to configure my view anyhow to enable tap gestures?
My code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touchesBegan");
self->touchDown = YES;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.value = 1.0;
} completion:nil];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (self->touchDown) {
NSLog(#"touchesEnded");
self->touchDown = NO;
[UIView animateWithDuration:0.3 animations:^{
self.value = 0.0;
} completion:nil];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
if (self->touchDown) {
NSLog(#"touchesCancelled");
self->touchDown = NO;
[UIView animateWithDuration:0.3 animations:^{
self.value = 0.5;
} completion:nil];
}
}
For a tap gesture I get:
2018-07-17 09:55:20.994645+0200 iOS Test[33049:2763212] touchesBegan
2018-07-17 09:55:21.092409+0200 iOS Test[33049:2763212] touchesCancelled
Have you tried to set recognizer.cancelsTouchesInView = NO; on your view
A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.
You should go through this.
From Apple documentation,
https://developer.apple.com/documentation/uikit/uigesturerecognizer?changes=_4&language=objc
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Sent to the gesture recognizer when one or more fingers touch down in the associated view.
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//Sent to the gesture recognizer when one or more fingers move in the associated view.
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Sent to the gesture recognizer when one or more fingers lift from the associated view.
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//Sent to the gesture recognizer when a system event (such as an incoming phone call) cancels a touch event.
}
Related
I want to make voice recorder app. The
recording should start when long touch begins and the
recording must end when user stops the gesture on the button.
UIGestureRecognizer *longGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(startrecording)];
How can I handle that when the user leaves the button?
in viewDidLoad method
UILongPressGestureRecognizer *longPressOnButton = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressOnButton:)];
longPressOnButton.delegate = self;
btn.userInteractionEnabled = YES;
[btn addGestureRecognizer:longPressOnButton];
- (void)longPressOnButton:(UILongPressGestureRecognizer*)gesture
{
// When you start touch the button
if (gesture.state == UIGestureRecognizerStateBegan)
{
//start recording
}
// When you stop touch the button
if (gesture.state == UIGestureRecognizerStateEnded)
{
//end recording
}
}
Also you can try or use the touchEvent concept
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)e {
// show touch-began state
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)e {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)e {
UITouch *touch = [touches anyObject];
.....
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)e {
}
As user3182143 said, using a UILongPressGestureRecorgnizer will solve your problem, but if you are interested there is another way using a UIButton. No need to add a UILongPressGestureRecorgnizer!
From your storyboard, drag an IBAction for a UIButton. And while you are adding its name, change the event to Touch Down.
Now drag another IBAction for the same UIButton and while changing its name, change the event to Touch up Inside (if it isn't that already).
- (IBAction)touchDownButtonAction:(UIButton *)sender {
NSLog(#"Start");
}
- (IBAction)touchUpInsideButtonAction:(UIButton *)sender {
NSLog(#"End");
}
Handle your recording based on the actions!
Here is a screenshot just in case:
So, I have a custom gesture recognizer added to a skmap, but the behavior is not consistent but rather weird. At first, both map and my gesture recognizer respond to gesture events.
But after panning or pinch to rotate & zoom, only gesture recognizer responds to gestures. While map does not respond to any gesture like pinch/panning/tap. This I call it a "locked" state.
In this state, map does not respond to anything. Trying to rotate/pinch will "unlock" it. But trying to pan will not. So for example, the sequence is, I pan the map, it follows. Then I pan the map several times, it does not move at all. Then I try to rotate it, the map stays still. Then I pan it the third time, it can follow my gesture again.
So my question is, what is the difference between this two state, what changes happened to the map when I move/try to rotate it.
My code is very simple, just for test purpose. myGestureRecognizer.m :
#import "myGestureRecognizer.h"
#import <MapKit/MapKit.h>
#implementation myGestureRecogniczer
-(id)init;
{
self = [super init];
self.delegate = self;
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Began");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Moved");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Canceled");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Ended");
}
#end
And mapViewController.m :
-(void)viewDidLoad
{
[super viewDidLoad];
self.myMapView = [[SKMapView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
self.myMapView.delegate = self;
self.myMapView.calloutView.delegate = self;
[self.view addSubview:self.myMapView];
myGestureRecogniczer *gestureRecognizer = [[myGestureRecogniczer alloc] init];
[self.myMapView addGestureRecognizer:gestureRecognizer];
}
I'm making a game using Xcode for iOS. This is a segment of code I have that makes the sprite jump up when the screen is tapped:
//tap/touch to jump (& play sound)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{
[self playSound];
jumpUp = 16;
}
How can I implement it so that the sprite just keeps going up whist you are touching the screen instead of just a single tap?
//Pseudo code:
while touchingScreen {
jumpUp +=1;
}
You need to start some kind of loop in touchesBegan that runs while the touch is lasting. Then in touchesEnded (and cancelled!) make that loop stop. You can use a repeating NSTimer or something like the following. You may need to do some adjustments to make it smooth enough for a game.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
// touching is a BOOL that keeps track of the touch event
// YES means that a touch is happening at the moment
touching = YES;
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:#selector(up) withObject:nil afterDelay:.0];
});
}
- (void)up {
// move your sprite further up
NSLog(#"up");
if (touching) {
// if the user is still touching repeat moving the sprite up
[self performSelector:#selector(up) withObject:nil afterDelay:0.1];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
// The finger was lifted, stop the up movement
touching = NO;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
touching = NO;
}
I am doing some custom drawing a UITableViewCell that I have subclassed. In there there is an OpenGL ES 2.0 control that needs user interaction to work... now if I start dragging horizontally the control responds but as soon as I go in the vertical direction then it starts to move the table view's viewport. How do I stop this interaction from going to the UITableView and limit it to my own UIView in the UITableViewCell?
You can try to subclass UITableView and override following methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// if user tapped on your custom view, disable scroll
self.scrollEnabled = NO;
// end if
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.scrollEnabled = YES;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
self.scrollEnabled = YES;
[super touchesCancelled:touches withEvent:event];
}
I don't think you can prevent user interaction on the UITableView, since it would affect all the subviews.
I'll try to send the UITableView interaction to the method you want it to use it.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[MyClass myScrollDidStartMethod];
}
or use - (void)scrollViewDidScroll:(UIScrollView *)sender so that you can work on the sender's contentOffset to get the scrolling direction (see this post for more info about this)
How do we detect a tap & hold on a UITableViewCell?
In iOS 3.2 or later you can use UILongPressGestureRecognizer
Here's the code lifted straight from my app. You should add these methods (and a boolean _cancelTouches member) to a class you derive from UITableViewCell.
-(void) tapNHoldFired {
self->_cancelTouches = YES;
// DO WHATEVER YOU LIKE HERE!!!
}
-(void) cancelTapNHold {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(tapNHoldFired) object:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self->_cancelTouches = NO;
[super touchesBegan:touches withEvent:event];
[self performSelector:#selector(tapNHoldFired) withObject:nil afterDelay:.7];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
if (self->_cancelTouches)
return;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self cancelTapNHold];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
[super touchesCancelled:touches withEvent:event];
}
//Add gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):
// Add long tap for the main tiles
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTap:)];
[tile addGestureRecognizer:longPressGesture];
[longPressGesture release];
-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(#"gestureRecognizer= %#",gestureRecognizer);
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(#"longTap began");
}
}
You should probably handle the UIControlTouchDown event and depending on what you mean by "hold", fire a NSTimer that will count an interval since you initiated the touch and invalidate upon firing or releasing the touch (UIControlTouchUpInside and UIControlTouchUpOutside events). When the timer fires, you have your "tap & hold" detected.