2011-09-07 112 views
0

转换方形图像或圆形图像很容易做...iphone:如何旋转带触摸事件的矩形图像?

但是如果一个矩形图像?

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

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的大小?

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

韦伯

回答

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

the documentation about transform method

的变换的原点是中心属性的值,或该层的anchorPoint财产,如果它被改变了。 (使用图层属性来获取底层Core Animation图层对象。)默认值是CGAffineTransformIdentity。

所以你应该可以通过这种方式设置你的旋转中心!

和确定的旋转角度,使用atan2atan2f功能:

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

热水使用angleInRadians? –

+0

只要使用它而不是你的'?????'在上面的代码中,这就是CGAffineTransformRotate的第二个参数所期望的那样(阅读文档;)) – AliSoftware

+0

好的,谢谢......但结果是不正确的... 让我首先阅读文档 –

0

你可以重新设置中心改造之后。

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