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.
Related
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 have a UIView in which I draw with a finger, it all goes fine and dandy, but I need this view to be scrollable. So I made a UIScrollView, and embedded my drawing view in it.
But here comes the problem, I can't draw anymore since every tap goes to UIScrollView and it intercepts them for scrolling purposes.
What I want to do, is to make UIScrollView only intercept two finger swipes and with single touches and swipes just draw regularly.
How do I do that?
I draw by using simpe touchesBegan, touchesMoved methods by using BezierPath, without gesture recognizers.
Check out answer https://stackoverflow.com/a/3173290/1492816 in Scrolling with two fingers with a UIScrollView
People report it as working for the kind of situation you are in.
I have tried this, as suggested in that answer, but to no avail. It says that method recognizePan: is not found. I changed it to nil, and scrolling works with two fingers only as intended, by drawing functionality is missing.
I feel like the touches are not getting to my view. What can be done?
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.scrollView action:#selector(recognizePan:)];
panGestureRecognizer.maximumNumberOfTouches = 1;
[scrollView addGestureRecognizer:panGestureRecognizer];
I have been looking to all the other similar topics here, using UIGestureRecognizers, using hitTest:withEvent, pointInside:withEvent: etc. but nothing seems to be ok for what I need to achieve.
Basically I have a main view (self.view of a common UIViewController) and a small rectangular UIScrollView attached onto it at the bottom: the scrollView is filled with some UIImageViews and the user can scroll it as usual.
But the user should also be able to drag one UIImageView (or a copy of it) from the UIScrollView to the main view, and, this is what I am finding really difficult, with the SAME dragging gesture, hence I need a way to:
1) Distinguish between normal horizontal scrolling gesture, which should be handled by the UIScrollView the usual way and a dragging gesture over the image view.
2) Once identified a dragging gesture, should propagate the touch to the superview, which will host a copy of the UIImageView and WITH the SAME dragging gesture continue the dragging over the main view even out of the bounds of the UIScrollView.
Please note that I know that if the UIScrollView has userInteractionEnabled = NO the touch is propagated to the subviews, but 1) I want to propagate it to the superview not the subviews, 2) the userInteractionEnabled property apparently becomes active only once the initial gesture is terminated, while I need to use a single dragging gesture.
Thank you very much for any help.
So, so far I have ended up implementing the touchesShouldBegin:withEvent:inContentView: method of my UIScrollView subclass but with delayContentTouches set to YES (default) instead of NO as #nhahtdh was suggesting.
Strangely enough even only implementing the method was sufficient for my subviews to intercept the dragging, and still my scrollview is scrolling properly, while with delayContentTouches set to NO I was not able to scroll it as all the subviews were starting to move around.
Really the credit for this is #nhahtdh, so man, if you post an answer I will accept it, thank you very much for your help.
I'm trying to come up with a UISCrollView in paging mode, where each page shows an image of a product and, besides being able to swipe between pages, I'd also like for the product image to switch to a "down" version of it when a specific product/page is tapped.
So far I tried the following:
1 - adding UIButtons as the pages of the scrollview: obviously, this way I can have the images switch to their "Selected" version on finger down, but the buttons, taking up the whole page, prevent the scrollview from detecting the swipe gesture.
2 - adding UIVIews instead of buttons, and an UITapGestureRecognizer: this way I can tap an image to select a product and the recognizer also lets the gesture pass on to the scrollview, allowing for swiping too. But the problem with this approach is that I can't switch images to their "selected" versions when the user touches them, since the tap recognizer only reports UIGestureRecognizerStateEnded.
Any ideas as to how to get both the button up/down and scrollview swipe behaviors?
Scrap everything (well not everything, but you get the gist). No gesture recognizers no nothing. I recently had this problem too, having mounted a UIView on a UIScrollview that needed to have some interactive elements in it. The solution, UIView's friggin awesome property called exclusiveTouch. Exclusive touch takes all of the events from the scrollview, and ignores them if the event is inside the UIView, then passes them directly to your view. And because UIButton inherits from UIView, all you need is self.button.exclusiveTouch = YES
Pretty cool, huh!?