Lecture5 Drawing

2019-07-19  本文已影响0人  快乐捣蛋鬼
lecture5_Today.png

Notes

1. Thrown Erros

在Swift中,函数可以抛出错误,而且你一定可以知道哪些方法会抛出错误,因为,在方法的末尾会有throws关键词。
func save() throws
You must put calls to funtions like this in a do{ } block and use the word try to call them

do {
    try context.save()
} catch let error {
    // error 遵循`Error protocol`(如:NSError)
    // 通常,error是有associated values的enum,从而可以获取错误的具体原因
    throw error // 这样会重新抛出错误
}

如果你并不关心error是什么,使用try
1.try! context.save()
如果你确定你调用的函数不会抛出错误,可以使用强制尝试try!,但是如果函数抛出了错误就会使你的程序崩溃。
2.try? context.save()
尝试这个函数,如果抛出了错误也不要崩溃,直接忽略掉吧!

2. Initializing a UIView 初始化一个UIView

init(frame: CGRect) // 帧编译初始化 - 使用代码的UIView初始化构造器
init(coder: NSCoder) // 编码器初始化 - 使用storyboard的UIView初始化构造器
awakeFromNib()  // this is only called if the UIView came out of a storyboard.

3. view的坐标体系

原点在左上角
Units are points, not pixels
boundaries 是drawing发生的地方
var bounds: CGRect// a view's internal drawing space's origin and size
view在哪里?与view的position有关,与view的drawing无关
var center: CGPoint// 相对于父坐标体系
var frame: CGRect // 相对于父坐标体系

4.Draw,绘图

在所有的iOS中,只用一种方法可以draw
override func draw(_ rect: CGRect)
NEVER call draw(CGRect)
如果希望重新绘制视图,因为改变了一些视图,可以通过调用:
setNeedsDisplay()setNeedsDisplay(_ rect: CGRect)

如何绘制一个图形?

  1. get a context to draw into
  2. create paths
  3. set drawing attributes like colors, fonts, textures, linewidths, linecaps
  4. stroke or fill the above-created paths with the given attributes
    示例如下:
// 创建一个UIBezierPath
let path = UIBeizerPath()

// 移动点的位置, 添加一些线条
path.move(to: CGPoint(80, 50))
path.addLine(to: CGPoint(140, 150))
path.addLine(to: CGPoint(10, 150))

// 关闭路径
path.close()

// 有了路径,可以设置一些属性,stroke/fill
UIColor.green.setFill() //setFill 是UIColor的一个方法,不是UIBezierPath的
UIColor.red.setStroke() // setStroke是UIColor的一个方法,不是UIBezierPath的
path.linewidth = 3.0
path.fill()
path.stroke()

More about UIBezierPath

// 你也可以用UIBezierPath画一些比较常见的形状
let roundedRect = UIBezierpath(roundRect:CGRect, cornerRadius: CGFloat)
let oval = UIBezierPath(ovalIn: CGRect)

// 可以使用UIBezierPath进行剪切
addClip()

For example, you could clip to a rounded rect to enforce the edges of a playing card
所有未来你画的东西都会在这个剪切的图形之内,如果你画在了图形的外面,就不会显示出来,因为被剪切掉了。

5. Draw text, 绘制文本

To draw in draw(CGRect), use NSAttributedString

let text = NSAttributedString(string: "Hello")
text.draw(at: aCGPoint)
let textSize: CGSize = text.size

Preferred font, 首选字体

static func preferredFont(forTextStyle: UIFontTextStyle) -> UIFont

some of the styles(see UIFontDescriptor documentation for more)...

UIFontTextStyle.headline
UIFontTextStyle.body            
UIFontTextStyle.footnote

根据用户在设置里的选择改变字体的大小

let metrics = UIFontMetrics(forTextStyle: .body)
let fontToUse = metrics.scaledFont(for: .font)

6. Drawing Images, 绘制图片

let imge: UIImage = UIImage(named: string)
image.draw(at point: aCGPoint) // the upper left corner put at aCGPoint
image.draw(in rect: aCGRect) // scales the image to fit aCGRect
image.drawAsPattern(in rect: aCGRect) // tiles the image into aCGRect

8.Int.arc4Random

根据一个上限生成一个随机数
cards.count.arc4Random


extension Int {
    var arc4Random: Int {
        if self > 0 {
            return Int(arc4random_uniform(UInt32(self)))
        } else if self < 0 {
            return -Int(arc4random_uniform(UInt32(abs(self))))
        } else {
            return 0
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读