Iphone 允许用户通过点击来选择UIPickerView行

Iphone 允许用户通过点击来选择UIPickerView行,iphone,objective-c,xcode,uipickerview,Iphone,Objective C,Xcode,Uipickerview,我试图使用UIPicker视图,其行为与iPhone代码示例中通常看到的行为有所不同 我想做的是允许用户滚动选取器内容,但不允许自动选择选取器的行(使用选取器委托中的“didSelectRow”方法)。相反,我希望允许用户触摸选择器的中心行,该行高亮显示,并成为选择 有没有办法做到这一点 提前感谢。UIPickerView只有两名代表 因此,我们只能使用7种方法通过委托控制UIPickerView –pickerView:组件的行高: –pickerView:widthForCompon

我试图使用UIPicker视图,其行为与iPhone代码示例中通常看到的行为有所不同

我想做的是允许用户滚动选取器内容,但不允许自动选择选取器的行(使用选取器委托中的“didSelectRow”方法)。相反,我希望允许用户触摸选择器的中心行,该行高亮显示,并成为选择

有没有办法做到这一点


提前感谢。

UIPickerView只有两名代表

因此,我们只能使用7种方法通过委托控制UIPickerView

–pickerView:组件的行高:
–pickerView:widthForComponent:
–pickerView:titleForRow:forComponent:
–pickerView:viewForRow:forComponent:reusingView:
–pickerView:didSelectRow:Uncomponent:
–PickerView中组件的数量:
–pickerView:行数组件:

就这些

在中,UITableView有更多用于管理选择的方法。 比如,, –tableView:将选择Rowatindexpath:
–tableView:didSelectRowAtIndexPath:
–tableView:将取消行索引路径:
–tableView:DidDecreawatindexpath:

然而

在UIPickerViewDelegate情况下,只有一种方法用于响应行选择

