can implement UITapGestureRecognizer with array of image view? - iphone

UITapGestureRecognizer can implement with array of image? can it work same as single image?
how it work?

It works on a single view. You will have to use a single recognizer per view.
You can however use the same target-action pair to handle all the gesture recognizers. If you are looking for the gesture to behave the same way for all the image views then this is the easiest thing to do. If the action must identify the view and handle it appropriately, you can use the view property to complete the appropriate action.
If you are looking for how gesture recognizers work, read this.

Related

When to use IBAction vs. UITapGestureRecognizer?

I have seen storyboard buttons combined with IBAction code as well as UITapGestureRecognizers (not speaking here of programtically defined buttons).
I am curious if there is any (not strongly opinionated) reason to prefer one over the other in specific situations.
Buttons have their own tap action events.
All other UI elements have no.
E.g., if you want to handle label tap event you have to use gesture recognizers.
UIButtons have actions. you don't need tap gesture for it. You may need tap gesture for UILabel or UIView.

Confining a swipe gesture to a certain area (iPhone)

I have just managed to implement detection of a swipe gesture for my app. However I would like to confine the area where the gesture is valid. Thinking about this I came up with a possible solution which would be to check whether the start & finish coordinates are within some area. I was just wondering if there's a better or preferred method of doing something like this.
Simply create an invisible UIView (= with transparent background) and set its frame so it encloses the region you want to detect the gesture into.
Then, simply add a UISwipeGestureRecognizer to that view, and you are done.
Read the generic UIGestureRecognizer Class Reference and the part of the Event Handling Guide for iOS that talks about UIGestureRecognizers for more info.
Of course you could also manage the detection of the swipe gesture by yourself using custom code like explained here in the very same guide but why bother when UIGestureRecognizers can manage everything for you?

How to prevent simultaneous UIGestureRecognizers

I am using a UIPanGestureRecognizer on several card-like views to let the user move the views around the screen. It's very nice that they can put down 3 fingers and pickup 3 cards at once, however, some of my functionality isn't designed to work like that.
I'd like to only allow 1 gesture recognizer to run at a time. Is there a preferred way to do this?
I've considered:
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: but it already returns 'NO' by default.
Setting an instance variable when the first gesture begins, but I'm concerned about multithreaded access to this variable (Should I use #synchronized, or would it be too much overhead?).
Keeping an array of the gesture recognizers and checking their state in gestureRecognizerShouldBegin: to ensure that none are in progress.
Thanks.
The best practice is using one (global) gesture recognizer in view that's being superview for your cards with hitTest: for determining which card has been touched. It will allow you to work with multiple touches correctly.
Put a single UIPanGestureRecognizer on the common superview of all your cards, and then do hit detection to find the card in question when the gesture starts. That way you only have 1 gesture recognizer, so only one gesture can run at a time.
Edit: BTW, your idea of keeping an ivar, while clumsy, would work. UIGestureRecognizer is part of UIKit and only operates on the main thread, so you don't have to worry about multithreaded access. But like I said, it's clumsy. Using a single "master" UIGestureRecognizer instead is cleaner.

Alternative to UIScrollVIew

I have a quick question. I am trying to implement an application where the user can navigate between the screens via swipe gestures. So I am using gesture recognisers to push and pop views, the problem with that is that I don't want the transition animation to apply to the entire screen as there are certain similar components and it looks weird.
I considered using a scroll view, however I don't want to load all the controllers at the same time.
Any suggestions?
I'd suggest looking into UIGestureRecognizer and setting the UIScrollView property delaysContentTouches to NO to prevent the scroll view from consuming touch events before your gesture recognizer has the change to process the input, if you'd prefer that approach.
Don't forget that UIGestureRecognizer offers you quite a lot of information when the gesture fires.
I would suggest using a scroll view and load only the visible controller. You can then look for this delegate method, and load the other controller lazily
- (void)scrollViewDidScroll:(UIScrollView *)scrollView

iPhone: detecting double taps on uiscrollview

Besides subclassing, is there a simple means to detect double taps on a UIImageView within a UIScrollView?
Thanks
I have created ZoomScrollView class (a drop-in subclass of UIScrollView) that can help you intercept any touches from a scroll view, and also handles double-tap zooming out of the box if that's what you want to do.
Grab it at github.com/andreyvit/ScrollingMadness/ (the README contains a long description of two UIScrollView tricks and the reasoning behind them).
Of course, if you did not want to zoom, and just wanted to intercept a double-tap on some inner image view, then subclassing is your friend. (Another way would be to attach a view controller to that image view or one of its parent views inside UIScrollView, then the controller will be part of the responder chain and will be able to handle the touches.)
Looking at UIImageView.h (within the UIKit framework) there are no public delegate methods or other methods that let you know if the image view has been double-tapped. You'll probably have to subclass.
The answer is NO.
http://developer.apple.com/library/ios/#samplecode/ScrollViewSuite/Introduction/Intro.html
Download the sample code (download link on the top).
See how apple did it.
See you.