2

i am making dialer animation like iRetro phone app animation.

For <180 degree , dialer moves back correctly Anti clockwise on its original position. but for > 180 degree its not working (not reaching to its original position Anti clockwise). This is my code:

- (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;
}
  • Hi can you help me regarding this i am looking for same functionality and have looked at many examples on the google but not found any for rotary dial, but examples of wheel rotating. Can you please guide me what and where from start. – iPhone Programmatically Dec 11 '13 at 11:50

1 Answers1

0

Look at this sample code and thelink

-(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;
    }


}
Community
  • 1
  • 1
ios7
  • 98
  • 1
  • 10