答案 0 :(得分:0)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [secCollectionImagesArray count];
NSLog(@"secCollectionimagesArray is:%@ ",secCollectionImagesArray);
}
- (MyCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
UILabel *descLabel = (UILabel *)[cell viewWithTag:110];
[descLabel setText:[secCollectionImagesArray objectAtIndex:indexPath.row]];
recipeImageView.image = [UIImage imageNamed:[secCollectionImagesArray objectAtIndex:indexPath.row]];
int pages = floor(collectionView.contentSize.width / collectionView.frame.size.width) + 1;
[pageControl setNumberOfPages:pages];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
third=[[thirdViewController alloc]initWithNibName:@"thirdViewController" bundle:nil];
third.recipeImagedname=[secCollectionImagesArray objectAtIndex:indexPath.row];
[self presentViewController:third animated:YES completion:nil];
}
//write this in thirdViewController ViewDidLoad
currentIndex=0;
eachImgView.image=[UIImage imageNamed:recipeImagedname];
alloc]initWithNibName:@"secondViewController" bundle:nil];
_second.secCollectionImagesArray=DataArray;
NSLog(@"dataArray:%@",DataArray);
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[swipeLeft setNumberOfTouchesRequired:1];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setNumberOfTouchesRequired:1];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.eachImgView addGestureRecognizer:swipeLeft];
[self.eachImgView addGestureRecognizer:swipeRight];
}
-(void)handleSwipe:(UISwipeGestureRecognizer*)sender
{
NSLog(@"swiped :");
NSLog(@"total item is %ld ",(long)DataArray.count);
NSInteger index = currentIndex;
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
index++;
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
index--;
}
if (index > 0 || index < ([DataArray count] - 1))
{
if (index== DataArray.count) {
index=0;
}
currentIndex = index;
[self showImageAtIndex:currentIndex];
}
else
{
if (index < 0) {
index=DataArray.count-1;
currentIndex = index;
[self showImageAtIndex:currentIndex];
}
currentIndex = index;
[self showImageAtIndex:currentIndex];
NSLog(@"Reached the end, swipe in opposite direction");
}
}
我希望此代码适合您