【发布时间】:2021-10-27 04:28:18
【问题描述】:
我想使用平移手势更改 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
}
我已检查以下内容,但没有针对此功能找到合适的解决方案。
How to resize UIView by dragging from its edges?
https://github.com/ppoh71/resizeRectangleOnTouchDrag
https://newbedev.com/how-to-resize-uiview-by-dragging-from-its-edges
我将不得不使用旋转/平移手势(更改位置)/调整框架 UIview 的大小,我还需要根据用户的需要添加多个 n 个视图。
【问题讨论】:
-
您能否发布一张图片,说明旋转后调整大小的视图现在的样子以及您想要的样子?
-
实际上,我想创建一个屏幕,例如在图像上写一些 n 个文本,文本可移动、可旋转和可调整大小等,以便在同一屏幕上添加文本和管理/排列,我想创建像照片编辑器应用程序。
标签: ios swift uiview uitextview uigesturerecognizer