Skip to content

Instantly share code, notes, and snippets.

@irekasoft
Created March 5, 2016 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irekasoft/15051a2ab34c86466d05 to your computer and use it in GitHub Desktop.
Save irekasoft/15051a2ab34c86466d05 to your computer and use it in GitHub Desktop.
Collection View with draggable cell. 5px padding each.
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>{
CGSize cellSize;
}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@end
//
// Created by Hijazi on 5/3/16.
// Copyright © 2016 iReka Soft. All rights reserved.
//
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
[self.collectionView addGestureRecognizer:longPressGesture];
}
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)gesture{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:{
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}break;
case UIGestureRecognizerStateChanged:{
[self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:gesture.view]];
}break;
case UIGestureRecognizerStateEnded:{
[self.collectionView endInteractiveMovement];
}break;
break;
default:
[self.collectionView cancelInteractiveMovement];
break;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
// 0.6213235294
[self updateCellSize];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 10;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImageView *iv_profilePicture = [cell.contentView viewWithTag:111];
iv_profilePicture.layer.cornerRadius = cellSize.width/2.0;
iv_profilePicture.transform = CGAffineTransformMakeScale(0.6, 0.6);
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)updateCellSize{
CGFloat margin = 5.0*2.0;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
float cellWidth = self.collectionView.bounds.size.width/3.0-margin;
cellSize = CGSizeMake(cellWidth, cellWidth*1);
}else{
float cellWidth = self.collectionView.bounds.size.width/4.0 - margin;
cellSize = CGSizeMake(cellWidth, cellWidth*1);
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return cellSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 5;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(5, 10, 5, 10);
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment