【问题标题】:create universal variable for imageview to add uipangesture为 imageview 创建通用变量以添加 uipangesture
【发布时间】:2020-05-25 08:51:21
【问题描述】:

我下面的 swift 代码声明了 2 个图像视图和 2 个 uipangesutres。此代码仅输出 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{
        $0.isUserInteractionEnabled = true

        view.addSubview($0)
        $0.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)    }}

【问题讨论】:

    标签: swift loops foreach var uipangesturerecognizer


    【解决方案1】:

    手势识别器(例如 `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 = $0
            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 = $0
            imgView.frame = CGRect(x: x, y: 100, width: 50, height: 50)
            x += 100
        }
    

    这就是您需要对手势识别器采用相同方法的原因。您需要为每个要添加的对象创建一个新的

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

        [image1, image2].forEach {
            $0.isUserInteractionEnabled = true
            view.addSubview($0)
            $0.translatesAutoresizingMaskIntoConstraints = false
    
            // create a NEW instance of UIPanGestureRecognizer
            let pg = UIPanGestureRecognizer(target: self, action: #selector(ViewController.moveMethod))
    
            // add it to $0 (an image view)
            $0.addGestureRecognizer(pg)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多