gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:24:46 25 4
gpt4 key购买 nike

下面我的 swift 代码声明了 2 个 ImageView 和 2 个 uipangesutres。此代码仅占用 2 个可以移动的 ImageView 。我想要做的是创建 1 uipangesture 并将其应用于所有 ImageView 。我认为有一种更有效的方法可以做到这一点。而不是为 View Controller 中需要移动的每个东西声明一个 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) }}

最佳答案

手势识别器(例如“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 个 ImageView 。它将为您提供一个具有蓝色背景、框架为(250.0, 100.0, 50.0, 50.0)的 ImageView 。这是因为您只创建了一个 ImageView ,并且每次通过循环都只更改其属性。

要获得间隔 50 点的三个 ImageView (红色、绿色和蓝色背景),您需要执行以下操作:

    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)
}

关于swift - 为 imageview 创建通用变量以添加 uipangesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60143583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com