在iPhone中旋轉對應於觸摸拖動的圖像




uiimageview core-animation (2)

如果您的目標是iOS 3.2或更高版本,則可以使用UIRotationGestureRecognizer 。 代碼看起來像這樣:

在你的設置方法( viewDidLoadinit等)中:

UIRotationGestureRecognizer *rotationGesture = 
  [[UIRotationGestureRecognizer alloc] initWithTarget:self
                                               action:@selector(handleRotate:)];
rotationGesture.delegate = self;
[myImageView addGestureRecognizer:rotationGesture];
[rotationGesture release];

事件處理程序:

- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
  if(recognizer.state == UIGestureRecognizerStateBegan || 
     recognizer.state == UIGestureRecognizerStateChanged)
  {
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, 
                                                        recognizer.rotation);
    [recognizer setRotation:0];
  }
}

github上我還有更多的手勢識別器的例子(包括上面的例子)。

我想旋轉的圖像順時針或逆時針方向對應於用戶的觸摸拖動速度。 我認為這可以用一些數學和邏輯來完成。 代碼示例是什麼樣的?


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        currentTouch=touch;
        if (CGRectContainsPoint([self.view frame], [touch locationInView:self.ViewRotationArrow]))
        {
            [self transformSpinnerwithTouches:touch];
        }
        else if (!CGRectContainsPoint([ViewRotationArrow frame], [touch locationInView:self.ViewRotationArrow])) {
            [self dispatchTouchEvent:[touch view]WithTouch:touch];
        }
    }
}

-(void)transformSpinnerwithTouches:(UITouch *)touchLocation
{
    CGPoint touchLocationpoint = [touchLocation locationInView:self.view];
    CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view];

    //Origin is the respective point. From that I am going to measure the
    //angle of the current position with respect to the previous position ....
    CGPoint origin;
    origin.x=240;
    origin.y=160;
    //NSLog(@"currentTouch Touch In Location In View:%F %F\n",touchLocationpoint.x,touchLocationpoint.y);
    //NSLog(@"currentTouch Touch previous Location In View:%F %F\n",PrevioustouchLocationpoint.x,PrevioustouchLocationpoint.y);
    CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
    CGAffineTransform newTransform =CGAffineTransformScale(ViewRotationArrow.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;
    NSLog(@"currentRotation of x %F  previousRotation %F\n",currentRotation,previousRotation);
    NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
    temp1=temp1+newAngle;
    //NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
    newTransform = CGAffineTransformRotate(newTransform, newAngle);
    [self animateView:ViewRotationArrow toPosition:newTransform];
}

 -(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
        CGPoint result;
        CGFloat x = secondPoint.x-firstPoint.x;
        CGFloat y = secondPoint.y-firstPoint.y;
        result = CGPointMake(x, y);
        return result;
}



-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
{
    //animating the rotator arrow...
    [UIView setAnimationsEnabled:YES];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.0750];
    ViewRotationArrow.transform = newTransform;
    [UIView commitAnimations];
}