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()
}
}
解决方案
现在,您只需要获取该位置并在那里添加一个视图。尝试更新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)
}
}
推荐阅读
- php - 如何将 sendmail.php 的执行限制为同一服务器内的一页?
- javascript - “FormFacade 说“语法错误:意外令牌”而不是查看分数页面
- tensorflow - 如何通过 TFP ODE 求解器计算梯度 wrt time?
- ruby-on-rails - 【Vue.js】v-for转的item添加删除按钮时如何删除
- django - Django Rest Framework:禁用更新中的保存
- flutter - 键盘出现时底部静电可以吗?
- javascript - 多个上传图片的问题
- random - 使用随机选择方法而不是使用 Fisher-Yates 对数组进行洗牌
- javascript - 如何按每个对象中的值顺序对数组中对象的索引进行排序
- python - InvalidArgumentError:reshape 的输入是一个具有 320 个值的张量,但请求的形状需要 5760 的倍数