OStack程序员社区-中国程序员成长平台

标题: iphone - 在 ScrollView 中旋转缩放 ImageView? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:21
标题: iphone - 在 ScrollView 中旋转缩放 ImageView?

我有一个 UIScrollView 有一个缩放 UIImageView,即它实现:

- (UIView *)viewForZoomingInScrollViewUIScrollView *)scrollView
{
    return self.imageView;
}

我正在尝试将 UIRotationGestureRecognizer 添加到此 imageView 中,我这样做如下:

_rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self actionselector(rotate];
[self.imageView addGestureRecognizer:_rotationGestureRecognizer];

-(void)rotateid)sender
{
    UIRotationGestureRecognizer *rotationGestureRecognizer = [(UIRotationGestureRecognizer*)sender retain];

    if(rotationGestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        self.lastRotation = 0.0;
        return;
    }

    CGFloat rotation = 0.0 - (self.lastRotation - rotationGestureRecognizer.rotation);
    rotationGestureRecognizer.view.transform = CGAffineTransformRotate(rotationGestureRecognizer.view.transform, rotation);
    self.lastRotation = rotationGestureRecognizer.rotation;

    [rotationGestureRecognizer release];
}

我只是想知道,是否有可能做到这一点?似乎 UIScrollView 阻止了对我的 UIImageView 的触摸,因为没有发生任何事情。 Apple 是否建议不要在 UIScrollView 中使用缩放 View 来执行此操作?



Best Answer-推荐答案


以下代码正在运行。将手势添加到 scrollView 而不是 imageView。

 UIRotationGestureRecognizer* _rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self actionselector(rotate];
 [self.scrollView addGestureRecognizer:_rotationGestureRecognizer];

Swift 5 版本:

let rotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(self.rotate(_))
scrollView.addGestureRecognizer(rotationGestureRecognizer)

关于iphone - 在 ScrollView 中旋转缩放 ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167646/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4