首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何绘制单点(Swift)

如何绘制单点(Swift)
EN

Stack Overflow用户
提问于 2019-10-17 13:42:24
回答 1查看 270关注 0票数 2

我正在制作一款应用程序,可以在点之间绘制线条来绘制图形。第一件事是用触控来画点,但是我已经尝试了很多,但是我仍然找不到首先画点的方法。下面是我的代码:

代码语言:javascript
运行
复制
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()
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-17 13:45:59

现在,您只需要获取该位置并在其中添加一个视图。尝试更新touchesBegan,使其如下所示:

代码语言:javascript
运行
复制
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)
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58425627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档