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

问题描述:

我目前正在构建一个必须以水平集合视图的形式呈现Twitter Feed的应用程序。以自定义分页为中心的Horizo​​ntaly UICollectionView单元格

3个推文(单元格)应该始终在屏幕上显示,并且横向居中以及两边的推文(前3个元素和下3个元素的前一个元素的最后一个元素)。

Here is a picture so it's easier to understand

当用户滚动应该滚动由3 3(或更少,如果有少actualy细胞左滚动)。自定义分页的排序。

我尝试使用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]; 
} 

所以伙计们,有没有人玩过这个,并可以帮助我完成任务?谢谢一堆。

+0

我根据你的问题有示例代码。 – user3182143

+0

无论如何,我现在发布代码。 – user3182143

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 images.So你需要有上述7你的图像或less.It是你的愿望。

然后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{ 
     } 
     } 
    } 
} 

滚动型的委托方法

- (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