2013-06-05 17 views
5

ponieważ nie mogłem użyć żadnej struktury do stworzenia albumu fotograficznego, staram się tworzyć własne za pomocą widoku kolekcji, ale utknąłem na samym początku.Obsługa Gesty dotykowe w UICollectionView

Moim celem jest wyświetlenie wszystkich obrazów z mojego serwisu internetowego w moim widoku kolekcji, ponieważ wszystkie wyświetlane, następnym krokiem jest, gdy ktoś dotknie dowolnej komórki, mogę otworzyć ją w nowym widoku, a także nawigować między wszystkimi.

tutaj jest podstawowy kod, który stworzyłem:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    [collectionController reloadData]; 
    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:nil action:@selector(touched)]; 

    tapGesture.numberOfTapsRequired = 1; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 

    return 1; 

} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 

    return 6; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *cellIdentifier = @"Cell"; 

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    [cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    [cell.imgCollection addGestureRecognizer:tapGesture]; 

    return cell; 
} 

-(void)touched:(UIGestureRecognizer *)tap{ 

    NSLog(@"the touch happened"); 
} 

Dzięki chłopaki.

Odpowiedz

14

Kilka rzeczy nie są bezpośrednio w kodzie:

Pierwszy, initWithTarget:action: nie powinny być przekazywane do nil wartość dla celu. Od the docs:

docelowej

Obiekt, który jest odbiorcą komunikatów wysyłanych przez działania odbiornika, gdy rozpozna gest. nil nie jest prawidłową wartością.

W twoim przypadku należy przekazać self jako cel, ponieważ chcesz wysłał wiadomość touched: do bieżącej instancji klasy.

Po drugie, selektor przekazany do initWithTarget:action: jest niepoprawny. Użyłeś @selector(touched), ale Twoja implementacja metody to - (void)touched:(UIGestureRecognizer *)tap;, której selektorem jest @selector(touched:) (pamiętaj o :).

Polecam lekturę this question on selectors, jeśli są one zdezorientowane.

Trzecie, nie można dołączyć pojedynczego UIGestureRecognizer do wielu widoków (patrz this SO question).

Aby to zadziałało, można utworzyć jedną UITapGestureRecognizer na komórkę kolekcji (może w podklasie). Albo jeszcze lepiej, wdrożyć swoją metodę UICollectionViewDelegatecollectionView:didSelectItemAtIndexPath:.

EDIT - Jak wdrożyć collectionView:didSelectItemAtIndexPath::

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    // Bind the collectionView's delegate to your view controller 
    // This could also be set without code, in your storyboard 
    self.collectionView.delegate = self; 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 6; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *cellIdentifier = @"Cell"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    [cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    return cell; 
} 

// I implemented didSelectItemAtIndexPath:, but you could use willSelectItemAtIndexPath: depending on what you intend to do. See the docs of these two methods for the differences. 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // If you need to use the touched cell, you can retrieve it like so 
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; 

    NSLog(@"touched cell %@ at indexPath %@", cell, indexPath); 
} 
+0

Got to, ja już rozwiążesz te problemy, mój błąd, kiedy moje tłumaczenia nazw metod, aby po angielsku. Dzięki za pomoc, teraz muszę to zrozumieć, jak to zrobić w UICollectionViewDelegate, ponieważ nie mam pojęcia. Czy możesz dać mi przykład lub coś w tym stylu? –

+0

Ok, więc muszę nazwać tę metodę - (BOOL) collectionView: (UICollectionView *) collectionView shouldSelectItemAtIndexPath: (NSIndexPath *) indexPath; i stworzyć w nim gest? –

+2

To jest znacznie prostsze. Jeśli zaimplementujesz metody delegatów, nie potrzebujesz rozpoznawania gestów. Delegat wywoła implementację swoich metod po wykryciu dotyku. Zauważ, że nie powinieneś wyraźnie wywoływać 'shouldSelect..' lub' didSelect..' jawnie, delegat robi to za ciebie. –

Powiązane problemy