Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-02-2010, 05:36 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 16
el3ktro is on a distinguished road
Default Question about UIGestureRecognizer

Hi,
I have the following situation: One view covering the whole screen, several subviews all over the place.

In the view controller (which controls the large root view) I've implemented several gesture recognizers, like this:

Code:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[[self view] addGestureRecognizer:singleTap];
[singleTap release];
I then have implemented the selector method in the view controller like this:

Code:
- (void)didSingleTap:(UITapGestureRecognizer*)gestureRecognizer {
}
This works all fine. What I need to do though: In didSingleTap I have to find out if the tap has occurred on top of one of the subviews. I know I can get the location of the touch with locationInView:, but this just gives me a coordinate. I need to get the actual UIView object on which the touch has occurred, so that I can manipulate the object that has been touched.

Before I have used gesture recognizers, I manually implemented touchesBegan:WithEvent: etc. in the root view, which worked fine and which actually gives me a UITouch object that contains a reference to the object on which the tap has occurred. But this means I have to manually implement the view, which I don't need and want (if possible).

One thing I thought of was to actually implement the gesture recognizers in each subview and use the root view controller as their action target. Would this work?
el3ktro is offline   Reply With Quote
Old 08-16-2010, 05:57 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 2
A Person is on a distinguished road
Default

Quote:
Originally Posted by el3ktro View Post
Hi,
I have the following situation: One view covering the whole screen, several subviews all over the place.

In the view controller (which controls the large root view) I've implemented several gesture recognizers, like this:

Code:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[[self view] addGestureRecognizer:singleTap];
[singleTap release];
I then have implemented the selector method in the view controller like this:

Code:
- (void)didSingleTap:(UITapGestureRecognizer*)gestureRecognizer {
}
This works all fine. What I need to do though: In didSingleTap I have to find out if the tap has occurred on top of one of the subviews. I know I can get the location of the touch with locationInView:, but this just gives me a coordinate. I need to get the actual UIView object on which the touch has occurred, so that I can manipulate the object that has been touched.

Before I have used gesture recognizers, I manually implemented touchesBegan:WithEvent: etc. in the root view, which worked fine and which actually gives me a UITouch object that contains a reference to the object on which the tap has occurred. But this means I have to manually implement the view, which I don't need and want (if possible).

One thing I thought of was to actually implement the gesture recognizers in each subview and use the root view controller as their action target. Would this work?
Well one thing you could do is in the
Code:
- (void)didSingleTap:(UITapGestureRecognizer*)gestureRecognizer {
}
you could either make CGRects that contain the bounds of your subviews and then go
Code:
- (void)didSingleTap:(UITapGestureRecognizer*)gestureRecognizer {
      CGPoint touchPoint = [gestureRecognizer locationInView:nil];
      if(CGRectContainsPoint(subViewRectOne, touchPoint)) {
         // do something
      }
}
and then just have if statements like that for every subview and if there is too many sub views then maybe try a for each or just a regular for loop.
A Person is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 353
13 members and 340 guests
ajay123123, Anwerbl, Arty Tales, ashaman64, baja_yu, ChrisYates, HemiMG, mini998, mottdog, newDev, Objective Zero, oceanlablight, Steven.C
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,878
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:59 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0