繁体   English   中英

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

[英]rotate image according to touch in iphone sdk

我想根据用户的触摸平稳地旋转图像。 用户在想要移动图像时移动触摸。 他还循环移动触摸。 我该怎么做呢?

它可能会帮助您....

 -(IBAction)rotateImage:(id)sender{

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

                                 }

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

如果您要使用单指移动来获取旋转图像。 就是您要寻找的。

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