以编程方式停止 iphone 上的 uipickerview 动画

发布于 2024-10-15 02:45:46 字数 193 浏览 3 评论 0原文

我有一个包含选择器和 UIToolbar 的 UIActionSheet。 UIToolBar 上有一个保存按钮。然而,我的一些用户报告说在 UIPickerView 停止旋转之前按下了保存按钮,因此仅检索初始值(旋转之前)。

有没有办法在用户点击保存时获取 UIPickerView 当前选定的项目或在旋转时获取活动选定项目的反馈?

谢谢

I have a UIActionSheet containing a picker and a UIToolbar. On the UIToolBar there is a save button. However, some of my users reported pressing the save button before the UIPickerView stops spinning thus only retrieving the initial value (before spinning).

Is there a way to get the currently selected item of the UIPickerView once the user taps save or get feedback of the active selected item while it's spinning?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

花开柳相依 2024-10-22 02:45:46

即使他们在选择器仍在旋转时关闭选择器,选择器仍然会在停止后使用最终选定的行调用委托,即使它不可见。假设您还没有释放它,您可以设置委托接收器来检查选择器是否可见,如果不可见,则保存所选值。

我这样做的假设是,很明显用户没有滚动到随机值 - 通常当他们滚动并关闭而没有等待它解决时,这意味着他们滚动到列表的最顶部或最底部。我想说在这两种情况下您可以安全地使用委托的结果。

Even if they dismiss the picker while it's still spinning, the picker will still call the delegate with the final selected row once it stops, even if it isn't visible. Assuming you haven't deallocated it yet, you can set the delegate receiver to check if the picker is visible, and if it isn't, save the selected value.

I do this assuming it's clear the user isn't scrolling to a random value - usually when they scroll and dismiss without waiting for it to settle, it means they scrolled to either the very top or very bottom of the list. I'd say you can safely use the result of the delegate in these two cases.

长途伴 2024-10-22 02:45:46

您是否想过使用 titleForRow 方法来解决这个问题。当 pickerview 滚动时,您的 titleForRow 方法将被调用以填充当前查看的行上方或下方的行(取决于选择器旋转的方式)。当选取器停止时,将调用 didSelectRow 方法,并且返回的行距 titleForRow 方法中填充的最后一行为 2 行。

我认为您可以选择在滚动中的某个点停止选择器(计时器来检测用于告诉 titleForRow 中的代码从调用中捕获组件和行的点),然后执行selectRow 在停止旋转后将值设置为您想要的值(并且调用 didSelectRow)。

Have you ever thought about using the titleForRow method to figure this out. As the pickerview is scrolling, your titleForRow method is being called to populate the rows above or below the currently viewed row(depending on which way the picker is spinning). When the picker stops, the didSelectRow method is called and the row returned is 2 rows from the last row populated in the titleForRow method.

I would think that you could choose to stop the picker at some point (timers to detect point that are used to tell code in titleForRow to capture component and row from the call) in the scrolling and then do a selectRow to set the value to what you want it to be after it stops spinning (and didSelectRow is called).

樱桃奶球 2024-10-22 02:45:46

我不认为这是一个你能够用 UIPickerView 本身解决的问题。

现在没有办法在动画停止的情况下选择哪一行(因此选择器视图选择它停止的行)。唯一的方法是使用 selectRow:inComponent:animated: 告诉选取器停止在哪一行,但是您如何知道那是哪一行呢?你不知道,因为选择器正在旋转......

我认为这只是 UIPickerView 的限制,Apple 可能会将其描述为预期行为。

I don't think this is a problem you're going to be able to solve with UIPickerView by itself.

There is no way to now which row is selected without the animation stopping (and thus the picker view selecting the row it stopped on). The only way would be to tell the picker which row to stop on, by using the selectRow:inComponent:animated: but how will you know which row that is? You don't know because the picker is spinning...

I think this is just a limitation of the UIPickerView and Apple would likely describe it as expected behaviour.

萌梦深 2024-10-22 02:45:46

我也有同样的问题...
我通过简单地在关闭或选择选择器值的方法上添加以下代码行来修复它。

[_picker selectRow:[_picker selectedRowInComponent:0] inComponent:0 animated:YES];

I had the same problem...
I fixed it by simply adding the following code line on the method that closes or selects the picker value.

[_picker selectRow:[_picker selectedRowInComponent:0] inComponent:0 animated:YES];
淡写薰衣草的香 2024-10-22 02:45:46

刚刚找到适合我的情况的解决方案。我不需要最后一个更新事件,该事件是在用户关闭选择器后停止旋转时生成的。所以这个方法停止滚动并且不会引起新的更新选择事件。似乎您将在此之后检查选择器中的选择是否正确(我不在乎,当我显示选择器时我会重新选择值)。

