美文网首页
ios swift移动旋转缩放手势

ios swift移动旋转缩放手势

作者: wenju | 来源:发表于2021-07-27 09:22 被阅读0次

    代码如下

    class ZoomView: UIImageView {
        
        var Drag: UIPanGestureRecognizer!
        var Rotate: UIRotationGestureRecognizer!
        var Scale: UIPinchGestureRecognizer!
    
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            
            backgroundColor = .red
            
            Drag = UIPanGestureRecognizer(target: self, action: #selector(onDrag(gesture:)))
            Rotate = UIRotationGestureRecognizer(target: self, action: #selector(onRotate(gesture:)))
            Scale = UIPinchGestureRecognizer(target: self, action: #selector(onScale(gesture:)))
    
            addGestureRecognizer(Drag)
            addGestureRecognizer(Rotate)
            addGestureRecognizer(Scale)
    
        }
        
        //拖拽手势方法具体实现
        @objc func onDrag(gesture: UIPanGestureRecognizer) {
            let translation = gesture.translation(in: self.superview)
            var center = gesture.view!.center
            center.x += translation.x
            center.y += translation.y
            gesture.view!.center = center
            gesture.setTranslation(CGPoint.zero, in: self.superview)
        }
        //旋转手势方法具体实现
        @objc func onRotate(gesture: UIRotationGestureRecognizer?) {
            if let transform = gesture?.view?.transform.rotated(by: gesture?.rotation ?? 0.0) {
                gesture?.view?.transform = transform
            }
            gesture?.rotation = 0
        }
        //缩放手势方法具体实现
        @objc func onScale(gesture: UIPinchGestureRecognizer?) {
            if let transform = gesture?.view?.transform.scaledBy(x: gesture?.scale ?? 0.0, y: gesture?.scale ?? 0.0) {
                gesture?.view?.transform = transform
            }
            gesture?.scale = 1
        }
        
    }
    

    StoryBoard使用

    使用截图.png

    相关文章

      网友评论

          本文标题:ios swift移动旋转缩放手势

          本文链接:https://www.haomeiwen.com/subject/rpwnmltx.html