ios - 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
}
我已经检查了以下内容,但没有针对此功能找到合适的解决方案。
https://github.com/ppoh71/resizeRectangleOnTouchDrag
https://newbedev.com/how-to-resize-uiview-by-dragging-from-its-edges
我将不得不使用旋转/平移手势(更改位置)/调整框架 UIview 的大小,我还需要根据用户的需要添加多个 n 个视图。
解决方案
奇怪行为的问题是由于您正在调整frame
视图的大小。由于您还要旋转视图然后调整大小,因此您需要调整bounds
视图的大小。当您旋转视图时,它会frame
不断变化,但bounds
保持不变。
推荐阅读
- javascript - ajax函数没有被触发
- angular-cli - 无法安装角度
- c# - Xamarin.Essentials“无法检测到当前 Activity。确保您已在 Activity 或 Application 类中调用了 Init。”
- python - python中带有特殊字符的字符串实习
- android - AsyncTask 没有执行多次
- c - 不使用结构的多项式数相加
- mysql - prestashop 数据库大容量表 ps_search_index
- runtime-error - 在 python 3.7 中学习二分搜索
- c# - 使用 DataContractJsonSerializer 将元素附加到文件中的 JSON 数组
- python - 回调()缺少 1 个必需的位置参数:“事件”