允许触摸通过UICollectionView传递给它的Superview

时间:2015-01-21 23:46:31

标签: ios uiscrollview xamarin.ios xamarin uicollectionview

我们说我有一个坐着UICollectionView的ViewController。如何让Touches通过UICollectionView并进入ViewController的TouchesBegan / TouchesMoved / TouchesEnded功能?我只需设置 UIScrollViews ,就可以通过ExclusiveTouch = false多次完成此操作,然后触摸将通过UIScrollView传递给它{&1}超级视角。但是同样的方法不能与UICollectionViews一起使用。有任何想法吗?

设置UICollectionView

partial class CyanViewController : BaseViewControllerWithCollection
{

    /*--------------------------------------------------------------------------------*/
    // Constructors
    /*--------------------------------------------------------------------------------*/

    public CyanViewController (IntPtr handle) : base (handle)
    {
    }

    /*--------------------------------------------------------------------------------*/

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        // Setup collection view
        this.SetupCollectionView();
    }

    /*--------------------------------------------------------------------------------*/

    public override void TouchesBegan (NSSet touches, UIEvent evt)
    {
        base.TouchesBegan (touches, evt);

        Console.WriteLine ("TouchesBegan");
    }

    /*--------------------------------------------------------------------------------*/
    // Private Methods
    /*--------------------------------------------------------------------------------*/

    private void SetupCollectionView ()
    {
        Console.WriteLine ("SetupCollectionView");
        try
        {
            // Instantiate collection view
            this.CollectionView = new UICollectionView(
                this.View.Bounds,
                new UICollectionViewFlowLayout() { 
                    ScrollDirection = UICollectionViewScrollDirection.Vertical,
                    ItemSize = new CGSize(75, 115),
                    SectionInset = new UIEdgeInsets(20, 20, 20, 20)
                }
            );

            // Setup delegate and data source
            this.CollectionView.Delegate = new ProductTypeCollectionViewDelegate(this);
            this.CollectionView.DataSource = new ProductTypeCollectionViewDataSource(this);
            this.CollectionView.RegisterClassForCell(typeof(BaseCollectionViewCell), BaseCollectionViewCell.s_millaCellId);
        }
        catch (Exception ex)
        {
            Console.WriteLine ("Exception : " + ex.Message);
            Console.WriteLine ("Exception : " + ex.StackTrace);
        }

        // Add collection view to view
        this.View.AddSubview(this.CollectionView);
    }

    /*--------------------------------------------------------------------------------*/
    // Class: SeedsCollectionViewDataSource
    /*--------------------------------------------------------------------------------*/

    public class ProductTypeCollectionViewDataSource : UICollectionViewDataSource
    {

        /*--------------------------------------------------------------------------------*/
        // Properties
        /*--------------------------------------------------------------------------------*/

        private CyanViewController _parentController;

        /*--------------------------------------------------------------------------------*/
        // Constructors
        /*--------------------------------------------------------------------------------*/

        public ProductTypeCollectionViewDataSource (
            CyanViewController a_parentController
        )
        {
            this._parentController = a_parentController;
        }

        /*--------------------------------------------------------------------------------*/

        private ProductTypeCollectionViewDataSource ()
        {
            throw new NotImplementedException ();
        }

        /*--------------------------------------------------------------------------------*/
        // UICollectionViewDataSource Implementation
        /*--------------------------------------------------------------------------------*/

        public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
        {
            var cell = (BaseCollectionViewCell)collectionView.DequeueReusableCell (BaseCollectionViewCell.s_millaCellId, indexPath);

            cell.Label.Text = "Woot";

            return cell;
        }

        /*--------------------------------------------------------------------------------*/

        public override nint GetItemsCount (UICollectionView collectionView, nint section)
        {
            return 10;
        }

        /*--------------------------------------------------------------------------------*/

    }

    /*--------------------------------------------------------------------------------*/
    // Class: SeedsCollectionViewDelegate
    /*--------------------------------------------------------------------------------*/

    public class ProductTypeCollectionViewDelegate : UICollectionViewDelegate
    {

        /*--------------------------------------------------------------------------------*/
        // Properties
        /*--------------------------------------------------------------------------------*/

        private CyanViewController _parentController;

        /*--------------------------------------------------------------------------------*/
        // Constructors
        /*--------------------------------------------------------------------------------*/

        public ProductTypeCollectionViewDelegate (
            CyanViewController a_parentController
        )
        {
            this._parentController = a_parentController;
        }

        /*--------------------------------------------------------------------------------*/

        private ProductTypeCollectionViewDelegate ()
        {
            throw new NotImplementedException ();
        }

        /*--------------------------------------------------------------------------------*/
        // UICollectionViewDelegate Implementation
        /*--------------------------------------------------------------------------------*/

        public async override void ItemSelected (UICollectionView collectionView, NSIndexPath indexPath)
        {
            Console.WriteLine ("ItemSelected indexPath.Row = " + indexPath.Row);
        }

        /*--------------------------------------------------------------------------------*/

    }

    /*--------------------------------------------------------------------------------*/

}

设置包含UIViewController的{​​{1}}。我想在CollectionView / TouchesBegan / Moved点击这里进行接触!

Ended

这是我的partial class BaseViewControllerWithCollection : UIViewController { /*--------------------------------------------------------------------------------*/ // Properties /*--------------------------------------------------------------------------------*/ public UICollectionView CollectionView { get; set; } /*--------------------------------------------------------------------------------*/ // Constructors /*--------------------------------------------------------------------------------*/ public BaseViewControllerWithCollection (IntPtr handle) : base (handle) { this.View.ExclusiveTouch = false; this.View.UserInteractionEnabled = true; } public override void TouchesBegan (NSSet touches, UIEvent evt) { base.TouchesBegan (touches, evt); Console.WriteLine ("TouchesBegan"); } public override void TouchesMoved (NSSet touches, UIEvent evt) { base.TouchesMoved (touches, evt); Console.WriteLine ("TOuchesMoved"); } public override void TouchesEnded (NSSet touches, UIEvent evt) { base.TouchesEnded (touches, evt); Console.WriteLine ("TouchesSended"); } /*--------------------------------------------------------------------------------*/ } 班级。我无法接触UICollectionView,所以我试着把它们放到这里,但是不能......

UIViewController

1 个答案:

答案 0 :(得分:2)

我不知道这是否是正确的方法,但是在集合视图子类中覆盖touchesBegan等,并且在super和nextResponder上调用它似乎都有效。在Objective-C中,我这样做了,

@implementation RDCollectionView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.nextResponder touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.nextResponder touchesEnded:touches withEvent:event];
}

然后,在底层视图中,我还实现了这三种方法,并处理了触摸。