Search code examples
iphoneobjective-cuiimageviewcgaffinetransform

iphone : How to rotate a rectangle image with touch events?


transform a square image or round image is easy to do...

But what if a rectangle image ???

This is the image I want to transform with touch event .

enter image description here

The circle center is (30 , 236) , if I touch any place on the screen

Imageview will transform the arrow to my touch point.

But the circle center still the same place .

I try to transform the image use a test angle

It will become like this ...

enter image description here

How to adjust the image ?

Also the code is here

- (void)viewDidLoad {
    CGRect arrowImageRect = CGRectMake(20.0f, 20.0f, 15.0f, 220.0f);
    arrow = [[UIImageView alloc] initWithFrame:arrowImageRect];
    [arrow setImage:[UIImage imageNamed:@"arrow.png"]];
    arrow.opaque = YES;
    [self.view addSubview:arrow];
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    NSLog(@"Position X: %f \n, Y: %f",touchPoint.x,touchPoint.y);
    CGAffineTransform transform = CGAffineTransformIdentity;
    arrow.transform = CGAffineTransformRotate(transform, ?????? );
}

The ?????? part should be the last solution ...

Or Maybe I need to resize the imageview ???

Thanks for any reply or answer :-)

Webber


Solution

  • Here is the final solution I figure this problem

    check this code

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch=[[event allTouches]anyObject];
    
        [self transformSpinnerwithTouches:touch];
    }
    
    -(void)transformSpinnerwithTouches:(UITouch *)touchLocation
    {
        CGPoint touchLocationpoint = [touchLocation locationInView:self.view];
        CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view];
        //origin is the respective piont from that i gonna measure the angle of the current position with respective to previous position ....
        CGPoint origin;
        origin.x = arrow.center.x;
        origin.y = arrow.center.y;
        CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
        CGAffineTransform newTransform = CGAffineTransformScale(arrow.transform, 1, 1);
        CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
        CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
        CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
        CGFloat newAngle = currentRotation- previousRotation;
        temp1 = temp1 + newAngle;
        //NSLog(@"temp1 is %f",temp1);
        //NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
        newTransform = CGAffineTransformRotate(newTransform, newAngle);
        [self animateView:arrow toPosition:newTransform];
    }
    -(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
    {
    arrow.transform = newTransform;
    }
    
    -(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
    {
        CGFloat x = secondPoint.x - firstPoint.x;
        CGFloat y = secondPoint.y - firstPoint.y;
        CGPoint result = CGPointMake(x, y);
        //NSLog(@"%f %f",x,y);
        return result;
    }