*newbie* xcode4, objects palette, * Gesture Recognizer implementation question.

Status
Not open for further replies.
I have been slowly getting in to a smidge of ios development, and am at the point where I am trying to implement some gestures. Reading online, there seem to be a lot of ways to do it, but I cannot find information on one that seems to be built in to interface builder.

6425855241_8f9eb639a1_z.jpg


So far, what I have done is this: I drag a gesture recognizer in to my view, then (not knowing better/not finding anything on this method of adding in gestures), I do the control click drag in to the views .h file to get a reference to it inserted. I'm pretty sure I'm going wrong here, even though I have gotten gesture recognition - but I don't understand how to configure/customize it and it doesnt seem to work the same way as I am seeing the old/regular way to do it online.

I do not mind doing it the other way, but I am mostly curious how to utilize this IB feature.

6425855303_4cc52258a4.jpg


6425855399_370f99a4bd_b.jpg


Any help or a link to an explanation would be great! Thanks.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
I gave up trying to wire stuff up in Xcode. I invariably end up in a position wondering why the hell I can do something that should be easy, and end up creating a bunch of object references I shouldn't need. I find it much easier to just code the things directly in the first place. (and it's much easier to find examples that work as expected instead of poorly written text or out dated videos showing the tools and mouse actions required)
 

HitScan

Ars Tribunus Militum
2,523
Subscriptor++
I don't have an opinion on whether you should be doing it in the XIB viewer or in code, but you need most likely need to connect the sent actions selector outlet to an Action. You don't actually need an outlet for the recognizer in your code (unless you need to do more than respond to it firing)

The same applies to code generated recognizers, you just need to use initWithTarget:action: and they should do their thing.

And you'll want to look at this if you haven't already: http://developer.apple.com/library/ios/ ... izers.html
 

Ars Moriendi

Ars Tribunus Militum
2,453
HitScan":3t626jkq said:
I don't have an opinion on whether you should be doing it in the XIB viewer or in code, but you need most likely need to connect the sent actions selector outlet to an Action. You don't actually need an outlet for the recognizer in your code (unless you need to do more than respond to it firing)

The same applies to code generated recognizers, you just need to use initWithTarget:action: and they should do their thing.

And you'll want to look at this if you haven't already: http://developer.apple.com/library/ios/ ... izers.html


I've look at that, it didnt seem to go beyond mentioning it (could be wrong, still getting used to all the terminology)
 

HitScan

Ars Tribunus Militum
2,523
Subscriptor++
Ars Moriendi":2ijsszed said:
HitScan":2ijsszed said:
I don't have an opinion on whether you should be doing it in the XIB viewer or in code, but you need most likely need to connect the sent actions selector outlet to an Action. You don't actually need an outlet for the recognizer in your code (unless you need to do more than respond to it firing)

The same applies to code generated recognizers, you just need to use initWithTarget:action: and they should do their thing.

And you'll want to look at this if you haven't already: http://developer.apple.com/library/ios/ ... izers.html


I've look at that, it didnt seem to go beyond mentioning it (could be wrong, still getting used to all the terminology)
Well, that page doesn't discuss anything about visual layout, but it should be helpful when it comes to writing a handler for your recognizer. Then you just mark that handler as an IBAction in the view's .h file, and connect the selector outlet to it. (Should be in File's Owner I think?)

Unfortunately, I've not played with them much either so I'm half-guessing (also can't test because I'm not near a Mac at the moment), but I really want to take a closer look at recognizers and storyboards.
 
To wire up your gesture recognizer, drag it from the "Objects" section and drop it onto the view you want to recognize the gesture. In the "Outlet Collections" section for your view you'll see a "gestureRecognizers" entry connected to the gesture recognizer.

In the view's class, add a - (IBAction)handleTapGesture:(UITapGestureRecognizer *)sender; method to perform whatever action you want to happen when the view is tapped.

Finally, back in IB, point the gesture recognizer's selector to "File's Owner" and choose the "handleTapGesture:" method.
 

Ars Moriendi

Ars Tribunus Militum
2,453
What I finally ended up doing/using:

Code:
    UITapGestureRecognizer *doubleTap = 
    [[UITapGestureRecognizer alloc]
     initWithTarget:self 
     action:@selector(tapDetected:)];
    doubleTap.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:doubleTap];

I dont know if this is the best way, but it keeps the all the code for the gestures in the .m (in viewDidLoad), and the code seems basically agnostic to which view the code is in.
 
Status
Not open for further replies.