swift - 在 viewController 中的 2 个不同的图像视图上使用 2 个不同的 UIPanGestureRecongizer
问题描述
我下面的代码初始化了 2 个不同的图像视图 pg 和 tim。我为 UIPanGestureRecognizer 创建了一个单独的 var,用于每个 imageview 和 func 以使用 UIPanGestureRecongizer 移动 imageview。现在我无法移动图像视图。当我单击 pg 时,它会消失。我只想开发一个功能,让我可以自由移动每个图像视图。我的所有代码都是以编程方式编写的,因此您只需复制代码即可。
import UIKit
class ViewController: UIViewController {
var pg = UIImageView()
var pgGesture = UIPanGestureRecognizer()
var pgCon = [NSLayoutConstraint]()
var tim = UIImageView()
var timGesture = UIPanGestureRecognizer()
var timCon = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[pg,tim].forEach({
$0.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview($0)
$0.backgroundColor = UIColor.systemPink
$0.isUserInteractionEnabled = true
})
pgGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.pgGestureMethod(_:)))
pg.addGestureRecognizer(pgGesture)
timGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.timGestureMethod(_:)))
tim.addGestureRecognizer(timGesture)
pgCon = [
pg.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :37.5),
pg.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
pg.widthAnchor.constraint(equalToConstant: 75),
pg.heightAnchor.constraint(equalToConstant: 50),
]
NSLayoutConstraint.activate(pgCon)
timCon = [
tim.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :120),
tim.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
tim.widthAnchor.constraint(equalToConstant: 75),
tim.heightAnchor.constraint(equalToConstant: 50),
]
NSLayoutConstraint.activate(timCon)
}
@objc func pgGestureMethod(_ sender: UIPanGestureRecognizer){
NSLayoutConstraint.deactivate(pgCon)
NSLayoutConstraint.deactivate(timCon)
self.view.bringSubviewToFront(pg)
let tranistioon = sender.translation(in: self.view)
pg.center = CGPoint(x: pg.center.x + tranistioon.x, y: pg.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
@objc func timGestureMethod(_ sender: UIPanGestureRecognizer){
NSLayoutConstraint.deactivate(timCon)
NSLayoutConstraint.deactivate(pgCon)
self.view.bringSubviewToFront(tim)
let tranistioon = sender.translation(in: self.view)
tim.center = CGPoint(x: tim.center.x + tranistioon.x, y: tim.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
}
解决方案
而不是constraints
,尝试给frame
两者imageViews
,即
override func viewDidLoad() {
super.viewDidLoad()
pg.frame = CGRect(x: 100, y: 100, width: 75, height: 50) //here....
tim.frame = CGRect(x: 200, y: 200, width: 75, height: 50) //here....
//rest of the code...
}
在上面的代码中,
frame
根据您的要求更改。- 从
viewDidLoad()
删除申请代码constraints
。
推荐阅读
- javascript - 如何在开发服务器中为 react-scripts 启动调用定义基本 URL
- html - nboots中两个div元素之间不必要的空格
- ios - 在旧 macOS 中运行旧 xcode 项目时遇到问题
- node.js - 如何从终端在 Linux 中手动安装 nodejs
- java - 是否可以检查方法是否在一组类中声明?
- node.js - 如何在 Firebase Cloud Firestore 中添加多行
- android - Android-Kotlin 无法使用 set Value 命令
- python - 为什么我使用 exec() 会出现语法错误?
- angular - AG-Grid 显示在多个 http 请求中获取的大量行
- html - 使 Bootstrap 导航栏品牌在其自己的行上可通过图像调整大小