我如何 move/drag 在 pdf 视图上查看多个视图?
How I can move/drag multiple views on pdf view?
我正在做的是,我查看了 pdf,它包含一个示例 pdf。
最重要的是,当用户单击导航栏中的添加按钮时,我添加了 1 个以上的签名者(自定义视图)视图。
场景 1:在 pdf 上添加第一个签名视图 (customview) 时,它正在添加,我可以 drag/move 在 pdf 上添加第一个签名视图 (Signatory1) , 这工作正常。
场景 2:在 pdf 上添加第二个签名视图 (Signatory2 customview) 时,它正在添加,我可以 drag/move 在 pdf 上添加第二个签名视图 (Signatory2) ,这也工作正常,但在这种情况下我不能 move/drag 第一个签名视图 (Signatory1)
场景 3:类似地,当在 pdf 上添加第三个签名视图(Signatory3 customview)时,它正在添加,我可以 drag/move 上的第三个签名视图(Signatory3) pdf,这也工作正常,但在这种情况下我不能 move/drag 第一个签名视图 (Signatory1) 和第二个签名视图 (Signatory2) 等等
问题是,我只能访问当前的 Signatory(我只能 move/drag 最近添加的当前 Signatory 视图),我不能 move/drag old签署方意见。
当我 click/touch (drag/move) pdf 视图上的任何特定签名视图时,如何根据我的选择 move/drag 任何签名?
这是一些代码,
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
customView1 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
customView2 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
customView3 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
customView4 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
customView5 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
loadPdf()
}
func loadPdf(){
if let path = Bundle.main.path(forResource: "appointment-letter", ofType: "pdf") {
if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
pdfView.displayMode = .singlePage // .singlePage //.singlePageContinuous //.twoUp
//by default display mode is - singlePageContinuous
pdfView.autoScales = true
pdfView.displayDirection = .vertical // .horizontal//.vertical
pdfView.document = pdfDocument
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin]
pdfView.zoomIn(self)
pdfView.autoScales = true
pdfView.backgroundColor = UIColor.white
//Imp line
pdfView.usePageViewController(true, withViewOptions: [:])
currentPageNumberOfPdf = self.pdfView.currentPage?.pageRef?.pageNumber ?? 0
print("Total Pages in PDF : ",pdfDocument.pageCount);
self.pdfView.bringSubviewToFront(customView1!)
}
}
}
@IBAction func addSignatoryButtonClicked(_ sender: Any) {
signatoryCount = signatoryCount + 1
if signatoryCount == 1 {
customView1?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView1!)
}
else if signatoryCount == 2 {
customView2?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView2!)
}
else if signatoryCount == 3 {
customView3?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView3!)
}
else if signatoryCount == 4 {
customView4?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView4!)
}
else if signatoryCount == 5 {
customView5?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView5!)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch?.location(in: self.pdfView)
// customView1?.center = touchLocation!
if signatoryCount == 1 {
customView1?.center = touchLocation!
return
}
else if signatoryCount == 2 {
customView2?.center = touchLocation!
return
}
else if signatoryCount == 3 {
customView3?.center = touchLocation!
return
}
else if signatoryCount == 4 {
customView4?.center = touchLocation!
return
}
else if signatoryCount == 5 {
customView5?.center = touchLocation!
return
}
// frame = view.convert(customView1!.frame, from: pdfView)
// print("touchesMoved \(frame!.dictionaryRepresentation)")
}
你已经很接近了。您只需要应用我在 中建议的相同方法,但要尊重超级视图的框架。
首先将这些助手添加到您的项目中:
extension CGPoint {
static func +(lhs: CGPoint, rhs: CGPoint) -> CGPoint {
.init(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
}
static func +=(lhs: inout CGPoint, rhs: CGPoint) {
lhs.x += rhs.x
lhs.y += rhs.y
}
}
extension UIView {
func translate(_ translation: CGPoint) {
let destination = center + translation
let minX = frame.width/2
let minY = frame.height/2
let maxX = superview!.frame.width-minX
let maxY = superview!.frame.height-minY
center = CGPoint(
x: min(maxX, max(minX, destination.x)),
y: min(maxY ,max(minY, destination.y)))
}
}
其次,在您的 ViewController.
中删除平移手势识别器和相应的方法
第三次将您的 SignatoryXibView 平移手势更改为下面的手势。这将平移视图的中心并尊重其父视图的框架:
@objc func pan(_ gesture: UIPanGestureRecognizer) {
translate(gesture.translation(in: self))
gesture.setTranslation(.zero, in: self)
setNeedsDisplay()
}
我正在做的是,我查看了 pdf,它包含一个示例 pdf。 最重要的是,当用户单击导航栏中的添加按钮时,我添加了 1 个以上的签名者(自定义视图)视图。
场景 1:在 pdf 上添加第一个签名视图 (customview) 时,它正在添加,我可以 drag/move 在 pdf 上添加第一个签名视图 (Signatory1) , 这工作正常。
场景 2:在 pdf 上添加第二个签名视图 (Signatory2 customview) 时,它正在添加,我可以 drag/move 在 pdf 上添加第二个签名视图 (Signatory2) ,这也工作正常,但在这种情况下我不能 move/drag 第一个签名视图 (Signatory1)
场景 3:类似地,当在 pdf 上添加第三个签名视图(Signatory3 customview)时,它正在添加,我可以 drag/move 上的第三个签名视图(Signatory3) pdf,这也工作正常,但在这种情况下我不能 move/drag 第一个签名视图 (Signatory1) 和第二个签名视图 (Signatory2) 等等
问题是,我只能访问当前的 Signatory(我只能 move/drag 最近添加的当前 Signatory 视图),我不能 move/drag old签署方意见。
当我 click/touch (drag/move) pdf 视图上的任何特定签名视图时,如何根据我的选择 move/drag 任何签名?
这是一些代码,
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
customView1 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
customView2 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
customView3 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
customView4 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
customView5 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
loadPdf()
}
func loadPdf(){
if let path = Bundle.main.path(forResource: "appointment-letter", ofType: "pdf") {
if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
pdfView.displayMode = .singlePage // .singlePage //.singlePageContinuous //.twoUp
//by default display mode is - singlePageContinuous
pdfView.autoScales = true
pdfView.displayDirection = .vertical // .horizontal//.vertical
pdfView.document = pdfDocument
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin]
pdfView.zoomIn(self)
pdfView.autoScales = true
pdfView.backgroundColor = UIColor.white
//Imp line
pdfView.usePageViewController(true, withViewOptions: [:])
currentPageNumberOfPdf = self.pdfView.currentPage?.pageRef?.pageNumber ?? 0
print("Total Pages in PDF : ",pdfDocument.pageCount);
self.pdfView.bringSubviewToFront(customView1!)
}
}
}
@IBAction func addSignatoryButtonClicked(_ sender: Any) {
signatoryCount = signatoryCount + 1
if signatoryCount == 1 {
customView1?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView1!)
}
else if signatoryCount == 2 {
customView2?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView2!)
}
else if signatoryCount == 3 {
customView3?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView3!)
}
else if signatoryCount == 4 {
customView4?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView4!)
}
else if signatoryCount == 5 {
customView5?.signatoryLabel.text = "Signatory \(signatoryCount)"
self.pdfView.addSubview(customView5!)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch?.location(in: self.pdfView)
// customView1?.center = touchLocation!
if signatoryCount == 1 {
customView1?.center = touchLocation!
return
}
else if signatoryCount == 2 {
customView2?.center = touchLocation!
return
}
else if signatoryCount == 3 {
customView3?.center = touchLocation!
return
}
else if signatoryCount == 4 {
customView4?.center = touchLocation!
return
}
else if signatoryCount == 5 {
customView5?.center = touchLocation!
return
}
// frame = view.convert(customView1!.frame, from: pdfView)
// print("touchesMoved \(frame!.dictionaryRepresentation)")
}
你已经很接近了。您只需要应用我在
首先将这些助手添加到您的项目中:
extension CGPoint {
static func +(lhs: CGPoint, rhs: CGPoint) -> CGPoint {
.init(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
}
static func +=(lhs: inout CGPoint, rhs: CGPoint) {
lhs.x += rhs.x
lhs.y += rhs.y
}
}
extension UIView {
func translate(_ translation: CGPoint) {
let destination = center + translation
let minX = frame.width/2
let minY = frame.height/2
let maxX = superview!.frame.width-minX
let maxY = superview!.frame.height-minY
center = CGPoint(
x: min(maxX, max(minX, destination.x)),
y: min(maxY ,max(minY, destination.y)))
}
}
其次,在您的 ViewController.
中删除平移手势识别器和相应的方法第三次将您的 SignatoryXibView 平移手势更改为下面的手势。这将平移视图的中心并尊重其父视图的框架:
@objc func pan(_ gesture: UIPanGestureRecognizer) {
translate(gesture.translation(in: self))
gesture.setTranslation(.zero, in: self)
setNeedsDisplay()
}