locationOfTouch and numberOfTouches - objective-c

hi
I have this recognizer, set with 2 touch, but it returns only one, not two CGPoint
-(void)gestureLoad {
UIGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(numTap2:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:2];
[self.view addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
}
- (void)numTap2:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:self.view];
NSLog(#"x %f y %f",location.x, location.y);
}
as I understand, I cycle the number of touch with these two methods, but I have not figured out how to:
-(CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view {
}
-(NSUInteger)numberOfTouches {
}
thanks a lot!

In numTap2, use:
CGPoint location = [recognizer locationOfTouch:touchIndex inView:self.view];
where touchIndex is either 0 or 1.

Related

pinch zooming in image viewer

I have an UIImageView. I need pinch zoom.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 90, 320, 38)];
[imageView setImage:[UIImage imageNamed:#"accesspanel.png"]];
[self.view addSubview: imageView];
You can add the imageview inside a scrollview and can use Scrollview delegate for the this purpose
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.delegate=self;
}
First ---> ADD Pinch Gesture to your Image View -:
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePinchGesture:)];
pgr.delegate = self;
[imageView addGestureRecognizer:pgr];
Second ---> Implement Pinch Gesture -:
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales.
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:#"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom.
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
lastScale = [gestureRecognizer scale]; // Store the previous. scale factor for the next pinch gesture call
}
}
UIPinchGestureRecognizer *pinchGestureRecognizer=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(pinchGestureDetected:)];
[pinchGestureRecognizer setDelegate:self];
[_third_imageview addGestureRecognizer:pinchGestureRecognizer];
- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state ==UIGestureRecognizerStateChanged)
{
CGFloat scale = [recognizer scale];
[recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];
[recognizer setScale:1.0];
}
}

UIImageView subclass doesn't work simultaneously gesture

I'm developing a iOs 6 app for iPad. I've developed a UIImageView subclass adding gesture recognizing (zoom, pinch and pan), but I've a problem: it doesn't admit simultaneously gestures. I first code it as a normal UIImageView, and it worked great, but now with the subclass it doesn't. My code:
image.m (UIImageView subclass)
- (id)initWithImage:(UIImage *)image {
self = [super initWithImage:image];
if (self) {
[self setMultipleTouchEnabled:YES];
[self setUserInteractionEnabled:YES];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panDetected:)];
[super addGestureRecognizer:panRecognizer];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchDetected:)];
[super addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotationDetected:)];
[super addGestureRecognizer:rotationRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 2;
[super addGestureRecognizer:tapRecognizer];
NSLog(#"lkjlkj");
}
return self;}
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer {
CGPoint translation = [panRecognizer translationInView:self.superview];
CGPoint imageViewPosition = self.center;
imageViewPosition.x += translation.x;
imageViewPosition.y += translation.y;
self.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView: self.superview];}
- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer{
CGFloat scale = pinchRecognizer.scale;
self.transform = CGAffineTransformScale(self.transform, scale, scale);
pinchRecognizer.scale = 1.0;}
- (void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer{
CGFloat angle = rotationRecognizer.rotation;
self.transform = CGAffineTransformRotate(self.transform, angle);
rotationRecognizer.rotation = 0.0;}
- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer{
[UIView animateWithDuration:0.25 animations:^{
self.center = CGPointMake(CGRectGetMidX(self.superview.bounds), CGRectGetMidY(self.superview.bounds));
self.transform = CGAffineTransformIdentity;
}];}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;}
viewcontroller.m (where I create a imageview from image which doesn't respond to multiple gestures simultaneously)
image *img = [[image alloc] initWithImage:imatgetemporal];
img.center = CGPointMake(525/2, 651/2);
[[self.view viewWithTag:2] addSubview:img];
Thanks for all!!
I guess you can solve it by adding the same delegate to the UIPanGestureRecognizer, UIPinchGestureRecognizer, UIRotationGestureRecognizer and to the UITapGestureRecognizer just after declaring them. The code would be like this:
tapRecognizer.delegate = self;
pinchRecognizer.delegate = self;
rotationRecognizer.delegate = self;
tapRecognizer.delegate = self;
Hope it helps.

How to scale and rotate smoothly?

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;
}

Zooming and Rotation an UIImageView

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);
}

location different CGPoint

Hi
how do I locate both the cgpoint? he gives me just one.
-(void)gestureLoad {
//GESTURE
UIGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(numTap2:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:2];
[self.view addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
}
- (void)numTap2:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:self.view];
...other actions...
}
thanks a lot!
From the description of -[UIGestureRecognizer numberOfTouches]:
Using the value returned by this
method in a loop, you can ask for the
location of individual touches using
the locationOfTouch:inView: method.
So, call -locationOfTouche:inView: for each touch to get the corresponding location.