–pickerView:didSelectRow:Incomonent:

  • 做一个新的

    • 与UIPickerView的位置相同
    • [yourcontrol setBackGroundColor:[UIColor clearColor]]
  • 作法

  • -(iAction)pickerControlTapped { [yourpicker selectRow:rand()%yourpickersize 不完整:0 动画:是]; } .3。在1和2之间建立连接

    [yourcontrol addTarget: self action: @selector(pickerControlTapped) forControlEvents: UIControlEventTouchUpInsied]; [yourcontrol addTarget:self 操作:@选择器(pickerControlTapped) 对于控制事件:uicontrolEventTouchUpInsided];
    将手势识别器添加到UIPickerView,它会触发对象中的目标方法:

    myGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerTapped:)];
    [myPicker addGestureRecognizer:myGR];
    
    // target method
    
    -(void)pickerTapped:(id)sender
    {
    // your code
    }
    

    以Martin Linklater的回答为基础,支持点击其他行的选择器: 有一些神奇的数字,但对我有用

    - (void) pickerTapped:(UITapGestureRecognizer*)gestureRecognizer
    {
        CGPoint location = [gestureRecognizer locationInView:self.pickerView];
    
        CGFloat halfViewHeight = self.pickerView.frame.size.height / 2;
    
        NSInteger row = -1;
        if (location.y < halfViewHeight - 22
            && location.y > halfViewHeight - 66)
        {
            row = [self.pickerView selectedRowInComponent:0] - 1;
        }
        else if (location.y < halfViewHeight + 22
                 && location.y > halfViewHeight - 22)
        {
            row = [self.pickerView selectedRowInComponent:0];
        }
        else if (location.y < halfViewHeight + 66
                 && location.y > halfViewHeight + 22)
        {
            row = [self.pickerView selectedRowInComponent:0] + 1;
        }
    
        if (row >= 0 && row < [self.content count])
        {
            id element = [self.content objectAtIndex:row];
    
            if (element)
            {
                [self.pickerView selectRow:row inComponent:0 animated:YES];
    
                // do more stuff
            }
        }
    }
    
    -(void)选取器上限:(UITapGestureRecognitor*)GestureRecognitor
    {
    CGPoint location=[GestureRecognitor locationInView:self.pickerView];
    CGFloat半视图高度=self.pickerView.frame.size.height/2;
    NSInteger行=-1;
    如果(位置y<半视图高度-22
    &&位置y>半视图高度-66)
    {
    行=[self.pickerView selectedRowInComponent:0]-1;
    }
    否则如果(位置y<半视图高度+22
    &&位置y>半视图高度-22)
    {
    行=[self.pickerView SelectedRowUncomponent:0];
    }
    否则如果(位置y<半视图高度+66
    &&位置y>半视图高度+22)
    {
    行=[self.pickerView SelectedRowUncomponent:0]+1;
    }
    如果(行>=0和行<[self.content count])
    {
    id元素=[self.content objectAtIndex:row];
    if(元素)
    {
    [self.pickerView selectRow:行不完整:0动画:是];
    //多做事
    }
    }
    }
    
    对于这个问题,我有一个相对简单的解决方案,对我来说效果很好。使用隐藏的自定义按钮,无需手势识别器即可实现轻触功能。这个解决方案适用于只有一个组件的选择器,但是我相信它可以适应更多的组件

    首先,在界面生成器中或通过编程方式添加一个按钮。将其隐藏并与拾取器一样宽,然后将其放置在视图层次中,使其正好位于拾取器的中心和前面

    我用这样的iAction来显示我的选取器。然而,如何显示和隐藏选择器实际上取决于您

    - (IBAction)showPicker:(id)sender
    {
        _picker.hidden = NO;
        _buttonPicker.hidden = NO;
    }
    
    选择选择器值的所有操作都发生在UIControlEventTouchUpInside事件的iAction中,类似这样

    - (IBAction)selectPicker:(id)sender
    {
        //Hide the button so that it doesn't get in the way
        _buttonPicker.hidden = YES;
    
        //Make sure we're within range
        NSInteger max = _values.count;
        NSInteger row = [_picker selectedRowInComponent:0];
        if(row >= 0 && row < max) {
            NSString *value = [_values objectAtIndex:row];
    
            //Set the label value and hide the picker
            _label.text = value;
            _picker.hidden = YES;
        }
    }
    
    -(iAction)选择器选择器:(id)发送者
    {
    //把按钮藏起来,这样它就不会碍事了
    _buttonPicker.hidden=是;
    //确保我们在射程之内
    NSInteger max=_values.count;
    NSInteger行=[\u选择器selectedRowUncomponent:0];
    如果(行>=0&&row

    我已经从工作代码中稍微修改了这个答案的代码,因此如果它完全坏了,我深表歉意。

    您能更好地解释一下为什么didSelectRow方法在这里对您没有帮助吗?您是否尝试过使用它?如果是,您遇到了什么问题?问题是它的工作原理与预期的一样。:/基本上,我看起来像是一个pickerView,其中picker会自动选择其中一个行值,但它只在用户触摸它时选择一个值。我也试着这么做,但我做不到。我就是这么想的。我看到应用程序在做我想做的事情,但我想我在用其他方式做。谢谢。太好了,请把这个标记为答案。注意:使其与UIPickerView的中心行的位置相同。我认为这是简单而精彩的。由于选择行的高度已知,因此可以精确而简单地定位。很好的东西,谢谢。对不起,我不得不再说一遍,这个解决方案是多么的优秀,这让我度过了一个下午。马丁,我偶然发现了你的解决方案,发现它非常有用。这么简单,它的工作。唯一的问题是,在我对点击的回应中,我如何将点击传递给拾取者,以便在被点击的线处获取对象,而不是在中心的对象?@DeanDavids您所指的页面似乎已经不存在了。你能提供另一个答案,或者解释一下解决方案吗?对于任何找到这个答案的谷歌用户,还有另一个stackoverflow答案,它处理允许点击通过并选择一个项目的问题——任何谷歌用户
    - (IBAction)selectPicker:(id)sender
    {
        //Hide the button so that it doesn't get in the way
        _buttonPicker.hidden = YES;
    
        //Make sure we're within range
        NSInteger max = _values.count;
        NSInteger row = [_picker selectedRowInComponent:0];
        if(row >= 0 && row < max) {
            NSString *value = [_values objectAtIndex:row];
    
            //Set the label value and hide the picker
            _label.text = value;
            _picker.hidden = YES;
        }
    }