我怎样才能 select 一个特定的圈子?

How can I select a specific circle?

我使用“绘制”创建了圆,并且可以将其放置在视图的任何位置。但是当我想通过单击来移动特定的圆圈时。它 select 的“最后”创建了圈子。 我怎样才能 select 一个特定的圈子?请指导我。 如果您还需要什么,请告诉我。

**CircleView**
import UIKit

class CircleView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
    }


    required init(coder aDecoder : NSCoder) {
        fatalError("init(coder : ) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext(){

            context.setLineWidth(2)
            UIColor.yellow.set()


            let circleCenter = CGPoint(x: frame.size.width / 2, y: frame.self.height / 2)
            let circleRadius = (frame.size.width - 10) / 2

            context.addArc(center: circleCenter, radius: circleRadius, startAngle: 0, endAngle: .pi * 2, clockwise: true)

            context.strokePath()
        }
    }


}

**ViewController**
import UIKit

class ViewController: UIViewController {


    var circleView = CircleView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    let circleWidth = CGFloat(100)
    var lastCircleCenter = CGPoint()
    var currentCenter = CGPoint()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.lightGray
        circleView.isUserInteractionEnabled = true
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first{
            lastCircleCenter  = touch.location(in: view)
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first{
            let circleCenter = touch.location(in: view)

            if circleCenter == lastCircleCenter{

            let circleHeight = circleWidth

            circleView = CircleView(frame: CGRect(x: circleCenter.x - circleWidth / 2, y: circleCenter.y - circleWidth / 2, width: 100, height: 100))

                view.addSubview(circleView)
            }
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard let touch = touches.first else {
            return
        }
        let location = touch.location(in: view)
        circleView.center = location
    }

}

我怎样才能select一个特定的圈子?请指导我。 如果您还需要什么,请告诉我。 我怎样才能 select 一个特定的圈子?请指导我。 如果您还需要什么,请告诉我。

如果您要创建多个圈子并将它们添加到您的视图中,我建议您在集合中跟踪创建的圈子。这样,每次触摸时,您都可以检查坐标是否与创建的任何圆圈相匹配。在此基础上,您可以确定是创建新圈子还是移动现有圈子。

示例:

class ViewController: UIViewController {
    var circleViews: [CircleView] = []
    let circleWidth = CGFloat(100)
    var draggedCircle: CircleView?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        // Do nothing if a circle is being dragged
        // or if we do not have a coordinate
        guard draggedCircle == nil, let point = touches.first?.location(in: view) else {
            return
        }

        // Do not create new circle if touch is in an existing circle
        // Keep the reference of the (potentially) dragged circle
        if let draggedCircle = circleViews.filter({ UIBezierPath(ovalIn: [=10=].frame).contains(point) }).first {
            self.draggedCircle = draggedCircle
            return
        }

        // Create new circle and store in an array
        let offset = circleWidth / 2
        let rect = CGRect(x: point.x - offset, y: point.y - offset, width: circleWidth, height: circleWidth)
        let circleView = CircleView(frame: rect)
        circleViews.append(circleView)
        view.addSubview(circleView)
        // The newly created view can be immediately dragged
        draggedCircle = circleView
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        // If touches end then a circle is never dragged
        draggedCircle = nil
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let draggedCircle = draggedCircle, let point = touches.first?.location(in: view) else {
            return
        }

        draggedCircle.center = point
    }
}