2013-05-23 5 views
2

첫 번째로 Objective C를 처음 접했을 때 어떤 도움이라도 대단히 감사하겠습니다. Gauge 앱을 쓰려고하는데 (결국 각도를 보여줄 것입니다). 내가 가진 문제는 니들 ​​이미지를 회전시킬 때 올바른 각도로 회전하지만 앵커 포인트에서 회전하지 않으면 이미지가 위치를 이동한다는 것입니다. 0 찌꺼기에서 90까지 그리고 다시 0으로 이동한다고해도 이미지는 원래 위치로 돌아 가지 않습니다! 내가 사용하는 코드는 다음과 같습니다. (PS I는 현재는 시뮬레이터에서 응용 프로그램을 실행하고)회전 지점에서 이미지 회전

*** Rotate Invoked by .... 
// Rotate Image 
[self rotateImage:_imgNeedle duration: vAnimationDuration curve: UIViewAnimationCurveLinear degrees:vValue]; 

-(void)rotateImage:(UIImageView *)image 
     duration:(NSTimeInterval)duration 
      curve:(int)curve 
     degrees:(CGFloat)degrees 
{ 
    // Setup the animation 
    [self setAnchorPoint:CGPointMake(.44,.85) forView:image]; 
    // image.transform = CGAffineTransformMakeRotation(d2r(degrees)); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:duration]; 
    [UIView setAnimationCurve:curve]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    // The transform matrix 
    CGAffineTransform transform = CGAffineTransformMakeRotation(d2r(degrees)); 

    image.transform = transform; 
    // Commit the changes 
    [UIView commitAnimations]; 
} 

// Set Anchor Point 
-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view 
{ 
    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y); 
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y); 

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform); 
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform); 

    CGPoint position = view.layer.position; 

    position.x -= oldPoint.x; 
    position.x += newPoint.x; 

    position.y -= oldPoint.y; 
    position.y += newPoint.y; 

    view.layer.position = position; 
    view.layer.anchorPoint = anchorPoint; 
} 

답변

0

나는 레이어 위치에 대해 잘 모르겠지만, 나는 anchorPoint, 프레임 변경을 변경할 때, 그래서 프레임을 조정합니다. 다시 rotateImage의 여러 호출 원래 위치로 이동하려는 경우

-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view 
{ 
    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y); 
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y); 

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform); 
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform); 
    /* 
    CGPoint position = view.layer.position; 

    position.x -= oldPoint.x; 
    position.x += newPoint.x; 

    position.y -= oldPoint.y; 
    position.y += newPoint.y; 

    view.layer.position = position; 
    */ 

    view.frame = CGRectOffset(view.frame, newPoint.x-oldPoint.x, newPoint.y-oldPoint.y); 
    view.layer.anchorPoint = anchorPoint; 
} 

는 또한, 당신은 그 전에 한 번 기준점을 설정한다고 생각합니다. 같은 rotateImage 메소드로 두 개의 별도 애니메이션을 만들면 앵커 포인트가 현재 구현으로 다시 계산됩니다.

-(void)rotateImage:(UIImageView *)image 
     duration:(NSTimeInterval)duration 
      curve:(int)curve 
     degrees:(CGFloat)degrees 
{ 
    // Setup the animation 
    // [self setAnchorPoint:CGPointMake(.44,.85) forView:image]; 
    // image.transform = CGAffineTransformMakeRotation(d2r(degrees)); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:duration]; 
    [UIView setAnimationCurve:curve]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    // The transform matrix 
    CGAffineTransform transform = CGAffineTransformMakeRotation(d2r(degrees)); 

    image.transform = transform; 
    // Commit the changes 
    [UIView commitAnimations]; 
} 
+0

감사합니다. 나는 변화를 만들었고 샘플 속도가 애니메이션 시간보다 적음을 발견했다. 그러나 더 좋을 때, 불행하게도 그것은 여전히 ​​옳지 않습니다. 스토리 보드의 디자인에서, 바늘은 다이얼의 중심에 있으며, 달리면 수직으로 위로 움직입니다. 각도를 변경하면 수평 움직임이 생깁니다. – user2413698

+0

@ user2413698 필요한 것을 모릅니다. 어쩌면 질문을 더 많은 정보로 업데이트 할 수 있습니다. 이 답변으로 직면 한 특정 문제가 해결되면 다른 질문을 올리십시오. –

+0

자동 레이아웃 해제 기능 - 바늘이 이제 축을 중심으로 올바르게 회전합니다 - 고정! – user2413698