如何在swift中以最简单的方式绘制一条线 [英] How to draw a line in the simplest way in swift

查看:120
本文介绍了如何在swift中以最简单的方式绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对swift和Xcode相当新,我正在努力制作一个tic tac toe游戏。除了如何通过三个x或o画一条线外,我已经弄明白了。我不知道如何绘制线条。我在网上找到了答案,但无法弄明白。有人可以帮忙吗



向前谢谢

解决方案

尝试查看UIBezierPath它会对你有所帮助画线很多。 文档以下是一个示例:

 覆盖func drawRect(rect:CGRect){
var aPath = UIBezierPath()

aPath.moveToPoint(CGPoint(x:/ *放置起始位置* /,y :/ *放置起始位置* /))

aPath.addLineToPoint(CGPoint(x:/ *下一个位置* /,y:/ *下一个位置* /))

//继续使用方法addLineToPoint,直到你到达要关闭路径的方式

aPath.closePath()

//如果你想要描边它带有红色
UIColor.redColor()。set()
aPath.stroke()
//如果你想要填充它
aPath.fill()
}

确保将此代码放在drawRect中。如上例所示。



如果您需要更新图纸,只需调用 setNeedsDisplay()进行更新。 / p>

I am fairly new to swift and Xcode and I am trying to make a tic tac toe game. I have everything figured out except how to draw a line through the three x's or o's. I have no idea on how to draw lines. I have looked on the web for the answer and can't figure it out. Can someone help.

Thanks Ahead

解决方案

Try looking into UIBezierPath it will help you a lot for drawing lines. Documentation Here is an example:

override func drawRect(rect: CGRect) {
      var aPath = UIBezierPath()

      aPath.moveToPoint(CGPoint(x:/*Put starting Location*/, y:/*Put starting Location*/))

      aPath.addLineToPoint(CGPoint(x:/*Put Next Location*/, y:/*Put Next Location*/))

      //Keep using the method addLineToPoint until you get to the one where about to close the path

      aPath.closePath()

      //If you want to stroke it with a red color
      UIColor.redColor().set()
      aPath.stroke()
      //If you want to fill it as well 
      aPath.fill()
}

Make sure you put this code in the drawRect. Like in the example above.

If you need to update the drawing just call setNeedsDisplay() to update.

这篇关于如何在swift中以最简单的方式绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