WatchKit InterfaceController 与 ViewController
WatchKit InterfaceController vs ViewController
在功能方面awakeWithContext, willActivate, didDeactivate
与viewDidLoad, viewWillAppear, viewDidAppear
相同吗?
我正在移植 Swift Apple Watch 教程中的代码,该教程是在人们不得不添加自己的手表 AppViewController 文件以测试其手表应用程序时创建的。
随着 Xcode 的正式手表发布,包含的文件和内容发生了变化,所以我想知道放在哪里。
例如,旧的 AppViewController 文件中有一些代码,所以我只是 copy/pasted 将其添加到新的 InterfaceController 中。我将 viewDidLoad, viewWillAppear, viewDidAppear
中的代码分别放入 awakeWithContext, willActivate, didDeactivate
中。
看来方法不一样。我收到 1 个错误,提示 setText 不存在:
bpmLabel.setText(currentBeatPattern.bpm) = "\(currentBeatPattern.bpm)"
…还有 2 个错误提示视图不存在:
iconLabel.frame = self.view.bounds
self.view.insertSubview(iconLabel, atIndex: 1)
好像 WatchKit 没有使用一些正常的 属性 方法之类的。
错误信息:
http://i.imgur.com/wXMdt3c.png
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
self.view.insertSubview(iconLabel, atIndex: 1) // Xcode error
}
override func willActivate() {
super.willActivate()
iconLabel.frame = self.view.bounds // Xcode error
iconLabel.textAlignment = .Center
iconLabel.font = UIFont.boldSystemFontOfSize(132)
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
newBeat()
NSTimer.scheduledTimerWithTimeInterval(8,
target: self,
selector: Selector("newBeat"),
userInfo: nil,
repeats: true)
beat()
}
func newBeat() {
// 1
if ++currentBeatPatternIndex == beatPatterns.count {
currentBeatPatternIndex = 0
}
// 2
currentBeatPattern = beatPatterns[currentBeatPatternIndex]
// 3
bpmLabel.setText(currentBeatPattern.bpm) = "\(currentBeatPattern.bpm)" // Xcode error
iconLabel.text = currentBeatPattern.icon
}
func beat() {
// 1
UIView.animateWithDuration(currentBeatPattern.duration / 2,
delay: 0.0,
options: .CurveEaseInOut,
animations: {
// 2
self.iconLabel.transform = CGAffineTransformScale(
self.iconLabel.transform, self.shrinkFactor, self.shrinkFactor)
},
completion: { _ in
// 3
UIView.animateWithDuration(self.currentBeatPattern.duration / 2,
delay: 0.0,
options: .CurveEaseInOut,
animations: {
// 4
self.iconLabel.transform = CGAffineTransformScale(
self.iconLabel.transform, self.expandFactor, self.expandFactor)
},
completion: { _ in
// 5
self.beat()
}
)
}
)
}
}
你说得对,awakeWithContext、willActivate 和 didDeactivate 与现有的 UIViewController 方法(如 viewDidLoad、viewWillAppear 和 viewDidUnload)非常相似。但是,您看到的错误与 WatchKit 当前的工作方式有关。为了让手表应用程序 运行,所有代码都在 iPhone 上执行,但 Apple Watch 本身承担了 UI 元素的责任。这意味着构成手表应用的任何视图都必须包含在手表应用的情节提要中。此外,作为直接结果,视图无法实例化并添加到父视图。所有 UI 元素都必须包含在手表应用程序的情节提要中,手表将根据它们在界面生成器中的排列来布置它们。这意味着您不能调用 addSubview 并且不能设置元素的框架。您只能调整其大小并设置其隐藏 属性 以在手表上隐藏或显示它。就此方法而言 –
bpmLabel.setText(currentBeatPattern.bpm) = "\(currentBeatPattern.bpm)"
你调用的方法有误。在 swift 中,参数包含在括号中。如果是你的意思,你可以这样称呼它
bpmLabel.setText("\(currentBeatPattern.bpm)")
但是setText是一个接受字符串参数的方法,不能用=赋值
就动画而言,我认为你运气不好。与 iOS 应用程序相比,手表应用程序目前更像是小部件,并且 UI 查看动画和帧数学之类的东西对您不可用。你绝对应该阅读
在 WatchKit 上,因为你无法像这样将 iOS 应用程序直接移植到手表上。
在功能方面awakeWithContext, willActivate, didDeactivate
与viewDidLoad, viewWillAppear, viewDidAppear
相同吗?
我正在移植 Swift Apple Watch 教程中的代码,该教程是在人们不得不添加自己的手表 AppViewController 文件以测试其手表应用程序时创建的。
随着 Xcode 的正式手表发布,包含的文件和内容发生了变化,所以我想知道放在哪里。
例如,旧的 AppViewController 文件中有一些代码,所以我只是 copy/pasted 将其添加到新的 InterfaceController 中。我将 viewDidLoad, viewWillAppear, viewDidAppear
中的代码分别放入 awakeWithContext, willActivate, didDeactivate
中。
看来方法不一样。我收到 1 个错误,提示 setText 不存在:
bpmLabel.setText(currentBeatPattern.bpm) = "\(currentBeatPattern.bpm)"
…还有 2 个错误提示视图不存在:
iconLabel.frame = self.view.bounds
self.view.insertSubview(iconLabel, atIndex: 1)
好像 WatchKit 没有使用一些正常的 属性 方法之类的。
错误信息: http://i.imgur.com/wXMdt3c.png
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
self.view.insertSubview(iconLabel, atIndex: 1) // Xcode error
}
override func willActivate() {
super.willActivate()
iconLabel.frame = self.view.bounds // Xcode error
iconLabel.textAlignment = .Center
iconLabel.font = UIFont.boldSystemFontOfSize(132)
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
newBeat()
NSTimer.scheduledTimerWithTimeInterval(8,
target: self,
selector: Selector("newBeat"),
userInfo: nil,
repeats: true)
beat()
}
func newBeat() {
// 1
if ++currentBeatPatternIndex == beatPatterns.count {
currentBeatPatternIndex = 0
}
// 2
currentBeatPattern = beatPatterns[currentBeatPatternIndex]
// 3
bpmLabel.setText(currentBeatPattern.bpm) = "\(currentBeatPattern.bpm)" // Xcode error
iconLabel.text = currentBeatPattern.icon
}
func beat() {
// 1
UIView.animateWithDuration(currentBeatPattern.duration / 2,
delay: 0.0,
options: .CurveEaseInOut,
animations: {
// 2
self.iconLabel.transform = CGAffineTransformScale(
self.iconLabel.transform, self.shrinkFactor, self.shrinkFactor)
},
completion: { _ in
// 3
UIView.animateWithDuration(self.currentBeatPattern.duration / 2,
delay: 0.0,
options: .CurveEaseInOut,
animations: {
// 4
self.iconLabel.transform = CGAffineTransformScale(
self.iconLabel.transform, self.expandFactor, self.expandFactor)
},
completion: { _ in
// 5
self.beat()
}
)
}
)
}
}
你说得对,awakeWithContext、willActivate 和 didDeactivate 与现有的 UIViewController 方法(如 viewDidLoad、viewWillAppear 和 viewDidUnload)非常相似。但是,您看到的错误与 WatchKit 当前的工作方式有关。为了让手表应用程序 运行,所有代码都在 iPhone 上执行,但 Apple Watch 本身承担了 UI 元素的责任。这意味着构成手表应用的任何视图都必须包含在手表应用的情节提要中。此外,作为直接结果,视图无法实例化并添加到父视图。所有 UI 元素都必须包含在手表应用程序的情节提要中,手表将根据它们在界面生成器中的排列来布置它们。这意味着您不能调用 addSubview 并且不能设置元素的框架。您只能调整其大小并设置其隐藏 属性 以在手表上隐藏或显示它。就此方法而言 –
bpmLabel.setText(currentBeatPattern.bpm) = "\(currentBeatPattern.bpm)"
你调用的方法有误。在 swift 中,参数包含在括号中。如果是你的意思,你可以这样称呼它
bpmLabel.setText("\(currentBeatPattern.bpm)")
但是setText是一个接受字符串参数的方法,不能用=赋值
就动画而言,我认为你运气不好。与 iOS 应用程序相比,手表应用程序目前更像是小部件,并且 UI 查看动画和帧数学之类的东西对您不可用。你绝对应该阅读 在 WatchKit 上,因为你无法像这样将 iOS 应用程序直接移植到手表上。