/
InterfaceController.swift
87 lines (69 loc) · 2.33 KB
/
InterfaceController.swift
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// InterfaceController.swift
// watchSample WatchKit Extension
//
// Created by Mauricio Giraldo on 2/5/16.
//
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController {
@IBOutlet var animationImage: WKInterfaceImage!
var frames = [UIImage]()
var session: WCSession? {
didSet {
if let session = session {
session.delegate = self
session.activateSession()
}
}
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
override func didAppear() {
super.didAppear()
print("hello!")
if WCSession.isSupported() {
print("requesting")
requestAnimations()
}
}
func requestAnimations() {
session = WCSession.defaultSession()
session!.sendMessage(["hello":"world"], replyHandler: { (response) -> Void in
print("received info!")
print(response)
if let frameData = response["frames"] as? Array<NSData> {
print(frameData)
for (_, frame) in frameData.enumerate() {
let image:UIImage = UIImage(data: frame)!
self.frames.append(image)
}
print("animations: \(self.frames)")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.showAnimation()
})
}
}, errorHandler: { (error) -> Void in
print("Error \(error)")
})
}
func showAnimation() {
// duration is 6 seconds for 6 frames so each frame should be visible for 1 second
let animated = UIImage.animatedImageWithImages(self.frames, duration: 6)
self.animationImage.setImage(animated)
self.animationImage.startAnimating()
}
}
extension InterfaceController: WCSessionDelegate {
}