Rotate image on center using one finger touch
Asked Answered
A

1

11

I want to rotate the below image on a center point using one finger touch...

And i want to display the value of the image with the label when I rotate the image using touch.

I have done the image rotation but the problem is that how to set the value of the image according to rotation.

The angle of the rotation is increase so i can not set the value.

Can any one help me?

The code is below

float fromAngle = atan2(firstLoc.y-imageView.center.y, 
                            firstLoc.x-imageView.center.x);

    NSLog(@"From angle:%f",fromAngle);
    float toAngle = atan2( currentLoc.y-imageView.center.y, 
                          currentLoc.x-imageView.center.x);

    NSLog(@"to Angle:%f",toAngle);
    // So the angle to rotate to is relative to our current angle and the
    // angle through which our finger moved (to-from)
    float newAngle =angle+(toAngle-fromAngle);


    NSLog(@"new angle:%.2f",newAngle);


 CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);

 imageView.transform=cgaRotate;


angle = newAngle;

Can any one help me ?

Antonietta answered 29/3, 2011 at 6:8 Comment(2)
What do you mean by 'value of the image'?Primipara
i have the image in which the the value from 0 to 50 are displayed.so when i rotate the image the particular value should be displayed in the label.Antonietta
P
22

Wasn't totally sure what you were after; but try out this code.

If you create a new View-based Application project called 'Rotation' and replace the code in RotationViewController.h and .m for the following you'll get a green block that you can rotate using your calculations. You can replace the green block UIView with your UIImageView, or anything else you want to spin.


RotationViewController.h

#import <UIKit/UIKit.h>

@interface RotationViewController : UIViewController
{
    UIView* m_block;
    UILabel* m_label;
    CGPoint m_locationBegan;
    float m_currentAngle;
}

- (float) updateRotation:(CGPoint)_location;

@end

RotationViewController.m

#import "RotationViewController.h"

double wrapd(double _val, double _min, double _max)
{
    if(_val < _min) return _max - (_min - _val);
    if(_val > _max) return _min - (_max - _val);
    return _val;
}

@implementation RotationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CGRect blockFrame = CGRectMake(0, 0, 200, 200);
    m_block = [[UIView alloc] initWithFrame:blockFrame];
    m_block.backgroundColor = [UIColor greenColor];
    m_block.center = self.view.center;
    [self.view addSubview:m_block];
    [m_block release];
    
    CGRect labelFrame = CGRectMake(0, 0, 320, 30);
    m_label = [[UILabel alloc] initWithFrame:labelFrame];
    m_label.text = @"Loaded";
    [self.view addSubview:m_label];
}

- (void) touchesBegan:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    m_locationBegan = location;
}

- (void) touchesMoved:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    [self updateRotation:location];
}

- (void) touchesEnded:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    m_currentAngle = [self updateRotation:location];
}

- (float) updateRotation:(CGPoint)_location
{
    float fromAngle = atan2(m_locationBegan.y-m_block.center.y, m_locationBegan.x-m_block.center.x);
    float toAngle = atan2(_location.y-m_block.center.y, _location.x-m_block.center.x);
    float newAngle = wrapd(m_currentAngle + (toAngle - fromAngle), 0, 2*3.14);

    CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
    m_block.transform = cgaRotate;
    
    int oneInFifty = (newAngle*50)/(2*3.14);
    
    m_label.text = [NSString stringWithFormat:@"Angle: %f 1in50: %i", newAngle, oneInFifty];

    return newAngle;
}

@end

Primipara answered 30/3, 2011 at 14:27 Comment(5)
@wex,Now if i want to move this image to some other position of screen, then how can i do it?Divinadivination
@Divinadivination you'll want to look into CGAffineTransformMakeTranslation and combine the result of that with cgaRotate in the above code. You'll have to have some method of toggling whether a touch-drag should effect rotation or translation though.Primipara
@Primipara This code is very helpful to me but I have one question. I have implemented this code in my app it works fine. In this code if I put finger anywhere on the screen rotation starts but I want such kind of functionality in which only when I put finger on "m_block" view at that time rotation occurs. so how will it be possible?Antipole
@Antipole I'm not in a position to test it right now; but try: after the three places where you get the 'location' put the code: if([self.view hitTest:location withEvent:_event] != m_block) return;Primipara
you can use M_PI instead of 3.14, but your naked eye wont really notice the difference. thanks for the code.Sundew

© 2022 - 2024 — McMap. All rights reserved.