2

我正在制作 iRetro 手机应用程序动画之类的拨号器动画。

对于 <180 度,拨号器在其原始位置逆时针正确移动。但是对于 > 180 度,它不起作用(逆时针未达到其原始位置)。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    dialerImageView_Width = _dialer_View.bounds.size.width;
    dialerImageView_Height= _dialer_View.bounds.size.height;

    radians1 = atan2f(_dialer_View.transform.b, _dialer_View.transform.a);

}

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

   UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation_Start = [touch locationInView:self.view];

    if (CGRectContainsPoint(_dialer_View.frame, touchLocation_Start)){
      startTouch_Angle = [self getAngle:touchLocation_Start];
    }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation_moved = [touch locationInView:self.view];

    if (CGRectContainsPoint(_dialer_View.frame, touchLocation_moved)){
        MovedTouch_Angle = [self getAngle:touchLocation_moved];
        [_dialer_View setTransform:CGAffineTransformMakeRotation(startTouch_Angle-MovedTouch_Angle)];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation_End = [touch locationInView:self.view];
    if (CGRectContainsPoint(_dialer_View.frame, touchLocation_End)){
        EndTouch_Angle = [self getAngle:touchLocation_End];

// for < 180
        if ((180/M_PI * (startTouch_Angle - EndTouch_Angle)) > 0.0f  && (180/M_PI * (startTouch_Angle - EndTouch_Angle)) <= 180.0f )
        {
            [UIView animateWithDuration:0.5F animations:^{
                [_dialer_View setTransform:CGAffineTransformMakeRotation(radians1)];
            }];
        }

// > 180
        else  {
            [UIView animateWithDuration:0.5F animations:^{
                [_dialer_View setTransform:CGAffineTransformMakeRotation(- 180 + radians1)];
                //_dialer_View.transform = CGAffineTransformIdentity;
            }];![enter image description here][1]
        }
    }
}

-(CGFloat) getAngle: (CGPoint) touchLocation
{
    CGFloat x1 = _dialer_View.center.x;
    CGFloat y1 = _dialer_View.center.y; 
    CGFloat x2 = touchLocation.x;
    CGFloat y2 = touchLocation.y;
    CGFloat baseAngle = atan2((x2-x1),(y2-y1));
    return baseAngle;
}
4

1 回答 1

0

查看此示例代码和链接

-(void)handleObject:(NSSet *)touches 
      withEvent:(UIEvent *)event 
         isLast:(BOOL)lst 
{
UITouch *touch =[[[event allTouches] allObjects] lastObject];
CGPoint curLoc = [touch locationInView:self.view];

    //NSLog(@"Touched Circle 1 ...");

    float fromAngle = atan2( firstLoc.y-circle1ImgVw.center.y, 
                            firstLoc.x-circle1ImgVw.center.x );
    float toAngle = atan2( curLoc.y-circle1ImgVw.center.y, 
                          curLoc.x-circle1ImgVw.center.x );

    // So the angle to rotate to is relative to our current angle and the
    // angle through which our finger moved (to-from)
    newAngle = angle + (toAngle - fromAngle);

    theta_deg1 = (newAngle/M_PI*180) + (newAngle > 0 ? 0 : 360);
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
[self handleObject:touches withEvent:event isLast:YES];

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.1];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        newAngle2 = theta_deg1 * M_PI / 180;
        timer1 = [NSTimer scheduledTimerWithTimeInterval: 0.01 target: self selector:@selector(handleTimer) userInfo: nil repeats: YES];
        [UIView commitAnimations];

 }


-(IBAction)handleTimer
{
newAngle2 -= 0.05;

if (newAngle2 < 0) 
{ 
    //kill timer and animation will end
    [timer1 invalidate];
    self.timer1 = nil;


}
    else
    {
        //NSLog(@"hadleTimer - newAngle2: %f ...", newAngle2);

//here is where you define what view you want to transform / rotate - e.g. circle1ImgVw
        CGAffineTransform transform=CGAffineTransformMakeRotation(newAngle2);
        circle1ImgVw.transform = transform;
    }


}
于 2013-12-09T13:47:38.630 回答