根据触摸旋转imageView

时间:2013-11-16 10:30:29

标签: ios cgaffinetransform touchmove

我正在旋转imageView,这是一个时钟的小手,有

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

现在我想按照我的手指顺时针/逆时针旋转它,以我的触摸速度。

我怎么能做到这一点?

1 个答案:

答案 0 :(得分:2)

检查此代码

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