gpt4 book ai didi

ios - 如何获取位置 UIImageView。有很多 UIImageView

转载 作者:行者123 更新时间:2023-11-28 07:39:08 25 4
gpt4 key购买 nike

我正在代码中创建多个 UIImageView,并且想要移动和删除我选择的任何 UIImageView。现在,我只能移动使用 UIPanGestureRecognizer 创建的最后一个 UIImageView

我希望能够选择移动或删除我创建的任何 UIImageView(使用 UIPanGestureRecognizerUITapGestureRecognizer),而不仅仅是最后一个一个。


这是我一直在使用的一些代码:

import UIKit

var pointX: CGFloat = 0.0
var pointY: CGFloat = 0.0
var w: CGFloat = 50
var h: CGFloat = 50
var flag: Int = 0

class ViewController: UIViewController {
@IBOutlet var tapG: UITapGestureRecognizer!
@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var view_safe: UIView!
var boxViewArray: Array<UIImageView> = []
var tap = UITapGestureRecognizer()
var drag = UIPanGestureRecognizer()

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func tapGesture_action(_ sender: UITapGestureRecognizer) {
let boxImage: UIImage
let point = sender.location(in: view_safe)

pointX = point.x
pointY = point.y

//四角形のイメージを生成
boxImage = makeBoxImage(x: pointX-(w/2), y: pointY-(h/2), width: w, height: h)

var tmp: UIImageView = UIImageView()
tmp = UIImageView(image: boxImage)
tmp.tag = flag
flag += 1

tap = UITapGestureRecognizer()

drag = UIPanGestureRecognizer(target: self, action: #selector(self.panView(sender:)))
tmp.isUserInteractionEnabled = true
tmp.addGestureRecognizer(drag)

boxViewArray.append(tmp)

self.ImageView.addSubview(boxViewArray.last!)
//self.ImageView.bringSubview(toFront: boxViewArray.last!)
}

func makeBoxImage(x: CGFloat, y: CGFloat, width w: CGFloat, height h: CGFloat) -> UIImage{
print("makeBoxImage 開始")
//イメージ処理の開始
let size = CGSize(width: ImageView.frame.width, height: ImageView.frame.height)
UIGraphicsBeginImageContextWithOptions(size, false, 1.0)
//コンテキスト
let context = UIGraphicsGetCurrentContext()
//サイズを決める
let drawRect = CGRect(x: x, y: y, width: w, height: h)
//パスを作る
let drawPath = UIBezierPath(rect: drawRect)
//塗り色
context?.setFillColor(red: 0.0, green: 1.0, blue: 1.0, alpha: 1.0)
//パスを塗る
drawPath.fill()
//線の色
context?.setStrokeColor(red: 0.0, green: 1.0, blue: 1.0, alpha: 1.0)
//パスを描く
drawPath.stroke()
//イメージコンテキストからUIImageを作る
let image = UIGraphicsGetImageFromCurrentImageContext()
//イメージ処理の終了
UIGraphicsEndImageContext()

return image!
}

@objc func panView(sender: UIPanGestureRecognizer){

//let tagNo = sender.view?.tag
//print(tagNo)
//移動量を取得
let move: CGPoint = sender.translation(in:self.view)
//ドラッグした部品の座標に移動量を加算
sender.view!.center.x += move.x
sender.view!.center.y += move.y
//移動量を0に
sender.setTranslation(CGPoint.zero, in: self.view)
}

@objc func delete_tapped(_ sender: UITapGestureRecognizer){
print("delete")
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print(#function)
for touch in touches {
let location = touch.location(in: view_safe)
print(location)

}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: view_safe)
let thePoint: CGPoint = location
let hitView: UIView = UIView()

hitView.hitTest(thePoint, with: event)
//NSLog("Hit Test : tag = %ld", hitView.tag)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()}
}

最佳答案

我只是帮助你理解逻辑。

您需要一个 ImageView 列表。在每次点击时,您将通过检查每个 imageview 位置来检查点击了什么 imageview。 *不要检查你在 ImageView 上的点击,检查你在 View Controller 上的点击。

如果您想通过平移移动对象,如果您在知道将移动哪个 ImageView 后将其应用于 ImageView ,那么您的代码是正确的。

希望能帮到你。

*我以前创建过这个逻辑,但有点棘手

关于ios - 如何获取位置 UIImageView。有很多 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52748212/

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