我有一个UIViewController.我想在其中一个以编程方式创建的视图中绘制一条线.看起来很简单,但我还没有找到有效的示例代码.
我是swift和Xcode的新手,我正在尝试制作一个tic tac toe游戏.除了如何通过三个x或o画一条线外,我已经弄明白了.我不知道如何画线.我已经在网上找到了答案,但无法弄明白.
我试图用UIBezierpath在UIView上绘制一条线.我想我错过了一些东西,但却无法找到它.
这是我的代码:
// Code for touch recognition
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = false
if let touch = touches.first as? UITouch? {
lastPoint = (touch?.location(in: fullview))!
//print(lastPoint)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = true
if let touch = touches.first as? UITouch? {
let currentPoint = touch?.location(in: fullview)
drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint!)
lastPoint = currentPoint!
//print(lastPoint)
//print("touch moved")
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { …Run Code Online (Sandbox Code Playgroud) 我试图创建图片中建模的UIView。虽然,我的代码似乎有问题。知道为什么吗?
在评论的帮助下解决了!正确的代码已更新!
更新:我创建了一个自定义UIView类这是基于注释的新代码。
从这里获取drawLine:使用UIBezierPath绘制一条线