광어네 맛집

[ Layer 및 초기화 ] View 그리기 / Graphics Hardware, UIKit, draw(_ rect: CGRect) _ 02 본문

Swift/About Swift

[ Layer 및 초기화 ] View 그리기 / Graphics Hardware, UIKit, draw(_ rect: CGRect) _ 02

Lautner Jacob 2022. 8. 9. 17:31
728x90
반응형

앞에서 쓴 글과 이어서 작성하고자 합니다

이번 글은 조금이나마 깊게 들어가고자 합니다!

 


 

UIView 내부에 업데이트 메서드는

SetNeedsDisplay, SetNeedLayout, displayIfNeeded, layoutIfNeed  있습니다

 

살펴보기 전에 UIKit이 어떻게 나온지에 대해서 살펴봐야 할 것 같습니다!

제일 상단에 보이는 UIKit 은 아래 low Level 인 그래픽 API 들을 거쳐 High Level로 만들어진 그래픽 API 입니다!

 

타 블로그 분들께서는 Metal로 나온 부분은 OpenGL이라고 칭하셨습니다!

무튼 앱이 부드럽게 그려지기위해서는 60FPS 를 유지해야 합니다

이를 위해 Graphics Hardware를 사용하는데 Low Level로 많은 코드, 가장 빠른 OpenGL이 있습니다

초록색 부분들은 보다 적은 코드양으로 효율적으로 그래픽에 접근하는 API들이 점차 나온 것입니다!

 

순서는 OpenGL -> Core Graphics -> Core Animation -> UIKit

으로 속도는 빠르고 조금 제한적인 기능을 가진 UIKit이 만들어진 것 입니다.

Core Graphics -> 종류는 CGRect, CGColor 등이 여기에 포함됩니다

( 마치 Objective C -> Swift, MRC -> ARC 같네요 ㅎㅎ)

 

UIkit은 빠르지만 보다 제한된 기능을 가지고 있어 Core Animation을 통해서 그리게 됩니다

여기서 흔히 쓰는 layer가 CoreAnimation 프로퍼티인 CALayer를 뜻합니다

종류는 여기서 확인하시면 됩니다~

 

이제 앞에서 나온 CALayer Resizing & Layout, Update메서드를 살펴볼 수 있게 되었습니다

앞의 글에서 draw(_ rect:) 메서드를 대신해서 쓸 때 다음과 같은 메서드를 이용하면 됩니다

 

우선 일반적으로 layout은 Size, point를 말하고

Display는 color, text, image를 말한다고 생각하면 이해하기 쉽습니다

 

setNeedsDisplay() / displayIfNeed()

공통점은 draw메서드를 직접 호출하지 않고 업데이트를 합니다

차이점은 비동기적 / 동기적(즉시) 업데이트를 합니다

 

setNeedsLayout() / layoutIfNeed()

마찬가지로 LayoutSubview를 직접 호출하지 않고 업데이트를 합니다

차이점은 비동기적 / 동기적(즉시) 업데이트를 합니다

 


 

해당 글과 조금 관련없을지도 모르겠지만 좋은 팁이 있어서 같이 적어놓고자 합니다

CALayer로 애니메이션 효과를 많이 넣으면 성능 저하 문제가 있다고 합니다

따라서 다음과 같은 프로퍼티를 설정해주면 성능이 향상된다고 합니다!

// 1. 렌더링 여부 ( false(default) = 매번 새롭게 그림 / true = 재사용 )
view.layer.shouldRasterize = false

// 2. 수행 위치 변경 ( false(default) = Main 쓰레드 / true = Background 쓰레드 )
view.layer.drawsAsynchronously = false

 


 

 

이것 저것 찾아보면서 방대하지만

그래픽과 Layer에 대해서 조금 더 알게 되어서 좋은 시간이 되었습니다!

( 피드백은 항상 감사합니다! )

 

 

 

 

출처: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004514

 

About Core Animation

About Core Animation Core Animation is a graphics rendering and animation infrastructure available on both iOS and OS X that you use to animate the views and other visual elements of your app. With Core Animation, most of the work required to draw each fr

developer.apple.com

https://tech.gc.com/demystifying-ios-layout/

 

Demystifying iOS Layout

Some of the most difficult issues to avoid or debug when you first start building iOS applications are those dealing with view layout and content. Often, these issues happen because of misconceptions about when view updates actually occur. Understanding ho

tech.gc.com

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/AnimatableProperties/AnimatableProperties.html#//apple_ref/doc/uid/TP40004514-CH11-SW1

 

Animatable Properties

Animatable Properties Many of the properties in CALayer and CIFilter can be animated. This appendix lists those properties, along with the animation used by default. CALayer Animatable PropertiesTable B-1 lists the properties of the CALayer class that you

developer.apple.com

https://babbab2.tistory.com/53

 

iOS) CALayer 제대로 이해하기

안녕하세요? 소들입니다 :) 오늘은 CALayer에 대해 공부해보려고 해요! UIView는 다음과 같이 CALayer 타입의 layer 라는 프로퍼티를 갖고 있거든여 이 layer는 우리가 직접적이건 간접적이건 꽤나? 건드

babbab2.tistory.com

https://developer.apple.com/documentation/quartzcore/calayer/1410855-setneedsdisplay

 

Apple Developer Documentation

 

developer.apple.com

 

728x90
반응형