2016-07-27 12 views
0

私は現在、水平コレクションビューの形でツイッターフィードを提示しなければならないアプリケーションを構築しています。カスタムページングを使用した、水平に配置されたUICollectionViewセル

3つのつぶやき(セル)は常に画面上に表示され、左右に中央だけでなく、ハーフツイート(前の3の最後の要素と次の3の最初の要素)の両側に表示する必要があります。それは(actualyある場合より少ないセルがスクロールする左以下)3により3をスクロールする必要があり、ユーザがスクロール

Here is a picture so it's easier to understand

。カスタムページングの並べ替え。

私は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

答えて

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 
関連する問題