larryryu / LSSwipeToDeleteCollectionViewLayout

The UICollectionViewLayout subclass adds swipe to delete functionality to a collectionview
MIT License
41 stars 4 forks source link

Collectionview Cell located at center - UICollectionViewScrollDirectionHorizontal #11

Open manisolutions15 opened 6 years ago

manisolutions15 commented 6 years ago

thanks for LSSWipeToDeleteCollectionViewLayout,

I am using UIEdgeInsetsMake to all collection cell bottom and scrolling horizontal, It will work iOS 10 and iOS 11.

iOS 9.0 cell will move to center after if cell size differ to one another.

img_0192

larryryu commented 6 years ago

I'm not sure I understand the issue. Code you please provide some sample code?

manisolutions15 commented 6 years ago

It only happen iOS 8 and iOS 9, Check below code specially sizeForItemAtIndexPath and insetForSectionAtIndex, We have to check "layoutAttributesForElementsInRect" LSSwipeToDeleteCollectionViewLayout.m bcs it will get wrong rect after update different cell size


//
//  SwipeToDeleteCollectionViewController.m
//  LSSwipeToDelete
//
//  Created by Lukman Sanusi on 1/9/14.
//  Copyright (c) 2014 Lukman Sanusi. All rights reserved.
//

#import "SwipeToDeleteCollectionViewController.h"

static NSString *LSCollectionViewCellIdentifier = @"Cell";

@interface SwipeToDeleteCollectionViewController () <UICollectionViewDelegateFlowLayout>
{
    NSMutableArray *colors;
}
@end

@implementation SwipeToDeleteCollectionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:LSCollectionViewCellIdentifier];
    [self.collectionView setAlwaysBounceVertical:YES];

    LSSwipeToDeleteCollectionViewLayout *layout = (LSSwipeToDeleteCollectionViewLayout *)self.collectionView.collectionViewLayout;
    [layout setSwipeToDeleteDelegate:self];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //flowlayout.itemSize = CGSizeMake(cellHeight*0.7, cellHeight);
    layout.swipeToDeleteDirection = LSSwipeToDeleteDirectionMin;
    layout.swipeToDeleteDelegate = self;
    layout.deletionDistanceTresholdValue = 100;
    layout.deletionVelocityTresholdValue = 100.0f;
    [self resetColors];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self becomeFirstResponder];
}
- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self resignFirstResponder];
}

-(void)resetColors{
    colors = @[[UIColor redColor], [UIColor purpleColor], [UIColor orangeColor], [UIColor greenColor], [UIColor redColor], [UIColor blueColor], [UIColor orangeColor], [UIColor greenColor], [UIColor redColor], [UIColor blueColor], [UIColor orangeColor], [UIColor greenColor]].mutableCopy;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if ( event.subtype == UIEventSubtypeMotionShake )
    {
        [self resetColors];
        [self.collectionView reloadData];
    }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
        [super motionEnded:motion withEvent:event];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:**(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    NSLog(@"Edge Insert");
    return UIEdgeInsetsMake(400 , 0, 0, 0);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return colors.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LSCollectionViewCellIdentifier forIndexPath:indexPath];

    [cell setBackgroundColor:colors[indexPath.row]];

    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item % 2)
    {
        return CGSizeMake(170.0f, 100.0f);
    }
    return CGSizeMake(70.0f, 100.0f);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10.0f;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10.0f;   
}

#pragma mark - LSSwipeToDeleteLayoutDelegate

-(void)swipeToDeleteLayout:(LSSwipeToDeleteCollectionViewLayout *)layout didDeleteCellAtIndexPath:(NSIndexPath *)indexPath{
    [colors removeObjectAtIndex:indexPath.row];
}