Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- cs
- 웹
- SwiftUI
- swift concurrency
- 멋사
- c++
- 문법
- widgetkit
- 영남대
- Swift
- 네트워크
- TCA
- 멋쟁이사자처럼
- 스위프트
- 후기
- 스유
- 위젯킷
- 알고리즘
- widget
- composable architecture
- 리액트
- 백준
- 운영체제
- 대외활동
- spritekit
- uikit
- ReactorKit
- Protocol
- dispatchqueue
- 컴퓨터그래픽스
Archives
- Today
- Total
맛동산이
SpriteKit) 코어모션을 이용한 중력 변화주기 [SwiftUI] 본문
코어모션은 장치의 가속도계와 자이로스코프, 사용 가능한 경우 보수계, 자력계 및 기압계가 포함한 데이터를 가지고 있는 프레임워크이다.
해당 프레임워크를 가지고 중력센서를 통해서 SpriteKit의 화면에 중력을 작용시켜보자!
PhysicsWorld
모든 scene은 자동적으로 물리현상을 구현하기 위해서 physicsWorld를 생성하며, physicsWorld는 SKScene위에 잇는 모든 노드들의 physics 프로퍼티에 접근할수 있다.
따라서 우리는 SpriteKit의 scene의 physicsWorld의 gravity를 줄 예정이다.
중력을 바꾸기 위해서는 중력을 바꾸는 값을 가져와야 하는데 해당 값을 coreMotion으로 기기의 기울기를 통해서 가져올수 있다.
CoreMotion을 통해서 기기의 변화를 인지하기
coreMotion을 사용하는 방법은 다음과 같다.
- CMCoreMotionManager 생성하기
- Manager세팅하기
CMCoreMotionManager 생성하기
let coreManager = CMCoreMotionManager()
Manager세팅하기
func setCoreMotionManager() { //변화를 인지하는 시간을 설정해줌 coreMotionManager.accelerometerUpdateInterval = 0.2 coreMotionManager.deviceMotionUpdateInterval = 0.2 coreMotionManager.gyroUpdateInterval = 0.2 //센서로 데이터를 받아오는 것을 시작함. coreMotionManager.startGyroUpdates() coreMotionManager.startAccelerometerUpdates() }
SpriteKit에 중력변화시키기
override func update(_ currentTime: TimeInterval) { if let accelerometerDate = motionManager?.coreMotionManager.accelerometerData { physicsWorld.gravity = CGVector(dx: accelerometerDate.acceleration.x * 9.8, dy: accelerometerDate.acceleration.y * 9.8) } }
- Vector에 9.8을 곱하는것은, 중력가속도를 주는 것이다.
반응형
'앱 > SwiftUI' 카테고리의 다른 글
SwiftUI) 2023 WWDC, 위젯에 버튼액션을 넣는방법(Widget with interactive button) (0) | 2024.02.25 |
---|---|
SpriteKit) PhysicsBody 물체에 따라서 프레임주기 [SwiftUI] (0) | 2023.08.28 |
SwiftUI) Widget 위젯 만들기 - 프로젝트 생성(1) (2) | 2023.08.28 |
SwiftUI) WidgetKit을 사용해서 Widget을 만들기(2) - configuration (0) | 2023.08.28 |
SwiftUI) WidgetKit을 사용해서 Widget을 만들기(3) - Provider (0) | 2023.08.28 |