UIView Controller v init initcoder TNSCoder IB Storyboard
UIView. Controller v 초기화 관련 메소드 ü init : 코드로 직접 생성할 때 호출 ü init(coder: <#T##NSCoder#>)=> IB에서 배치한 경우 자동으로 호출 ü Storyboard. instantiate. View. Controller(with. Identifier identifier: String) -> UIView. Controller : storyboard에서 디자인 한 View. Controller를 생성하고자 하는 경우 사용하는 메소드 ü UITable. View. Controller는 style을 설정할 수 있는 초기화 메소드 추가 ü UINavigation. Controller는 root. View. Controller: 메소드가 추가 ü UIPage. View. Controller는 UIPage. View. Controller. init(transition. Style: <#T##UIPage. View. Controller. Transition. Style#>, navigation. Orientation: <#T##UIPage. View. Controller. Navigation. Orientation#>, options: <#T##[String : Any]? #>)메소드가 존재 3
UIView. Controller v 멤버 ü UIView view=> 화면에 출력되는 뷰(Table. View. Controller에는 UITable. View 타입의 table. View 소유) ü UINavigation. Controller navigation. Controller ü UINavigation. Item navigation. Item ü UITab. Bar. Controller tab. Bar. Controller ü UITab. Bar. Item tab. Bar. Item ü String title - Navigation. Controller에 포함되어 있으면 title. View에 출력되며 Tab. Bar. Controller에 포함되 어 있다면 Tab. Bar. Item의 제목으로 출력 ü content. Size. For. View. In. Popover => 팝 오버 뷰로 출력할 때의 사이즈(i. Pad에서만 사용 가능) ü modal. In. Popover => 팝 오버 뷰로 출력(i. Pad 에서만 사용 가능) ü hides. Bottom. Bar. When. Pushed => 푸시되었을 때의 탭 바 출력 여부 ü presenting. View. Controller => 자신을 출력한 뷰 컨트롤러 4
Touch Event v UIResponder 클래스의 First. Responder 관련 속성과 메소드 ü var next: UIResponder? ü var is. First. Responder: Bool ü var can. Become. First. Responder: Bool ü func become. First. Responder() -> Bool ü var can. Resign. First. Responder: Bool ü func resign. First. Responder() -> Bool 8
Touch Event 10
Touch Event v UITouch: 터치에 관련된 정보를 저장하기 위한 클래스 ü Time. Interval timestamp: 터치가 발생한 시간(readonly) ü UITouch. Phase phase: 터치의 상태를 저장하는 프로퍼티(readonly) UITouch. began, UITouch. moved, UITouch. stationary, UITouch. ended, UITouch. cancelled ü Int tap. Count: 탭 한 횟수 ü UIView view: 터치가 발생한 뷰 ü UIWindow window: 터치가 발생한 윈도우 ü location(in: UIView) -> CGPoint – view좌표계에서 터치한 위치 v UIEvent: 화면에 발생한 이벤트를 가지고 있는 클래스 ü Time. Interval timestamp: 이벤트가 발생한 시간을 저장(readonly) ü func all. Touches()-> (Set<UITouch>) : 모든 UITouch 객체 ü func touches(for: UIWindow)-> (Set<UITouch>) : window에서 발생한 모든 터치 객체 ü func touches(for: UIView)-> (Set<UITouch>) : view에서 발생한 모든 터치 객체 11
1. Single View Application 프로젝트 작성(Touch. Test) 2. 이미지를 1개 추가 3. Main. storyboard의 뷰에 2개의 레이블(label 1, label 2)과 이미지 뷰(img. View)를 추가하고 IBOutlet 변수 생성 img. View의 초기 이미지를 적당히 설정하고 user. Interaction. Enabled 속성을 YES로 변경 13
4. View. Controller. swift 파일에 터치 메소드 오버라이딩 override func touches. Began(_ touches: Set<UITouch>, with event: UIEvent}(? let touch = touches. first let tap. Count = touch!. tap. Count label 1. text = "터치 시작; " label 2. text = "(tap. Count) 번 탭" { override func touches. Moved(_ touches: Set<UITouch>, with event: UIEvent} (? label 1. text = "터치 이동; " let touch = touches. first if touch!. view == img. View} img. View. center = touch!. location(in: self. view( { { override func touches. Ended(_ touches: Set<UITouch>, with event: UIEvent} (? label 1. text = "터치 종료" { 14
뷰 컨트롤러의 방향 전환 v 앱의 방향을 설정하는 메소드 – App. Delegate 클래스에 오버라이딩 func application(_ application: UIApplication, supported. Interface. Orientations. For window: UIWindow? )-> UIInterface. Orientation. Mask v 뷰 컨트롤러의 회전 지원 여부를 결정하는 프로퍼티 var should. Autorotate: Bool { get } v 뷰 컨트롤러의 회전 지원 방향을 결정하는 프로퍼티 : 특정 방향만 지원하고자 하는 경우 오버라이딩 var supported. Interface. Orientations: UIInterface. Orientation. Mask v View. Controller에서 제공되는 회전 관련 메소드 ü 회전이 일어나고 처리되기 전에 호출되는 메소드 func view. Will. Transition(to size: CGSize, with coordinator: UIView. Controller. Transition. Coordinator) v 뷰 컨트롤러 없이 회전을 처리하고자 하면 UIDevice. orientation. Did. Change. Notification 과 UIDevice 클래스의 인스턴스의 orientation 속성을 이용 15
1. View. Controller. swift 파일에 회전 관련 프로퍼티와 메소드 오버라이딩 //뷰 컨트롤러가 돌아가는 것을 결정하는 Bool 타입의 뷰 컨트롤러 인스턴스 프로퍼티 override var should. Autorotate: Bool { return true // or false } //특정 방향 지원여부를 설정하는 메소드 override var supported. Interface. Orientations: UIInterface. Orientation. Mask { return [. all] } //뷰의 크기가 변경될 때 호출되는 메소드 override func view. Will. Transition(to size: CGSize, with coordinator: UIView. Controller. Transition. Coordinator) { if UIDevice. current. orientation. is. Landscape { print("가로로 회전") } else { print("세로로 회전") } } 16
2. View. Controller. swift 파일에 회전 관련 노티피케이션을 처리해주는 메소드 작성 var did. Rotate: (Notification) -> Void = { notification in switch UIDevice. current. orientation { case. landscape. Left, . landscape. Right: print("Noti - landscape") case. portrait, . portrait. Upside. Down: print("Noti - Portrait") default: print("other") } } 3. View. Controller. swift 파일의 view. Did. Load 메소드에 회전 관련 노티피케이션을 등록 Notification. Center. default. add. Observer(for. Name: UIDevice. orientation. Did. Change. Notification, object: nil, queue: . main, using: did. Rotate) 노티피케이션을 삭제하고자 하는 경우 아래 코드를 추가 Notification. Center. default. remove. Observer(self, name: UIDevice. orientation. Did. Change. Notification, object: nil) 17
Motion v 중력 센서를 이용해서 흔들기를 감지해서 처리 가능 v 사용되는 메소드 ü func motion. Began(_ motion: UIEvent. Subtype, with event: UIEvent? ) ü func motion. Ended(_ motion: UIEvent. Subtype, with event: UIEvent? ) ü func motion. Cancelled(_ motion: UIEvent. Subtype, with event: UIEvent? ) v 흔들기 이벤트나 메뉴 컨트롤러를 사용하고자 하는 경우는 View. Controller가 First. Responder가 되어야 함 v 뷰 컨트롤러가 First. Responder가 되기 위해서는 can. Become. First. Responder 프로퍼티를 오버라이딩해서 true를 리턴해주어야 하고 이벤트를 사용하기 전에 become. First. Responder를 호출 18
1. View. Controller. swift 파일에 View. Controller의 First. Responder 설정 여부를 리턴 하는 속성 오버라이딩 override var can. Become. First. Responder: Bool{ get{ return true } } 2. View. Controller. swift 파일의 view. Did. Load 메소드에 추가 self. become. First. Responder() 3. View. Controller. swift 파일에 흔들기를 시작하면 호출되는 메소드 오버라이딩 override func motion. Began(_ motion: UIEvent. Subtype, with event: UIEvent? ){ let alert = UIAlert. Controller(title: "모션 테스트", message: "흔들기 시작", preferred. Style: . alert) let ok = UIAlert. Action(title: "확인", style: . default) alert. add. Action(ok) self. present(alert, animated: true) } override func motion. Ended(_ motion: UIEvent. Subtype, with event: UIEvent? ) { if motion ==. motion. Shake { print("Why are you shaking me? ") } } 20
메뉴 v UIMenu. Controller 클래스는 메뉴를 나타내는 클래스로 UIMenu. Controller의 shared라는 프로퍼티로 객체 소유 v UIMenu. Controller의 멤버 ü BOOL is. Menu. Visible => 메뉴의 출력 여부 ü set. Menu. Visible(<#T##menu. Visible: Bool##Bool#>, animated: <#T##Bool#>) => 메뉴 표시 ü set. Target. Rect(<#T##target. Rect: CGRect##CGRect#>, in: <#T##UIView#>)=> 메뉴 위치와 크기 설 정 ü menu. Frame: CGFrame => 메뉴의 위치와 크기 ü menu. Items: [UIMenu. Item] => 하위메뉴 항목 설정 ü arrow. Direction: UIMenu. Controller. Arrow. Direction => 메뉴의 꼭지점 방향 v UIMenu. Item클래스는 메뉴의 세부 항목을 나타내는 클래스로 이벤트를 연결해서 사용 v UIMenu. Item의 멤버 ü init(title: <#T##String#>, action: <#T##Selector#>) ü title은 메뉴에 보여지는 텍스트 ü action은 메뉴를 터치 했을 때 호출되는 메소드 v 메뉴는 motion과 마찬가지로 View. Controller가 First. Responder가 되어야만 사용이 가능 21
1. View. Controller. swift 파일에 메뉴를 누르면 호출되는 메소드 구현 @ objc func menu. Event() } let alert = UIAlert. Controller(title: "메뉴 테스트 , "message: "메뉴 선택 , "preferred. Style: . alert( let ok = UIAlert. Action(title: "확인 , "style: . default( alert. add. Action(ok( self. present(alert, animated: true( { 2. View. Controller. swift 파일의 view. Did. Load 메소드에 추가 let long. Press = UILong. Press. Gesture. Recognizer. init(target: self, action: #selector(handle. Long. Press)) long. Press. minimum. Press. Duration = 2. 0; long. Press. allowable. Movement = 15; self. view. add. Gesture. Recognizer(long. Press) 22
3. View. Controller. swift 파일에 길게 누르면 호출되는 제스쳐 메소드 구현 @objc func handle. Long. Press(sender : UILong. Press. Gesture. Recognizer) { self. become. First. Responder() let menu. Item = UIMenu. Item(title: "Menu", action: #selector(menu. Event)) UIMenu. Controller. shared. menu. Items = [menu. Item] let pt = sender. location(in: self. view) if #available(i. OS 13. 0, *) { print("13 이상"); UIMenu. Controller. shared. show. Menu(from: self. view, rect: CGRect(x: pt. x 50, y: pt. y+15, width: 100, height: 100)) } else { print("13 미만"); UIMenu. Controller. shared. set. Target. Rect(CGRect(x: pt. x 50, y: pt. y+15, width: 100, height: 100), in: self. view) UIMenu. Controller. shared. set. Menu. Visible(true, animated: true) } } 23
UIApplication v UIApplication 클래스의 shared 프로퍼티는 애플리케이션의 공통된 요소를 접근하는 UIApplication 객체를 리 턴해주는 프로퍼티 v UIApplication객체가 delegate를 호출하면 자신의 App. Delegate Class 객체에 접근 v is. Network. Activity. Indicator. Visible는 상태 바에 있는 Activity. Indicator. View를 보여주는 옵션 : i. OS 13에서는 deprecated 되었는데 앱 안에서 직접 디자인 하는 것을 권장 v application. Icon. Badge. Number는 아이콘에 붙는 배지 숫자 v 로딩 시 뷰의 방향 설정은 status. Bar. Orientation에 방향을 설정해주면 되는데 동적으로 변경하지는 못함 v 동적으로 변경하고자 하는 경우에는 UIDevice. current. orientation에 방향을 설정 let orientation. Value = UIInterface. Orientation. Mask. landscape. Left. raw. Value UIDevice. current. set. Value(orientation. Value, for. Key: "orientation") v 이벤트 관련 메소드 ü begin. Ignoring. Interaction. Events ü end. Ignoring. Interaction. Events v sleep 모드: BOOL is. Idle. Timer. Disabled 24
클립보드 v 클립보드 생성: UIPasteboard. general v 클립보드의 데이터 ü 클립보드객체. string = 문자열(UIImage, NSURL, UIColor 도 가능) ü 클립보드객체. set. Items(<#T##items: [[String : Any]]##[[String : Any]]#>, options: <#T##[UIPasteboard. Option : Any]#>) 25
- Slides: 25