2011-09-07 110 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;