简体   繁体   English

根据iPhone SDK中的触摸旋转图像

[英]rotate image according to touch in iphone sdk

I want to rotate an image smoothly according to the touch of the user. 我想根据用户的触摸平稳地旋转图像。 The user moves touch as he wants to move the image. 用户在想要移动图像时移动触摸。 He also moves the touch circularly. 他还循环移动触摸。 How do I do this? 我该怎么做呢?

IT MAY HELP TO U .... 它可能会帮助您....

 -(IBAction)rotateImage:(id)sender{

                 UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
                 [rotationRecognizer setDelegate:self];
                 ["YOUR IMAGEVIEW" addGestureRecognizer:rotationRecognizer];
                 [rotationRecognizer release];  

                                 }

ROTATE ROTATE

  CGFloat lastRotation;
    CGFloat angle;
         -(void)rotate:(UIRotationGestureRecognizer *)sender{

[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];

if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

    lastRotation = 0.0;
    return;
}

CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
//angle = rotation;
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
angle = lastRotation;
      }
{
//Add rotate gesture to your imageView

 UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateGestureAction:)];
[imageView addGestureRecognizer:rotateGesture];
[rotateGesture release];

  [imageView setUserInteractionEnabled:YES];
  [imageView setMultipleTouchEnabled:YES];

}

- (void)rotateGestureAction:(UIRotationGestureRecognizer *)rotate 
 {
    UIImageView *imageView=(UIImageView *)rotate.view;

float prevRotation=0.0;
if (rotate.state == UIGestureRecognizerStateBegan) 
{
    prevRotation = 0.0;
} 
float thisRotate = rotate.rotation - prevRotation;
prevRotation = rotate.rotation;
yourImageView.transform = CGAffineTransformRotate(self.view.transform, thisRotate);
}

If you are asking for rotation image using single finger movement. 如果您要使用单指移动来获取旋转图像。 This is what you are seeking for. 就是您要寻找的。

-(void)rotateImage:(id)sender 
{
    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) 
    {
    lastRotation = 0.0;
    return;
}

CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

CGAffineTransform currentTransform = [(UIRotationGestureRecognizer*)sender view].transform;

CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

lastRotation = [(UIRotationGestureRecognizer*)sender rotation];

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM