UIPanGestureRecognizer Collision - iphone

I have 6 UIImageViews each connected to UIPanGestureRecognizer and they are all connected to the same method. The method is:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
I am following Ray Wenderlich's tutorial on using GestureRecognizers. So, I was wondering how to detect collisions so that when one image collides with another image, some code is run. The code is different for each image.
Thanks

If you want move the image with the recognizer maybe you should attach the recognizer to your view.
Belonging to this, the fastest way to do this, is (in the method that change the frame at your UIImageView)
for (UIImageView *iv in _imageArray){
if (CGRectIntersectsRect(iv.frame, _selectedImageView.frame)) {
NSLog(#"Collision");
}
}
_selectedImageView is the image that your are moving and _imageArray is an array that contains all your UIImageView (in your case are 6).

Related

Get UIPinchGestureRecognizer finger positions

Im using the UIGestureRecognizer to transform a view and its workin perfectly, but now I want to use it to transform my view width and height independently. The only way that comes to my mind to solve this is getting the two finger positions and make an if clause to recognize if the user is trying to increase width or height, but for this I need to get each finger position involved in the Pinch Gesture. But I cant find any method to do this I was wondering if this is posible or if there is another alternative for achieving this.
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, 1);//To transform height insted of width just swap positions of the second and third parameter.
NSLog(#"%f",recognizer.scale);
recognizer.scale = 1;
}
Got the answer if somebody needs to do this, there is a method called location of touch. With this method you can get each touch x and y position. But call it when the Gesture recognizer state began because it crashes if you do it in the state changed. Save this values in some variables and you are good to go. Hope it helps someone who is interested.
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
if(recognizer.state == UIGestureRecognizerStateBegan){
NSLog(#"pos : 0%f, %f",[recognizer locationOfTouch:0 inView:self.view].x,[recognizer locationOfTouch:0 inView:self.view].y);
NSLog(#"pos 1: %f, %f",[recognizer locationOfTouch:1 inView:self.view].x,[recognizer locationOfTouch:1 inView:self.view].y);
}
if(recognizer.state == UIGestureRecognizerStateChanged){
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, 1);
//NSLog(#"%f",recognizer.scale);
recognizer.scale = 1;
}
if(recognizer.state == UIGestureRecognizerStateEnded){
}

Trying to move UIImageView on UIPanGestureRecognizer

Hi Guys I am trying to move UIImageView using the following code on UIPanGestureRecognizer
CGPoint pointMove;
pointMove.x = holderView.center.x + (currentPoint.x - lastPoint.x);
pointMove.y = holderView.center.y + (currentPoint.y - lastPoint.y);
holderView.center = pointMove;
lastPoint = currentPoint;
But this is giving a jumble on moving not moving smoothly.
Please help me
i got a best example of Dragging Image using UIPanGestureRecognizer
please download this demo and check it :-
https://github.com/elc/iCodeBlogDemoPhotoBoard
hope it's help's you
here are some same Asked Question please visit link:-
Drag + Rotation using UIPanGestureRecognizer touch getting off track
swapping images using pan gesture
How to use UIPanGestureRecognizer to move object? iPhone/iPad
Try to change this in your code.
CGPoint translation = [gestureRecognizer translationInView:[holderView superview]];
[holderView setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
I hope this will solve your problem

iPhone4 iOS5 UIRotationGestureRecognizer, how to remember rotation offset for subsequent rotations?

I'm trying to create a knob-like behavior in one of my views with a UIRotationGestureRecognizer. This works, and positions the view as expected. However, every single time a gesture is performed, the rotation of the recognizer resets, so the knob starts at 0 every time.
How can I remember the last rotation of the UIRotationGestureRecognizer to let the user adjust the knob UIView without resetting it every single time?
I'm trying to make the recognizer start calculating rotation changes from the view's last known rotation:
knob starts at 0, recognizer is at 0
recognizer is rotated to 45 degrees
recognizer stops rotating
the knob is left at 45 degrees //this is already happening with the provided code snippet
next touch:
//this is what's is happening
recognizer starts at 0, rotates the knob back to 0
//I want it to be:
recognizer starts at 45, rotates the knob as in the example above.
- (IBAction)rotateView:(id)sender {
if([sender isKindOfClass:[UIRotationGestureRecognizer class]])
{
UIRotationGestureRecognizer* recognizer = sender;
CGAffineTransform transform = CGAffineTransformMakeRotation([recognizer rotation]);
rotatingView.transform = transform;
}
}
You should be able to get the current rotation of the rotatingView from it's transform property. Store this value into a savedRotation variable when the gesture begins. Make sure to assign a delegate to handle the gestureRecognizerShouldBegin callback.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)recognizer
{
savedRotation = atan2(rotatingView.transform.b, rotatingView.transform.a);
return YES;
}
- (void)rotateView:(UIRotationGestureRecognizer*)recognizer
{
rotatingView.transform = CGAffineTransformMakeRotation(recognizer.rotation+savedRotation);
}
Transform the transform:
rotatingView.transform = CGAffineTransformRotate(rotatingView.transform, [recognizer rotation]);
You have to make sure that you reset the rotation after the transform. Otherwise they will stack on top of each other and you get the "interesting" behavior.
rotatingView.transform = CGAffineTransformScale(rotatingView.transform, recognizer.scale, recognizer.scale);
[recognizer setRotation:0]; // this line
You should also do this for any translation or scaling transformations that you may do when handling gestures. The methods there are:
[recognizer setScale:1];
[recognizer setTranslation:CGPointZero inView:recognizer.view.superview];

How to make dragging faster using velocityView in UIPanGestureRecognizer?

Having a little trouble understanding how to implement a faster dragging speed for the code I'm using below (written by #PaulSoltz) which allows you to drag an object across the screen. I realize you have to use the velocityInView method from UIPanGestureRecognizer, and I understand it returns the x velocity vector and y velocity vector. If velocity = distance over time, then for instance velocityx = (x2 - x1) / time and I'm unsure how to use this formula to get what I need. Basically I just want to be able to adjust the speed of my movement to make it a little faster. Maybe I'm over thinking things, but if anyone could help me understand this it would be appreciated. Thanks.
- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer {
UIView *myView = [gestureRecognizer view];
CGPoint translate = [gestureRecognizer translationInView:[myView superview]];
if ([gestureRecognizer state] == UIGestureRecognizerStateChanged || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[myView setCenter:CGPointMake(myView.center.x + translate.x, myView.center.y + translate.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[myView superview]];
}
}
Just multiply the components of the translation vector by some constant.
[myView setCenter:CGPointMake(myView.center.x + translate.x * 2, myView.center.y + translate.y * 2)];

swipeGesture, touchMoved are called sametime?

i want to do different operation in SwipeGesture and TouchMoved.But both are called
when we swipe.any help please?
Drecoginizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipeFromD:)];
Drecoginizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:Drecoginizer];
As I understood you do not want the selector to react on swipe gesture if this swipe is outside of the circle. In this case, in the beginning of the swipe selector you should check if the swipe is over the circle something like so:
CGPoint lClick = [recognizer locationOfTouch:0 inView:self.view];
//Distance from the center of the circle to the taped point
int lDistance = sqrt(pow(lClick.x - lCircleCenter.x, 2) + pow(lClick.y - lCircleCenter.y, 2));
if ((int)lDistance > (int)lCircleRadius) {
return;
}
Hope it helps you