3

我在缩放放置在 uiscrollview 内的 uiimageview 时遇到问题。我也在 StackOverflow 中搜索并检查了与我的问题相关的所有问题。我也尝试了 StackOverflow 中发布的所有答案。没有什么对我有用。

首先,我将 uiimageview 放在 nib 文件中的 uiscrollview 中,然后从相机胶卷中获取图像并填充图像视图。然后我使用 uirotationgesturerecognizer 来旋转图像。

这是我想要做的代码。

- (void)viewDidLoad
{
 [super viewDidLoad];
 NSLog(@"%@",[[UIDevice currentDevice] model]);

// Do any additional setup after loading the view, typically from a nib.
 self.imagePicker = [[[UIImagePickerController alloc] init] autorelease];

 self.picChosenImageView.layer.shouldRasterize = YES;
 self.picChosenImageView.layer.rasterizationScale = [UIScreen mainScreen].scale;

 self.picChosenImageView.layer.contents = (id)[UIImage imageNamed:@"test"].CGImage;
 self.picChosenImageView.layer.shadowColor = [UIColor blackColor].CGColor;
 self.picChosenImageView.layer.shadowOpacity = 0.8f;
 self.picChosenImageView.layer.shadowRadius = 8;
 self.picChosenImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.picChosenImageView.bounds].CGPath;

 UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc]initWithTarget:self
                                                                                               action:@selector(handleRotate:)] autorelease];
 rotationRecognizer.delegate = self;
 [self.picChosenImageView addGestureRecognizer:rotationRecognizer];

 self.containerView.delegate = self;
 self.containerView.contentSize = self.picChosenImageView.layer.frame.size;
 self.containerView.maximumZoomScale = 4.0f;
 self.containerView.minimumZoomScale = 1.0f;
 angle = 0.0f;
 useRotation = 0.0;
 isRotationStarted=FALSE;
 isZoomingStarted = FALSE; 
}

-(void)lockZoom
{
 maximumZoomScale = self.containerView.maximumZoomScale;
 minimumZoomScale = self.containerView.minimumZoomScale;

 self.containerView.maximumZoomScale = 1.0;
 self.containerView.minimumZoomScale = 1.0;
 self.containerView.clipsToBounds = false;
 self.containerView.scrollEnabled = false;
}

-(void)unlockZoom
{
    self.containerView.maximumZoomScale = maximumZoomScale;
    self.containerView.minimumZoomScale = minimumZoomScale;
    self.containerView.clipsToBounds = true;
    self.containerView.scrollEnabled = true;
}

#pragma mark - ScrollView delegate methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return  self.picChosenImageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{

    CGRect frame = self.picChosenImageView.frame;
    frame.origin = CGPointZero;
    self.picChosenImageView.frame = frame;
    //self.picChosenImageView.transform = prevTransform;

}

-(void) scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    if(!isZoomingStarted)
    {
        self.picChosenImageView.transform = CGAffineTransformRotate(self.picChosenImageView.transform, angle);
        NSLog(@"The zooming started");
        isZoomingStarted = TRUE;
        CGSize contentSize = self.containerView.bounds.size;
        CGRect contentFrame = self.containerView.bounds;
        NSLog(@"frame on start: %@", NSStringFromCGRect(contentFrame));
        NSLog(@"size on start: %@", NSStringFromCGSize(contentSize));
        //prevTransform = self.picChosenImageView.transform;
    }
}

-(void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    if(isZoomingStarted)
    {
        self.picChosenImageView.transform = CGAffineTransformRotate(self.picChosenImageView.transform, angle);
        isZoomingStarted = FALSE;
        CGSize contentSize = self.containerView.contentSize;
        CGRect contentFrame = self.containerView.bounds;
        NSLog(@"frame on end: %@", NSStringFromCGRect(contentFrame));
        NSLog(@"size on end: %@", NSStringFromCGSize(contentSize));
    }
}

#pragma mark - GestureRecognizer methods

- (void) handleRotate:(UIRotationGestureRecognizer *)recognizer
{
    if(isZoomingStarted == FALSE)
    {
        if([recognizer state] == UIGestureRecognizerStateBegan)
        {
            angle = 0.0f;
            [self lockZoom];
        }

        useRotation+= recognizer.rotation;

        while( useRotation < -M_PI )
        {
            useRotation += M_PI*2;
        }
        while( useRotation > M_PI )
        {
            useRotation -= M_PI*2;
        }
        NSLog(@"The rotated value is %f",RADIANS_TO_DEGREES(useRotation));
        self.picChosenImageView.transform = CGAffineTransformRotate([self.picChosenImageView transform],
                                                                    recognizer.rotation);
        [recognizer setRotation:0];

        if([recognizer state] == UIGestureRecognizerStateEnded)
        {
            angle = useRotation;
            useRotation = 0.0f;
            isRotationStarted = FALSE;
            self.containerView.hidden = NO;
            //prevTransform = self.picChosenImageView.transform;
            [self unlockZoom];
        }
    }
}

我的问题是,我能够成功地进行放大和缩小。我可以随心所欲地旋转 uiimageview。将 uiimageview 旋转到某个角度后,当我尝试放大时,imageview 会回到原始位置(将自身旋转回零度),然后进行缩放。我想保留旋转并缩放。我尝试保存以前的转换并在后面的 scrollDidzoom 和 scrollDidBegin 委托方法中分配。没有工作。请帮助我发现我忽略的错误。

4

2 回答 2

0

尝试使用CGAffineTransformScale而不是仅调整框架的大小以进行缩放:

anImage.transform = CGAffineTransformScale(anImage.transform, 2.0, 2.0);

更改缩放转换可能会解决您的旋转问题。

希望这可以帮助。

于 2012-08-09T17:06:01.110 回答
0

我有同样的问题。UIScrollView 正在控制 UIImageView 并且它使用没有旋转的变换。

所以我没有为滚动提供图像参考,并且我添加了 UIPinchGestureRecognizer 用于缩放。

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
  return nil
}

拖动仍在工作:)

// viewDidLoad
var pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(pinchRecogniezed))
scrollView.addGestureRecognizer(pinchGestureRecognizer)


func pinchRecogniezed(sender: UIPinchGestureRecognizer) {
    if sender.state == .Began || sender.state == .Changed {
        let scale = sender.scale
        imageView.transform = CGAffineTransformScale(imageView.transform, scale, scale)
        sender.scale = 1
    }
}
于 2016-06-06T14:32:06.137 回答