swift 版本
import UIKit
class ViewController: UIViewController {
var collectionView:UICollectionView?
var currentIndexPath:IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
let cvLayout = UICollectionViewFlowLayout()
cvLayout.itemSize = view.bounds.size
cvLayout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: cvLayout)
collectionView?.delegate = self
collectionView?.dataSource = self
// collectionView?.isPagingEnabled = true
collectionView?.isScrollEnabled = false
collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "defaultCell")
view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
view.addSubview(collectionView!)
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(leftAction(_:)))
leftSwipe.direction = .left
view.addGestureRecognizer(leftSwipe)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func leftAction(_ sender: UITapGestureRecognizer) {
if currentIndexPath == nil {
currentIndexPath = IndexPath(row: 0, section: 0)
}
if (currentIndexPath?.row)! + 1 >= (collectionView?.numberOfItems(inSection: currentIndexPath!.section))! {// 控制滑到最右边
return
}
currentIndexPath = IndexPath(row: (currentIndexPath?.row)! + 1, section: (currentIndexPath?.section)!)
collectionView?.scrollToItem(at: currentIndexPath!, at: .right, animated: true)
}
}
extension ViewController: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "defaultCell", for: indexPath)
let red = CGFloat(arc4random() % 256) / 255.0
let green = CGFloat(arc4random() % 256) / 255.0
let blue = CGFloat(arc4random() % 256) / 255.0
// print("\(red) - \(green) - \(blue)")
cell.backgroundColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1.0)
return cell
}
}
OC版本
#import "ViewController.h"
@interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSIndexPath *currentIndexPath;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftAction:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
}
- (void)leftAction:(UISwipeGestureRecognizer *)sender {
if (self.currentIndexPath == nil) {
self.currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:0 ];
}
if (self.currentIndexPath.item + 1 >= [self.collectionView numberOfItemsInSection:self.currentIndexPath.section]) { // 控制滑到最右边
return;
}
self.currentIndexPath = [NSIndexPath indexPathForItem:self.currentIndexPath.row + 1 inSection:self.currentIndexPath.section];
[self.collectionView scrollToItemAtIndexPath:self.currentIndexPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
}
#pragma mark - collectionView
- (UICollectionView *)collectionView
{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = self.view.bounds.size;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.scrollEnabled = NO;
// [_collectionView registerNib:[UINib nibWithNibName:@"" bundle:nil] forCellWithReuseIdentifier:@""];
[_collectionView registerClass:UICollectionViewCell.self forCellWithReuseIdentifier:@"cell"];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:_collectionView];
}
return _collectionView;
}
#pragma mark collectionViewDataSource - UICollectionViewDelegateFlowLayout
/* 个数 **/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
/* 选中 **/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
/* cell设置 **/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:(arc4random() % 256) /255.0 green:(arc4random() % 256) /255.0 blue:(arc4random() % 256) /255.0 alpha:1];
return cell;
}