在起点画一个矩形,跟着我的动作在Swift结束手势

Draw a rectangle the starting point and follow my movement to end the gesture in Swift

我在 上提出了问题,并得到了完美的答复。

要获取UIPanGestureRecognizer的起点和终点,使用代码:

var view = UIView()

func panGestureMoveAround(gesture: UIPanGestureRecognizer) {

    var locationOfBeganTap: CGPoint

    var locationOfEndTap: CGPoint

    if gesture.state == UIGestureRecognizerState.Began {
        locationOfBeganTap = gesture.locationInView(view)
    } else if gesture.state == UIGestureRecognizerState.Ended {
        locationOfEndTap = gesture.locationInView(view)
    }
}

为了更好的观看效果,我会绘制一个捕捉起点的矩形并自动调整大小以完成移动。

如何在起点画一个矩形,然后跟随我的动作结束手势?

您需要创建一个 UIView 的子类,将其命名为类似 RectView 的名称,如下所示:

@IBDesignable
class RectView: UIView {
    @IBInspectable var startPoint:CGPoint?{
        didSet{
            self.setNeedsDisplay()
        }
    }

    @IBInspectable var endPoint:CGPoint?{
        didSet{
            self.setNeedsDisplay()
        }
    }

    override func drawRect(rect: CGRect) {
        if (startPoint != nil && endPoint != nil){
            let path:UIBezierPath = UIBezierPath(rect: CGRectMake(min(startPoint!.x, endPoint!.x),
            min(startPoint!.y, endPoint!.y),
            fabs(startPoint!.x - endPoint!.x),
            fabs(startPoint!.y - endPoint!.y)))
        path.stroke()
    }
    }

}

然后在你的视图控制器中你可以这样说:

@IBAction func panGestureMoveAround(sender: UIPanGestureRecognizer) {
        var locationOfBeganTap: CGPoint

        var locationOfEndTap: CGPoint

        if sender.state == UIGestureRecognizerState.Began {
            locationOfBeganTap = sender.locationInView(rectView)
            rectView.startPoint = locationOfBeganTap
            rectView.endPoint = locationOfBeganTap

        } else if sender.state == UIGestureRecognizerState.Ended {
            locationOfEndTap = sender.locationInView(rectView)
                        rectView.endPoint = sender.locationInView(rectView)
        } else{
            rectView.endPoint = sender.locationInView(rectView)
        }
    }

Swift 5个版本

class RectView: UIView {
    var startPoint: CGPoint? {
        didSet {
            setNeedsDisplay()
        }
    }

    var endPoint: CGPoint? {
        didSet {
            setNeedsDisplay()
        }
    }

    override func draw(_ rect: CGRect) {
        guard let startPoint = startPoint, let endPoint = endPoint else {
            return
        }

        let path = UIBezierPath(
            rect: CGRect(
                x: min(startPoint.x, endPoint.x),
                y: min(startPoint.y, endPoint.y),
                width: abs(startPoint.x - endPoint.x),
                height: abs(startPoint.y - endPoint.y)
            )
        )
        path.stroke()
    }
}
@objc func panGestureMoveAround(_ sender: UIPanGestureRecognizer) {
    var locationOfBeganTap: CGPoint

    switch sender.state {
    case .began:
        locationOfBeganTap = sender.location(in: rectView)
        rectView.startPoint = locationOfBeganTap
        rectView.endPoint = locationOfBeganTap
    case .changed:
        rectView.endPoint = sender.location(in: rectView)
    case .ended:
        rectView.endPoint = sender.location(in: rectView)
    default:
        break
    }
}