我正在代码中创建多个UIImageView
s,并希望移动和删除我选择的任何UIImageView
s。现在,我只能移动使用UIImageView
创建的最后一个UIPanGestureRecognizer
。
我希望能够选择移动或删除我创建的任何UIImageView
(使用UIPanGestureRecognizer
或UITapGestureRecognizer
),而不仅仅是最后一个。
下面是一些我一直在使用的代码:
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,则代码是正确的。
希望对你有帮助。
*我以前创造过这种逻辑,但有点棘手