Hi! My problem is not how to implement multiple custom gesture but how to handle multiple Custom Gestures. I have added a pinch custom gesture on corner points of the view and one finger rotation gesture on the view. For pinch custom gesture on corner points, I've used SPUserResizableView and for one finger custom gesture, I've used KTOneFingerRotationGestureRecognizer. But only one gesture is working. How can I resolve this? I've even tried using SPUserResizableView and default rotation gesture and it's not working. What may be the issue? Thank You.
may this links can help you. Simultaneous gesture recognizers in Iphone SDK
https://stackoverflow.com/questions/3160489/handle-tap-gesture-with-an-argument-iphone-ipad
Check it out it's all about gestures ,you doesn't need to use KTOneFingerRotationGestureRecognizer and SPUserResizableView.
You don't really need to use SPUserResizableView to handle the pinch gesture in the corners. You could probably get away with transparent subviews created programmatically, with the pinch gesture recognizer added to them.
Related
I am developing an iOS drawing application where the main feature is that a user can load an image as background and then he can start finger drawing on top of it, zooming and panning the image with the draws on top of the image itself.
The schema of the application is the following one.
MainUiView->UIScrollView->UIImageView
Fingers drawing and tracking is managed on the touchesBegan, touchesMoved, touchesEnded override of the UIResponder.
The Zoom and Panning of the UIImageView is managed by the UIScrollViewDelegate implementation.
I have a button that switches the app mode from Drawing to Zooming and Panning mode in the following way:
if you want to draw:
[scrollView setUserInteractionEnabled:NO];
[imageView setUserInteractionEnabled:YES];
so the touches are managed in the touchesBegan, touchesMoved, touchesEnded methods.
if you want to Pan and Zoom with two fingers using the Pinch to Zoom gesture
[scrollView setUserInteractionEnabled:YES];
[imageView setUserInteractionEnabled:NO];
so the two fingers gesture is managed by the UIScrollViewDelegate
Now I am trying to evolve the app removing the button to switch from draw to zoom and pan mode.
My idea was to track the two fingers presence in the touchesBegan and then to disable the user interaction in the UIImageView to enable the user interaction in the scrollview, but the problem is that after doing this the touches are still managed in the touchesMoved method, nothing comes to the scrollViewDelegate until I lift my fingers from screen, after that, if I punt fingers again on the screen everything is managed by the scrollViewDelegate.
So to sum up: is it possible to stop the touches cycle after touchesBegan and propagate it to the ScrollViewDelegate?
If this is not possible, how can I achieve the zoom, panning and drawing feature on a UImageView? Should I remove the scrollview and try to zoom and pan the UImageView through the UIResponder methods ?
Thanks for help
Try changing your implementation to use gesture recognisers instead of touch handling using the UIResponder methods.
Main benefits of this approach are :
You can use UIPanGestureRecognizer for drawing, or even create a
custom recogniser
You can have dependency relationships between recognisers, using
requireGestureRecognizerToFail, even across views
You can loop through all recognisers on a view and disable only specific set of gestures without disabling touches on the entire view
Gesture recognisers have the same touch related methods, which you can use to move your touch handling code from current implementation over to the gestures
So, your image view will have either pan or your custom gesture recogniser, with single touch, for drawing, and you can loop through scrollview's recognisers, and mark them not trigger if 'draw' gesture is triggered, using requireGestureRecognizerToFail.
With this, you do not need to enable/disable user interaction on any of the views, although you will have to do a it of trial-and-error to get all gestures to work with each other correctly.
Happy coding :)
I'm stuck with a problem with pinch gesture. So I have a pinch gesture on an UIImageView which is itself in a collectionView, and when the user starts zooming, I add new, separates ViewController to control the zoom and pan gestures on the image, so I add my UIImageView to the new ViewController's view. The problem is, when I change my UIImageView hierarchy, the pinch gesture attached to it stops working, so the user has to take off his fingers and start pinch again in order to zoom the image. So basically, I am searching solutions to one off these:
1. Make pinch gesture work right when changing gesture.view hierarchy
OR
2. Make pinch gesture work right when changing gesture.view. So another way is to add a new view to the new ViewController and attach gesture to that view. But it still resets the gesture touches and you need to start it again.
If anyone has any suggestions, please help. Thanks a lot.
Ok, I found the answer. You just need to set userinteraction of vc's view to NO. Works like a charm.
I created two custom gesture recognizer.
PinGestureRecognizer. this gesture is using for drag the view in horizontal direction.
TagGestureRecognizer. this gesture is using for increase the height of the view till user drag the view in vertically.
Now, i want to fail one gesture if another gesture got recognized.
Example:
if user drag the view horizontally in >=10 pixel need to recognized PinGestureRecognizer and fail TapGestureRecognizer and vice versa.
How can i achieve the functionality.
I know iOS7 provide the methods like
shouldBeRequiredToFailByGestureRecognizer:
shouldRequireFailureOfGestureRecognizer:
If its equal to my problem or not i was not able to understand. if its correct means kindly suggest me to get same functionality in iOS4 and above.
Thanks in advance....
Use the gestureRecognizerShouldBegin delegate method for the PinGestureRecognizer, for example:
// To handle not scrolling vertically
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint translation = [panGestureRecognizer translationInView:myView];
return fabs(translation.y) < fabs(translation.x);
}
I'll try and keep it simple.
I have a UIScrollview with around 10 images attached. I currently have it so that i can touch an image and drag it around on the scroll view.
I did this by creating the subclass UIImageview and implementing the touchesMoved etc. I can still scroll the view fine, but the problem comes when trying to drag an image too fast. It seems the program first checks if the view is being scrolled and then fires touchesMoved in the UIImage class.
Is there anyway I can switch this around so that the first check is if an image is touched, then if not pass the response onto the scrollview.
Any help would be great. Thanks.
The simplest way to do this would be use one finger to move an image, and two fingers to scroll the view.
If you're on iOS 5, this is super easy:
self.scrollView.panGestureRecognizer.minimumNumberOfTouches = 2;
If you want to support older versions of iOS, you have to do a little more work:
for (UIGestureRecognizer *gesture in self.scrollView.gestureRecognizers){
if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]){
((UIPanGestureRecognizer *)gesture).minimumNumberOfTouches = 2;
}
}
If you want to use one-finger gestures for both, there are a few ways to do it. You could attach a UIPanGestureRecognizer to each image view. You might need to tell the scroll view's own UIPanGestureRecognizer to defer to the image view recognizers, using the requireGestureRecognizerToFail: message.
Another way would be to set the scroll view's UIPanGestureRecognizer's delegate to an object you create that implements the gestureRecognizer:shouldReceiveTouch: method. In that method, you can check whether the touch's view is one of your image views. If so, return NO to prevent the scroll view's pan gesture recognizer from activating.
Your question is a little confusing to me but I will assume that you have a UIScrollView with 10 UIImageViews inside it which you want to drag around.
My suggestion would be to use a gesture recognizer (UIPanGestureRecognizer) attached to every UIIImageView in order to implement the dragging behaviour. I find gesture recognizers to be a more solid approach on this kind of behavior.
If you don't know how to work with gesture recognizers, I can post a short code example to demonstrate how you can drag any type of UIView. Just ask in a comment and I will write it.
I have a grid of thumbnails each of which is a UIView. Each thumbnail has a pan gesture attached. If a pan gesture begins on one thumbnail, subsequent thumbnails do not acknowledge the pan gesture even though my finger is indeed moving across them. How do I get all thumbnails to react to a pan gesture even if the gesture did not initiate in the thumbnail?
Thanks,
Doug
I'm assuming what you're trying to do is basically scrolling through a 2d array of thumbnails? If that's the case, try putting your thumbnails into a UIScrollView. This should take care of panning through pictures. Then put UITapGestureRecognizers in each individual thumbnail if you want to perform some action upon tapping a thumbnail.
Attach the pan gesture to the superview, and use hitTest:inPoint: to determine which thumbnail the gesture is moving through.