UIRotationGestureRecognizer on UIImageview - iphone

How to rotate UIImageView with two fingure touch in iOS SDK..as I know this is very simple but because of I am new in this technology so cant understand...
How to use UIRotationGestureRecognizer to implement this...
Please help me to solve this problem...
Thanks.

Code this on view did load or the imageview create function : m_ctrlImgVwShowImage - your's imageview
UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)] autorelease];
[rotationRecognizer setDelegate:self];
[m_ctrlImgVwShowImage addGestureRecognizer:rotationRecognizer];
//lastRotation is a cgfloat member variable
-(void)rotate:(id)sender {
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
_lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = m_ctrlImgVwShowImage.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[m_ctrlImgVwShowImage setTransform:newTransform];
_lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

Related

Expand View On dragging

Unfortunately I couldn't find anything about my problem on stackoverflow or maybe I just used the wrong keywords.
I want to have a view which can be moved and increased or decreased by the user. See the image as a reference. There are 4 anchor points with which the user is able to define the frame of the rectangle.
Can you help finding a easy way how to achieve this?
Thanks so much and best regards from Germany,
Chris
use this i've using it for imageview but you can change it for UIView and make sure that userInterAction is True for that view
declare it in .h file
CGFloat lastScale;
in viewDidLoad:
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[imgRotate addGestureRecognizer:pinchRecognizer];
-(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];
[imgRotate setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

Transform not working

I am trying to rotate an object around it's current position,
CGAffineTransform trans = CGAffineTransformMakeRotation(compass_heading );
trans = CGAffineTransformTranslate(trans, 0, 90);
sun_image.transform = trans;
But it has no effect and sun_image doesn't move!
But if I change it to this and translate on X axis it moves! What does this mean?
CGAffineTransform trans = CGAffineTransformMakeRotation(compass_heading );
trans = CGAffineTransformTranslate(trans, 90, 0);
sun_image.transform = trans;
Here is how you can rotate an object:
CGFloat rotation = M_PI_2;
CGAffineTransform currentTransform = sun_image.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[sun_image setTransform:newTransform];
With CGAffineTransformTranslate you can move an object, so you must use CGAffineTransformRotate.
For location manager :
-(void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
//set the anchorPoint as the lower right corner of the layer
sun_image.layer.anchorPoint = CGPointMake(1, 1);
[sun_image.layer setAffineTransform:CGAffineTransformMakeRotation(-1*newHeading.trueHeading*3.14159/180)];
}
I generally do this for translation transforms:
sun_image.transform = CGAffineTransformMakeTranslation(dx,dy);
First set center of your UIImageView with your view center property like bellow..
yourImageView.center = self.view.center;
after set GestureRecognizer like bellow..
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[yourImageView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
and then use this bellow method for rotate UIImageView
-(void)rotate:(id)sender
{
UIView *imgTempGest = [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];
[imgTempGest setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
take lastRotation variable of CGFloat like bellow..
CGFloat lastRotation;

Rotate Zooming ImageView in ScrollView?

I have a UIScrollView that has a zooming UIImageView, ie it implements:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
I'm trying to add a UIRotationGestureRecognizer to this imageView and I do it as follows:
_rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[self.imageView addGestureRecognizer:_rotationGestureRecognizer];
-(void)rotate:(id)sender
{
UIRotationGestureRecognizer *rotationGestureRecognizer = [(UIRotationGestureRecognizer*)sender retain];
if(rotationGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
self.lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (self.lastRotation - rotationGestureRecognizer.rotation);
rotationGestureRecognizer.view.transform = CGAffineTransformRotate(rotationGestureRecognizer.view.transform, rotation);
self.lastRotation = rotationGestureRecognizer.rotation;
[rotationGestureRecognizer release];
}
I'm just wondering, is it possible to even do this? It seems the UIScrollView is blocking the touches to my UIImageView because nothing is happening. Does Apple recommend to not do this with a zooming view in a UIScrollView?
Following code is working.Add gesture to scrollView instead of imageView.
UIRotationGestureRecognizer* _rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[self.scrollView addGestureRecognizer:_rotationGestureRecognizer];
Swift 5 Version:
let rotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(self.rotate(_:)))
scrollView.addGestureRecognizer(rotationGestureRecognizer)
-(void)rotate:(UIRotationGestureRecognizer *)sender
{
[self 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];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

How to stretch images on touch direction in iPhone sdk?

I am working on customizing images. My requirement is to stretch or shrink image in touch direction & cropping image. I have done cropping but how I can stretch & shrink image in touch direction. Is it possible?
Your question in not clear.but if you want to scale or rotate image with touch event.then in viewdidload write this code.
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]
initWithTarget:self action:#selector(rotateImage:)];
[self.view addGestureRecognizer:rotationGesture];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(scaleImage:)];
[self.view addGestureRecognizer:pinchGesture];
[pinchGesture release];
And using this code you can rotate or scale images in touch direction.
- (void)scaleImage:(UIPinchGestureRecognizer *)recognizer
{
if([recognizer state] == UIGestureRecognizerStateEnded) {
previousScale = 1.0;
return;
}
CGFloat newScale = 1.0 - (previousScale - [recognizer scale]);
CGAffineTransform currentTransformation = yourimage.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransformation,
newScale, newScale);
yourimage.transform = 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 = yourimage.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransformation, newRotation);
yourimage.transform = newTransform;
previousRotation = [recognizer rotation];
}

Bounds and anchoring of programmatic UIScrollView with zooming

So, I've managed to create a UIScrollView with zooming method programmatically, but I'm kind of stuck how to solve an issue I'm having with zooming:
When I zoom in or out the point at where it expands/retracts does not happen where I do the pinch gesture, it happens on a corner.
After zooming in or out, it will leave extra space outside the bounds, and I cannot scroll the image more than half the width & height of the image.
Other than this, I'm so close to getting it working 100%. I've tried playing around with achorpoints, but it looks like the scroll view and image view does not respond to this.
Here is the important stuff in the code listing:
UIScrollView *mapScrollView;
UIImageView *mapImageView;
CGFloat lastScale = 0;
NSMutableArray *map_List;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];
map_List = [[NSMutableArray alloc] init];
[map_List addObject:#"Pacific_Map_8bit.png"];
[map_List addObject:#"Atlantic_Map_8bit.png"];
CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);
mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];
mapScrollView.contentSize = CGSizeMake(2437, 1536);
UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageView = [[UIImageView alloc] initWithImage: mapImage];
mapScrollView.bounces = NO;
[mapImage release];
[mapScrollView addSubview:mapImageView];
[self addSubview:mapScrollView];
mapImageView.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[mapImageView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
}
return self;
}
// the scale method thats triggered to zoom when pinching
-(void)scale:(id)sender {
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
NSLog(#"map scale %f", scale);
CGFloat mapWidth = mapImageView.frame.size.width;
CGFloat mapHeight = mapImageView.frame.size.height;
NSLog(#"map width %f", mapWidth);
if(scale>=1 & mapWidth<=4000 || scale<1 & mapWidth>=1234){
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
mapScrollView.contentSize = CGSizeMake(mapWidth, mapHeight);
}
Thanks!
UIScrollView has built-in zooming support. All you need to do is set the minimumZoomScale and maximumZoomScale properties, and return a view to be used for zooming using viewForZoomingInScrollView.