Ios 如何使用swift 3.0使用单手指一次旋转视图并调整其大小

Ios 如何使用swift 3.0使用单手指一次旋转视图并调整其大小,ios,swift3,rotation,resize,Ios,Swift3,Rotation,Resize,我想在swift 3.0中使用单手指一次旋转并调整视图大小。我在objective-c中找到了许多解决方案,但我一直无法将其转换为swift。我使用了以下代码来旋转视图 let panRotateGesture = UIPanGestureRecognizer(target: self, action: #selector(self.rotateViewPanGesture)) rotateView?.addGestureRecognizer(panRotateGesture)

我想在swift 3.0中使用单手指一次旋转并调整视图大小。我在objective-c中找到了许多解决方案,但我一直无法将其转换为swift。我使用了以下代码来旋转视图

    let panRotateGesture = UIPanGestureRecognizer(target: self, action: #selector(self.rotateViewPanGesture))
    rotateView?.addGestureRecognizer(panRotateGesture)
    panRotateGesture.require(toFail: panRotateGesture)
    textView?.addSubview(rotateView!)
旋转视图范围函数

func rotateViewPanGesture(_ recognizer: UIPanGestureRecognizer) {

    if recognizer.state == .began {

        deltaAngle = atan2(Float(recognizer.location(in: textView).y - textView!.center.y), Float(recognizer.location(in: textView).x - textView!.center.x))
        startTransform = (textView?.transform)!

    } else if recognizer.state == .changed {

        let ang: Float = atan2(Float(recognizer.location(in: textView!.superview).y - textView!.center.y), Float(recognizer.location(in: textView!.superview).x - textView!.center.x))
        let angleDiff: Float = deltaAngle - ang
        textView?.transform = CGAffineTransform(rotationAngle: -(CGFloat)(angleDiff))
        textView?.setNeedsDisplay()

    } else if recognizer.state == .ended {

        deltaAngle = atan2(Float(recognizer.location(in: textView).y - textView!.center.y), Float(recognizer.location(in: textView).x - textView!.center.x))
        startTransform = (textView?.transform)!
        textView?.setNeedsDisplay()

    }

}
这里的textView是我的视图,我想旋转它调整它的大小

下面是我调整视图大小的代码

    func resizeViewPanGesture(_ recognizer: UIPanGestureRecognizer) {

        let translation = recognizer.translation(in: recognizer.view)

        if let view = recognizer.view {
            view.transform = (textView?.transform.translatedBy(x: translation.x, y: translation.y))!
        }

        recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
    }

}

但这对我不起作用。有人请帮我解决这个问题,我如何在swift 3中一次用一个手指旋转和调整视图大小?我已经用以下代码解决了我的问题。它可以帮助我用一个手指旋转和调整图像大小

 func stickerResizeGesture(_ recognizer: UIPanGestureRecognizer) {

    let superview = recognizer.view?.superview

    stickerContainerView = superview

    for element in (superview?.subviews)! {

        if element.accessibilityHint == "Main Image" {
            imgSticker = element as! UIImageView
        }

        if element.accessibilityHint == "Delete" {
            deleteView = element as? UIImageView
        }

        if element.accessibilityHint == "Rotate" {
            rotateView = element as? UIImageView
        }

        if element.accessibilityHint == "Resize" {
            resizeView = element as? UIImageView
        }
    }

    let touchLocation = recognizer.location(in: stickerContainerView?.superview)

    let center = CalculateFunctions.CGRectGetCenter((stickerContainerView?.frame)!)

    if recognizer.state == .began {

        prevPoint = recognizer.location(in: stickerContainerView?.superview)


        //Rotate
        deltaAngle = Float(atan2(touchLocation.y - center.y, touchLocation.x - center.x) - CalculateFunctions.CGAffineTrasformGetAngle(self.startTransform))

        initialBounds = stickerContainerView?.bounds

        initialDistance = CalculateFunctions.CGpointGetDistance(center, point2: touchLocation)
        //stickerContainerView?.setNeedsDisplay()


    } else if recognizer.state == .changed {

        if (stickerContainerView?.bounds.size.width)! < CGFloat(100.0) {


            stickerContainerView?.bounds = CGRect(x: (stickerContainerView?.bounds.origin.x)!,
                                                  y: (stickerContainerView?.bounds.origin.y)!,
                                                  width: (stickerContainerView?.bounds.size.width)!,
                                                  height: (stickerContainerView?.bounds.size.width)!)

            imgSticker.frame = CGRect(x: 12,
                                      y: 12,
                                      width: (stickerContainerView?.bounds.size.width)! - (resizeView?.frame.width)!,
                                      height: (stickerContainerView?.bounds.size.height)! - (resizeView?.frame.height)!)

            resizeView?.frame = CGRect(x: (stickerContainerView?.bounds.size.width)! - (resizeView?.frame.width)!,
                                       y: (stickerContainerView?.bounds.size.height)! - (resizeView?.frame.height)!,
                                       width: (resizeView?.frame.width)!,
                                       height: (resizeView?.frame.height)!)


            rotateView?.frame = CGRect(x: 0,
                                       y: (stickerContainerView?.bounds.size.height)! - (rotateView?.frame.height)!,
                                       width: (rotateView?.frame.width)!,
                                       height: (rotateView?.frame.height)!)

            deleteView?.frame = CGRect(x: 0,
                                       y: 0,
                                       width: (deleteView?.frame.width)!,
                                       height: (deleteView?.frame.height)!)

        } else if (stickerContainerView?.bounds.size.height)! < CGFloat(100.0) {

            stickerContainerView?.bounds = CGRect(x: (stickerContainerView?.bounds.origin.x)!,
                                                  y: (stickerContainerView?.bounds.origin.y)!,
                                                  width: (stickerContainerView?.bounds.size.height)!,
                                                  height: (stickerContainerView?.bounds.size.height)!)


            imgSticker.frame = CGRect(x: 12, y: 12,
                                      width: (stickerContainerView?.bounds.size.width)! - (resizeView?.frame.width)!,
                                      height: (stickerContainerView?.bounds.size.height)! - (resizeView?.frame.height)!)

            resizeView?.frame = CGRect(x: (stickerContainerView?.bounds.size.width)! - (resizeView?.frame.width)!,
                                       y: (stickerContainerView?.bounds.size.height)! - (resizeView?.frame.height)!,
                                       width: (resizeView?.frame.width)!,
                                       height: (resizeView?.frame.height)!)

            rotateView?.frame = CGRect(x: 0, y: (stickerContainerView?.bounds.size.height)! - (rotateView?.frame.height)!,
                                       width: (rotateView?.frame.width)!,
                                       height: (rotateView?.frame.height)!)

            deleteView?.frame = CGRect(x: 0,
                                       y: 0,
                                       width: (deleteView?.frame.width)!,
                                       height: (deleteView?.frame.height)!)

        } else {

            let point: CGPoint = recognizer.location(in: stickerContainerView!.superview)

            let newHeight = (stickerContainerView?.bounds.size.width)! + CGFloat(point.y - prevPoint.y)
            let newWidth = (stickerContainerView?.bounds.size.height)! + CGFloat(point.y - prevPoint.y)

            if newHeight < CGFloat(100.0) && newWidth < CGFloat(100.0) {

                stickerContainerView?.bounds = CGRect(x: stickerContainerView!.bounds.origin.x,
                                                      y: stickerContainerView!.bounds.origin.y,
                                                      width: 100.0,
                                                      height: 100.0)


            } else {

                stickerContainerView?.bounds = CGRect(x: stickerContainerView!.bounds.origin.x,
                                                      y: stickerContainerView!.bounds.origin.y,
                                                      width: newWidth,
                                                      height: newHeight)

            }


            imgSticker.frame = CGRect(x: 12,
                                      y: 12,
                                      width: (stickerContainerView?.bounds.size.width)! - (resizeView?.frame.width)!,
                                      height: (stickerContainerView?.bounds.size.height)! - (resizeView?.frame.height)!)

            resizeView?.frame = CGRect(x: (stickerContainerView?.bounds.size.width)! - (resizeView?.frame.width)!,
                                       y: (stickerContainerView?.bounds.size.height)! - (resizeView?.frame.height)!,
                                       width: (resizeView?.frame.width)!,
                                       height: (resizeView?.frame.height)!)

            rotateView?.frame = CGRect(x: 0,
                                       y: (stickerContainerView?.bounds.size.height)! - 25,
                                       width: (rotateView?.frame.width)!,
                                       height: (rotateView?.frame.height)!)

            deleteView?.frame = CGRect(x: 0,
                                       y: 0,
                                       width: (deleteView?.frame.width)!,
                                       height: (deleteView?.frame.height)!)

            prevPoint = recognizer.location(in: stickerContainerView?.superview)

        }

        // Rotate

        let ang: Float = atan2(Float(touchLocation.y - center.y), Float(touchLocation.x - center.x))

        let angleDiff = deltaAngle - ang

        stickerContainerView?.transform = CGAffineTransform(rotationAngle: -(CGFloat)(angleDiff))
        stickerContainerView?.setNeedsDisplay()


    } else if recognizer.state == .ended {


        prevPoint = recognizer.location(in: stickerContainerView?.superview)

        //Rotate

        let ang: Float = atan2(Float(touchLocation.y - center.y), Float(touchLocation.x - center.x))
        startTransform = (stickerContainerView?.transform)!

        // stickerContainerView?.setNeedsDisplay()


    }

}
func贴纸大小调整手势(u识别器:uipangestureerecognizer){
让superview=识别器.view?superview
粘贴容器视图=超级视图
对于(superview?.SubView)中的元素{
如果element.accessibilityHint==“主映像”{
imgSticker=元素为!UIImageView
}
如果element.accessibilityHint==“删除”{
deleteView=元素为UIImageView
}
如果element.accessibilityHint==“旋转”{
rotateView=元素为UIImageView
}
如果element.accessibilityHint==“调整大小”{
resizeView=元素为UIImageView
}
}
让touchLocation=recognizer.location(在:SticketContainerView?superview中)
让中心=CalculateFunctions.CGRectGetCenter((贴纸容器视图?.frame)!)
如果识别器.state==.已开始{
prevPoint=识别器.位置(位于:stickerContainerView?.superview)
//轮换
deltaAngle=Float(atan2(touchLocation.y-center.y,touchLocation.x-center.x)-CalculateFunctions.cGraffeTrasFormGetAngle(self.startTransform))
initialBounds=粘贴容器视图?.bounds
initialDistance=CalculateFunctions.CGpointGetDistance(中心,点2:touchLocation)
//贴纸ContainerView?.setNeedsDisplay()
}否则,如果识别器.state==.changed{
如果(粘贴容器视图?.bounds.size.width)!