UICollectionView分页和滑动

时间:2015-04-01 06:25:14

标签: ios objective-c

我正在查看滑动是否可以使用四个方向。

我可以获得一些示例代码吗? 喜欢这个应用程序:LINK

或任何想法?

我正在使用 UICollectionview

1 个答案:

答案 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");

}

}

我希望此代码适合您