2015-01-21 4 views
5

मान लें कि मेरे पास UICollectionView पर एक व्यू कंट्रोलर है। मैं UICollectionView और व्यू कंट्रोलर के TouchesBegan/TouchesMoved/TouchesEnded फ़ंक्शंस में गुजरने के लिए टच कैसे प्राप्त कर सकता हूं? मैंने UIScrollViews के साथ ExclusiveTouch = false सेट करके कई बार ऐसा किया है और स्पर्श को इसके पर्यवेक्षण में UIScrollView से गुज़रने की अनुमति दी जाएगी। लेकिन वही दृष्टिकोण 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 सेट कि 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 में अंतिम रूप नहीं मिल सका तो मैं उन्हें यहाँ हो रही है की कोशिश की, लेकिन नहीं कर सका ....

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

क्या आप चाहते हैं कि संग्रह संग्रह और अंतर्निहित दृश्य, या केवल अंतर्निहित दृश्य दोनों द्वारा स्पर्श किया जाए? – rdelmar

+0

मैं संग्रह दृश्य को काम करना चाहता हूं, लेकिन जब आप टच्सबैगन/मूव/एंडेड को कैप्चर करने के लिए अंतर्निहित दृश्य रखने के लिए बस टैप करते हैं, या दबाए रखें। तो शायद दोनों। UIScrollViews दोनों कर सकते हैं, इसलिए मैं यह पता लगाने की कोशिश कर रहा हूं कि UICollectionViews क्यों नहीं कर सकता। या अगर वे कर सकते हैं वे कैसे कर सकते हैं? – LampShade

उत्तर

2

अगर यह सही तरीका है मैं नहीं जानता, लेकिन में touchesBegan, आदि अधिभावी संग्रह दृश्य उपclass, और इसे सुपर पर और अगली रेस्पॉन्डर पर कॉल करने के लिए काम करता प्रतीत होता है। ऑब्जेक्टिव-सी में, मैं ऐसा किया,

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

फिर, अंतर्निहित विचार में, मैं भी इन तीन तरीकों से लागू किया है, और अंतिम रूप से संभाला।

+0

ठीक है, यह समझ में आता है। मैंने Xamarin में संग्रह अन्य चीजों के साथ सफलतापूर्वक पहले ऐसा किया है। लेकिन किसी कारण से मेरे टच फ़ंक्शन को UICollectionView या ViewController के साथ भी बुलाया नहीं जा रहा है। यह बहुत अजीब है। यह लगभग कलेक्शन व्यू स्पर्शों को पकड़ रहा है और उन्हें कहीं और जाने नहीं दे रहा है। इसलिए ExclusingTouch = false – LampShade

+0

@LampShade के साथ मेरा प्रयोग: हो सकता है कि आपका संग्रहदृश्य कक्ष उन घटनाओं को प्राप्त कर रहा हो – Gokul

+0

यह समाधान साफ़ है। मैं देशी ऐप के लिए कर रहा हूं और आपका विचार उपयोगी है। –

संबंधित मुद्दे