首页 > 解决方案 > UITextView/UIView 通过使用平移手势从边缘拖动来改变框架宽度 Swift iOS

问题描述

我想使用平移手势更改 UITextView / UIView 框架宽度(拖动红点以更改框架宽度),根据行的框架宽度编号相应地排列。任何人都可以请建议我如何实现这一目标。

在此处输入图像描述

我已经为红点视图添加了平移手势,并且可以正常更改 UIview 的宽度,但是在旋转视图之后它无法正常工作。

func addCustomView() {
    myView = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
    myView.backgroundColor = .yellow
    
    //Left Dot
    let leftDotView = UIView()
    leftDotView.backgroundColor = .red
    myView.addSubview(leftDotView)
    let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(leftDotMoveGesture(_:)))
    panRecognizer.delegate = self
    leftDotView.addGestureRecognizer(panRecognizer)

    leftDotView.translatesAutoresizingMaskIntoConstraints = false
    myView.addConstraint(NSLayoutConstraint(item: leftDotView, attribute: .leading, relatedBy: .equal, toItem: myView, attribute: .leading, multiplier: 1, constant: 0))
    myView.addConstraint(NSLayoutConstraint(item: leftDotView, attribute: .top, relatedBy: .equal, toItem: myView, attribute: .top, multiplier: 1, constant: 30))
    myView.addConstraint(NSLayoutConstraint(item: leftDotView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 40))
    myView.addConstraint(NSLayoutConstraint(item: leftDotView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 10))

    //Right Dot
    let rightDotView = UIView()
    rightDotView.backgroundColor = .red
    myView.addSubview(rightDotView)
    let rightDotPanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(rightDotMoveGesture(_:)))
    rightDotPanRecognizer.delegate = self
    rightDotView.addGestureRecognizer(rightDotPanRecognizer)

    rightDotView.translatesAutoresizingMaskIntoConstraints = false
    myView.addConstraint(NSLayoutConstraint(item: rightDotView, attribute: .trailing, relatedBy: .equal, toItem: myView, attribute: .trailing, multiplier: 1, constant: 0))
    myView.addConstraint(NSLayoutConstraint(item: rightDotView, attribute: .top, relatedBy: .equal, toItem: myView, attribute: .top, multiplier: 1, constant: 30))
    myView.addConstraint(NSLayoutConstraint(item: rightDotView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 40))
    myView.addConstraint(NSLayoutConstraint(item: rightDotView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 10))
    
    //UItextView
    let txtView = UITextView()
    txtView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
    txtView.font = UIFont.systemFont(ofSize: 15.0)
    txtView.isUserInteractionEnabled = false
    txtView.backgroundColor = .clear
    myView.addSubview(txtView)
    txtView.translatesAutoresizingMaskIntoConstraints = false
    myView.addConstraint(NSLayoutConstraint(item: txtView, attribute: .leading, relatedBy: .equal, toItem: myView, attribute: .leading, multiplier: 1, constant: 10))
    myView.addConstraint(NSLayoutConstraint(item: txtView, attribute: .trailing, relatedBy: .equal, toItem: myView, attribute: .trailing, multiplier: 1, constant: -10))
    myView.addConstraint(NSLayoutConstraint(item: txtView, attribute: .top, relatedBy: .equal, toItem: myView, attribute: .top, multiplier: 1, constant: 10))
    myView.addConstraint(NSLayoutConstraint(item: txtView, attribute: .bottom, relatedBy: .equal, toItem: myView, attribute: .bottom, multiplier: 1, constant: -10))
    
    self.view.addSubview(myView)
    
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedView(_:)))
    panGesture.delegate = self
    myView.isUserInteractionEnabled = true
    myView.addGestureRecognizer(panGesture)

    //add rotate gesture.
    let rotate = UIRotationGestureRecognizer.init(target: self, action: #selector(handleRotate(recognizer:)))
    rotate.delegate = self
    myView.addGestureRecognizer(rotate)

}

//MARK: Gesture Recognizer

@objc func handleRotate(recognizer : UIRotationGestureRecognizer) {
    if let view = recognizer.view {
        view.transform = view.transform.rotated(by: recognizer.rotation)
        recognizer.rotation = 0
    }
}

@objc func draggedView(_ sender:UIPanGestureRecognizer){
    self.view.bringSubviewToFront(myView)
    let translation = sender.translation(in: self.view)
    myView.center = CGPoint(x: myView.center.x + translation.x, y: myView.center.y + translation.y)
    sender.setTranslation(CGPoint.zero, in: self.view)
}
@objc func leftDotMoveGesture(_ recognizer: UIPanGestureRecognizer) {
        let touchLocation = recognizer.location(in: self.view)
        var getSuperView = myView.frame
        getSuperView.size.width = getSuperView.size.width + (getSuperView.origin.x - touchLocation.x)
        getSuperView.origin.x = touchLocation.x
        myView.frame = getSuperView
    }

@objc func rightDotMoveGesture(_ recognizer: UIPanGestureRecognizer) {
    let touchLocation = recognizer.location(in: self.view)
    var getSuperView = myView.frame
    getSuperView.size.width = touchLocation.x - getSuperView.origin.x
    myView.frame = getSuperView
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

我已经检查了以下内容,但没有针对此功能找到合适的解决方案。

如何通过从边缘拖动来调整 UIView 的大小?

https://github.com/ppoh71/resizeRectangleOnTouchDrag

https://newbedev.com/how-to-resize-uiview-by-dragging-from-its-edges

我将不得不使用旋转/平移手势(更改位置)/调整框架 UIview 的大小,我还需要根据用户的需要添加多个 n 个视图。

标签: iosswiftuiviewuitextviewuigesturerecognizer

解决方案


奇怪行为的问题是由于您正在调整frame视图的大小。由于您还要旋转视图然后调整大小,因此您需要调整bounds视图的大小。当您旋转视图时,它会frame不断变化,但bounds保持不变。


推荐阅读