菜鸟教程小白 发表于 2022-12-13 10:48:33

ios - 如何正确拖放、旋转和缩放 UIView?


                                            <p><p>在我的 iOS 应用程序中,我使用 UITouchGestureRecognizer 拖放、缩放和旋转 UIImageView。我还将信息存储在 ImageView 的最后位置,以便在应用重新启动时检索它。</p>

<p>这在 iOS7 上运行良好,但我最近开始在 iOS8 上进行测试,但遇到了一些问题,尤其是在缩放方面。每当 ImageView 在旋转参数中具有非 0 角度时,缩放就不会像预期的那样起作用并减小图像的大小。</p>

<p>我不明白操作系统为什么会这样,这是我的代码:</p>

<pre><code>-(void) viewDidAppear:(BOOL)animated{
    ;
    ;
    for (GIFavorite *favorite in self.favorites){
      UIImage *image = favorite.image;
      ImageView *imageView = [ initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
      imageView.center = CGPointMake(favorite.x, favorite.y);
      imageView.transform = CGAffineTransformMakeRotation(favorite.rotation);
      imageView.transform = CGAffineTransformScale(imageView.transform, favorite.scale, favorite.scale);
      imageView.image = image;
      imageView.userInteractionEnabled = YES;
      UIPanGestureRecognizer * panRecognizer = [ initWithTarget:self action:@selector(handlePan:)];
      panRecognizer.delegate = self;
      ;
      UIRotationGestureRecognizer * rotationRecognizer = [ initWithTarget:self action:@selector(handleRotate:)];
      rotationRecognizer.delegate = self;
      ;
      UIPinchGestureRecognizer * pinchRecognizer = [ initWithTarget:self action:@selector(handlePinch:)];
      pinchRecognizer.delegate = self;
      ;
      ;
    }
}
</code></pre>

<p>我猜我进行转换的方式是错误的,因为当我将 <code>CGAffineTransformMakeRotation</code> 指令放在 <code>CGAffineTransformScale</code> 之后而不是减小大小时,它会增加它。</p >

<p>我哪里做错了?</p>

<p>编辑:handleRotate 的代码</p>

<pre><code>- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    ImageView *imageView = recognizer.view;
    CGFloat radians = atan2f(imageView.transform.b, imageView.transform.a);
    favorite.rotation = radians;
    recognizer.rotation = 0;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>首先,你需要让你的 ViewController 实现 UIGestureRecognizerDelegate。</p>

<p><strong>SWIFT 3:-</strong></p>

<pre><code>func setUpGestures () {

    let panGesture = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(gesture:)))
    self.imageView.addGestureRecognizer(panGesture)

    let rotationGesture = UIRotationGestureRecognizer(target: self, action:#selector(self.handleRotationGesture(gesture:)))
    self.imageView.addGestureRecognizer(rotationGesture)

    let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.handlePinchGesture(gesture:)))
    self.imageView.addGestureRecognizer(pinchGesture)
}

func handlePinchGesture(gesture: UIPinchGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
      //print(&#34;UIPinchGestureRecognizer&#34;)
      gesture.view?.transform = (gesture.view?.transform)!.scaledBy(x: gesture.scale, y: gesture.scale)
      gesture.scale = 1.0
    }
}

func handleRotationGesture(gesture: UIRotationGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
      //print(&#34;UIRotationGestureRecognizer&#34;)
      gesture.view?.transform = (gesture.view?.transform)!.rotated(by: gesture.rotation)
      gesture.rotation = 0
    }
}

func handlePanGesture(gesture: UIPanGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
      //print(&#34;UIPanGestureRecognizer&#34;)
      let translation = gesture.translation(in: view)
      gesture.view?.transform = (gesture.view?.transform)!.translatedBy(x: translation.x, y: translation.y)
      gesture.setTranslation(CGPoint(x: 0, y: 0), in: view)
    }
}
</code></pre>

<p>希望,这就是您要找的。任何问题都可以回复我。 :)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何正确拖放、旋转和缩放 UIView?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32538629/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32538629/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何正确拖放、旋转和缩放 UIView?