2

我试图UIImageView在 a 内进行旋转,UIScrollView但是当我尝试缩放/取消缩放时,我的旋转回到 0

这是我的轮换代码:

- (void)rotateImage:(UIRotationGestureRecognizer*)rotate
  {
  if ([rotate state] == UIGestureRecognizerStateEnded)
  {
    rotateAngle += [spin rotation];
    return;
  }

  myView.transform = CGAffineTransformMakerotation(rotateAngle + [rotate rotation]);
}

关于UIScrollView,我只是返回 myView-(UIView*)viewForZoomingInScrollView:

最后一个信息,在我的界面生成器中,这是我的视图堆栈:

UIImageView
UIView (myView)
UISCrollView

这意味着我在和UIView之间有一个UIImageViewUIScrollView

4

2 回答 2

4

我宁愿建议您使用捏合手势来处理缩放。它会看起来更整洁,更均匀。向视图添加捏合手势。对于缩放,在其选择器方法中添加以下代码

   - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer
{


   myView.transform = CGAffineTransformScale(recogniser.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;

}

用于旋转,

    -(void)handleRotate:(UIRotationGestureRecognizer *)rec
{


    myView.transform = CGAffineTransformRotate(rec.view.transform, rec.rotation);
    rec.rotation = 0;
}

确保将 self 声明为手势的委托并实现以下委托方法

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

从中删除 myView

 -(UIView*)viewForZoomingInScrollView:  

在实施我的解决方案之前的方法。让手势单独处理缩放而不是滚动视图。:) 祝你好运

于 2012-12-14T14:08:09.243 回答
3

捏手势使用苹果库中的代码

不要忘记添加UIGestureRecognizerDelegate,UIScrollViewDelegate

self.ScrollView= [[UIScrollView alloc]init];
self.ScrollView.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
[self.view addSubview:self.ScrollView];

// for pinch Gesture
self.ScrollView.minimumZoomScale=0.5;
self.ScrollView.maximumZoomScale=6.0;
self.ScrollView.contentSize=CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
self.ScrollView.delegate = self;

[self.ScrollView addSubview:imageView];

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{ 
    return imageView;
}

对于旋转,

UIRotationGestureRecognizer  *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotate:)];
    rotation.delegate =self;
    [self.ScrollView addGestureRecognizer:rotation];

- (IBAction)handleRotate:(UIRotationGestureRecognizer *)recognizer {

    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    recognizer.rotation = 0;

}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
于 2013-12-02T10:07:27.417 回答