简体   繁体   中英

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 .

在此输入图像描述

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 ...

在此输入图像描述

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

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

From the documentation about transform method :

The origin of the transform is the value of the center property, or the layer's anchorPoint property if it was changed. (Use the layer property to get the underlying Core Animation layer object.) The default value is CGAffineTransformIdentity.

So you should be able to set the center of your rotation this way!

And for determining the rotation angle, use the atan2 or atan2f function:

float angleInRadians = atan2f(touchPoint.y-circleCenter.y , touchPoint.x-circleCenter.x);

You can re set the center after transform.

CGPoint centerBeforeTransform = view.center;
//your transform code
view.center = centerBeforeTransform;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM