GMSMapView catch only first touch event - iphone

touchBegin is called only once after viewWillAppear method executed.
I have added UITouch and UISwipe recognizer, enabled user interaction and enabled all gestures but GMSMapView respond only once.
self.mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 320, 206) camera:nil];
[self.mapView_ setUserInteractionEnabled:YES];
[self.mapView_.settings setAllGesturesEnabled:YES];
[self.view addSubview: mapView_];
[self.view sendSubviewToBack:self.mapView_]; // I have to do this because
self.mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
UIGestureRecognizer *uiGestureRecognizer = [[UIGestureRecognizer alloc]init];
uiGestureRecognizer.delegate = self;
UISwipeGestureRecognizer * gestureRecognizer = [[UISwipeGestureRecognizer alloc]init];
gestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp & UISwipeGestureRecognizerDirectionRight & UISwipeGestureRecognizerDirectionLeft & UISwipeGestureRecognizerDirectionDown;
[gestureRecognizer addTarget:self action:#selector(selecterMethod)];
self.mapView_.gestureRecognizers = [NSArray arrayWithObjects:uiGestureRecognizer,gestureRecognizer, nil];

Related

UITapGestureRecognizer doesn't work in SKView

I have in my view controller this code running scene :
SKView * _skView;
ClassicGameScene* _scene;
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
if (!_skView) {
_skView = [[SKView alloc] initWithFrame:self.view.bounds];
ClassicGameScene *scene =[[ClassicGameScene alloc] initWithSize:_skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[_skView presentScene:scene];
[self.view addSubview:_skView];
[self.view setUserInteractionEnabled:YES];
_scene = scene;
};
}
And in my scene I have this one :
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
[self createSceneContents];
}
return self;
}
-(void) createSceneContents
{
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:singleTap];
[self addBackground];
[self addLabels];
[self preparePlocha];
}
But when I tap on scene, my gesture recognizer doesn't
work (tap procedure is not fired - tested through breakpoint). Can someone help me, please?
Thank you.

Multiple gestureRecognizers on views- one shouldn't pass touch

