Transform not working - iphone

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;

Related

Image gone certain Zoomout level

Im using GestureRecognizer delegate for pinching for images. I used UIPinchGestureRecognizer delegate for pinching. But, when i pinch zoomIn it doesn't have any problem. When i zoomOut certain level the images are gone from view.
code:
UIPinchGestureRecognizer *pinchGesture1 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(ahandlePinch1:)];
[myImageView addGestureRecognizer:pinchGesture1];
-(void)ahandlePinch1:(UIPinchGestureRecognizer*)sender {
mCurrentScale += [sender scale] - mLastScale;
mLastScale = [sender scale];
if (sender.state == UIGestureRecognizerStateEnded)
{
mLastScale = 1.0;
}
CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, mCurrentScale, mCurrentScale);
myImageView.transform = newTransform;
}
Do this :
if([pinch state] == UIGestureRecognizerStateEnded)
{
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [pinch scale]);
CGAffineTransform currentTransform = myImageView.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[myImageView setTransform:newTransform];
lastScale = [pinch scale];
And it should work for everything (in / out).

How to rotate and resize uiview at same time?

I'm using SPUserResizableView to resize my view. i had added rotation funcitionality to it . when i touch corner handel view should rotate for this purpose i have added some code but its not working properly. here is my code :
CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
CGPoint currentTouchPoint = [touch locationInView:view];
CGPoint previousTouchPoint = [touch previousLocationInView:view];
CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);
[self setTransform:CGAffineTransformRotate([view transform], angleInRadians)];
Note: when user touch corner handel a method get called in which i have written this piece of code. If user touches edges handel that method wont called.
Thanks.
try this :
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[imgBg addGestureRecognizer:pinchRecognizer]; // imgBg is my imageview you can set your view
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[imgBg addGestureRecognizer:rotationRecognizer];
#pragma mark -
#pragma mark - GestureRecognizer Method
-(void)scale:(id)sender {
[viewGesture bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0; // declacre float instance in .h file
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 {
[viewGesture 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];
}

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

UIRotationGestureRecognizer

I have a very weird problem, I am implementing UIRotationGestureRecognizer in my application to rotate an image. My problem is that sometimes when you start rotating the image it jumps to the other finger.
Example: I am dragging my image with one finger, and after that I use my second finger to start rotating, but the image goes to the second finger. Is that normal? can it be fixed?
The code I am using is:
if([recognizer state] == UIGestureRecognizerStateEnded) {
prevRotation = 0.0;
return;
}
CGFloat newRotation = 0.0 - (prevRotation - [recognizer rotation]);
CGAffineTransform currentTransformation = image.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransformation, newRotation);
image.transform = newTransform;
prevRotation = [recognizer rotation];
I tried saving the last location of the center and applying it again, but it is not working either, like this:
lastPoint=image.center;
if([recognizer state] == UIGestureRecognizerStateEnded) {
prevRotation = 0.0;
return;
}
CGFloat newRotation = 0.0 - (prevRotation - [recognizer rotation]);
CGAffineTransform currentTransformation = image.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransformation, newRotation);
image.transform = newTransform;
prevRotation = [recognizer rotation];
image.center=lastPoint;
Try this code:
-(void)rotate:(id)sender
{
[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
self.imageTag = [[sender view]tag];
NSLog(#"sender tag %d", self.imageTag);
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];
}

UIRotationGestureRecognizer on UIImageview

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