ios - 如何绘制一个点(Swift)

标签 ios swift

我正在制作一个应用程序,可以在点之间画线来制作图形。首先是使用触摸来绘制点,但我尝试了很多,但我仍然找不到首先绘制点的方法。这是我的代码:

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    var xpoint: CGFloat = 0
    var ypoint: CGFloat = 0
    var opacity: CGFloat = 1.0

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self.view)
            xpoint = location.x
            ypoint = location.y
        }
    }

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

最佳答案

现在,您只需获取该位置并在那里添加一个 View 。尝试将 touchesBegan 更新为如下所示:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self.view)
        xpoint = location.x
        ypoint = location.y

        //Initialize the view at the correct spot
        //We set the view's frame by giving it an origin (that's the CGPoint we build from the x and y coordinates) and giving it a size, which can be anything really
        let pointView = UIView(frame: CGRect(origin: CGPoint(x: xpoint, y: ypoint), size: CGSize(width: 25, height: 25))

        //Round the view's corners so that it is a circle, not a square
        view.layer.cornerRadius = 12.5

        //Give the view a background color (in this case, blue)
        view.backgroundColor = .blue

        //Add the view as a subview of the current view controller's view
        self.view.addSubview(view)
    }
}

关于ios - 如何绘制一个点(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58425627/

相关文章:

ios - 随机放置的 UILabel 旋转后尺寸错误

ios - xib 内的视频 View 未拉伸(stretch)至 Controller 的宽度

ios - 无法使用类型为 (Int64, String) 的参数列表调用 setValue

ios - CBPeripheral 对象的名称始终为 nil 值并且处于断开状态

ios - webViewDidFinishLoad() 似乎没有在 Swift 中运行

iOS如何检测用户是否更改了系统时间,如果没有则将设备时间与服务器时间进行比较

ios - 比较两个 UIImages Objective-c

ios - 如何为 UINavigationBar setBackgroundImage 设置动画 : forBarMetrics:?

ios图表框架值(value)重叠

ios - 如何在 uiimage 中找到特定颜色并更改该颜色