Horizo​​ntaly以自定义分页为中心的UICollectionView单元格

时间:2016-07-27 13:55:08

标签: ios objective-c uicollectionview uicollectionviewlayout

我目前正在构建一个必须以水平集合视图的形式呈现Twitter Feed的应用。

3个推文(单元格)应始终在屏幕上可见,并且水平居中,以及两侧的推文(前3个的最后一个元素和下一个3的第一个元素)。

Here is a picture so it's easier to understand

当用户滚动时,它应该滚动3乘3(或者更小,如果实际上有更少的单元格滚动)。自定义分页的排序。

我尝试使用Custom layout并且它可以正常工作并使细胞居中,就像我想要的那样在两侧显示半条推文。但我无法使用我想要的滚动功能(3by3)。并且它不会让我完全显示第一个或最后一个单元格...无论如何,当我有下面的代码时,它只是取消自定义布局行为,但是滚动3乘3 ...

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
float pageWidth = ((self.view.frame.size.width / 4)*3);

float currentOffset = scrollView.contentOffset.x;
float targetOffset = targetContentOffset->x;
float newTargetOffset = 0;

if (targetOffset > currentOffset)
    newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth;
else
    newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth;

if (newTargetOffset < 0)
    newTargetOffset = 0;
else if (newTargetOffset > scrollView.contentSize.width)
    newTargetOffset = scrollView.contentSize.width;

targetContentOffset->x = currentOffset;
[scrollView setContentOffset:CGPointMake(newTargetOffset, 0) animated:YES];
}

所以,伙计们,之前有没有人玩过这个并且可以帮助我完成任务?非常感谢。

1 个答案:

答案 0 :(得分:0)

ViewController.h

#import <UIKit/UIKit.h>
#import "CustomCell.h"
#import "DetailimageViewController.h"

@interface RootViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
   IBOutlet UICollectionView *collectionViewHorizontalVertical;
}

ViewController.m

#import "RootViewController.h"
@interface RootViewController ()
{

  NSMutableArray *arrayImages;
  DetailimageViewController *detailImageVC ;
}

@end

@implementation RootViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view from its nib.
  arrayImages = [[NSMutableArray alloc]initWithObjects:@"iMove.jpg",@"iPad.jpg",@"iPhone.jpg",@"iPod.jpg",@"iRing.jpg",@"iTV.jpg",@"iwatch.jpeg",nil];
  UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;

  UINib *cellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
  [collectionViewHorizontalVertical registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
}

//UICollectionView DataSource and Delegate Methods

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

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  return arrayImages.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *cellIdentifier = @"cvCell";
  CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
  cell.imgViewCollection.image = [UIImage imageNamed:[arrayImages objectAtIndex:indexPath.row]];

  return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"The indexPath is - %ld",(long)indexPath.item);
  int ind = (int)indexPath.item;
  detailImageVC = [[DetailimageViewController alloc]initWithNibName:@"DetailimageViewController" bundle:nil];
  detailImageVC.arrayImagesCount = arrayImages;
  detailImageVC.intSelectedIndex = ind;
  [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d",detailImageVC.intSelectedIndex] forKey:@"SelectedCollectionViewID"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  [self.navigationController pushViewController:detailImageVC animated:YES];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{
   return CGSizeMake(200, 200);
}
@end

在上面的编码中我有7张图像。所以你需要让你的图像在7以上。这是你的愿望。

然后是DetailimageViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>


@interface DetailimageViewController : UIViewController<UIScrollViewDelegate>
{

}

@property (strong, nonatomic) NSMutableArray *arrayImagesCount;
@property (strong, nonatomic) IBOutlet UIScrollView *scroll;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) IBOutlet UIImageView *imageviewScrollView
@property (assign, nonatomic) NSInteger seletedIndex;
@property (strong, nonatomic) UIImage *imageSelection;
@property (nonatomic, assign) int intSelectedIndex;

@property (nonatomic, strong) NSArray *pageImages;
@property (nonatomic, assign) CGFloat lastContentOffset;

- (IBAction)actionBack:(id)sender;

@end

DetailimageViewController.m

#import "DetailimageViewController.h"

@interface DetailimageViewController ()
{
  UIImageView *subimg;
}

@end

@implementation DetailimageViewController

