0

在我的应用程序中,我使用 SMRotatory 轮进行旋转。它作为旋转工作正常。

但我想检测旋转是顺时针还是逆时针?

//My code is as follow:

- (BOOL)continueTrackingWithTouch:(UITouch*)touch withEvent:(UIEvent*)event
{

    CGFloat radians = atan2f(container.transform.b, container.transform.a);
   // NSLog(@"rad is %f", radians);
    CGPoint pt = [touch locationInView:self];

    float dx = pt.x  - container.center.x;
    float dy = pt.y  - container.center.y;
    float ang = atan2(dy,dx);
    float angleDifference = deltaAngle - ang;
   // NSLog(@"%f",angleDifference);



    if (angleDifference>=0) {
        //NSLog(@"positive");
        [[NSUserDefaults standardUserDefaults]setValue:@"positive" forKey:@"CheckValue"];
    }
    else
    {
      //  NSLog(@"negative");
        [[NSUserDefaults standardUserDefaults]setValue:@"negative" forKey:@"CheckValue"];
    }

    container.transform = CGAffineTransformRotate(startTransform, -angleDifference);

    bg.transform=CGAffineTransformRotate(startTransform, -angleDifference);

    return YES;
}
4

1 回答 1

0

尝试这样的事情:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    CGPoint currentTouch1=[touch locationInView:self.superview];
    CGPoint currentTouch2=[touch previousLocationInView:self.superview];

    CGFloat x = currentTouch2.x - currentTouch1.x;
    CGFloat y = currentTouch2.y - currentTouch1.y;
    CGFloat dist = sqrtf(x*x + y*y);
    CGFloat cx = currentTouch1.x - self.superview.bounds.size.width / 2;
    CGFloat cy = currentTouch2.y - self.superview.bounds.size.height / 2;
    CGFloat cdist = sqrtf(cx*cx + cy*cy);
    CGFloat angle = atan2(dist, cdist);
    //NSLog(@"%f",angle);

    CGFloat radians = atan2f(super.transform.b, super.transform.a); 
    mDegrees = radians * (180 / M_PI);
    if (mDegrees < 0.0)
    {
        mDegrees+=360;
    }

    NSLog(@"Angle is = %f",angle); //positive is clockwise, negative is counterclockwise

    CGAffineTransform transform = CGAffineTransformRotate(self.transform, angle);
    self.transform = transform;
    touch1 = currentTouch1;
    touch2 = currentTouch2;
}
于 2013-03-06T11:33:52.567 回答