在Apple Watch上显示可缩放,可滚动的图像

时间:2015-11-05 13:03:28

标签: api maps watchkit implementation apple-watch

我正在尝试制作一个Apple Watch应用程序(扩展程序),它以类似于Apple的地图应用程序的方式显示地图。我希望能够显示地图的一部分,然后能够使用数字表冠进行缩放和手势滚动。

我认为这应该可以通过Watch OS2实现,但我还没想出要使用哪些API。

目前平台上有可能吗?

更新:我认为我不能使用WKInterfaceMap,因为我想生成自己的地图,而不是使用现有地图。

1 个答案:

答案 0 :(得分:0)

Inside Watch OS2 API只有WKInterfacePicker才能访问Digit Crown  所以我们不能直接使用Digit Crown For WKInterfaceMap,但我们可以通过使用WKInterfacePicker

来使用它
  • 将WKInterfaceMap和WKInterfacePicker放入Group
  • 设置WKInterfacePicker大小宽度固定= 2高度固定= 2 我们不能将大小设置为零

    enter image description here

  • 注意:不要隐藏WKInterfacePicker一段时间因为隐藏而重新设置焦点
  • 为Picker Controller添加以下代码

      class InterfaceController: WKInterfaceController{
             @IBOutlet var mapView : WKInterfaceMap!
             @IBOutlet var tempPicker:WKInterfacePicker!
    
             var zoomArray:[Double] = [1.0,0.98,0.96,0.94,0.92,0.90,0.88,0.86,0.84,0.82,0.80,0.78,0.76,0.74,0.72,0.70,0.68,0.66,0.64,0.62,0.60,0.58,0.56,0.54,0.52,0.50,0.48,0.46,0.44,0.42,0.40,0.38,0.36,0.34,0.32,0.30,0.28,0.26,0.24,0.22,0.20,0.18,0.16,0.14,0.12,0.10,0.09,0.08,0.07,0.06,0.05,0.04,0.03,0.02,0.01,0.009,0.008,0.007,0.006,0.005,0.004,0.003,0.002,0.001]
    
              override func awakeWithContext(context: AnyObject?) {
                        super.awakeWithContext(context)
    
                        let pickerItems: [WKPickerItem] = zoomArray.map {
                            let pickerItem = WKPickerItem()
                            pickerItem.caption = String($0)
                            pickerItem.title = String($0)
                           return pickerItem
                         }
    
                       self.tempPicker.setItems(pickerItems)
                       self.tempPicker.setSelectedItemIndex(25)
    
                       let span = MKCoordinateSpanMake(zoomArray[25], zoomArray[25])
                      let lc = watchDelegate.locationModel.myLocation ?? CLLocationCoordinate2D.init(latitude: 0.0, longitude: 0.0)//watchDelegate.locationModel.myLocation is current user Location
                      let region = MKCoordinateRegionMake(lc, span)
                      self.mapView.setRegion(region)
               }
    
           @IBAction func pickerChanged(value: Int) {
    
             let lc = watchDelegate.locationModel.myLocation ?? CLLocationCoordinate2D.init(latitude: 0.0, longitude: 0.0)
             let span = MKCoordinateSpanMake(zoomArray[value], zoomArray[value])
             let region = MKCoordinateRegionMake(lc,
                                            span)
             self.mapView.setRegion(region)
    
             self.tempPicker.focus()
          }
     }
    
    • 将@IBAction和IBOutlet连接到WKInterfacePicker和WKInterfaceMap