如何在旋转时检测 UIPickerView 中心行
由于屏幕空间不足,我尝试在用户旋转滚轮时在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想如果您确实需要此功能,最好的方法是使用 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.
我可能误解了,但如果你想要显示当前所选图像的描述,你应该实现 pickerView:didSelectRow:inComponent: 并在那里设置描述标签,而不是在 viewForRow:forComponent: 中执行。它会是这样的:
这样您将始终显示所选选择器行的描述。
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:
This way you would always be displaying the description of the selected picker row.