๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
iOS ๐ŸŽ/iOS

Animation 2 - Core Animation

by yongmin.Lee 2022. 3. 21.

 

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
  • infrastructure => ๊ฐœ๋ฐœ์ž๋Š” ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ์ง€์ ๊ณผ ๊ฐ™์€ ๋ช‡๊ฐ€์ง€ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  Core Animation์ด ์‹œ์ž‘ํ•˜๋„๋ก ์ง€์‹œํ•˜๋ฉด,  Core Animation์€ ์ง์ ‘ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์กฐ์ž‘ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ์‹ค์ œ ๋“œ๋กœ์ž‰ ์ž‘์—…์„ ์˜จ๋ณด๋“œ ๊ทธ๋ž˜ํ”ฝ ํ•˜๋“œ์›จ์–ด๋กœ ์ „๋‹ฌํ•จ์œผ๋กœ์จ CPU์— ๋ถ€๋‹ด์„ ์ฃผ์ง€์•Š๊ณ  ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋งŒ๋“ ๋‹ค
  • Core Animation ์ธํ”„๋ผ์˜ ํ•ต์‹ฌ์—๋Š” Layer๊ฐ์ฒด(CALayer)๊ฐ€ ์žˆ๋‹ค

 

CALayer ?

  • An object that manages image-based content and allows you to perform animations on that content.
  • Layer๋Š” ์ž์ฒด ๋ชจ์–‘(own appearance)์„ ์ •์˜ํ•˜์ง€ ์•Š๊ณ . ๋น„ํŠธ๋งต์„ ๋‘˜๋Ÿฌ์‹ผ ์ƒํƒœ์ •๋ณด๋งŒ ๊ด€๋ฆฌ => onscreen์— ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” visual conten์™€ content์˜ ์†์„ฑ(such as a background color, border, and shadow), content์˜ geometry (such as its position, size, and transform)๋“ฑ์„ ๊ด€๋ฆฌ 
  • ๋Œ€๋ถ€๋ถ„์˜ Layer๊ฐ์ฒด๋Š” ์•ฑ์—์„œ ์‹ค์ œ drawing์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹ , Layer๋Š” ์•ฑ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ปจํ…์ธ ๋ฅผ ์บก์ณํ•˜์—ฌ backing store ๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋น„ํŠธ๋งต์œผ๋กœ ์บ์‹œํ•ด๋†“์Œ์œผ๋กœ์จ ๊ทธ๋ž˜ํ”ฝ ํ•˜๋“œ์›จ์–ด๋กœ ์‰ฝ๊ฒŒ ์กฐ์ž‘ ํ•  ์ˆ˜ ์žˆ๋„๋กํ•œ๋‹ค.
  • ๋น„ํŠธ๋งต ์ž์ฒด๋Š” view drawing ์ž์ฒด ๋˜๋Š” fixed image์ผ ์ˆ˜ ์žˆ๊ธฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Layer๋Š” ๋ชจ๋ธ ๊ฐ์ฒด๋กœ ๊ฐ„์ฃผ
  • ์ดํ›„์— Layer์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด, Layer๊ฐ์ฒด์™€ ๊ด€๋ จ๋œ ์ƒํƒœ ์ •๋ณด(state information)๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ณ , ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ํŠธ๋ฆฌ๊ฑฐํ•˜๋ฉด Core Animation์€ Layer์˜ ๋น„ํŠธ๋งต ๋ฐ ์ƒํƒœ์ •๋ณด๋ฅผ ๊ทธ๋ž˜ํ”ฝ ํ•˜๋“œ์›จ์–ด๋กœ ์ „๋‹ฌํ•˜์—ฌ ํ•˜๋“œ์›จ์–ด๊ฐ€ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ˆ˜ํ–‰

 

CABasicAnimation

  • An object that provides basic, single-keyframe animation capabilities for a layer property
  • ์ฆ‰ ๋ ˆ์ด์–ด ์†์„ฑ์— ๋Œ€ํ•ด ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด
  • CABasicAnimation ์ธ์Šคํ„ด์Šค๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋ฐฉ์‹์„ ํŠน์ •ํ•˜๋Š” key path๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ƒ์„ฑ

์˜ˆ์‹œ ์ฝ”๋“œ

import UIKit

class ViewController: UIViewController {
    private struct KeyPath {
        struct Position {
            static let ps = "position"
            static let x = "position.x"
        }
    }
    
    private var square: CALayer = CALayer()
    let image = UIImage(named: "green.jpg")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureSquare()
        configureButton()
    }
    
    func configureSquare() {
        square.frame = CGRect(x: 50, y: 100, width: 50, height: 50)
        square.contents = image?.cgImage
        view.layer.addSublayer(square)
    }
    
    func configureButton() {
        let animationButton = UIButton(frame: CGRect(x: 300, y: 100, width: 50, height: 50))
        animationButton.addTarget(self, action: #selector(moveAction), for: .touchUpInside)
        
        animationButton.setTitle("Move", for: .normal)
        animationButton.setTitleColor(.green, for: .normal)
        view.addSubview(animationButton)
    }
    
    @objc func moveAction() {
        let animation = CABasicAnimation()
        animation.keyPath = KeyPath.Position.x
        animation.fromValue = 50
        animation.toValue = 150
        animation.duration = 3
        animation.fillMode = CAMediaTimingFillMode.forwards
        animation.isRemovedOnCompletion = false
        
        self.square.add(animation, forKey: "basic")
        self.square.position = CGPoint(x: 150, y: 100)
    }
}

์ถœ์ฒ˜ : https://green1229.tistory.com/76

 

 

 

 

์ฐธ๊ณ ์ž๋ฃŒ

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/CoreAnimationBasics/CoreAnimationBasics.html

https://developer.apple.com/documentation/quartzcore

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

https://zeddios.tistory.com/747

https://sihyungyou.github.io/iOS-CoreAnimation/

https://woozzang.tistory.com/141?category=909641

https://green1229.tistory.com/76

https://jeonyeohun.tistory.com/363?category=881841

https://soooprmx.com/ios%EC%97%90%EC%84%9C-%EC%82%AC%EC%9A%A9%ED%95%A0-%EC%88%98-%EC%9E%88%EB%8A%94-%EC%95%A0%EB%8B%88%EB%A9%94%EC%9D%B4%EC%85%98-%EA%B5%AC%ED%98%84-%EA%B8%B0%EB%B2%95%EB%93%A4%EC%97%90-%EB%8C%80%ED%95%9C/

 

 

 

 

'iOS ๐ŸŽ > iOS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

MVVM-C  (0) 2022.03.28
Xcode 13, No StoryBoard Settings without SceneDelegate  (0) 2022.03.22
CALayer vs UIView  (0) 2022.03.18
Animation 1 - UIView Animation  (0) 2022.03.17
Core Graphics๋กœ ๋‘ ์ด๋ฏธ์ง€ ํ•ฉ์„ฑํ•˜๊ธฐ  (0) 2022.03.17