-
Notifications
You must be signed in to change notification settings - Fork 2
Drawing
Попов Эльдар edited this page Apr 22, 2021
·
6 revisions
x = Horizontal = width
y = Vertical = height
- Now notice this rect, see the draw rect. What is that draw rect argument, both up on draw and down in set needs display? That is purely an optimization rectangle.That if, you had another view on top of view and it went away and it expose a little rectangle of view, the system would call your draw with just that rectangle. But you are allowed to redraw your whole view if you want. But if you can be efficient about only drawing the rectangle that was exposed, you can be efficient. So it is purely an optimization. You can ignore it if you want just draw your whole view. So if you have a simple view that is easy to draw, then you can just ignore that rect. How do i implement this draw rect? I overwrite it, now i want to draw. We are gonna do it using core graphics (this underlie drawing level layer). There is also an object-orientated way to draw with a class called UIBezierPath. It's just putting a little bit of an object oriented way you can build an object that contains some drawings. Which is nice if you wanna repeatedly draw that object. The fundamental core concept of drawing in core graphics, how we do it? The number one thing about core graphics is that it's context based, so you have to get a context. UIBezierPath will automatically get the context for you, but if you don't use UIBezierPath you have to get a context. Now, in drawRect, you can use this Swift global function UIGraphicsGetCurrentContext and it will give you a context to draw. But there could be other context, printing or drawing on and off screen buffer. We will not talk about it. But when you are in drawRect, it is easy. You just call this one global function, it will give you the context you are currently drawing it. Okay, one you have the context, now you use the context to create what are called paths. Paths are just arcs and line to's and it move to's, which is like jump over a little bit. It is just a combination of all those, that is what a path is. You gonna built some path. Then you are gonna set a bunch of drawing attributes (line widths, textures, color, fonts etc). And the you do 2 things with your path, you stroke(обводка) if or you fill it. So stroke(обводка) it means draw a line along my path, you know, with a certain line width and color, and fill means fill in the area that my line encloses. This is the only way to draw basically in core graphics and you might like, wow, that seems really limiting. All i can do is arcs and lines and fill them in and stroke(обводка) them, but it is amzing what you can do when you build on top of that primitive powerful mechanisms.
UIBezierPath does the same thing, it just does a lot of it under the covers but it has methods for settings line with, and things like that.
Close my path - method of UIBezierPath, which draws a line back to where you started, whatever that was.
Now kind of been tricking you because you'd look at this and it looks like as i called this it draws on the screen, but no. I just wanted to kind of give you visualization what is going on. In fact, when i did all that, nothing happend. All i was doing was building his UIBezierPath object up. If i wanna appear on screen i have to set my drawing attributes and tell it to stroke(обводка) or fill.
Like this: