Skip to content

Instantly share code, notes, and snippets.

@keicoder
Created March 12, 2014 10:34
Show Gist options
  • Save keicoder/9504432 to your computer and use it in GitHub Desktop.
Save keicoder/9504432 to your computer and use it in GitHub Desktop.
objective-c : graphics and animations on iOS
//graphics and animations on iOS
//drawRect vs. setNeedsDisplay
//When a view is refreshed, the drawRect function for that view is called. This function draws content to the view every time it is called. Because drawRect is called often, it should be a very lightweight. Don’t allocate memory in drawRect, and never call drawRect directly from your code. (We will discuss overriding drawRect further in Chapter 8, Creating Custom UIViews and UIViewControllers.)
//So, if you can’t call drawRect from your code, how do you refresh a view? The answer is to call the function setNeedsDisplay. Because resources are scarce on mobile devices, iOS attempts to optimize resource intensive processes whenever possible. Drawing content to the screen can require a lot of resources. Instead of manually calling drawRect to refresh a view, set the view as setNeedsDisplay. When a view has the setNeedsDisplay flag set, iOS automatically refreshes the view when it is most efficient. The time delay between drawRect and setNeedsDisplay is unnoticeable, on the order of milliseconds. But, by allowing iOS to call drawRect on its own schedule, iOS can optimize multiple drawRect calls and determine the most efficient way to execute the command.
//Note: In order to optimize the performance of an iOS app, iOS renders only views visible on the screen. This means that if a view is off screen or covered by another view, iOS does not refresh the content in that portion of the view.
//Offset
//offset takes a CGSize structure that defines the (width, height) offset. The default offset is (0,-1) which means the shadow has a horizontal offset of zero and a vertical offset of -1. So, (0,-1) defines a one-pixel top shadow. Similarly, (0,1) would define a one-pixel bottom shadow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment