1

I am rotating imageView which is minuit hand of a clock, with

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
    UITouch *touch = [touches anyObject];
    if ([touch view] == _imgViewMinuit)
    {
        NSLog(@"Touched");
        _imgMinuitHand.transform = CGAffineTransformRotate(_imgMinuitHand.transform,DEGREES_RADIANS(6));
     }
 }

Now i want to rotate it as per my finger, clockwise/anticlockwise, with speed of my touch.

How I can achieve this ?

sourabhkumbhar
  • 259
  • 4
  • 11

1 Answers1

2

check this code

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[[event allTouches]anyObject];

    [self transformSpinnerwithTouches:touch];
}

-(void)transformSpinnerwithTouches:(UITouch *)touchLocation
{
    CGPoint touchLocationpoint = [touchLocation locationInView:self.view];
    CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view];
    //origin is the respective piont from that i gonna measure the angle of the current position with respective to previous position ....
    CGPoint origin;
    origin.x = arrow.center.x;
    origin.y = arrow.center.y;
    CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
    CGAffineTransform newTransform = CGAffineTransformScale(arrow.transform, 1, 1);
    CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
    CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
    CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
    CGFloat newAngle = currentRotation- previousRotation;
    temp1 = temp1 + newAngle;
    //NSLog(@"temp1 is %f",temp1);
    //NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
    newTransform = CGAffineTransformRotate(newTransform, newAngle);
    [self animateView:arrow toPosition:newTransform];
}
-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
{
arrow.transform = newTransform;
}

-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
    CGFloat x = secondPoint.x - firstPoint.x;
    CGFloat y = secondPoint.y - firstPoint.y;
    CGPoint result = CGPointMake(x, y);
    //NSLog(@"%f %f",x,y);
    return result;
}
Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • 1
    Thank you... very much... its working. just 2 issues now. 1. As i am having two hands for clock, need to rotate both when user touches them and drags them. 2. Can I rotate those with fix angle of 6 degree, or 30 degree ? coz i am further calculating time based on angle. and hour hand should only move with difference of an hour in clock. – sourabhkumbhar Nov 16 '13 at 11:13
  • you are only moving the rotation by 6 degree each time, you need to calculate the angle the hand should be at based on the position of the touch say you have a touch point x,y relative to an origin at the centre of the clock, you can calculate the angle with trigonometry CGFloat angle = atan2(y, x); _imgHand.transform = CGAffineTransformMakeRotation(angle); – Vizllx Nov 16 '13 at 11:17
  • @SourabhKumbhar Accept the answer, so that it can be helpful for others. – Vizllx Nov 16 '13 at 11:18
  • Ok. And what should i do, if need to do this only when user touch the image and drags it ? – sourabhkumbhar Nov 16 '13 at 11:26