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

时间:2018-10-10 20:21:57

标签: ios swift macos swift4

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

我希望能够选择移动或删除我创建的任何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()}
}

2 个答案:

答案 0 :(得分:0)

您可以使用此代码从超级视图中删除ImageView。

@objc func delete_tapped(_ sender: Any){
    let image = sender as! UIImageView
    image.removeFromSuperview()
    print("delete")
}

答案 1 :(得分:0)

我会在逻辑上帮助您。

您需要一个图像视图列表。在每次点击时,您将通过检查每个图像查看位置来检查点击的图像查看。 *不要检查图像视图上的水龙头,请检查视图控制器上的水龙头。

如果要平移对象,则在知道要移动哪个imageview后将其应用到imageview上,代码就是正确的。

希望会帮助您。

* i之前已经创建了此逻辑,但是有点棘手