使用 CGPath 创建一个子视图,而不是在该子视图上添加 UIPanGestureRecognizer
Create a subView using CGPath and than adding UIPanGestureRecognizer on that subview
我看到了几个关于向子视图添加 GestureRecognzier
的答案,但我的问题是我事先没有可用的子视图框架。我正在绘制一个 CGPath
并且在 Touches Ended
方法中,我想创建一个新的 subView
,其框架等于 CGPath
边界框。在那之后,我想用 PanGestureRecognizer
拖那个 subView
。我正在尝试实现 Evernote crop
功能,用户可以在其中选择特定的视图区域并将其移动到其他位置。这是该解决方案的正确方法吗?
不太明白UIGestureRecognizer
和.frame
之间的关系。你可以简单地添加 UIGestureRecognizer
到对象,一旦它的初始化工作完成。
尝试在绘制子视图后直接在TouchEnd
方法中添加手势。
import UIKit
class GestureResearchVC: UIViewController{
var subViewByCGPath: UIView?
override func viewDidLoad() {
super.viewDidLoad()
}
func createSubView(){
//creat subview
subViewByCGPath = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
subViewByCGPath?.backgroundColor = UIColor.yellow
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 50,y: 50), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
subViewByCGPath?.layer.addSublayer(shapeLayer)
self.view.addSubview(subViewByCGPath!)
//add pan gesture to subViewByCGPath
let gesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureAction(rec:)))
subViewByCGPath?.addGestureRecognizer(gesture)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if subViewByCGPath == nil {
print("touch end once")
createSubView()
}else{
print("touch end repeatedly")
}
}
func panGestureAction(rec: UIPanGestureRecognizer){
print("pannnnnnn~")
let transPoint = rec.translation(in: self.view)
let x = rec.view!.center.x + transPoint.x
let y = rec.view!.center.y + transPoint.y
rec.view!.center = CGPoint(x: x, y: y)
rec.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
//distinguish state
switch rec.state {
case .began:
print("began")
case .changed:
print("changed")
case .ended:
print("ended")
default:
print("???")
}
}
}
我看到了几个关于向子视图添加 GestureRecognzier
的答案,但我的问题是我事先没有可用的子视图框架。我正在绘制一个 CGPath
并且在 Touches Ended
方法中,我想创建一个新的 subView
,其框架等于 CGPath
边界框。在那之后,我想用 PanGestureRecognizer
拖那个 subView
。我正在尝试实现 Evernote crop
功能,用户可以在其中选择特定的视图区域并将其移动到其他位置。这是该解决方案的正确方法吗?
不太明白UIGestureRecognizer
和.frame
之间的关系。你可以简单地添加 UIGestureRecognizer
到对象,一旦它的初始化工作完成。
尝试在绘制子视图后直接在TouchEnd
方法中添加手势。
import UIKit
class GestureResearchVC: UIViewController{
var subViewByCGPath: UIView?
override func viewDidLoad() {
super.viewDidLoad()
}
func createSubView(){
//creat subview
subViewByCGPath = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
subViewByCGPath?.backgroundColor = UIColor.yellow
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 50,y: 50), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
subViewByCGPath?.layer.addSublayer(shapeLayer)
self.view.addSubview(subViewByCGPath!)
//add pan gesture to subViewByCGPath
let gesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureAction(rec:)))
subViewByCGPath?.addGestureRecognizer(gesture)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if subViewByCGPath == nil {
print("touch end once")
createSubView()
}else{
print("touch end repeatedly")
}
}
func panGestureAction(rec: UIPanGestureRecognizer){
print("pannnnnnn~")
let transPoint = rec.translation(in: self.view)
let x = rec.view!.center.x + transPoint.x
let y = rec.view!.center.y + transPoint.y
rec.view!.center = CGPoint(x: x, y: y)
rec.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
//distinguish state
switch rec.state {
case .began:
print("began")
case .changed:
print("changed")
case .ended:
print("ended")
default:
print("???")
}
}
}