UIPanGestureRecognizer beyondBounds - objective-c

I have this code that retrieves the coordinates of an object when it is panned:
UITapGestureRecognizer *moveBuildingTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(moveobject:)];
Method moveobject contents:
CGPoint tapPoint=[recognizer locationOfTouch:0 inView:self.view];
I use this to change the frame of - move it -an imageview using these coordinates.
However, upon dragging the image around - triggering the uipangesturerecognizer action, I found that when I drag it to the absolute bottom, I get an error that -[UIPanGestureRecognizer locationOfTouch:inView:]: index (0) beyond bounds (0).
How can I solve this exception and prevent the user from dragging past this point?
Thanks

It's weird that your moveobject: method gets called even though the private touches array of the gesture recognizer seems to be empty.
Anyway, in general, if you don't handle multitouch gestures within gesture recognizer, I would suggest to use [recognizer locationInView:] rather then locationOfTouch:inView:.
Btw:
Your talking about a UIPanGestureRecognizer while in the code you're using a UITapGestureRecognizer.
The code I would recommend to handle dragging of a particular view looks like this:
//...
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[someView addGestureRecognizer:panGR];
//...
- (void)handlePan:(UIPanGestureRecognizer *)gr
{
CGPoint translation = [gr translationInView:gr.view];
gr.view.frame = CGRectOffset(gr.view.frame, translation.x, translation.y);
[gr setTranslation:CGPointZero inView:gr.view];
}

You should check the numberOfTouches in your UIGestureRecognizer like:
if (recognizer.numberOfTouches) {
CGPoint tapPoint = [recognizer locationOfTouch:0 inView:self.view];
}

Related

Change selector while a pan gesture is going on

I have a UIView C which is a subview of UIView B wich is a subview of UIView A. I add a UIPanGestureRecogizer to UIView C with a selector called "selectorX" and I want that when UIView C goes out of UIView A frame, then its superview changes to UIView A and I want also to change its UIPanGestureRecognizer with another selector "selectorY".
This is my code:
-(void)selectorX:(UIPanGestureRecognizer*)sender{
CGPoint translation = [sender translationInView:self.view];
sender.view.center = CGPointMake(sender.view.center.x, sender.view.center.y +translation.y);
[sender setTranslation:CGPointMake(0, 0) inView:sender.view];
UIView *uiViewC=(ImagenFichaView *)sender.view;
if (sender.view.center.y+translation.y<-50) {
[uiViewC removeFromSuperView]
CGPoint newCenter=[sender locationInView:uiViewA];
[uiViewA addSubview:uiViewC];
uiViewC.center=newCenter;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(selectorY:)];
panRecognizer.delegate=self;
[uiViewC removeGestureRecognizer:sender];
[uiViewC addGestureRecognizer:panRecognizer];
}
}
Everything goes well but the transition between the 2 UIPanGestureRecognizers is not continuous. When uiViewC changes its superView, the drag action stops. I have to take off my finger from the screen and start the movement again. What can I do to make the movement continuos?
Thank you very much
To get a continuous movement I didn't remove uiViewC from uiViewB, I just added it to uiViewA and it solved the problem.

How can I get touch offset when using UITouch

How can I get touch offset when using UITouch? I am doing a project like a remote control. I have a view set to multi-touched and I want the view to act like a touch pad for Mac, so I need the touch offsets when people move to control the mouse. Any ideas?
This can be accomplished with a UIPanGestureRecognizer by measuring the diagonal distance from the center of the screen to the current touch location.
#import <QuartzCore/QuartzCore.h>
Declare the gesture and hook it to self.view so that the entire screen responds to touch events.
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(myPanRecognizerMethod:)];
[pan setDelegate:self];
[pan setMaximumNumberOfTouches:2];
[pan setMinimumNumberOfTouches:1];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:pan];
Then in this method, we use the gesture recognizers state: UIGestureRecognizerStateChanged to update and integer that measures the diagonal distance between touch location and screen center as the touch location changes.
-(void)myPanRecognizerMethod:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) {
CGPoint touchLocation = [sender locationOfTouch:0 inView:self.view];
NSNumber *distanceToTouchLocation = #(sqrtf(fabsf(powf(self.view.center.x - touchLocation.x, 2) + powf(self.view.center.y - touchLocation.y, 2))));
NSLog(#"Distance from center screen to touch location is == %#",distanceToTouchLocation);
}
}

MKMapView and the responder chain

I have an MKMapView with a single subview:
MKMapView *mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 200, 200)];
subView.backgroundColor = [UIColor grayColor];
[mapView addSubview:subView];
I would expect that because the subview does not handle any touch events, all touch events would be passed along to the parent map view (via the responder chain). I would then expect that panning and pinching in the subview would pan and pinch the map.
This, unfortunately, does not appear to be the case. Does anyone know of a way to get the map view in to the responder chain?
I realize overriding hitTest in my subview can achieve what I'm expecting here, but I can't use that approach because I have other gestures I need to respond to in the subview.
How about handling all gestures with UIGestureRecognizers (properly setup to ignore other gesture recognizer or to fire simultanously with them) added to your mapView and disabling userInteractionEnabled of your subview?
I use the following code to listen to Taps on the mapView without interfering with the standard gestures:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mtd_handleMapTap:)];
// we require all gesture recognizer except other single-tap gesture recognizers to fail
for (UIGestureRecognizer *gesture in self.gestureRecognizers) {
if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
UITapGestureRecognizer *systemTap = (UITapGestureRecognizer *)gesture;
if (systemTap.numberOfTapsRequired > 1) {
[tap requireGestureRecognizerToFail:systemTap];
}
} else {
[tap requireGestureRecognizerToFail:gesture];
}
}
- (void)mtd_handleMapTap:(UITapGestureRecognizer *)tap {
if ((tap.state & UIGestureRecognizerStateRecognized) == UIGestureRecognizerStateRecognized) {
// Get view frame rect in the mapView's coordinate system
CGRect viewFrameInMapView = [self.mySubview.superview convertRect:self.mySubview.frame toView:self.mapView];
// Get touch point in the mapView's coordinate system
CGPoint point = [tap locationInView:self.mapView];
// Check if the touch is within the view bounds
if (CGRectContainsPoint(viewFrameInMapView, point)) {
// tap was on mySubview
}
}
}

