iphone:如何使用触摸事件旋转矩形图像?

时间:2011-09-07 07:56:43

标签: iphone objective-c uiimageview cgaffinetransform

变换方形图像或圆形图像很容易做到......

但如果是矩形图像???

这是我想用触摸事件转换的图像。

enter image description here

如果我触摸屏幕上的任何位置,圆心是(30,236)

Imageview会将箭头转换为我的触摸点。

但圆圈中心仍然是同一个地方。

我尝试使用测试角度转换图像

会变成这样......

enter image description 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, ?????? );
}

??????部分应该是最后的解决方案...

或者我可能需要调整imageview的大小???

感谢您的回复或回答: - )

韦伯

3 个答案:

答案 0 :(得分:2)

这是我解决这个问题的最终解决方案

检查此代码

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

答案 1 :(得分:0)

来自the documentation about transform method

  

变换的原点是中心属性的值,如果更改了图层的anchorPoint属性。 (使用layer属性获取基础Core Animation层对象。)默认值为CGAffineTransformIdentity。

所以你应该能够以这种方式设置旋转的中心!

要确定旋转角度,请使用atan2atan2f功能:

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

答案 2 :(得分:0)

您可以在变换后重新设置中心。

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