- (void)_resetPickerViewDelegates {
    id delegate = [_pickerView.delegate retain];
    id dataSource = [_pickerView.dataSource retain];

    _pickerView.delegate = nil;
    _pickerView.dataSource = nil;
    [_pickerView reloadAllComponents];
    _pickerView.delegate = delegate;
    _pickerView.dataSource = dataSource;

    [delegate release];
    [dataSource release];    
}

Just found solution, which works for my case. I don't need that last update event, generated when picker stops spinning after user dismissed it. So this method stops scrolling and don't causes new update selection event. Seems you'll going to check if selections are ok in picker after this (I don't care, I reselect values when I show picker).

- (void)_resetPickerViewDelegates {
    id delegate = [_pickerView.delegate retain];
    id dataSource = [_pickerView.dataSource retain];

    _pickerView.delegate = nil;
    _pickerView.dataSource = nil;
    [_pickerView reloadAllComponents];
    _pickerView.delegate = delegate;
    _pickerView.dataSource = dataSource;

    [delegate release];
    [dataSource release];    
}
你怎么这么可爱啊 2024-10-22 02:45:46

从组件中检索所选对象

[component1Array objectAtIndex:[myPicker selectedRowInComponent:1]];

单击“保存”按钮时,您可以通过其中

  • “component1Array”是 NSArray 或
    NSMutableArray,用于
    显示对象(原始内容)
    你的组件。
  • “myPicker”是一个 UIPickerView 对象
    显示选择器。
  • “[myPicker selectedRowInComponent:1]”
    将返回当前选择的
    来自组件中 myPicker 的原始编号
    ‘1’。

而“[myPicker selectedRowInComponent:2]”将从组件“2”中的 myPicker 返回当前选定的原始编号。
从您的组件中检索选定的对象。

 [component2Array objectAtIndex:[myPicker selectedRowInComponent:2]];

您可以通过希望它对您有用来

On "save" button click, You can retrieve the selected object from your component by

[component1Array objectAtIndex:[myPicker selectedRowInComponent:1]];

where,

  • "component1Array" is an NSArray or
    NSMutableArray, object which used to
    display the objects (raw contents) in
    your component.
  • "myPicker" is an UIPickerView object
    which displays the picker.
  • "[myPicker selectedRowInComponent:1]"
    will return the currently selected
    raw no from the myPicker in component
    '1'.

And "[myPicker selectedRowInComponent:2]" will return the currently selected raw no from the myPicker in component '2'.
You can retrieve the selected object from your component2 by

 [component2Array objectAtIndex:[myPicker selectedRowInComponent:2]];

Hope it works for you.

阳光①夏 2024-10-22 02:45:46

这可能是一个“黑客”,但它确实有效。我的情况略有不同。我正在使用一个 UIPicker,但可以更改它所操作的对象。

我的问题是,您可以选择一个对象,旋转选择器,当它完成旋转时,选择另一个对象。最终发生的事情是新对象获得了该旋转的最终值,而不是被忽略。

在设置 UIPicker 时,我做了什么,我会根据它应该更新的对象给它一个唯一的标签

- (void)updatePickerView:(UIPickerView*)pickerView
{
    ...
    pickerView.tag = [self.myObject.position integerValue];
    [pickerView selectRow:i inComponent:0 animated:NO];
}

然后,当它完成旋转时,我检查以确保标签仍然对齐。

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        if (pickerView.tag == [self.myObject.position integerValue]) {
            ...
        }
    }

我不确定它在苹果方面是如何运作的,但这足以让它为我工作。

作为旁注,我不确定这是否重要,但我的 UIPicker 位于 UITableViewCell 中,但因为它是一个可重用的单元格,而且因为我遇到了问题我是这样的,我假设 UIPicker 从一个位置移动到另一个位置时是相同的(因为屏幕上一次只有一个)。

This might be a "hack," but it works. My case is slightly different. I'm using one UIPicker, but can change what object it is manipulation.

My issue was that you could select one object, spin the picker, and while it is finishing its spin, select a different object. What ended up happening was the new object was getting the final value of that spin, rather than it being ignored.

What I did what when setting up the UIPicker I'd give it a unique tag based on the object that it was supposed to update.

- (void)updatePickerView:(UIPickerView*)pickerView
{
    ...
    pickerView.tag = [self.myObject.position integerValue];
    [pickerView selectRow:i inComponent:0 animated:NO];
}

Then when it finishes its spin, I check to make sure that tag lines up still.

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        if (pickerView.tag == [self.myObject.position integerValue]) {
            ...
        }
    }

I'm not sure how it is functioning on Apple's side, but this was enough to make it work for me.

As a side note, and I'm not sure if this matters, but my UIPicker is in a UITableViewCell but since it is a reusable cell and because I was having the issues that I was, I assume the UIPicker is the same when it is moved from one position to another (as there is only one on screen at a time).

素染倾城色 2024-10-22 02:45:46
var isSpinning  = false (Global)

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  self.isSpinning = true
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  self.isSpinning = false
}
var isSpinning  = false (Global)

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  self.isSpinning = true
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  self.isSpinning = false
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文