使用UIScrollView进行缩放并使用UIRotationGestureRecognizer进行旋转

时间:2012-12-13 15:22:50

标签: iphone ios ipad uiscrollview cgaffinetransform

我正试图在UIImageView内进行UIScrollView轮播,但当我尝试缩放/取消缩放时,我的轮播会回到0

以下是我的轮换代码:

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

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

关于UIScrollView,我只需在-(UIView*)viewForZoomingInScrollView:

中返回myView

最后一条信息,在我的界面构建器中,这是我的视图堆栈:

UIImageView
UIView (myView)
UISCrollView

这表示我在UIViewUIImageView

之间有一个UIScrollView

2 个答案:

答案 0 :(得分: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:  
在实施我的解决方案之前的

方法。让我们只用手势来处理缩放而不是滚动视图。祝你好运

答案 1 :(得分: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;
}