I'm getting problem when I tried to move the after rotating the same image. My code is working fine when I tried to move the image before rotating it. After rotating the image, when I tried to move the rotated image, the image is not moving in the direction which I dragged. Can any one help me.
Thanks in advance. I'm using the code
#import "myDraggableImage.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/CoreAnimation.h>
#implementation myDraggableImage
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
[[self superview] bringSubviewToFront:self];
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[self addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[self addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self addGestureRecognizer:panRecognizer];
[panRecognizer release];
CGRect frame = [self frame];
printf("\n the x co ordinte of the frame is :%f",frame.origin.x);
printf("\n the y co ordinte of the frame is :%f",frame.origin.y);
printf("\n the widht co ordinte of the frame is :%f",frame.size.width);
printf("\n the height co ordinte of the frame is :%f",frame.size.height);
}
-(void)rotate:(id)sender
{
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
CGRect frame = [self frame];
}
-(void)scale:(id)sender
{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)move:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
{
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
CGFloat finalX = translatedPoint.x ;//+ (.15*[(UIPanGestureRecognizer*)sender velocityInView:self].x);
CGFloat finalY = translatedPoint.y ;//+ (.15*[(UIPanGestureRecognizer*)sender velocityInView:self].y);
if(finalX < 0)
{
finalX = 0;
}
else if(finalX > 460)
{
finalX = 440;
}
if(finalY < 40)
{
finalY = 40;
}
else if(finalY > 300)
{
finalY = 300;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
-(void)dealloc
{
[super dealloc];
}
#end
touchesBegan:withEvent: is a wrong place to set up your gestures as it will add a new gestures every time the user. As for the translation function, it's hard to put a finger on what's wrong. Some of the process here seems overdone. While I haven't implemented the animation part (to bring it in, right?), take a look at this sample project. It could help you get started in the right direction.
Related
I have some questions about using a UIPanGestureRecognizer. Is it possible to use it in moving a UIImageView one at a time.
The flow is like this. I have 2 images. image A and B. If you select image A,image B cannot be moved?.
Thanks guys.
you can set UIPanGestureRecognizer for particular UIImageView object only.
//set UIPanGestureRecognizer for first image
UIPanGestureRecognizer *panRecognizer1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage1:)];
[panRecognizer1 setMinimumNumberOfTouches:1];
[panRecognizer1 setMaximumNumberOfTouches:1];
[panRecognizer1 setDelegate:self];
[img1 addGestureRecognizer:panRecognizer1];
//set UIPanGestureRecognizer for second image
UIPanGestureRecognizer *panRecognizer2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage2:)];
[panRecognizer2 setMinimumNumberOfTouches:1];
[panRecognizer2 setMaximumNumberOfTouches:1];
[panRecognizer2 setDelegate:self];
[img1 addGestureRecognizer:panRecognizer2];
and write two method for image moving
-(void)moveImage1:(id)sender {
//coding for moving first image
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[viewGesture bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.04*[(UIPanGestureRecognizer*)sender velocityInView:viewGesture].x);
CGFloat finalY = translatedPoint.y + (.04*[(UIPanGestureRecognizer*)sender velocityInView:viewGesture].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < -5000) { finalX = 0; }
else if(finalX > 5000) { finalX = 320; }
if(finalY < -5000) { finalY = 0; }
else if(finalY > 5000) { finalY = 460; }
}
else {
if(finalX < -5000) { finalX = 0; }
else if(finalX > 5000) { finalX = 320;}
if(finalY < -5000) { finalY = 0; }
else if(finalY > 5000) { finalY = 460; }
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
-(void)moveImage2:(id)sender {
//coding for moving second image
}
and also see this with uicollectionview
I have seen some app, they are able to scale and rotate the image at the same time. It does not require to release the finger touch.
My following code require to:
1. Touch to scale
2. Release
3. Touch to rotate
How do i scale and rotate at the same time?
In my main code:
UIPanGestureRecognizer *imagePanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage:)];
[imagePanGesture setMinimumNumberOfTouches:1];
[imagePanGesture setMaximumNumberOfTouches:1];
[tempImageView addGestureRecognizer:imagePanGesture];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scaleImage:)];
[tempImageView addGestureRecognizer:pinchGesture];
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotateImage:)];
[tempImageView addGestureRecognizer:rotationGesture];
In individual scale and rotate
- (void)scaleImage:(UIPinchGestureRecognizer *)recognizer
{
if([recognizer state] == UIGestureRecognizerStateEnded)
{
previousScale = 1.0;
return;
}
CGFloat newScale = 1.0 - (previousScale - [recognizer scale]);
CGAffineTransform currentTransformation = [recognizer view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransformation, newScale, newScale);
[[recognizer view] setTransform:newTransform];
previousScale = [recognizer scale];
}
- (void)rotateImage:(UIRotationGestureRecognizer *)recognizer
{
if([recognizer state] == UIGestureRecognizerStateEnded) {
previousRotation = 0.0;
return;
}
CGFloat newRotation = 0.0 - (previousRotation - [recognizer rotation]);
CGAffineTransform currentTransformation = [recognizer view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransformation, newRotation);
[[recognizer view] setTransform:newTransform];
previousRotation = [recognizer rotation];
}
You may check this http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
I believe what you are looking for is
-gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
Here is the code you are looking for... Add this method to your .m file.
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
I would like to zoom and rotate an UIImageView. Here is my code:
#synthesize immagine, velocita, locationManager, direzione;
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
double degrees = newHeading.magneticHeading;
double radians = degrees * M_PI / 180;
[UIView animateWithDuration:0.05 animations:^{
self.immagine.transform = CGAffineTransformMakeRotation(-radians);
}];
}
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return immagine;
}
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
[scrollView setDelegate:self];
[scrollView setContentSize:CGSizeMake(320, 460)];
immagine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mappa1"]];
immagine.frame = CGRectMake(0, 0, 320, 460);
immagine.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:immagine];
locationManager.headingFilter = kCLHeadingFilterNone;
[locationManager startUpdatingHeading];
[super viewDidLoad];
}
But when I'm zooming, the UIImageView exit from the View. Can anyone help me? Thanks in advance!
I have problem like this and these two function will solve your problem
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
CGRect originalFrame=recognizer.view.frame;
CGRect newFrame=CGRectMake(originalFrame.origin.x, originalFrame.origin.y, originalFrame.size.width*recognizer.scale, originalFrame.size.height*recognizer.scale);
if (newFrame.size.width>70&&newFrame.size.width<150)
{
recognizer.view.frame=newFrame;
}
}
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
{
[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)recognizer view]];
if([(UIRotationGestureRecognizer*)recognizer state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)recognizer rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)recognizer view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)recognizer view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)recognizer rotation];
// recognizer.view.transform=CGAffineTransformMakeRotation(([recognizer rotation] * 180) / M_PI);
}
-(void)scale:(id)sender {
[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender {
[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
-(void)move:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) { finalX = 0; } else if(finalX > 768) {
finalX = 768;
}
if(finalY < 0) { finalY = 0; } else if(finalY > 1024) {
finalY = 1024;
}
}
else {
if(finalX < 0) { finalX = 0; } else if(finalX > 1024) {
finalX = 768;
}
if(finalY < 0) { finalY = 0; } else if(finalY > 768) {
finalY = 1024;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
-(void)tapped:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
This is my code and the UIImageView I want to move is called "Birdie". I know I have to create a -(void)Birdie UIImageView recognizer but I used the following which didn't worked.
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[Birdie addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[Birdie addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[Birdie addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[Birdie addGestureRecognizer:tapRecognizer];
[self.view addSubview:Birdie];
}
Thanks alot in advanced!!
You need to put some values in the in the interface, something like:
//Zoom
CGFloat lastScaleFactor;
//Movement
CGPoint startLocation;
float distance;
//Rotation
CGFloat valore;
Then:
- (void)zoomAction:(UIGestureRecognizer *)sender {
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
if (factor > 1) {
self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1), lastScaleFactor + (factor-1));
} else {
self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor);
}
//Save the new "scale" factor
if (sender.state == UIGestureRecognizerStateEnded) {
if (factor > 1) {
lastScaleFactor += (factor-1);
} else {
lastScaleFactor *= factor;
}
}
}
- (void)panAction:(UIPanGestureRecognizer *)gesture {
if ((gesture.state == UIGestureRecognizerStateChanged) || (gesture.state == UIGestureRecognizerStateEnded)) {
CGPoint location = [gesture locationInView:self.view];
[self.yourImage setCenter:location];
}
}
- (void)rotationAction:(UIRotationGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
[gesture setRotation:valore * 10];
}
if (gesture.state == UIGestureRecognizerStateChanged) {
valore = gesture.rotation / 10;
if (valore > 1.0) valore = 1.0;
if (valore < 0.2) valore = 0.2;
self.yourImage.alpha = valore;
}
NSLog(#"rotazione %f", gesture.rotation / 10);
}
In this case I used rotation to change the alpha, but you can do what you need
I need to simulate the photo gallery effects.
I have uiimageview, I need to zoom in and out, scale and - if I double tapped (or shook the iPad - I want the image to return to its actual size.
I tried a lot, but all the available examples are buggy and not complete; I don't want to use uiwebview.
Any suggestion, please?
here's the code
in view did load
:
holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:myimage];
imageview.contentMode = UIViewContentModeScaleAspectFit;
//holderView.contentMode = UIViewContentModeScaleAspectFit ;
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
and the following is the code I use for the functions
-(void)scale:(id)sender {
[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if(zooming)
{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
OldTransform = newTransform;
OldCenter = [(UIPinchGestureRecognizer*)sender view].center;
Does_Panned = YES ;
Does_transformed = YES ;
}
else {
}
}
-(void)rotate:(id)sender {
if(zooming)
{
[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
OldTransform = newTransform ;
OldCenter = [(UIPinchGestureRecognizer*)sender view].center;
Does_Panned = YES ;
Does_transformed = YES ;
}
}
-(void)move:(id)sender {
if(zooming)
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
OldCenter = translatedPoint ;
Does_Panned = YES ;
Does_transformed = NO ;
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 768) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 1024) {
finalY = 1024;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 1024) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 768) {
finalY = 1024;
}
}
[UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDelay: 1.0];
[UIView setAnimationDuration:.20];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
else{
if([(UIPanGestureRecognizer*)sender maximumNumberOfTouches] == 1)
{
UIPanGestureRecognizer* recognize = (UIPanGestureRecognizer*)sender;
//change the current window width and level
if([recognize state] == UIGestureRecognizerStateBegan) {
Bigen =[recognize translationInView:recognize.view ];// [[sender view] center];
BeginX = Bigen.x;
BeginY = Bigen.y;
}
else if(
( [recognize state] == UIGestureRecognizerStateChanged )||
(recognize.state == UIGestureRecognizerStateEnded) )
{
CGPoint translation = [recognize translationInView:recognize.view ];
End = translation ;
EndX = End.x ;
EndY = End.y ;
double offsetw=0;
double offsetl=0;
if(EndX>BeginX)
{
offsetw=50*(EndX-BeginX);
}
else if(EndX<BeginX)
{
offsetw=50*(EndX-BeginX);
}
else
{
offsetw=0;
}
if(EndY>BeginY)
{
offsetl=50*(EndY-BeginY);
}
else if(EndY<BeginY)
{
offsetl=50*(EndY-BeginY);
}
else
{
offsetl=0;
}
ChangeWW_Wl(offsetw, offsetl);
[self AddImage];
}
//if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
/* CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
EndX = translatedPoint.x;
EndY = translatedPoint.y;*/
//}
}
}
}
(void)tapped:(id)sender {
if(zooming)
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
how to update it to reset the image to its original size at shaking or double tap
how to over come the improper zooming (zoom with large factor at the first time )
You should watch the "Designing Apps with Scroll Views" video from WWDC2010, it tells you how to do this exact thing.