Objective C: Drawing with Fingers on UIScrollView

I am trying to make a sketch pad app.
I used UIScrollView for paging and UIImageView for the drawing.
I put the UIImageView on top of the scrollView but it's not added to UIScrollView so it will not scroll.
The issue now...
It's not writing when...
[scrollView setScrollEnable:YES];
[scrollView setUserInteractionEnabled:YES];
i need to set it to NO with the use of a button for it to write,
is there a way that i can scroll and write at the same time without using any button??
This is definitely not the correct way of building such an application.
A UIScrollView is meant for scrolling content not for drawing. And you don't need a UIImageView to draw content either, a simple UIView would be enough.
Here you're best bet would be to create one UIScrollView and disable it's scrolling because you'll be handling it with two fingers, while the drawing will be handled pan another gesture recognizer.
UIPanGestureRecognizer *twoFingerScrolling = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(onTwoFingerScroll:)] autorelease];
[twoFingerScrolling setMinimumNumberOfTouches:2];
[twoFingerScrolling setMaximumNumberOfTouches:2];
UIPanGestureRecognizer *oneFingerDraw = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(onOneFingerDraw:)] autorelease];
[oneFingerDraw setMinimumNumberOfTouches:1];
[oneFingerDraw setMaximumNumberOfTouches:1];
[yourScollView addGestureRecognizer:twoFingerScrolling];
[yourScollView addGestureRecognizer:oneFingerDraw];
And later on in your code you can easily process both events, the scrolling:
- (void)onTwoFingerScroll:(UIPanGestureRecognizer*)sender
{
// Calculate the content offset from the shifting that occured
//[yourScrollView setContentOffset:theContentOffset]
}
And the drawing (which can be done by the Quartz Tookit)
- (void)onOneFingerDraw:(UIPanGestureRecognizer*)sender
{
// Processing the drawing by using comparing:
if (sender.state == UIGestureRecognizerStateBegan)
{ /* drawing began */ }
else if (iRecognizer.state == UIGestureRecognizerStateChanged)
{ /* drawing occured */ }
else if (iRecognizer.state == UIGestureRecognizerStateEnded)
{ /* drawing ended /* }
}
Hope this helps.

How do I pass the user touch from one object to another object?

I am developing an application that allows the user at a certain point to drag and drop 10 images around. So there are 10 images, and if he/she drags one image onto another, these two are swapped.
A screenshot of how this looks like:
So when the user drags one photo I want it to reduce its opacity and give the user a draggable image on his finger which disappears again if he drops it outside of any image.
The way I have developed this is the following. I have set a UIPanGesture for these UIImageViews as:
for (UIImageView *imgView in editPhotosView.subviews) {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(photoDragged:)];
[imgView addGestureRecognizer:panGesture];
imgView.userInteractionEnabled = YES;
[panGesture release];
}
Then my photoDragged: method:
- (void)photoDragged:(UIPanGestureRecognizer *)gesture {
UIView *view = gesture.view;
if ([view isKindOfClass:[UIImageView class]]) {
UIImageView *imgView = (UIImageView *)view;
if (gesture.state == UIGestureRecognizerStateBegan) {
imgView.alpha = 0.5;
UIImageView *newView = [[UIImageView alloc] initWithFrame:imgView.frame];
newView.image = imgView.image;
newView.tag = imgView.tag;
newView.backgroundColor = imgView.backgroundColor;
newView.gestureRecognizers = imgView.gestureRecognizers;
[editPhotosView addSubview:newView];
[newView release];
}
else if (gesture.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [gesture translationInView:view.superview];
[view setCenter:CGPointMake(view.center.x + translation.x, view.center.y + translation.y)];
[gesture setTranslation:CGPointZero inView:view.superview];
}
else { ... }
}
}
Thus as you see I add a new UIImageView with 0.5 opacity on the same spot as the original image when the user starts dragging it. So the user is dragging the original image around. But what I want to do is to copy the original image when the user drags it and create a "draggable" image and pass that to the user to drag around.
But to do that I have to pass the user touch on to the newly created "draggable" UIImageView. While it's actually set to the original image (the one the user touches when he starts dragging).
So my question is: How do I pass the user's touch to another element?.
I hope that makes sense. Thanks for your help!
Well, you can pass the UIPanGestureRecognizer object to another object by creating a method in your other object which takes the gesture recognizer as a parameter.
- (void)myMethod:(UIPanGestureRecognizer *)gesture
{
// Do stuff
}
And call from your current gesture recognizer using....
[myOtherObject myMethod:gesture];
Not entirely sure I'm understanding your question here fully. :-/
Maybe:
[otherObject sendActionsForControlEvents:UIControlEventTouchUpInside];
Or any other UIControlEvent
In the end I decided to indeed drag the original image and leave a copy at the original place.
I solved the issue with the gesture recognizers I was having by re-creating them and assigning them to the "copy", just like PragmaOnce suggested.