swift - 在 viewController 中的 2 个不同的 ImageView 上使用 2 个不同的 UIPanGestureRecongizer

标签 swift tags imageview uipangesturerecognizer cgpoint

下面的代码初始化了 2 个不同的 ImageView pg 和 tim。我为 UIPanGestureRecognizer 创建了一个单独的变量,用于每个 ImageView ,并使用 UIPanGestureRecongizer 来移动 ImageView 。现在我无法移动 ImageView 。当我点击 pg 时它就消失了。我只是想开发一个功能,让我可以自由移动每个 ImageView 。我的所有代码都是以编程方式编写的,因此您只需复制代码即可。

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)


}
}

最佳答案

尝试将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...
}

在上面的代码中,

  1. 根据您的要求更改框架
  2. viewDidLoad() 中删除应用约束的代码。

关于swift - 在 viewController 中的 2 个不同的 ImageView 上使用 2 个不同的 UIPanGestureRecongizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58796200/

相关文章:

ios - 在 UITapGestureRecognizer 中传递参数

swift - 动态 NSTooltips : Event Key Modifier(s) Affecting Tooltips

grails - 我可以在 GSP 之外使用 grails 标签吗?

amazon-web-services - Kubernetes 动态配置的 EBS 卷上的自定义标签

javascript - 在 HTML 中隐藏信息以供 JavaScript 查找

c# - Xamarin C# - 如何从变量以编程方式引用资源文件

android - 如何在屏幕中央显示 ImageView ?

swift - 列出专辑标题时如何使用右侧的表格 View "index"?

android - 如何使 ImageButton 仅显示图像?

xcode - 在 Swift 中将数据转发到目标 VC(带框架)?