为 imageview 创建通用变量以添加 uipangesture

create universal variable for imageview to add uipangesture

我的 swift 下面的代码声明了 2 个图像视图和 2 个 uipangesutre。这段代码只是 ouptus 2 个可以四处移动的图像视图。我想要做的是创建 1 个 uipangesture 并将其应用于所有图像视图。我认为有一种更有效的方法可以做到这一点。而不是为每个需要在视图控制器中移动的东西声明一个 var。

    import UIKit

  class ViewController: UIViewController {


var image1 = UIImageView(); var image2 = UIImageView()
var image1Pan = UIPanGestureRecognizer()
var image2Pan = UIPanGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()

    image1Pan = UIPanGestureRecognizer(target: self, action: #selector(ViewController.moveMethod))
    image1.addGestureRecognizer(image1Pan)

    image2Pan = UIPanGestureRecognizer(target: self, action: #selector(ViewController.moveMethod))
    image2.addGestureRecognizer(image2Pan)








    [image1,image2].forEach{
        [=10=].isUserInteractionEnabled = true

        view.addSubview([=10=])
        [=10=].translatesAutoresizingMaskIntoConstraints = false
    }



    // Do any additional setup after loading the view.
    NSLayoutConstraint.activate ([




        image1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
        image1.topAnchor.constraint(equalTo: view.topAnchor, constant : 50),

        image1.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10, constant: 0),
        image1.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.40, constant: 0),
        image1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),




        image2.topAnchor.constraint(equalTo: view.topAnchor, constant : 50),

        image2.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10, constant: 0),
        image2.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.40, constant: 0),
        image2.leadingAnchor.constraint(equalTo: image1.trailingAnchor, constant : 0),





    ])

    image1.backgroundColor = .blue
        image2.backgroundColor = .brown


}

@objc func moveMethod(_ sender: UIPanGestureRecognizer){



    let tranistioon = sender.translation(in: self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + tranistioon.x, y: sender.view!.center.y + tranistioon.y)
    sender.setTranslation(CGPoint.zero,in: self.view)    }}

手势识别器(例如`UIPanGestureRecognizer)是一个对象,而不是属性。

这段代码似乎很明显:

    // create a NEW instance of UIImageView each time throught the loop
    let imgView = UIImageView()

    var x: CGFloat = 50
    [UIColor.red, UIColor.green, UIColor.blue].forEach {

        // add imgView to self.view
        view.addSubview(imgView)

        // set imgView's properties
        imgView.backgroundColor = [=10=]
        imgView.frame = CGRect(x: x, y: 100, width: 50, height: 50)
        x += 100
    }

不会产生 3 次图片浏览。它将为您提供 一个 具有蓝色背景和框架 (250.0, 100.0, 50.0, 50.0) 的图像视图。那是因为您只创建了一个图像视图,并且每次循环都只是更改其属性。

要获得间隔 50 磅的三个图像视图(红色、绿色和蓝色背景),您需要这样做:

    var x: CGFloat = 50
    [UIColor.red, UIColor.green, UIColor.blue].forEach {

        // create a NEW instance of UIImageView each time throught the loop
        let imgView = UIImageView()

        // add it to self.view
        view.addSubview(imgView)

        // set its properties
        imgView.backgroundColor = [=11=]
        imgView.frame = CGRect(x: x, y: 100, width: 50, height: 50)
        x += 100
    }

这就是为什么您需要对手势识别器采用相同的方法。您需要一个 new 一个用于您希望将其添加到的每个对象。

根据你要做什么,你可以稍微简化一下:

    [image1, image2].forEach {
        [=12=].isUserInteractionEnabled = true
        view.addSubview([=12=])
        [=12=].translatesAutoresizingMaskIntoConstraints = false

        // create a NEW instance of UIPanGestureRecognizer
        let pg = UIPanGestureRecognizer(target: self, action: #selector(ViewController.moveMethod))

        // add it to [=12=] (an image view)
        [=12=].addGestureRecognizer(pg)
    }