我试图使用watchOS 2在Apple Watch上渲染线条/步骤图.与iOS 9不同,watchOS 2不支持Quartz.它只支持Core Graphics.我尝试编写一些代码来绘制折线图但我得到一个错误"CGContextRestoreGState:无效的上下文0x0.这是一个严重的错误.这个应用程序,或它使用的库,正在使用无效的上下文,从而有助于整体系统稳定性和可靠性降低.这个通知是礼貌的:请解决这个问题.在即将到来的更新中它将成为一个致命错误."
以下是我使用的代码:
import WatchKit
import Foundation
import UIKit
class InterfaceController: WKInterfaceController{
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
let path = UIBezierPath()
let startPoint = CGPointMake(0.0, 0.0)
path.moveToPoint(startPoint)
let nextPoint = CGPointMake(20.0, 20.0)
path.addLineToPoint(nextPoint)
path.lineWidth = 1.0
UIColor.whiteColor().setStroke()
path.stroke()
}
override func willActivate() {
super.willActivate()
}
override func didDeactivate() {
super.didDeactivate()
}
}
Run Code Online (Sandbox Code Playgroud)
我的最终结果应该像Apple Watch上的Stocks app一样.每当用户点击特定股票时,他将能够查看/可视化该股票的统计数据.任何人都可以帮助我实现这一目标.
我使用以下命令向iOS应用程序发送字典:
- (void)sendMessage:(NSDictionary<NSString *, id> *)message
replyHandler:(nullable void (^)(NSDictionary<NSString *, id> *replyMessage))replyHandler
errorHandler:(nullable void (^)(NSError *error))errorHandler;
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误.我想知道我们的父应用程序如何在watchOS2中处理此请求.
在watchOS1中,我曾经openParentApplication从父应用程序获取数据,并且Appdelegate有一个handleWatchKitExtensionRequest处理该请求的数据.我们如何在watchOS2中处理这个问题?
我的界面控制器:
- (void)awakeWithContext:(id)context
{
[super awakeWithContext:context];
if([WCSession isSupported])
{
self.watchSession = [WCSession defaultSession];
self.watchSession.delegate = self;
[self.watchSession activateSession];
}
[self sendRequestWithActionType:@"InitialView"];
}
-(void)sendRequestWithActionType:(NSString *)action
{
NSDictionary *requst = @{@"request":action};
[[WCSession defaultSession] sendMessage:requst
replyHandler:^(NSDictionary *replyHandler) {
[self setTextForLabelWithData:[replyHandler valueForKey:@"response"]];
}
errorHandler:^(NSError *error) {
NSLog(@"");
}
];
}
Run Code Online (Sandbox Code Playgroud) 从watchOS 2开始,我们有一个ExtensionDelegate对象,类似于UIApplicationDelegate(对应用程序生命周期事件做出反应).
我想获得对第一个Interface Controller对象的引用,该对象将在启动时显示,以在其上设置属性(例如,传入数据存储对象).
根据文档,手上的rootInterfaceController财产WKExtension支持初始控制器:
根接口控制器位于应用程序的主故事板中,并具有与之关联的主入口点对象.WatchKit在启动时显示根接口控制器,尽管应用程序可以在启动序列完成之前呈现不同的接口控制器.
所以我尝试以下内容ExtensionDelegate:
func applicationDidFinishLaunching() {
guard let initialController = WKExtension.sharedExtension().rootInterfaceController else {
return
}
initialController.dataStore = DataStore()
}
Run Code Online (Sandbox Code Playgroud)
即使显示正确的接口控制器,此时rootInterfaceController也是零.有趣的是,如果我在willActivate()我的Interface Controller中查询相同的属性,则属性设置正确.
在iOS应用程序中,您已经可以获得根视图控制器了applicationDidFinishLaunching(),我认为它应该对watchOS起作用.
有没有办法在界面控制器显示之前(从外部)设置属性?这是一个错误吗?
非常感谢你的回答!
我一直在阅读RayWenderlich团队的WatchOS 2 By Tutorial一书中的例子,特别是第18章.它们都运行良好.在我自己的应用程序中,我试图从手表发送按钮按下iPhone应用程序上的按钮.以下是手表和手机中Swift的相关代码:
看:
//
// InterfaceController.swift
// Wasted Time Extension
//
// Created by Michael Rowe on 7/21/15.
// Copyright © 2010-2015 Michael Rowe. All rights reserved.
//
import WatchKit
import WatchConnectivity
import Foundation
class InterfaceController: WKInterfaceController,WCSessionDelegate {
@IBOutlet var wasteLabel: WKInterfaceLabel!
@IBOutlet var costLabel: WKInterfaceLabel!
@IBOutlet var counter: WKInterfaceLabel!
@IBOutlet var statusButton: WKInterfaceButton!
// our watchconnective session
var session : WCSession?
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
override func willActivate() {
// This method …Run Code Online (Sandbox Code Playgroud) 如何在Apple Watch上绘制自定义图形?
如果我理解正确,我们只能在Apple Watch上使用图像和标准控件.如果是这样,是否可以在内存中的图像上绘制自定义图形,然后将这些图像放在屏幕上?
我正在研究苹果手表应用程序.在应用程序中,我有一个视图,用户可以在3个给定结果之间左右滑动.我使用WKInterfaceLabel来显示结果信息.在每次滑动时,标签都会使用新文本进行更新.
查看截图:
我想在滑动时为文本更改设置动画.我怎样才能做到这一点?
任何帮助将不胜感激.谢谢!
简短:我不知道如何从CMSensorRecorder中获取CMenscord的数据后从CMSensorDataList中提取CMRecordedAccelerometerData.Apple尚未提供任何文档.
也许有人暗示我?;)
func startMovementDetection(){
var accDataList = self.cmSensorRecorder!.accelerometerDataFrom(self.startDate, to: NSDate()) as CMSensorDataList
CMRecordedAccelerometerData() //that's the class i want to extract from CMSensorDataList
}
Run Code Online (Sandbox Code Playgroud)
好的,问题在这里解决了:Swift中的NSFastEnumeration
使用Swift 3.0,它将更改为:
extension CMSensorDataList: Sequence {
public func makeIterator() -> NSFastEnumerationIterator {
return NSFastEnumerationIterator(self)
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何设计一个依赖于从服务器获取最新源的watchOS应用程序.您是否需要使用应用程序上下文并让iPhone在后台使用WatchConnectivity框架将其推送出来,或者您是否会在Apple Watch上使用NSURLSession?请记住,这是针对watchOS 2的.
自动UI测试/单元测试不是iOS中的神话,但可以在watchOS上进行自动UI测试吗?我错过了watchOS 2或Xcode 7的任何新功能.我查看了分析器,但我无法弄清楚如何在手表操作系统上自动化测试.
任何帮助表示赞赏.
这似乎是whatchOS 2发行说明中描述的已知问题:
如果您没有在Watch Simulator上看到地图图块加载,请先在iOS模拟器中启动地图图块.之后,地图图块将加载.
我尝试了这些步骤,没有成功,地图总是显示一个空网格:
有人经历过这个吗?
提前致谢
watchos-2 ×10
watchkit ×7
ios ×6
apple-watch ×5
swift ×5
animation ×1
core-motion ×1
dictionary ×1
graphics ×1
mkmapview ×1
nsurlsession ×1
objective-c ×1