Xamarin iOS (9.2):手势在模拟器中不起作用

Posted

技术标签:

【中文标题】Xamarin iOS (9.2):手势在模拟器中不起作用【英文标题】:Xamarin iOS (9.2): Gesture not working in emulator 【发布时间】:2016-01-29 08:19:20 【问题描述】:

有人可以解释我的代码无法正常工作的原因吗?我正在尝试通过在UIPickerModel 中将UITableViewCell 添加到View 来在ios 中创建多选UIPicker,然后将UITapGestureRecognizer 添加到每个单元格。

但模拟器不会响应我的触摸板的任何点击。

这是代码:

class PickerDataModel : UIPickerViewModel

    /*
    <summary>
        the items we wish to display
    </summary>
    */
    public List<string> Items  get; private set; 

    public PickerDataModel()
    
        Items = new List<string>();
    

    /*
    <summary>
        called by the picker to get the number of spinner items
    </summary>
    */    
    public override nint GetRowsInComponent(UIPickerView picker, nint component)
    
        return Items.Count;
    

    /* <summary>called by the picker to get the number of spinner items</summary> */    
    public override nint GetComponentCount(UIPickerView picker)
    
        return 1;
    

    /* <summary>called when a row is selected in the spinner</summary> */
    public override void Selected(UIPickerView picker, nint row, nint component)
    

    

    /* 
    <summary>
        Custom row view.
        The view param is the reusable view for the row. It will be null initially.

        You can add subviews or do anything within the view. But a lazy-initialization 
        block is preferred rather than every time this method is called.

        **Note** GetTitle() is no longer overridden since we aren't using 
        the default row view.
    </summary>
    */
    public override UIView GetView(UIPickerView picker, 
                                   nint row, nint component, UIView view)
    
        if (view == null)
        
            CGSize rowSize = picker.RowSizeForComponent(component);
            UITableViewCell cell = 
                    new UITableViewCell(new CGRect(new CGPoint(0, 0), rowSize));
            view = cell;
            cell.BackgroundColor = UIColor.Clear;
            cell.UserInteractionEnabled = true;
            UITapGestureRecognizer singleTapGestureRecognizer = 
                                            new UITapGestureRecognizer();
            singleTapGestureRecognizer.AddTarget(() => Console.WriteLine("Tapped"));
            singleTapGestureRecognizer.NumberOfTapsRequired = 1;
            cell.AddGestureRecognizer(singleTapGestureRecognizer);

            cell.TextLabel.Text = Items[(int) row];

            cell.Tag = row;                    
        
        return view;
    

    void Test()
    
        Console.WriteLine("Tapped");
    

    private void ToggleSelection(UITapGestureRecognizer recognizer)
                        
        ((UITableViewCell)recognizer.View).Accessory = UITableViewCellAccessory.Checkmark;
    

【问题讨论】:

【参考方案1】:

您可能需要在手势识别器中设置 Delegate 并实现此方法以返回 true:

https://developer.xamarin.com/api/member/UIKit.UIGestureRecognizerDelegate.ShouldRecognizeSimultaneously/p/UIKit.UIGestureRecognizer/UIKit.UIGestureRecognizer/

【讨论】:

以上是关于Xamarin iOS (9.2):手势在模拟器中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin.ios 手势密码

Xamarin IOS UILabel在UIScrollView中点击手势不起作用

如何在 IO (Xamarin.IO) 中动态启用和滚动 UIScrollView?

Xamarin.Forms 警告:尝试使用 iOS 图像/手势识别器在其视图不在窗口层次结构中的 * 上呈现 *

iOS 模拟器无法启动

单击链接会冻结应用程序