Trying to move UIImageView on UIPanGestureRecognizer - iphone

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

Related

Rotating UIImageView Moves the Image Off Screen

I have a simple rotation gesture implemented in my code, but the problem is when I rotate the image it goes off the screen/out of the view always to the right.
The image view that is being rotated center X gets off or increases (hence it going right off the screen out of the view).
I would like it to rotate around the current center, but it's changing for some reason. Any ideas what is causing this?
Code Below:
- (void)viewDidLoad
{
[super viewDidLoad];
CALayer *l = [self.viewCase layer];
[l setMasksToBounds:YES];
[l setCornerRadius:30.0];
self.imgUserPhoto.userInteractionEnabled = YES;
[self.imgUserPhoto setClipsToBounds:NO];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotationDetected:)];
[self.view addGestureRecognizer:rotationRecognizer];
rotationRecognizer.delegate = self;
}
- (void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer
{
CGFloat angle = rotationRecognizer.rotation;
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, angle);
rotationRecognizer.rotation = 0.0;
}
You want to rotate the image around it's center, but that's not what it is actually happening. Rotation transforms take place around the origin. So what you have to do is to apply a translate transform first to map the origin to the center of the image, and then apply the rotation transform, like so:
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, self.imageView.bounds.size.width/2, self.imageView.bounds.size.height/2);
Please note that after rotating you'll probably have to undo the translate transform in order to correctly draw the image.
Hope this helps
Edit:
To quickly answer your question, what you have to do to undo the Translate Transform is to subtract the same difference you add to it in the first place, for example:
// The next line will add a translate transform
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 10, 10);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, radians);
// The next line will undo the translate transform
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -10, -10);
However, after creating this quick project I realized that when you apply a rotation transform using UIKit (like the way you're apparently doing it) the rotation actually takes place around the center. It is only when using CoreGraphics that the rotation happens around the origin. So now I'm not sure why your image goes off the screen. Anyway, take a look at the project and see if any code there helps you.
Let me know if you have any more questions.
The 'Firefox' image is drawn using UIKit. The blue rect is drawn using CoreGraphics
You aren't rotating the image around its centre. You'll need correct this manually by translating it back to the correct position

limiting the panning bounds of UIPanGestureRecognizer

I have a view that I am currently hiding at the bottom of the screen. Now I want to be able to move the view by scrolling it vertically, through the y-axis. But I don't want it to go up further than full height of the view (i.e: I don't want to see a white space at the bottom). I've written this code:
- (IBAction)panHighlightReadingVC:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
CGPoint newCenter = CGPointMake(self.view.bounds.size.width / 2,
roundf(recognizer.view.center.y + translation.y));
CGFloat velocityY = [recognizer velocityInView:self.view].y;
if ((recognizer.view.frameY > self.view.frameHeight - recognizer.view.frameHeight || velocityY > 0)) {
recognizer.view.center = newCenter;
[recognizer setTranslation:CGPointZero inView:self.view];
}
}
This sort of work, if I scroll it slowly. If I scroll it very fast, then there is a chance that the frameY of the view is less than the `superView.frameHeight - recognizer.view.frameHeight. How do I fix this?
When you are calculating the newCenter, limit it to the bounds that you actually want it to fit in. You probably don't need that last if statement if you are limiting the newCenter to be within your limits.
Also, do yourself a favor and only use center when it actually makes things simpler. In your case I think you should use frame. Then you don't have to worry about dividing by 2.
Try something like this:
- (IBAction)panHighlightReadingVC:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
CGFloat newY = MAX(recognizer.view.frame.origin.y + translation.y, self.view.frameHeight - recognizer.view.frameHeight);
CGRect newFrame = CGRectMake(recognizer.view.frame.origin.x, newY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
recognizer.view.frame = newFrame;
[recognizer setTranslation:CGPointZero inView:self.view];
}
Also, velocity doesn't seem to apply in your situation, it only complicates things.

UIPanGestureRecognizer Collision

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).

How can I animate the road in iPhone Xcode?

I am going to animate the road, simply calling 1 image again and again from top to bottom. Can anyone suggest how this is possible?
You'll want to look at a UIScrollView. Put a UIImageView in it, then use the following code:
-(void)scroll{
CGPoint currentPoint = scrollView.contentOffset;
CGPoint scrollPoint = CGPointMake(currentPoint.x, currentPoint.y + 1);
[scrollView setContentOffset:scrollPoint animated:NO];
if(scrollPoint.y == scrollView.contentSize.height+5)//If we've reached the bottom, reset slightly past top, to give ticker look
{
[scrollView setContentOffset:CGPointMake (0,-125)];
}
}
Run that code with a looping NSTimer and you'll be set :)

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