typedef enum ScrollDirection 
{
  ScrollDirectionNone,
  ScrollDirectionRight,
  ScrollDirectionLeft,
  ScrollDirectionUp,
  ScrollDirectionDown,
  ScrollDirectionCrazy,
} ScrollDirection;


@synthesize arrayImagesCount;


@synthesize scroll;
@synthesize seletedIndex;
@synthesize imageSelection;
@synthesize intSelectedIndex;

@synthesize pageImages = _pageImages;



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

   scroll.layer.cornerRadius=8.0f;
   scroll.layer.masksToBounds=YES;
   scroll.layer.borderColor=[[UIColor redColor]CGColor];
   scroll.layer.borderWidth= 1.0f;
   NSInteger pageCount = arrayImagesCount.count;
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   int intCollectionViewIndex = [[defaults valueForKey:@"SelectedCollectionViewID"]intValue];
   if(intSelectedIndex == intCollectionViewIndex)
   {
      for (int i=intSelectedIndex; i<[arrayImagesCount count]; i++)
      {
         CGRect frame;
         if(i == intCollectionViewIndex)
         {
           frame.origin.x = self.scroll.frame.size.width *intCollectionViewIndex;
           frame.origin.y=0;
           frame.size=self.scroll.frame.size;
           subimg=[[UIImageView alloc]initWithFrame:CGRectMake(self.scroll.frame.size.width *intCollectionViewIndex,0,self.scroll.frame.size.width,self.scroll.frame.size.height)];
           subimg.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayImagesCount objectAtIndex:i]]];
           [self.scroll  addSubview:subimg];
           [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width*i,-20) animated:YES];
           self.scroll.contentSize = CGSizeMake(self.scroll.frame.size.width*arrayImagesCount.count, self.scroll.frame.size.height);
         }
         else{
         }
      }
   }
 }

ScrollView委派方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   NSLog(@" Offset = %@ ",NSStringFromCGPoint(scrollView.contentOffset));
   ScrollDirection scrollDirection;
   if (self.lastContentOffset > scrollView.contentOffset.x)
   {
      scrollDirection = ScrollDirectionRight;
      NSLog(@"The scrollview is scrolling right side");
      for(int j=intSelectedIndex;j<[arrayImagesCount count];j--)
      {
        CGRect frameRight;
        frameRight.origin.x = self.scroll.frame.size.width *j;
        frameRight.origin.y=0;
        frameRight.size=self.scroll.frame.size;
        subimg=[[UIImageView alloc]initWithFrame:CGRectMake(self.scroll.frame.size.width *j,0,self.scroll.frame.size.width,self.scroll.frame.size.height)];
        subimg.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayImagesCount objectAtIndex:j]]];
        [self.scroll  addSubview:subimg];
        self.scroll.contentSize = CGSizeMake(self.scroll.frame.size.width*arrayImagesCount.count, self.scroll.frame.size.height);
       }
     }
     else if (self.lastContentOffset < scrollView.contentOffset.x)
     {
       scrollDirection = ScrollDirectionLeft;
       NSLog(@"The scrollview is scrolling left side");
       for(int k=intSelectedIndex;k<[arrayImagesCount count];k++)
       {
          CGRect frameLeft;
          frameLeft.origin.x = self.scroll.frame.size.width *k;
          frameLeft.origin.y=0;
          frameLeft.size=self.scroll.frame.size;
          subimg=[[UIImageView alloc]initWithFrame:CGRectMake(self.scroll.frame.size.width *k,0,self.scroll.frame.size.width,self.scroll.frame.size.height)];
          subimg.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayImagesCount objectAtIndex:k]]];
         [self.scroll  addSubview:subimg];
          self.scroll.contentSize = CGSizeMake(self.scroll.frame.size.width*arrayImagesCount.count, self.scroll.frame.size.height);
         }
     }
    self.lastContentOffset = scrollView.contentOffset.x;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
   CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];
   NSLog(@"The translation.x is - %f",translation.x);
   NSLog(@"The scrollview content off set is - %f",scrollView.contentOffset.x);
    if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x > 0) {
    // handle dragging to the right
     NSLog(@"It is dragging to right side");
    } else {
    // handle dragging to the left
     NSLog(@"It is dragging to left side");
    }
}

- (IBAction)actionBack:(id)sender
{
  [self.navigationController popViewControllerAnimated:YES];
}
@end