2015-11-14 35 views
2

在watchOS 2界面控制器中,我显示了一个按钮和一个WKInterfacePicker。使用动画设置WKInterfacePicker高度选择任意项目

当用户按下按钮时,选取器隐藏并显示动画。视觉效果很好。

这有一个意想不到的副作用:用动画设置高度会改变所选项目,到目前为止我还没有发现它。

func setDurationPickerVisibility(duration: NSTimeInterval) { 
    print ("self.durationPickerHidden=\(self.durationPickerHidden)") 
    animateWithDuration(duration) { 
     if self.durationPickerHidden { 
      self.durationPicker.resignFocus() 
      self.durationPicker.setHeight(0.0) 
      self.durationPicker.setHidden(self.durationPickerHidden) 
     } else { 
      self.durationPicker.setHeight(self.durationPickerHeigth) 
      print ("animation setting durationPicker Index to \(self.durationPickerIndex)") 
      self.durationPicker.setSelectedItemIndex(self.durationPickerIndex) 
     } 
    } 
    if !self.durationPickerHidden { 
     print ("direct setting durationPicker Index to \(self.durationPickerIndex)") 
     self.durationPicker.setSelectedItemIndex(durationPickerIndex) 
     self.durationPicker.setHidden(self.durationPickerHidden) 
     self.durationPicker.focus() 
     self.durationPicker.setSelectedItemIndex(durationPickerIndex) 
    } 
} 


@IBAction func durationPickerChanged(value: Int) { 
    print("durationPickerChanged: \(value)") 
    //... 
} 
运行此代码时

,控制台显示的输出如下:

self.durationPickerHidden=false 
direct setting durationPicker Index to 24 
animation setting durationPicker Index to 24 
durationPickerChanged: 21 

你看,选择器改变,我没有设定一个指标21。

我想这个代码的许多变化,关键部分似乎是以下几点:

  • 当我没有动画运行,一切正常。 (选择索引24)
  • 如果setFocus()位于动画内部或外部,则没有区别。
  • 它没有什么区别,如果setSelectedItemIndex是内部或外部的动画
  • 时self.durationPicker.setHeight(self.durationPickerHeigth)是动画(这是视觉效果的关键)的内部,然后拾取器选择该任意项21.

有没有人知道我如何设置动画选取器的高度,并仍然在控制哪个项目被选中?

回答

1

我发现下面的解决办法,使无形的问题:

var durationPickerWorkaroundNecessary = false 
func setDurationPickerVisibility(duration: NSTimeInterval) { 
    print ("self.durationPickerHidden=\(self.durationPickerHidden)") 
    animateWithDuration(duration) { 
     if self.durationPickerHidden { 
      self.durationPicker.resignFocus() 
      self.durationPicker.setHeight(0.0) 
      self.durationPicker.setHidden(self.durationPickerHidden) 
     } else { 
      self.durationPicker.setHeight(self.durationPickerHeigth) 
      print ("animation setting durationPicker Index to \(self.durationPickerIndex)") 
      self.durationPicker.setSelectedItemIndex(self.durationPickerIndex) 
      self.durationPicker.setHidden(self.durationPickerHidden) 
      self.durationPicker.focus() 
      self.durationPickerWorkaroundNecessary = true 
     } 
    } 
} 


@IBAction func durationPickerChanged(value: Int) { 
    print("durationPickerChanged: \(value)") 
    if durationPickerWorkaroundNecessary == true { 
     durationPickerWorkaroundNecessary = false 
     if durationPickerIndex != value { 
      print("durationPickerChanged: fixing value") 
      durationPicker.setSelectedItemIndex(durationPickerIndex) 
      return 
     } 
    } 
    durationPickerIndex = value 


    //... 
} 

的基本思路是忽略了动画后的第一个变化事件。 丑,但它的作品。

在动画中设置focus()会使动画更平滑。

+0

你有没有发现任何其他方式呢?我不想做像上面这样的事情,因为这看起来像一个bug,如果Apple修复它,那么看起来你的代码可能会破坏。 我很高兴我发现这篇文章的想法,因为我无法弄清楚为什么我设置的价值没有采取,它原来是setHeight调用改变我的价值。我最终做的是在设置高度后使用dispatch_after调用设置值.05秒。不知道这是否可以用于动画。 – Dan

+0

不在watchOS 2.x.我将在watchOS 3中再次访问。由于iOS 9.x中没有相关的严重“功能”,因此该应用程序迄今为止尚未在应用商店中制作。 –

+0

就这个问题而言,我只是发现,在我的情况下,如果初始高度(如故事板中定义的)高于特定大小,则不会发生。 – Dan