2015-01-21 16 views
5

Giả sử tôi có một ViewController với một số UICollectionView ngồi trên đó. Làm cách nào để tôi có được Chạm để chuyển qua số UICollectionView và vào các chức năng TouchesBegan/TouchesMoved/TouchesEnded của ViewController? Tôi đã thực hiện điều này nhiều lần với UIScrollViews chỉ đơn giản bằng cách đặt ExclusiveTouch = false và sau đó chạm sẽ được phép chuyển qua số UIScrollView cho thiết bị giám sát của nó. Nhưng cách tiếp cận tương tự đó không hoạt động với UICollectionViews. Bất kỳ ý tưởng?Cho phép các chạm chạm vào UICollectionView đến Superview

Set của lên 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); 
     } 

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

    } 

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

} 

Sets lên UIViewController chứa CollectionView. Tôi muốn nhận được các liên lạc trong số TouchesBegan/Moved/Ended tại đây!

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"); 
    } 

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

} 

Đây là lớp của tôi UICollectionView. Tôi không thể có được chạm vào UIViewController vì vậy tôi đã cố gắng nhận được chúng ở đây, nhưng không thể ....

public class MyCollectionView : UICollectionView 
{ 
    public MyCollectionView (CGRect frame, UICollectionViewLayout layout) : base (frame, layout) 
    { 
     this.ExclusiveTouch = false; 
     this.UserInteractionEnabled = true; 

     this.BackgroundView.UserInteractionEnabled = true; 
     this.BackgroundView.ExclusiveTouch = false; 
    } 

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

     Console.WriteLine ("MyCollectionVIew TouchesBegan"); 
    } 

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

     Console.WriteLine ("MyCollectionVIew TouchesMoved"); 
    } 

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

     Console.WriteLine ("MyCollectionVIew TouchesEnded"); 
    } 
} 
+0

Bạn có muốn chạm được nhận diện bởi cả chế độ xem bộ sưu tập và chế độ xem cơ bản hoặc chỉ chế độ xem cơ bản? – rdelmar

+0

Tôi muốn collectionview hoạt động, nhưng khi bạn chỉ cần nhấn hoặc nhấn giữ để có chế độ xem cơ bản để nắm bắt TouchesBegan/Đã di chuyển/Đã kết thúc. Có lẽ cả hai. UIScrollViews có thể làm cả hai, vì vậy tôi đang cố gắng tìm ra lý do tại sao UICollectionViews không thể. Hoặc nếu họ có thể làm thế nào họ có thể? – LampShade

Trả lời

2

Tôi không biết nếu điều này là cách chính xác, nhưng trọng touchesBegan, vv trong phân lớp xem bộ sưu tập, và gọi nó trên siêu cũng như trên nextResponder dường như làm việc. Trong Mục tiêu-C, tôi đã làm điều này,

@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]; 
} 

Sau đó, trong chế độ xem cơ bản, tôi cũng đã thực hiện ba phương pháp này và xử lý các lần chạm.

+0

Phải, điều đó có ý nghĩa. Tôi đã làm điều đó trước khi thành công với những thứ khác mà Bộ sưu tập trong Xamarin. Nhưng đối với một số lý do chức năng Touches của tôi thậm chí không được gọi với UICollectionView hoặc ViewController. Điều này rất kỳ quặc. Nó gần giống như CollectionView đang nắm bắt các chạm và không cho phép họ đi bất cứ nơi nào khác. Do đó thử nghiệm của tôi với ExclusingTouch = false – LampShade

+0

@LampShade: Có thể là ô xem bộ sưu tập của bạn đang nhận các sự kiện đó – Gokul

+0

Giải pháp này sạch sẽ. Tôi đang làm cho ứng dụng bản địa và ý tưởng của bạn là hữu ích. –

Các vấn đề liên quan