0

현재 가로 수집보기의 형태로 트위터 피드를 제공해야하는 앱을 제작하고 있습니다.사용자 정의 페이징을 사용하는 수평 중심의 UICollectionView 셀

3 개의 짹짹 (셀)은 화면에서 항상 볼 수 있어야하고 가로형 중앙뿐만 아니라 하프 짹짹 (이전 3의 마지막 요소와 다음 3의 첫 번째 요소)이 양쪽에 있어야합니다.

Here is a picture so it's easier to understand

사용자가 스크롤이 3 3 스크롤한다

(actualy 적은 셀이 있는지 이하인 스크롤 왼쪽). 사용자 정의 페이징의 정렬.

나는 Custom layout을 사용해 보았고 셀이 중앙에 오도록 만들어서 원하는대로 양쪽에 절반 트윗을 표시했습니다. 그러나 나는 그것이 원하는 스크롤 (3by3)으로 작동하게 만들 수 없다. 그리고 ... 제가 다음 코드를했을 때 어쨌든, 그냥 사용자 지정 레이아웃 동작을 취소하지만, 스크롤 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

답변

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 위의 이미지를 가질 필요가 또는 less.It 당신의 소원 7 images.So 있습니다.

그런 다음 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