如何在旋转时检测 UIPickerView 中心行

发布于 2024-12-19 10:48:15 字数 968 浏览 2 评论 0原文

由于屏幕空间不足,我尝试在用户旋转滚轮时在 UIPickerView 中显示图像的描述(旋转时 UILabel 显示在 pickerView 的右侧)。我使用 viewForRow: 来检测旋转并显示描述。问题是,使用我到目前为止编写的代码,它无法显示前 2 行和最后 2 行的描述。 这是我的代码:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIImageView *image=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:[zodiacSigns objectAtIndex:row]]]autorelease];
    selectedZodiac.frame=CGRectMake(80, 80, 50, 50);
    selectedZodiac.hidden=NO;
    @try {
    //Here -2 is offset of the view in central row
        selectedZodiac.text=[zodiacSigns objectAtIndex:row-2];
    }
    @catch (NSException *exception) {
        NSLog(@"Exception caught");
    }
    @finally {

    }
    if([yourZodiacPicker selectedRowInComponent:0]!=0)
    {
        [self.view insertSubview:selectedZodiac aboveSubview:self.view];
    }
    NSLog(@"reuseView");
    return image;
}

任何帮助将不胜感激!

Because of lack of screen space, i try to display description (UILabel appearing right to pickerView when spinning) of image in UIPickerView when user spins the wheel. I use viewForRow: to detect the spin and to show the description. The problem is, using the code i've written so far, it can't display the description of first 2 rows and last 2 rows.
Here is my code:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIImageView *image=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:[zodiacSigns objectAtIndex:row]]]autorelease];
    selectedZodiac.frame=CGRectMake(80, 80, 50, 50);
    selectedZodiac.hidden=NO;
    @try {
    //Here -2 is offset of the view in central row
        selectedZodiac.text=[zodiacSigns objectAtIndex:row-2];
    }
    @catch (NSException *exception) {
        NSLog(@"Exception caught");
    }
    @finally {

    }
    if([yourZodiacPicker selectedRowInComponent:0]!=0)
    {
        [self.view insertSubview:selectedZodiac aboveSubview:self.view];
    }
    NSLog(@"reuseView");
    return image;
}

Any help would be greatly appreciated!

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

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

发布评论

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

评论(2

苏佲洛 2024-12-26 10:48:15

我想如果您确实需要此功能,最好的方法是使用 ScrollView/TableView 和匹配的覆盖图像重新创建 PickerView。

当滚动视图停止时,禁用选择/突出显示并滚动到最近的“单元格”。

然后确定屏幕中间的单元格。
您还应该在内容之前和之后添加一个视图,以便用户可以将第一个/最后一个单元格移动到屏幕中间。

I guess the best way would to recreate the PickerView with a ScrollView/TableView and a matching overlay image if you really need this functionality.

Disable selection/highlight and scroll to the nearest "cell" when the scrollview stops.

Then determinate the cell that is in the middle of the screen.
You should also add a view before and after the content, so the user can move the first/last cell to the middle of the screen.

奶茶白久 2024-12-26 10:48:15

我可能误解了,但如果你想要显示当前所选图像的描述,你应该实现 pickerView:didSelectRow:inComponent: 并在那里设置描述标签,而不是在 viewForRow:forComponent: 中执行。它会是这样的:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    selectedZodiac.frame=CGRectMake(80, 80, 50, 50);
    selectedZodiac.hidden=NO;
    selectedZodiac.text=[zodiacSigns objectAtIndex:row];
}

这样您将始终显示所选选择器行的描述。

I might have misunderstood, but if what you want is to display a description of the image currently selected, you should implement pickerView:didSelectRow:inComponent: and set the description label there, instead of doing it in viewForRow:forComponent:. It would be something like this:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    selectedZodiac.frame=CGRectMake(80, 80, 50, 50);
    selectedZodiac.hidden=NO;
    selectedZodiac.text=[zodiacSigns objectAtIndex:row];
}

This way you would always be displaying the description of the selected picker row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文