2011-02-11 4 views
0

나는 약간의 도움이 필요하다. UIButton 또는 UIImageView를 손가락을 따라 회전시키는 방법 (Touch and hold, UILongPressGestureRecognizer)? Thx 4 help손가락을 따르는 UIButton 또는 UIImage보기를 회전하는 방법 (길게 터치)?

UPD : 내가 뭘 잘못하고 있는지 이해하지 못하겠습니까?

- (void)viewDidLoad { 

UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; 
[self.view addGestureRecognizer:tapgr]; 
[tapgr release];  
[super viewDidLoad]; 

}

-(void)tap:(UITapGestureRecognizer *)gesture { 

CGPoint touch = [gesture locationInView:self.view]; 
CGPoint center = myImage.center; 
float dx,dy,wtf; 
dx = touch.x-center.x; 
dy = touch.y-center.y; 
wtf = atan2f(dy, dx); 

[self rotateImage:self.myImage withAngle:wtf]; 

}

- (void)rotateImage:(UIImageView *)image withAngle:(float)newAngle 

{ image.transform = CGAffineTransformMakeRotation (newAngle);

}

답변

0
- (void) LongPress:(UILongPressGestureRecognizer *)gesture { 

    CGPoint p = [gesture locationInView:self.view]; 

    CGPoint zero; 
    zero.x = self.view.bounds.size.width/2.0; 
    zero.y = self.view.bounds.size.height/2.0; 

    CGPoint newPoint; 

    newPoint.x = p.x - zero.x; 
    newPoint.y = zero.y - p.y; 

    angle = atan2(newPoint.x, newPoint.y); 



    UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionAllowUserInteraction; 

    [UIView animateWithDuration:0.2 delay:0.0 options:options 
        animations:^{myButton.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angle);} 
        completion:^(BOOL finished) { 
         [UIView animateWithDuration:1.0 delay:0.5 options:options animations:^{ 
          myButton.transform = CGAffineTransformRotate(CGAffineTransformIdentity, [self detectQuarter:angle]); 
         } completion:nil]; 
        }]; 
} 

#define M_PI_3_4 3*M_PI_4 

-(CGFloat)detectQuarter:(CGFloat)anAngle { 
    if ((anAngle >= -M_PI_4)&&(anAngle <= M_PI_4)) return 0; 
    if ((anAngle > M_PI_4) && (anAngle <= 3*M_PI_4)) return M_PI_2; 
    if ((anAngle >= -M_PI_3_4) && (anAngle < -M_PI_4)) return -M_PI_2; 
    else return M_PI; 
} 
0

은 다행 난 당신이 좋은 방법이 있으면 바로 터치 이벤트 메서드에서이 작업을 수행,


-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{ 

    float dX = touchPoint.x-objPos.x;  // distance along X 
    float dY = touchPoint.y-objPos.y;  // distance along Y 
    float radians = atan2(dY, dX);   // tan = opp/adj 

    //Now we have to convert radians to degrees: 
    float degrees = radians*M_PI/360; 

    return degrees; 
} 

을 triginometry 기억한다. 이것은 수학이 옳다 need.The 수 있습니다 거의 모든 코드입니다,하지만 난 사용해 setTransform 물건에 대해 확실하지 않다

CGAffineTransform current = view.transform; 

[view setTransform:CGAffineTransformRotate(current,
[self degreesTorotateObjectWithPosition:view.frame.origin
andTouchPoint:[touch locationInView:parentView]] //Note: parentView = The view that your object to rotate is sitting in.

(나는 ... 그것이라고 잊었). 나는 학교에서 이것을 브라우저에 쓰고있다. 당신은 여기에서 그것을 알아낼 수 있어야합니다. 터치는 무엇

행운을 빌어 요,

Aurum의 독수리

+0

들으입니까? –

+0

터치는 viewController가 터치 이벤트를받는 메소드에서 얻는 'UITouch' 객체입니다. –

+0

내 코드를 추가하고 도움이 필요합니다. 정말 피곤합니다. –

관련 문제