用动画设置WKInterfacePicker高度选择任意项

时间:2015-11-14 11:16:35

标签: swift animation watchkit watch-os-2

在watchOS 2界面控制器中,我显示一个按钮和一个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 个答案:

答案 0 :(得分: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()可使动画更流畅。