0

转换方形图像或圆形图像很容易......

但是如果一个矩形图像呢???

这是我想用 touch event 转换的图像。

在此处输入图像描述

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

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

但圆心依旧在原处。

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

会变成这样……

在此处输入图像描述

如何调整图像?

代码也在这里

- (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, ?????? );
}

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

或者也许我需要调整图像视图的大小???

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

韦伯

4

3 回答 3

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;
}
于 2011-11-04T04:09:47.717 回答
0

有关转换方法的文档中

变换的原点是 center 属性的值,或者是图层的 anchorPoint 属性(如果已更改)。(使用 layer 属性获取底层 Core Animation 图层对象。)默认值为 CGAffineTransformIdentity。

所以你应该可以这样设置你的旋转中心!

并且要确定旋转角度,请使用atan2oratan2f函数:

float angleInRadians = atan2f(touchPoint.y-circleCenter.y , touchPoint.x-circleCenter.x);
于 2011-09-07T08:23:14.983 回答
0

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

CGPoint centerBeforeTransform = view.center;
//your transform code
view.center = centerBeforeTransform;
于 2011-09-07T11:00:45.153 回答