swift - 使用动画设置 WKInterfacePicker 高度选择任意项目

标签 swift animation watchkit watchos-2

在 watchOS 2 界面 Controller 中,我显示了一个按钮和一个 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。

有谁知道我如何设置带有动画的选择器的高度并且仍然控制哪个项目被选中?

最佳答案

我发现了以下使问题不可见的解决方法:

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()可以让动画更流畅。

关于swift - 使用动画设置 WKInterfacePicker 高度选择任意项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33707802/

相关文章:

javascript - EaselJs 教程。动画不运行

ios - WatchOS2 Watch Connectivity 仍然需要功能中的应用程序组吗?

ios - Xcode 派生数据路径错误

ios - AVCamSwift前置摄像头保存图像垂直翻转

swift - 如何在 Xcode7 的 UITests 下找到带谓词的按钮?

ios - 从 JSON 解析返回数据 - Swift

android - 如何在没有第 3 方库的情况下对 RecyclerView 项目执行滑动手势

android - 如何制作触摸和拖动幻灯片动画

ios - Swift,如何将字典拆分为 60kb 的 block ?

ios - 将另一个 View Controller 加载到 UItabbarcontroller