On parentClass(inheriting from UIView) i have:
[self addGestureRecognizer:_tapGesture]; // _tapGesture is UITapGestureRecognizer, with delegate on parentClass
On someClass:
[_myImageView addGestureRecognizer:_imageViewGestureRecognizer]; // _imageViewGestureRecognizer is UITapGestureRecognizer, with delegate on someClass
The problem is that when i tap on myImageView both gesture recognizers are firing. I want only _imageViewGestureRecognizer to work.
I've tried:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch {
UIView *gestureView = recognizer.view;
CGPoint point = [touch locationInView:gestureView];
UIView *touchedView = [gestureView hitTest:point withEvent:nil];
if ([touchedView isEqual:_imageViewGestureRecognizer]) {
return NO;
}
return YES;
}
But it ofc doesn't take upon consideration gesture recognizer from super class.
I did this little test and it worked perfectly...
#implementation View
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped1)];
[self addGestureRecognizer:tap];
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
img.userInteractionEnabled = YES;
img.frame = CGRectMake(0, 0, 100, 100);
[self addSubview:img];
UITapGestureRecognizer* tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped2)];
[img addGestureRecognizer:tap2];
return self;
}
-(void)tapped1 {
NSLog(#"Tapped 1");
}
-(void)tapped2 {
NSLog(#"Tapped 2");
}
#end
What you wanted was iOS's default behaviour. Once a subview has taken care of a touch, its superview won't receive the touch anymore. Did you set userInteractionEnabled on the imageView?

Adding Alertview with coordinates of the Pressed place in Mapview

Im Having MapView.. I added this as a subview of ViewController's View. I have the following Code in ViewDidLoad:
[self.view addSubview:mapView];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(mapLongPress:)];
longPressGesture.minimumPressDuration = 2;
[mapView addGestureRecognizer:longPressGesture];
[longPressGesture release];
And ,
- (void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(#"Gesture");
if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
CGPoint touchLocation = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D coordinate;
coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];
These i Got from StackOverFlow.. But Its not Working.. Did i need to do anything more in that?
Try adding [self.view addSubview:mapView]; after [mapView addGestureRecognizer:longPressGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(mapLongPress:)];
longPressGesture.minimumPressDuration = 2;
longPressGesture.delegate = self;
[self.mapView addGestureRecognizer:longPressGesture];
[self.view addSubview:mapView];
[longPressGesture release];
Just add the UIGestureRecognizerDelegate to ViewController and do the above code given by me!!!
Try this:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(mapLongPress:)];
lpgr.minimumPressDuration = 2.0;
[self.mapView addGestureRecognizer:lpgr];
This is using ARC, so I am not sure if you should release the gesture at this moment right now. Try it without releasing it, then try it with releasing it. See if that affects the gesture.

detect a touch down event on UIImageView(s)

I placed 4 UIImageView on UIView and I named them
UIImageView myImg1 = [[UIImageView alloc] init];
UIImageView myImg2 = [[UIImageView alloc] init];
UIImageView myImg3 = [[UIImageView alloc] init];
UIImageView myImg4 = [[UIImageView alloc] init];
How can I tell the compiler that I have just touched down UIImageView 1 or UIImaveView 2. If I only can use UIImageView to do this task, not buttons at all. Please guide me about this issue. Thanks
A stylish solution is to use the UIGestureRecognizer object:
in your loadView method (or anywhere you code the UIImageView(s) drawing...):
UITapGestureRecognizer *tapRecognizer;
UIImageView *anImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
[anImageView setTag:0]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
[anImageView setBackgroundColor:[UIColor redColor]];
[anImageView setUserInteractionEnabled:TRUE];
tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewDidTapped:)] autorelease];
tapRecognizer.numberOfTapsRequired = 1;
[anImageView addGestureRecognizer:tapRecognizer];
[contentView addSubview:anImageView];
[anImageView release];
UIImageView *anotherImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 180, 100, 100)];
[anotherImageView setTag:1]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
[anotherImageView setBackgroundColor:[UIColor greenColor]];
[anotherImageView setUserInteractionEnabled:TRUE];
tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewDidTapped:)] autorelease];
tapRecognizer.numberOfTapsRequired = 1;
[anotherImageView addGestureRecognizer:tapRecognizer];
[contentView addSubview:anotherImageView];
[anotherImageView release];
This is a simple imageViewDidTapped: sample method that you can use to distinguish between the two sample UIImageView objects above:
- (void)imageViewDidTapped:(UIGestureRecognizer *)aGesture {
UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)aGesture;
UIImageView *tappedImageView = (UIImageView *)[tapGesture view];
switch (tappedImageView.tag) {
case 0:
NSLog(#"UIImageView 1 was tapped");
break;
case 1:
NSLog(#"UIImageView 2 was tapped");
break;
default:
break;
}
}
You should consider using the tag property for this. Give each image a unique tag, then use this information to determine which image was touched. For example, set the tags by
UIImageView myImg1 = [[UIImageView alloc] init];
myImg1.tag = 1;
and when you receive a touch for img, call
img.tag
to retrieve the tag.
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleFingerTap.numberOfTapsRequired = 1;
[myImg1 addGestureRecognizer:singleFingerTap];
[myImg2 addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
And action method
-(void)handleSingleTap:(UIGestureRecognizer*)sender{
//your code here
}
You can do like this.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if(CGRectContainsPoint(myImg1.view.frame,touchLocation))
{
NSLog(#" Image 1");
}
else if(CGRectCont..
Note..the NSLog will be outputted if the point lies in more than one image view..
in that case..check which image view is on top..

How to use gesture recognizer in an OpenGLES application of iPhone?

While I know how to use gesture recognizer in a view-based application,but when I apply the same ideas in a OpenGLSE-based application:
for example,
I add a TapGestureRecognizer,and when I tap on the EAGLView,it crashes.
So can anyone show me a standard usage of UITapGestureRecognizer in an OpenGLES-based application?
best wishes.
Here some sample code from one of my opengles games with gesture support. (Doesn't crash and hope it helps)
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = [[UIScreen mainScreen] bounds];
rect.size.height = 320;
rect.size.width = 480;
rect.origin.x = 0;
rect.origin.y = 0;
glView = [[EAGLView alloc] initWithFrame:rect pixelFormat:GL_RGB565_OES depthFormat:GL_DEPTH_COMPONENT16_OES preserveBackbuffer:NO];
[self.view addSubview: glView];
[glView addSubview: minimapView];
if(!shell->InitApplication())
printf("InitApplication error\n");
[NSTimer scheduledTimerWithTimeInterval:(1.0 / kFPS) target:self selector:#selector(update) userInfo:nil repeats:YES];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(Panned:)];
[glView addGestureRecognizer:[pan autorelease]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(Tapped:)];
[glView addGestureRecognizer:[tap autorelease]];
UITapGestureRecognizer *dbltap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(DoubleTapped:)];
[dbltap setNumberOfTapsRequired:2];
[glView addGestureRecognizer:[dbltap autorelease]];
UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LongPressed:)];
[glView addGestureRecognizer:[longpress autorelease]];
}
And the selector function
- (void) LongPressed:(UILongPressGestureRecognizer*)sender{
NSLog(#"Long Pressed");
}