├── supplement.png ├── SPEasyCollectionView ├── IMG_2468.JPG ├── EasyCell.h ├── TestCell.h ├── SPEasyCollectionView │ ├── SPBaseReusableView.m │ ├── SPBaseCell.h │ ├── SPBaseReusableView.h │ ├── Easytools │ │ ├── EasyTools.h │ │ └── EasyTools.m │ ├── SPBaseCell.m │ ├── SPEasyCollectionView.h │ └── SPEasyCollectionView.m ├── ViewController.h ├── AppDelegate.h ├── main.m ├── EasyCell.m ├── TestCell.m ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── AppDelegate.m ├── ViewController.m └── EasyCell.xib ├── SPEasyCollectionView.xcodeproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── project.pbxproj ├── .gitignore └── README.md /supplement.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tr2e/SPEasyCollectionView/HEAD/supplement.png -------------------------------------------------------------------------------- /SPEasyCollectionView/IMG_2468.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tr2e/SPEasyCollectionView/HEAD/SPEasyCollectionView/IMG_2468.JPG -------------------------------------------------------------------------------- /SPEasyCollectionView.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SPEasyCollectionView/EasyCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // EasyCell.h 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "SPBaseCell.h" 10 | 11 | @interface EasyCell : SPBaseCell 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /SPEasyCollectionView/TestCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestCell.h 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/7. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "SPBaseCell.h" 10 | 11 | @interface TestCell : SPBaseCell 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /SPEasyCollectionView/SPEasyCollectionView/SPBaseReusableView.m: -------------------------------------------------------------------------------- 1 | // 2 | // SPBaseReusableView.m 3 | // UMY 4 | // 5 | // Created by Tree on 2017/7/24. 6 | // Copyright © 2017年 UNI. All rights reserved. 7 | // 8 | 9 | #import "SPBaseReusableView.h" 10 | 11 | @implementation SPBaseReusableView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /SPEasyCollectionView/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /SPEasyCollectionView/SPEasyCollectionView/SPBaseCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // SPBaseCell.h 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SPBaseCell : UICollectionViewCell 12 | @property (nonatomic, strong) id data; 13 | @end 14 | -------------------------------------------------------------------------------- /SPEasyCollectionView/SPEasyCollectionView/SPBaseReusableView.h: -------------------------------------------------------------------------------- 1 | // 2 | // SPBaseReusableView.h 3 | // UMY 4 | // 5 | // Created by Tree on 2017/7/24. 6 | // Copyright © 2017年 UNI. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SPBaseReusableView : UICollectionReusableView 12 | @property (nonatomic, strong) id datas; 13 | @end 14 | -------------------------------------------------------------------------------- /SPEasyCollectionView/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /SPEasyCollectionView/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /SPEasyCollectionView/EasyCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // EasyCell.m 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "EasyCell.h" 10 | 11 | @implementation EasyCell 12 | 13 | /* 14 | // Only override drawRect: if you perform custom drawing. 15 | // An empty implementation adversely affects performance during animation. 16 | - (void)drawRect:(CGRect)rect { 17 | // Drawing code 18 | } 19 | */ 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /SPEasyCollectionView/SPEasyCollectionView/Easytools/EasyTools.h: -------------------------------------------------------------------------------- 1 | // 2 | // EasyTools.h 3 | // SPNetworking 4 | // 5 | // Created by Tree on 2016/11/29. 6 | // Copyright © 2016年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface EasyTools : NSObject 13 | 14 | 15 | id loadViewFromXib(id classType,id owner); 16 | id loadViewController(id classType,id owner); 17 | 18 | /** 19 | * 加载Xib - xibName(string),owner 20 | */ 21 | id loadViewFromXibWithName(NSString *xibName,id owner); 22 | /** 23 | * 加载ViewController - storyBoardName(string),identifier(string) 24 | */ 25 | id loadViewControllerFromStoryboard(NSString *name,NSString *identifier); 26 | 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /SPEasyCollectionView/SPEasyCollectionView/SPBaseCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // SPBaseCell.m 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "SPBaseCell.h" 10 | 11 | @interface SPBaseCell() 12 | @property (nonatomic, strong) UIView *maskView; 13 | @end 14 | 15 | @implementation SPBaseCell 16 | 17 | - (void)setSelected:(BOOL)selected{ 18 | [super setSelected:selected]; 19 | if (selected) { 20 | self.maskView.hidden = NO; 21 | [self addSubview:self.maskView]; 22 | [self bringSubviewToFront:_maskView]; 23 | }else{ 24 | _maskView.hidden = YES; 25 | [_maskView removeFromSuperview]; 26 | _maskView = nil; 27 | } 28 | } 29 | 30 | - (UIView *)maskView{ 31 | if (_maskView == nil) { 32 | UIView *maskView = [[UIView alloc] initWithFrame:self.bounds]; 33 | maskView.backgroundColor = [UIColor whiteColor]; 34 | maskView.alpha = 0.4f; 35 | maskView.hidden = YES; 36 | _maskView = maskView; 37 | } 38 | return _maskView; 39 | } 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /SPEasyCollectionView/TestCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestCell.m 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/7. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "TestCell.h" 10 | 11 | @interface TestCell() 12 | @property (nonatomic, weak)UILabel *testLabel; 13 | @end 14 | 15 | @implementation TestCell 16 | 17 | 18 | - (instancetype)initWithFrame:(CGRect)frame{ 19 | if (self == [super initWithFrame:frame]) { 20 | [self setupUI]; 21 | } 22 | return self; 23 | } 24 | 25 | - (void)layoutSubviews{ 26 | [super layoutSubviews]; 27 | 28 | } 29 | 30 | - (void)setupUI{ 31 | 32 | self.backgroundColor = [UIColor lightGrayColor]; 33 | UILabel *numLabel = [[UILabel alloc] init]; 34 | numLabel.textColor = [UIColor whiteColor]; 35 | numLabel.contentMode = UIViewContentModeCenter; 36 | numLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold]; 37 | 38 | [self.contentView addSubview:numLabel]; 39 | self.testLabel = numLabel; 40 | 41 | } 42 | 43 | - (void)setData:(id)data{ 44 | [super setData:data]; 45 | 46 | _testLabel.text = (NSString *)data; 47 | [_testLabel sizeToFit]; 48 | _testLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); 49 | 50 | } 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /SPEasyCollectionView/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xcuserstate 23 | 24 | ## Obj-C/Swift specific 25 | *.hmap 26 | *.ipa 27 | *.dSYM.zip 28 | *.dSYM 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | 55 | #Code Injection 56 | # 57 | # After new code Injection tools there's a generated folder /iOSInjectionProject 58 | # https://github.com/johnno1962/injectionforxcode 59 | 60 | iOSInjectionProject/ 61 | -------------------------------------------------------------------------------- /SPEasyCollectionView/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /SPEasyCollectionView/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /SPEasyCollectionView/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /SPEasyCollectionView/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "SPEasyCollectionView.h" 11 | 12 | 13 | @interface ViewController () 14 | @property (nonatomic, weak) SPEasyCollectionView *collectionView; 15 | @property (weak, nonatomic) IBOutlet SPEasyCollectionView *storyboardTest; 16 | @end 17 | 18 | @implementation ViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | 23 | // 代码创建 24 | SPEasyCollectionView *easyView = [[SPEasyCollectionView alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 200)]; 25 | easyView.delegate = self; 26 | easyView.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 200); 27 | easyView.scrollDirection = SPEasyScrollDirectionHorizontal; 28 | easyView.xibName = @"EasyCell"; 29 | easyView.needAutoScroll = YES; 30 | easyView.datas = @[@"1",@"2",@"3",@"4"]; 31 | easyView.minLineSpace = 0;//务必设置此参数,否则会造成轮播后期偏移误差 32 | [self.view addSubview:easyView]; 33 | 34 | // storyboard 35 | _storyboardTest.selectIndex = ^(NSInteger index) {// 点击位置 36 | 37 | }; 38 | 39 | NSMutableArray *datasM = [NSMutableArray arrayWithCapacity:20]; 40 | for (NSInteger i = 0; i < 40; i++) { 41 | [datasM addObject:[NSString stringWithFormat:@"%ld",i]]; 42 | } 43 | _storyboardTest.canEdit = YES; 44 | 45 | // chain calls 46 | _storyboardTest.sp_cellClassName(^NSString *{ 47 | return @"TestCell"; 48 | }).sp_itemsize(^CGSize{ 49 | return CGSizeMake(100, 100); 50 | }).sp_minLineSpace(^NSInteger{ 51 | return 20; 52 | }).sp_minInterItemSpace(^NSInteger{ 53 | return 10; 54 | }).sp_scollDirection(^SPEasyScrollDirection{ 55 | return SPEasyScrollDirectionVertical; 56 | }).sp_inset(^UIEdgeInsets{ 57 | return UIEdgeInsetsMake(20, 20, 20, 20); 58 | }).sp_backgroundColor(^UIColor *{ 59 | return [UIColor colorWithRed:173/255.0 green:216/255.0 blue:230/255.0 alpha:1]; 60 | });//LightBLue #ADD8E6 173,216,230 61 | 62 | 63 | _storyboardTest.datas = [datasM copy]; 64 | 65 | } 66 | 67 | 68 | // 点击位置 69 | - (void)easyCollectionView:(UICollectionView *)collectionView didSelectItemAtIndex:(NSInteger)index{ 70 | 71 | 72 | } 73 | 74 | 75 | - (void)didReceiveMemoryWarning { 76 | [super didReceiveMemoryWarning]; 77 | // Dispose of any resources that can be recreated. 78 | } 79 | 80 | 81 | @end 82 | -------------------------------------------------------------------------------- /SPEasyCollectionView/SPEasyCollectionView/Easytools/EasyTools.m: -------------------------------------------------------------------------------- 1 | // 2 | // EasyTools.m 3 | // SPNetworking 4 | // 5 | // Created by Tree on 2016/11/29. 6 | // Copyright © 2016年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "EasyTools.h" 10 | 11 | @implementation EasyTools 12 | 13 | id loadViewControllerFromXib(NSBundle *bundle,NSString *xibName,Class className,id owner,NSString *restorationIdentifier) 14 | { 15 | UINib *nib = [UINib nibWithNibName:xibName bundle:bundle]; 16 | 17 | NSArray *list; 18 | @try { 19 | list = [nib instantiateWithOwner:owner options:nil]; 20 | } 21 | @catch (NSException *exception) { 22 | assert(NO); 23 | } 24 | @finally { 25 | 26 | } 27 | 28 | for(UIViewController *vc in list) 29 | { 30 | if([vc isMemberOfClass:className] && (restorationIdentifier == nil || [vc.restorationIdentifier isEqualToString:restorationIdentifier]) ) 31 | return vc; 32 | } 33 | return nil; 34 | 35 | } 36 | 37 | id loadViewControllerWithIdentifier(id classType, id owner, NSString *identifier) 38 | { 39 | NSString * className = [classType description]; 40 | 41 | Class c = NSClassFromString(className); 42 | 43 | if (c == nil) { 44 | return nil; 45 | } 46 | return loadViewControllerFromXib(nil, className, c, owner, identifier); 47 | } 48 | 49 | id loadViewController(id classType,id owner) 50 | { 51 | 52 | return loadViewControllerWithIdentifier(classType,owner,nil); 53 | } 54 | 55 | id loadViewFromXibWithName(NSString *xibName,id owner) 56 | { 57 | NSArray *objects = [[NSBundle mainBundle] loadNibNamed:xibName owner:owner options:nil]; 58 | 59 | for (NSObject *objec in objects) { 60 | if ([objec isKindOfClass:[UIView class]]) { 61 | return objec; 62 | } 63 | } 64 | return nil; 65 | } 66 | 67 | id loadViewFromXib(id classType,id owner) 68 | { 69 | NSString *className = NSStringFromClass(classType); 70 | 71 | return [[[NSBundle mainBundle] loadNibNamed:className owner:owner options:nil] lastObject]; 72 | // return loadViewControllerWithIdentifier(classType, owner, nil); 73 | } 74 | 75 | id loadViewControllerFromStoryboard(NSString *name,NSString *identifier) 76 | { 77 | UIStoryboard *storyboard = [UIStoryboard storyboardWithName:name bundle:nil]; 78 | UIViewController *vc = nil; 79 | 80 | if (identifier == nil) 81 | { 82 | vc = [storyboard instantiateInitialViewController]; 83 | } 84 | else 85 | { 86 | vc = [storyboard instantiateViewControllerWithIdentifier:identifier]; 87 | } 88 | 89 | if ([vc isKindOfClass:[UIViewController class]]) { 90 | return vc; 91 | } 92 | return nil; 93 | } 94 | @end 95 | -------------------------------------------------------------------------------- /SPEasyCollectionView/EasyCell.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # SPEasyCollectionView 3 | 1.方便快速的构建`UICollectionView`,告别繁琐而丑陋的数据源、代理方法 4 | 5 | 2.支持链式传参 6 | 7 | 3.支持轮播 8 | 9 | 4.支持长按重排 10 | 11 | ![iOS8x-.gif](http://upload-images.jianshu.io/upload_images/1742463-4601a1c424019561.gif?imageMogr2/auto-orient/strip) 12 | ![cycle_pic.gif](http://upload-images.jianshu.io/upload_images/1742463-c85b0fdeb9160592.gif?imageMogr2/auto-orient/strip) 13 | 14 | 使用示例: 15 | 16 | 链式 17 | ``` 18 | // 基本样式 19 |    CGRect contentframe = self.centerContentView.bounds; 20 | CGFloat itemWidth = (contentframe.size.height - 3*itemMargin)/4.0; 21 | CGSize itemSize = CGSizeMake(itemWidth, itemWidth); 22 | 23 | SPEasyCollectionView *brandSelect = [[SPEasyCollectionView alloc] initWithFrame:self.centerContentView.bounds]; 24 | brandSelect.sp_inset(^UIEdgeInsets{ 25 | return UIEdgeInsetsMake(0, 15, 0, 15); 26 | }).sp_xibName(^NSString *{ 27 | return @"BrandSelectCell"; 28 | }).sp_delegate(^id{ 29 | return ws; 30 | }).sp_itemsize(^CGSize{ 31 | return itemSize; 32 | }).sp_minLineSpace(^NSInteger{ 33 | return itemMargin; 34 | }).sp_minInterItemSpace(^NSInteger{ 35 | return itemMargin; 36 | }).sp_scollDirection(^SPEasyScrollDirection{ 37 | return SPEasyScrollDirectionHorizontal; 38 | }).sp_backgroundColor(^UIColor *{ 39 | return [UIColor clearColor]; 40 | }); 41 | 42 | brandSelect.datas = datas; 43 | brandSelect.alpha = 0; 44 | 45 | [self.centerContentView addSubview:brandSelect]; 46 | self.brandSelectView = brandSelect; 47 | ``` 48 | 普通 49 | ``` 50 |    // 代码创建 轮播 51 |    SPEasyCollectionView *easyView = [[SPEasyCollectionView alloc] 52 | initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 200)]; 53 | easyView.delegate = self; 54 | easyView.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 200); 55 | easyView.scrollDirection = SPEasyScrollDirectionHorizontal; 56 | easyView.xibName = @"EasyCell"; 57 | easyView.needAutoScroll = YES; 58 | easyView.datas = @[@"1",@"2",@"3",@"4"]; 59 | easyView.minLineSpace = 0;//务必设置此参数,否则会造成轮播后期偏移误差 60 | [self.view addSubview:easyView]; 61 | ``` 62 | 63 | ### 特别需要注意的是 因为将reload方法绑定在Setdata方法中执行而且storyboard特殊的执行顺序,所以希望务必将`xx.datas = @[]`放在配置参数最后调用 64 | 65 | ### 如果使用这个封装的话,请将你的CollectionViewCell或者你的ReuseView 都继承自`SPBase` 66 | 67 | ### 20170724:添加CollectionView中`SupplementaryView`的支持 更新具体Api如下 68 | ``` 69 | // Register Header 70 | @property (nonatomic, copy) NSString *headerXibName; 71 | @property (nonatomic, copy) NSString *headerClassName; 72 | // Register Footer 73 | @property (nonatomic, copy) NSString *footerXibName; 74 | @property (nonatomic, copy) NSString *footerClassName; 75 | // Header Size 76 | @property (nonatomic, assign) CGSize headerSize; 77 | // Footer Size 78 | @property (nonatomic, assign) CGSize footerSize; 79 | ``` 80 | ![SupplementaryView](https://github.com/Tr2e/SPEasyCollectionView/raw/master/supplement.png) 81 | -------------------------------------------------------------------------------- /SPEasyCollectionView/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /SPEasyCollectionView/SPEasyCollectionView/SPEasyCollectionView.h: -------------------------------------------------------------------------------- 1 | // 2 | // SPEasyCollectionView.h 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @class SPBaseCell; 12 | @class SPEasyCollectionView; 13 | 14 | 15 | typedef NS_ENUM(NSInteger,SPEasyScrollDirection) { 16 | 17 | SPEasyScrollDirectionVertical, 18 | SPEasyScrollDirectionHorizontal 19 | 20 | }; 21 | 22 | typedef void(^SPEasyCollectionSelect)(NSInteger index); 23 | 24 | // chain calls 25 | typedef SPEasyCollectionView *(^SPEasyCollectionViewItemSize)(CGSize(^)(void)); 26 | typedef SPEasyCollectionView *(^SPEasyCollectionViewHeaderSize)(CGSize(^)(void)); 27 | typedef SPEasyCollectionView *(^SPEasyCollectionViewFooterSize)(CGSize(^)(void)); 28 | typedef SPEasyCollectionView *(^SPEasyCollectionViewinset)(UIEdgeInsets(^)(void)); 29 | typedef SPEasyCollectionView *(^SPEasyCollectionViewMinLineSpace)(NSInteger(^)(void)); 30 | typedef SPEasyCollectionView *(^SPEasyCollectionViewMinInterItemSpace)(NSInteger(^)(void)); 31 | typedef SPEasyCollectionView *(^SPEasyCollectionViewScrollDirection)(SPEasyScrollDirection(^)(void)); 32 | typedef SPEasyCollectionView *(^SPEasyCollectionViewDelegate)(id(^)(void)); 33 | typedef SPEasyCollectionView *(^SPEasyCollectionViewCellXibName)(NSString *(^)(void)); 34 | typedef SPEasyCollectionView *(^SPEasyCollectionViewCellClassName)(NSString *(^)(void)); 35 | typedef SPEasyCollectionView *(^SPEasyCollectionViewHeaderXibName)(NSString *(^)(void)); 36 | typedef SPEasyCollectionView *(^SPEasyCollectionViewHeaderClassName)(NSString *(^)(void)); 37 | typedef SPEasyCollectionView *(^SPEasyCollectionViewFooterXibName)(NSString *(^)(void)); 38 | typedef SPEasyCollectionView *(^SPEasyCollectionViewFooterClassName)(NSString *(^)(void)); 39 | typedef SPEasyCollectionView *(^SPEasyCollectionViewBackgroundColor)(UIColor *(^)(void)); 40 | 41 | 42 | @protocol SPEasyCollectionViewDelegate 43 | @optional 44 | 45 | - (void)easyCollectionView:(UICollectionView *)collectionView didSelectItemAtIndex:(NSInteger )index; 46 | 47 | @end 48 | 49 | 50 | @interface SPEasyCollectionView : UIView 51 | 52 | @property (nonatomic, strong) UICollectionView *collectionView; 53 | 54 | // Timer 55 | @property (nonatomic, assign) BOOL needAutoScroll; 56 | @property (nonatomic, assign) NSTimeInterval timerInterval; 57 | // Register cell 58 | @property (nonatomic, copy) NSString *xibName; 59 | @property (nonatomic, copy) NSString *cellClassName; 60 | // Register Header 61 | @property (nonatomic, copy) NSString *headerXibName; 62 | @property (nonatomic, copy) NSString *headerClassName; 63 | // Register Footer 64 | @property (nonatomic, copy) NSString *footerXibName; 65 | @property (nonatomic, copy) NSString *footerClassName; 66 | // Header Size 67 | @property (nonatomic, assign) CGSize headerSize; 68 | // Footer Size 69 | @property (nonatomic, assign) CGSize footerSize; 70 | // Basic settings 71 | @property (nonatomic, assign) BOOL bounces; 72 | @property (nonatomic, assign) BOOL pageEnabled; 73 | // Appearance Settings 74 | @property (nonatomic, assign) CGSize itemSize; 75 | @property (nonatomic, assign) UIEdgeInsets inset; 76 | @property (nonatomic, assign) NSInteger minLineSpace; 77 | @property (nonatomic, strong) UIColor *backgroundColor; 78 | @property (nonatomic, assign) NSInteger minInterItemSpace; 79 | @property (nonatomic, assign) SPEasyScrollDirection scrollDirection; 80 | 81 | // Edit 82 | @property (nonatomic, assign) BOOL canEdit; 83 | @property (nonatomic, assign) NSTimeInterval activeEditingModeTimeInterval; 84 | 85 | // chain calls 86 | @property (nonatomic, readonly) SPEasyCollectionViewinset sp_inset; 87 | @property (nonatomic, readonly) SPEasyCollectionViewItemSize sp_itemsize; 88 | @property (nonatomic, readonly) SPEasyCollectionViewHeaderSize sp_headersize; 89 | @property (nonatomic, readonly) SPEasyCollectionViewFooterSize sp_footersize; 90 | @property (nonatomic, readonly) SPEasyCollectionViewMinLineSpace sp_minLineSpace; 91 | @property (nonatomic, readonly) SPEasyCollectionViewScrollDirection sp_scollDirection; 92 | @property (nonatomic, readonly) SPEasyCollectionViewMinInterItemSpace sp_minInterItemSpace; 93 | @property (nonatomic, readonly) SPEasyCollectionViewDelegate sp_delegate; 94 | @property (nonatomic, readonly) SPEasyCollectionViewCellXibName sp_xibName; 95 | @property (nonatomic, readonly) SPEasyCollectionViewCellClassName sp_cellClassName; 96 | @property (nonatomic, readonly) SPEasyCollectionViewHeaderXibName sp_headerXibName; 97 | @property (nonatomic, readonly) SPEasyCollectionViewHeaderClassName sp_headerClassName; 98 | @property (nonatomic, readonly) SPEasyCollectionViewFooterXibName sp_footerXibName; 99 | @property (nonatomic, readonly) SPEasyCollectionViewFooterClassName sp_footerClassName; 100 | @property (nonatomic, readonly) SPEasyCollectionViewBackgroundColor sp_backgroundColor; 101 | 102 | 103 | @property (nonatomic, copy) NSArray *datas; 104 | @property (nonatomic, copy) NSArray *sectionDatas; 105 | 106 | @property (nonatomic, copy) SPEasyCollectionSelect selectIndex; 107 | @property (nonatomic, weak) id delegate; 108 | 109 | 110 | @end 111 | -------------------------------------------------------------------------------- /SPEasyCollectionView.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 042683CB1F049EF5006613B6 /* IMG_2468.JPG in Resources */ = {isa = PBXBuildFile; fileRef = 042683CA1F049EF5006613B6 /* IMG_2468.JPG */; }; 11 | 044888311E9636C8007EC616 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 044888301E9636C8007EC616 /* main.m */; }; 12 | 044888341E9636C8007EC616 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 044888331E9636C8007EC616 /* AppDelegate.m */; }; 13 | 044888371E9636C8007EC616 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 044888361E9636C8007EC616 /* ViewController.m */; }; 14 | 0448883A1E9636C8007EC616 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 044888381E9636C8007EC616 /* Main.storyboard */; }; 15 | 0448883C1E9636C9007EC616 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0448883B1E9636C9007EC616 /* Assets.xcassets */; }; 16 | 0448883F1E9636C9007EC616 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0448883D1E9636C9007EC616 /* LaunchScreen.storyboard */; }; 17 | 044888491E963706007EC616 /* SPEasyCollectionView.m in Sources */ = {isa = PBXBuildFile; fileRef = 044888481E963706007EC616 /* SPEasyCollectionView.m */; }; 18 | 044888501E963C59007EC616 /* SPBaseCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 0448884F1E963C59007EC616 /* SPBaseCell.m */; }; 19 | 044888521E964EB0007EC616 /* EasyCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 044888511E964EB0007EC616 /* EasyCell.xib */; }; 20 | 044888571E964F47007EC616 /* EasyCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 044888561E964F47007EC616 /* EasyCell.m */; }; 21 | 044BDFA51E974A0700A8F742 /* TestCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 044BDFA41E974A0700A8F742 /* TestCell.m */; }; 22 | 044BDFA91E97614400A8F742 /* EasyTools.m in Sources */ = {isa = PBXBuildFile; fileRef = 044BDFA81E97614400A8F742 /* EasyTools.m */; }; 23 | 045704601F260153007D58BA /* SPBaseReusableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0457045F1F260153007D58BA /* SPBaseReusableView.m */; }; 24 | /* End PBXBuildFile section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | 042683CA1F049EF5006613B6 /* IMG_2468.JPG */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = IMG_2468.JPG; sourceTree = ""; }; 28 | 0448882C1E9636C8007EC616 /* SPEasyCollectionView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SPEasyCollectionView.app; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | 044888301E9636C8007EC616 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 30 | 044888321E9636C8007EC616 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 31 | 044888331E9636C8007EC616 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 32 | 044888351E9636C8007EC616 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 33 | 044888361E9636C8007EC616 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 34 | 044888391E9636C8007EC616 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 35 | 0448883B1E9636C9007EC616 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 36 | 0448883E1E9636C9007EC616 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 37 | 044888401E9636C9007EC616 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 38 | 044888471E963706007EC616 /* SPEasyCollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPEasyCollectionView.h; sourceTree = ""; }; 39 | 044888481E963706007EC616 /* SPEasyCollectionView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPEasyCollectionView.m; sourceTree = ""; }; 40 | 0448884E1E963C59007EC616 /* SPBaseCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPBaseCell.h; sourceTree = ""; }; 41 | 0448884F1E963C59007EC616 /* SPBaseCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPBaseCell.m; sourceTree = ""; }; 42 | 044888511E964EB0007EC616 /* EasyCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = EasyCell.xib; sourceTree = ""; }; 43 | 044888551E964F47007EC616 /* EasyCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EasyCell.h; sourceTree = ""; }; 44 | 044888561E964F47007EC616 /* EasyCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EasyCell.m; sourceTree = ""; }; 45 | 044BDFA31E974A0700A8F742 /* TestCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestCell.h; sourceTree = ""; }; 46 | 044BDFA41E974A0700A8F742 /* TestCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestCell.m; sourceTree = ""; }; 47 | 044BDFA71E97614400A8F742 /* EasyTools.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EasyTools.h; sourceTree = ""; }; 48 | 044BDFA81E97614400A8F742 /* EasyTools.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EasyTools.m; sourceTree = ""; }; 49 | 0457045E1F260153007D58BA /* SPBaseReusableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPBaseReusableView.h; sourceTree = ""; }; 50 | 0457045F1F260153007D58BA /* SPBaseReusableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPBaseReusableView.m; sourceTree = ""; }; 51 | /* End PBXFileReference section */ 52 | 53 | /* Begin PBXFrameworksBuildPhase section */ 54 | 044888291E9636C8007EC616 /* Frameworks */ = { 55 | isa = PBXFrameworksBuildPhase; 56 | buildActionMask = 2147483647; 57 | files = ( 58 | ); 59 | runOnlyForDeploymentPostprocessing = 0; 60 | }; 61 | /* End PBXFrameworksBuildPhase section */ 62 | 63 | /* Begin PBXGroup section */ 64 | 044888231E9636C8007EC616 = { 65 | isa = PBXGroup; 66 | children = ( 67 | 0448882E1E9636C8007EC616 /* SPEasyCollectionView */, 68 | 0448882D1E9636C8007EC616 /* Products */, 69 | ); 70 | sourceTree = ""; 71 | }; 72 | 0448882D1E9636C8007EC616 /* Products */ = { 73 | isa = PBXGroup; 74 | children = ( 75 | 0448882C1E9636C8007EC616 /* SPEasyCollectionView.app */, 76 | ); 77 | name = Products; 78 | sourceTree = ""; 79 | }; 80 | 0448882E1E9636C8007EC616 /* SPEasyCollectionView */ = { 81 | isa = PBXGroup; 82 | children = ( 83 | 044888461E9636F3007EC616 /* SPEasyCollectionView */, 84 | 044888321E9636C8007EC616 /* AppDelegate.h */, 85 | 044888331E9636C8007EC616 /* AppDelegate.m */, 86 | 044BDFA31E974A0700A8F742 /* TestCell.h */, 87 | 044BDFA41E974A0700A8F742 /* TestCell.m */, 88 | 044888551E964F47007EC616 /* EasyCell.h */, 89 | 044888561E964F47007EC616 /* EasyCell.m */, 90 | 044888511E964EB0007EC616 /* EasyCell.xib */, 91 | 044888351E9636C8007EC616 /* ViewController.h */, 92 | 044888361E9636C8007EC616 /* ViewController.m */, 93 | 044888381E9636C8007EC616 /* Main.storyboard */, 94 | 0448883B1E9636C9007EC616 /* Assets.xcassets */, 95 | 0448883D1E9636C9007EC616 /* LaunchScreen.storyboard */, 96 | 044888401E9636C9007EC616 /* Info.plist */, 97 | 042683CA1F049EF5006613B6 /* IMG_2468.JPG */, 98 | 0448882F1E9636C8007EC616 /* Supporting Files */, 99 | ); 100 | path = SPEasyCollectionView; 101 | sourceTree = ""; 102 | }; 103 | 0448882F1E9636C8007EC616 /* Supporting Files */ = { 104 | isa = PBXGroup; 105 | children = ( 106 | 044888301E9636C8007EC616 /* main.m */, 107 | ); 108 | name = "Supporting Files"; 109 | sourceTree = ""; 110 | }; 111 | 044888461E9636F3007EC616 /* SPEasyCollectionView */ = { 112 | isa = PBXGroup; 113 | children = ( 114 | 044BDFA61E97614400A8F742 /* Easytools */, 115 | 0457045E1F260153007D58BA /* SPBaseReusableView.h */, 116 | 0457045F1F260153007D58BA /* SPBaseReusableView.m */, 117 | 044888471E963706007EC616 /* SPEasyCollectionView.h */, 118 | 044888481E963706007EC616 /* SPEasyCollectionView.m */, 119 | 0448884E1E963C59007EC616 /* SPBaseCell.h */, 120 | 0448884F1E963C59007EC616 /* SPBaseCell.m */, 121 | ); 122 | path = SPEasyCollectionView; 123 | sourceTree = ""; 124 | }; 125 | 044BDFA61E97614400A8F742 /* Easytools */ = { 126 | isa = PBXGroup; 127 | children = ( 128 | 044BDFA71E97614400A8F742 /* EasyTools.h */, 129 | 044BDFA81E97614400A8F742 /* EasyTools.m */, 130 | ); 131 | path = Easytools; 132 | sourceTree = ""; 133 | }; 134 | /* End PBXGroup section */ 135 | 136 | /* Begin PBXNativeTarget section */ 137 | 0448882B1E9636C8007EC616 /* SPEasyCollectionView */ = { 138 | isa = PBXNativeTarget; 139 | buildConfigurationList = 044888431E9636C9007EC616 /* Build configuration list for PBXNativeTarget "SPEasyCollectionView" */; 140 | buildPhases = ( 141 | 044888281E9636C8007EC616 /* Sources */, 142 | 044888291E9636C8007EC616 /* Frameworks */, 143 | 0448882A1E9636C8007EC616 /* Resources */, 144 | ); 145 | buildRules = ( 146 | ); 147 | dependencies = ( 148 | ); 149 | name = SPEasyCollectionView; 150 | productName = SPEasyCollectionView; 151 | productReference = 0448882C1E9636C8007EC616 /* SPEasyCollectionView.app */; 152 | productType = "com.apple.product-type.application"; 153 | }; 154 | /* End PBXNativeTarget section */ 155 | 156 | /* Begin PBXProject section */ 157 | 044888241E9636C8007EC616 /* Project object */ = { 158 | isa = PBXProject; 159 | attributes = { 160 | LastUpgradeCheck = 0830; 161 | ORGANIZATIONNAME = Tr2e; 162 | TargetAttributes = { 163 | 0448882B1E9636C8007EC616 = { 164 | CreatedOnToolsVersion = 8.3; 165 | ProvisioningStyle = Automatic; 166 | }; 167 | }; 168 | }; 169 | buildConfigurationList = 044888271E9636C8007EC616 /* Build configuration list for PBXProject "SPEasyCollectionView" */; 170 | compatibilityVersion = "Xcode 3.2"; 171 | developmentRegion = English; 172 | hasScannedForEncodings = 0; 173 | knownRegions = ( 174 | en, 175 | Base, 176 | ); 177 | mainGroup = 044888231E9636C8007EC616; 178 | productRefGroup = 0448882D1E9636C8007EC616 /* Products */; 179 | projectDirPath = ""; 180 | projectRoot = ""; 181 | targets = ( 182 | 0448882B1E9636C8007EC616 /* SPEasyCollectionView */, 183 | ); 184 | }; 185 | /* End PBXProject section */ 186 | 187 | /* Begin PBXResourcesBuildPhase section */ 188 | 0448882A1E9636C8007EC616 /* Resources */ = { 189 | isa = PBXResourcesBuildPhase; 190 | buildActionMask = 2147483647; 191 | files = ( 192 | 0448883F1E9636C9007EC616 /* LaunchScreen.storyboard in Resources */, 193 | 0448883C1E9636C9007EC616 /* Assets.xcassets in Resources */, 194 | 0448883A1E9636C8007EC616 /* Main.storyboard in Resources */, 195 | 044888521E964EB0007EC616 /* EasyCell.xib in Resources */, 196 | 042683CB1F049EF5006613B6 /* IMG_2468.JPG in Resources */, 197 | ); 198 | runOnlyForDeploymentPostprocessing = 0; 199 | }; 200 | /* End PBXResourcesBuildPhase section */ 201 | 202 | /* Begin PBXSourcesBuildPhase section */ 203 | 044888281E9636C8007EC616 /* Sources */ = { 204 | isa = PBXSourcesBuildPhase; 205 | buildActionMask = 2147483647; 206 | files = ( 207 | 045704601F260153007D58BA /* SPBaseReusableView.m in Sources */, 208 | 044888501E963C59007EC616 /* SPBaseCell.m in Sources */, 209 | 044BDFA91E97614400A8F742 /* EasyTools.m in Sources */, 210 | 044888371E9636C8007EC616 /* ViewController.m in Sources */, 211 | 044888491E963706007EC616 /* SPEasyCollectionView.m in Sources */, 212 | 044888341E9636C8007EC616 /* AppDelegate.m in Sources */, 213 | 044BDFA51E974A0700A8F742 /* TestCell.m in Sources */, 214 | 044888571E964F47007EC616 /* EasyCell.m in Sources */, 215 | 044888311E9636C8007EC616 /* main.m in Sources */, 216 | ); 217 | runOnlyForDeploymentPostprocessing = 0; 218 | }; 219 | /* End PBXSourcesBuildPhase section */ 220 | 221 | /* Begin PBXVariantGroup section */ 222 | 044888381E9636C8007EC616 /* Main.storyboard */ = { 223 | isa = PBXVariantGroup; 224 | children = ( 225 | 044888391E9636C8007EC616 /* Base */, 226 | ); 227 | name = Main.storyboard; 228 | sourceTree = ""; 229 | }; 230 | 0448883D1E9636C9007EC616 /* LaunchScreen.storyboard */ = { 231 | isa = PBXVariantGroup; 232 | children = ( 233 | 0448883E1E9636C9007EC616 /* Base */, 234 | ); 235 | name = LaunchScreen.storyboard; 236 | sourceTree = ""; 237 | }; 238 | /* End PBXVariantGroup section */ 239 | 240 | /* Begin XCBuildConfiguration section */ 241 | 044888411E9636C9007EC616 /* Debug */ = { 242 | isa = XCBuildConfiguration; 243 | buildSettings = { 244 | ALWAYS_SEARCH_USER_PATHS = NO; 245 | CLANG_ANALYZER_NONNULL = YES; 246 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 247 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 248 | CLANG_CXX_LIBRARY = "libc++"; 249 | CLANG_ENABLE_MODULES = YES; 250 | CLANG_ENABLE_OBJC_ARC = YES; 251 | CLANG_WARN_BOOL_CONVERSION = YES; 252 | CLANG_WARN_CONSTANT_CONVERSION = YES; 253 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 254 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 255 | CLANG_WARN_EMPTY_BODY = YES; 256 | CLANG_WARN_ENUM_CONVERSION = YES; 257 | CLANG_WARN_INFINITE_RECURSION = YES; 258 | CLANG_WARN_INT_CONVERSION = YES; 259 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 260 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 261 | CLANG_WARN_UNREACHABLE_CODE = YES; 262 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 263 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 264 | COPY_PHASE_STRIP = NO; 265 | DEBUG_INFORMATION_FORMAT = dwarf; 266 | ENABLE_STRICT_OBJC_MSGSEND = YES; 267 | ENABLE_TESTABILITY = YES; 268 | GCC_C_LANGUAGE_STANDARD = gnu99; 269 | GCC_DYNAMIC_NO_PIC = NO; 270 | GCC_NO_COMMON_BLOCKS = YES; 271 | GCC_OPTIMIZATION_LEVEL = 0; 272 | GCC_PREPROCESSOR_DEFINITIONS = ( 273 | "DEBUG=1", 274 | "$(inherited)", 275 | ); 276 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 277 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 278 | GCC_WARN_UNDECLARED_SELECTOR = YES; 279 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 280 | GCC_WARN_UNUSED_FUNCTION = YES; 281 | GCC_WARN_UNUSED_VARIABLE = YES; 282 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 283 | MTL_ENABLE_DEBUG_INFO = YES; 284 | ONLY_ACTIVE_ARCH = YES; 285 | SDKROOT = iphoneos; 286 | TARGETED_DEVICE_FAMILY = "1,2"; 287 | }; 288 | name = Debug; 289 | }; 290 | 044888421E9636C9007EC616 /* Release */ = { 291 | isa = XCBuildConfiguration; 292 | buildSettings = { 293 | ALWAYS_SEARCH_USER_PATHS = NO; 294 | CLANG_ANALYZER_NONNULL = YES; 295 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 296 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 297 | CLANG_CXX_LIBRARY = "libc++"; 298 | CLANG_ENABLE_MODULES = YES; 299 | CLANG_ENABLE_OBJC_ARC = YES; 300 | CLANG_WARN_BOOL_CONVERSION = YES; 301 | CLANG_WARN_CONSTANT_CONVERSION = YES; 302 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 303 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 304 | CLANG_WARN_EMPTY_BODY = YES; 305 | CLANG_WARN_ENUM_CONVERSION = YES; 306 | CLANG_WARN_INFINITE_RECURSION = YES; 307 | CLANG_WARN_INT_CONVERSION = YES; 308 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 309 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 310 | CLANG_WARN_UNREACHABLE_CODE = YES; 311 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 312 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 313 | COPY_PHASE_STRIP = NO; 314 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 315 | ENABLE_NS_ASSERTIONS = NO; 316 | ENABLE_STRICT_OBJC_MSGSEND = YES; 317 | GCC_C_LANGUAGE_STANDARD = gnu99; 318 | GCC_NO_COMMON_BLOCKS = YES; 319 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 320 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 321 | GCC_WARN_UNDECLARED_SELECTOR = YES; 322 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 323 | GCC_WARN_UNUSED_FUNCTION = YES; 324 | GCC_WARN_UNUSED_VARIABLE = YES; 325 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 326 | MTL_ENABLE_DEBUG_INFO = NO; 327 | SDKROOT = iphoneos; 328 | TARGETED_DEVICE_FAMILY = "1,2"; 329 | VALIDATE_PRODUCT = YES; 330 | }; 331 | name = Release; 332 | }; 333 | 044888441E9636C9007EC616 /* Debug */ = { 334 | isa = XCBuildConfiguration; 335 | buildSettings = { 336 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 337 | INFOPLIST_FILE = SPEasyCollectionView/Info.plist; 338 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 339 | PRODUCT_BUNDLE_IDENTIFIER = com.Tr2e.SPEasyCollectionView; 340 | PRODUCT_NAME = "$(TARGET_NAME)"; 341 | }; 342 | name = Debug; 343 | }; 344 | 044888451E9636C9007EC616 /* Release */ = { 345 | isa = XCBuildConfiguration; 346 | buildSettings = { 347 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 348 | INFOPLIST_FILE = SPEasyCollectionView/Info.plist; 349 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 350 | PRODUCT_BUNDLE_IDENTIFIER = com.Tr2e.SPEasyCollectionView; 351 | PRODUCT_NAME = "$(TARGET_NAME)"; 352 | }; 353 | name = Release; 354 | }; 355 | /* End XCBuildConfiguration section */ 356 | 357 | /* Begin XCConfigurationList section */ 358 | 044888271E9636C8007EC616 /* Build configuration list for PBXProject "SPEasyCollectionView" */ = { 359 | isa = XCConfigurationList; 360 | buildConfigurations = ( 361 | 044888411E9636C9007EC616 /* Debug */, 362 | 044888421E9636C9007EC616 /* Release */, 363 | ); 364 | defaultConfigurationIsVisible = 0; 365 | defaultConfigurationName = Release; 366 | }; 367 | 044888431E9636C9007EC616 /* Build configuration list for PBXNativeTarget "SPEasyCollectionView" */ = { 368 | isa = XCConfigurationList; 369 | buildConfigurations = ( 370 | 044888441E9636C9007EC616 /* Debug */, 371 | 044888451E9636C9007EC616 /* Release */, 372 | ); 373 | defaultConfigurationIsVisible = 0; 374 | defaultConfigurationName = Release; 375 | }; 376 | /* End XCConfigurationList section */ 377 | }; 378 | rootObject = 044888241E9636C8007EC616 /* Project object */; 379 | } 380 | -------------------------------------------------------------------------------- /SPEasyCollectionView/SPEasyCollectionView/SPEasyCollectionView.m: -------------------------------------------------------------------------------- 1 | // 2 | // SPEasyCollectionView.m 3 | // SPEasyCollectionView 4 | // 5 | // Created by Tree on 2017/4/6. 6 | // Copyright © 2017年 Tr2e. All rights reserved. 7 | // 8 | 9 | #import "SPEasyCollectionView.h" 10 | #import "SPBaseReusableView.h" 11 | #import "SPBaseCell.h" 12 | #import "EasyTools.h" 13 | 14 | #define SPEasyPageControlSize CGSizeMake(10,10) 15 | 16 | typedef NS_ENUM(NSInteger,SPDragDirection) { 17 | SPDragDirectionRight, 18 | SPDragDirectionLeft, 19 | SPDragDirectionUp, 20 | SPDragDirectionDown 21 | }; 22 | 23 | @interface SPEasyCollectionView() 24 | @property (nonatomic, strong) UICollectionViewFlowLayout *layout; 25 | 26 | // Cycle Function Part 27 | @property (nonatomic, strong) UIPageControl *pageControl; 28 | @property (nonatomic, assign) NSUInteger totalItemCount; 29 | @property (nonatomic, weak) NSTimer *timer; 30 | 31 | // Active Cell Part 32 | @property (nonatomic, assign) BOOL isEqualOrGreaterThan9_0; 33 | @property (nonatomic, assign) CGFloat edgeIntersectionOffset; 34 | @property (nonatomic, assign) CGPoint centerOffset; 35 | @property (nonatomic, assign) SPDragDirection dragDirection; 36 | @property (nonatomic, weak) UILongPressGestureRecognizer *longGestureRecognizer; 37 | @property (nonatomic, weak) NSIndexPath *activeIndexPath; 38 | @property (nonatomic, weak) NSIndexPath *sourceIndexPath; 39 | @property (nonatomic, weak) SPBaseCell *activeCell; 40 | @property (nonatomic, strong) CADisplayLink *displayLink; 41 | @property (nonatomic, strong) NSMutableArray *activeCells; 42 | @property (nonatomic, strong) UIView *snapViewForActiveCell; 43 | @property (nonatomic, assign) CGFloat changeRatio; 44 | 45 | 46 | @end 47 | 48 | NSString * const ReuseIdentifier = @"SPCell"; 49 | NSString * const SectionHeaderIdentifier = @"SectionHeader"; 50 | NSString * const SectionFooterIdentifier = @"SectionFooter"; 51 | 52 | @implementation SPEasyCollectionView 53 | 54 | - (instancetype)initWithFrame:(CGRect)frame{ 55 | if (self = [super initWithFrame:frame]) { 56 | [self initializeMainView]; 57 | self.activeCells = [NSMutableArray array]; 58 | } 59 | return self; 60 | } 61 | 62 | - (void)awakeFromNib{ 63 | [super awakeFromNib]; 64 | // [self initializeMainView]; 65 | } 66 | 67 | - (instancetype)initWithCoder:(NSCoder *)aDecoder{ 68 | if (self = [super initWithCoder:aDecoder]) { 69 | [self initializeMainView]; 70 | } 71 | return self; 72 | } 73 | 74 | - (void)layoutSubviews{ 75 | 76 | // 修正collectionView通过xib初始化时frame不准确 77 | _collectionView.frame = self.bounds; 78 | 79 | // 80 | 81 | // backgroundColor 82 | self.collectionView.backgroundColor = self.backgroundColor?_backgroundColor:[UIColor whiteColor]; 83 | 84 | // pageControl 85 | CGSize size = SPEasyPageControlSize; 86 | CGFloat width = size.width * 1.5; 87 | CGFloat height = size.height; 88 | CGFloat x = self.center.x - width/2; 89 | CGFloat y = self.bounds.size.height - height* 2; 90 | _pageControl.frame = CGRectMake( x, y, width, height); 91 | _pageControl.hidden = !_needAutoScroll; 92 | 93 | // super 94 | [super layoutSubviews]; 95 | 96 | } 97 | 98 | #pragma mark - chain calls 99 | - (SPEasyCollectionViewinset)sp_inset{ 100 | return ^SPEasyCollectionView *(UIEdgeInsets(^inset)()){ 101 | self.inset = inset(); 102 | return self; 103 | }; 104 | } 105 | 106 | - (SPEasyCollectionViewItemSize)sp_itemsize{ 107 | return ^SPEasyCollectionView *(CGSize(^itemSize)()){ 108 | self.itemSize = itemSize(); 109 | return self; 110 | }; 111 | } 112 | 113 | - (SPEasyCollectionViewMinLineSpace)sp_minLineSpace{ 114 | return ^SPEasyCollectionView *(NSInteger(^minLineSpace)()){ 115 | self.minLineSpace = minLineSpace(); 116 | return self; 117 | }; 118 | } 119 | 120 | - (SPEasyCollectionViewMinInterItemSpace)sp_minInterItemSpace{ 121 | return ^SPEasyCollectionView *(NSInteger(^minInterItemSpace)()){ 122 | self.minInterItemSpace = minInterItemSpace(); 123 | return self; 124 | }; 125 | } 126 | 127 | - (SPEasyCollectionViewScrollDirection)sp_scollDirection{ 128 | return ^SPEasyCollectionView *(SPEasyScrollDirection(^direction)()){ 129 | self.scrollDirection = direction(); 130 | return self; 131 | }; 132 | } 133 | 134 | - (SPEasyCollectionViewDelegate)sp_delegate{ 135 | return ^SPEasyCollectionView *(id(^delegate)()){ 136 | self.delegate = delegate(); 137 | return self; 138 | }; 139 | } 140 | 141 | - (SPEasyCollectionViewCellXibName)sp_xibName{ 142 | return ^SPEasyCollectionView *(NSString *(^xibName)()){ 143 | self.xibName = xibName(); 144 | return self; 145 | }; 146 | } 147 | 148 | - (SPEasyCollectionViewCellClassName)sp_cellClassName{ 149 | return ^SPEasyCollectionView *(NSString *(^className)()){ 150 | self.cellClassName = className(); 151 | return self; 152 | }; 153 | } 154 | 155 | - (SPEasyCollectionViewBackgroundColor)sp_backgroundColor{ 156 | return ^SPEasyCollectionView *(UIColor *(^backgroundColor)()){ 157 | self.backgroundColor = backgroundColor(); 158 | return self; 159 | }; 160 | } 161 | 162 | - (SPEasyCollectionViewHeaderXibName)sp_headerXibName{ 163 | return ^SPEasyCollectionView *(NSString *(^headerXibName)()){ 164 | self.headerXibName = headerXibName(); 165 | return self; 166 | }; 167 | } 168 | 169 | - (SPEasyCollectionViewHeaderClassName)sp_headerClassName{ 170 | return ^SPEasyCollectionView *(NSString *(^headerClassName)()){ 171 | self.headerClassName = headerClassName(); 172 | return self; 173 | }; 174 | } 175 | 176 | - (SPEasyCollectionViewFooterXibName)sp_footerXibName{ 177 | return ^SPEasyCollectionView *(NSString *(^footerXibName)()){ 178 | self.headerXibName = footerXibName(); 179 | return self; 180 | }; 181 | } 182 | 183 | - (SPEasyCollectionViewFooterClassName)sp_footerClassName{ 184 | return ^SPEasyCollectionView *(NSString *(^footerClassName)()){ 185 | self.footerClassName = footerClassName(); 186 | return self; 187 | }; 188 | } 189 | 190 | - (SPEasyCollectionViewHeaderSize)sp_headersize{ 191 | return ^SPEasyCollectionView *(CGSize(^headerSize)()){ 192 | self.headerSize = headerSize(); 193 | return self; 194 | }; 195 | } 196 | 197 | - (SPEasyCollectionViewFooterSize)sp_footersize{ 198 | return ^SPEasyCollectionView *(CGSize(^footerSize)()){ 199 | self.footerSize = footerSize(); 200 | return self; 201 | }; 202 | } 203 | 204 | #pragma mark - properties 205 | 206 | - (void)setCanEdit:(BOOL)canEdit{ 207 | _canEdit = canEdit; 208 | 209 | if (canEdit) { 210 | [self wakeupEditingMode]; 211 | } 212 | 213 | } 214 | 215 | - (void)setNeedAutoScroll:(BOOL)needAutoScroll{ 216 | _needAutoScroll = needAutoScroll; 217 | 218 | [self invalidateTimer]; 219 | if (needAutoScroll) { 220 | _collectionView.pagingEnabled = YES; 221 | [self setupTimer]; 222 | } 223 | } 224 | 225 | - (void)setDatas:(NSArray *)datas{ 226 | if (_datas == datas) { 227 | return; 228 | } 229 | _datas = datas; 230 | 231 | // if (self.collectionView.canLoadMore) { 232 | // self.collectionView.sp_datas = [datas mutableCopy]; 233 | // } 234 | 235 | _totalItemCount = _needAutoScroll?datas.count * 500:datas.count; 236 | if (_needAutoScroll) { 237 | [self setupPageControl]; 238 | } 239 | [UIView performWithoutAnimation:^{ 240 | [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]]; 241 | }]; 242 | 243 | } 244 | 245 | - (void)setItemSize:(CGSize)itemSize{ 246 | _itemSize = itemSize; 247 | _layout.itemSize = self.itemSize.width?CGSizeMake(_itemSize.width, _itemSize.height):CGSizeMake(self.bounds.size.width, self.bounds.size.height); 248 | } 249 | 250 | - (void)setMinInterItemSpace:(NSInteger)minInterItemSpace{ 251 | _minInterItemSpace = minInterItemSpace; 252 | _layout.minimumInteritemSpacing = _minInterItemSpace?_minInterItemSpace:0; 253 | } 254 | 255 | - (void)setMinLineSpace:(NSInteger)minLineSpace{ 256 | _minLineSpace = minLineSpace; 257 | _layout.minimumLineSpacing = _minLineSpace?_minLineSpace:0; 258 | } 259 | 260 | - (void)setBounces:(BOOL)bounces{ 261 | _bounces = bounces; 262 | _collectionView.bounces = bounces; 263 | } 264 | 265 | - (void)setPageEnabled:(BOOL)pageEnabled{ 266 | _pageEnabled = pageEnabled; 267 | _collectionView.pagingEnabled = pageEnabled; 268 | } 269 | 270 | - (void)setInset:(UIEdgeInsets)inset{ 271 | _inset = inset; 272 | _collectionView.contentInset = inset; 273 | } 274 | 275 | - (void)setScrollDirection:(SPEasyScrollDirection)scrollDirection{ 276 | _scrollDirection = scrollDirection; 277 | _layout.scrollDirection = (UICollectionViewScrollDirection)scrollDirection; 278 | } 279 | 280 | - (void)setCellClassName:(NSString *)cellClassName{ 281 | _cellClassName = cellClassName; 282 | [_collectionView registerClass:NSClassFromString(_cellClassName) forCellWithReuseIdentifier:ReuseIdentifier]; 283 | } 284 | 285 | - (void)setXibName:(NSString *)xibName{ 286 | _xibName = xibName; 287 | [_collectionView registerNib:[UINib nibWithNibName:_xibName bundle:nil] forCellWithReuseIdentifier:ReuseIdentifier]; 288 | } 289 | 290 | - (void)setHeaderXibName:(NSString *)headerXibName{ 291 | _headerXibName = headerXibName; 292 | [_collectionView registerNib:[UINib nibWithNibName:headerXibName bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SectionHeaderIdentifier]; 293 | } 294 | 295 | - (void)setHeaderClassName:(NSString *)headerClassName{ 296 | _headerClassName = headerClassName; 297 | [_collectionView registerClass:NSClassFromString(headerClassName) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SectionHeaderIdentifier]; 298 | } 299 | 300 | - (void)setFooterXibName:(NSString *)footerXibName{ 301 | _footerXibName = footerXibName; 302 | [_collectionView registerNib:[UINib nibWithNibName:footerXibName bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SectionFooterIdentifier]; 303 | } 304 | 305 | - (void)setFooterClassName:(NSString *)footerClassName{ 306 | _footerClassName = footerClassName; 307 | [_collectionView registerClass:NSClassFromString(footerClassName) forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SectionFooterIdentifier]; 308 | } 309 | 310 | - (void)setHeaderSize:(CGSize)headerSize{ 311 | _headerSize = headerSize; 312 | _layout.headerReferenceSize = headerSize; 313 | } 314 | 315 | - (void)setFooterSize:(CGSize)footerSize{ 316 | _footerSize = footerSize; 317 | _layout.footerReferenceSize = footerSize; 318 | } 319 | 320 | #pragma mark - main view 321 | - (void)initializeMainView{ 322 | 323 | // layout 324 | UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 325 | layout.scrollDirection = _scrollDirection?UICollectionViewScrollDirectionVertical:UICollectionViewScrollDirectionHorizontal; 326 | _layout = layout; 327 | 328 | // collectionview 329 | UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout]; 330 | collectionView.delegate = self; 331 | collectionView.dataSource = self; 332 | collectionView.scrollsToTop = YES; 333 | collectionView.showsVerticalScrollIndicator = NO; 334 | collectionView.showsHorizontalScrollIndicator = NO; 335 | 336 | [self addSubview:collectionView]; 337 | self.collectionView = collectionView; 338 | 339 | } 340 | 341 | #pragma mark - Page Control 342 | - (void)setupPageControl{ 343 | 344 | if (_pageControl) { 345 | return; 346 | } 347 | 348 | UIPageControl *pageControl = [[UIPageControl alloc] init]; 349 | pageControl.hidden = YES; 350 | pageControl.numberOfPages = _datas.count; 351 | pageControl.currentPageIndicatorTintColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8]; 352 | pageControl.pageIndicatorTintColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.8]; 353 | pageControl.userInteractionEnabled = NO; 354 | pageControl.currentPage = [self getRealShownIndex:[self currentIndex]]; 355 | _pageControl = pageControl; 356 | [self addSubview:pageControl]; 357 | 358 | } 359 | 360 | #pragma mark - Timer 361 | - (void)setupTimer{ 362 | 363 | NSTimer *timer = [NSTimer timerWithTimeInterval:_timerInterval?_timerInterval:3 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES]; 364 | [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 365 | _timer = timer; 366 | 367 | } 368 | 369 | - (void)invalidateTimer{ 370 | 371 | [_timer invalidate]; 372 | _timer = nil; 373 | 374 | } 375 | 376 | - (void)setupCADisplayLink{ 377 | 378 | if (self.displayLink) { 379 | return; 380 | } 381 | CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleEdgeIntersection)]; 382 | [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; 383 | self.displayLink = displayLink; 384 | 385 | } 386 | 387 | - (void)invalidateCADisplayLink{ 388 | 389 | [self.displayLink setPaused:YES]; 390 | [self.displayLink invalidate]; 391 | self.displayLink = nil; 392 | 393 | } 394 | 395 | #pragma mark - Editing Model 396 | - (void)wakeupEditingMode{ 397 | 398 | [self addLongPressGestureRecognizer]; 399 | 400 | } 401 | 402 | - (void)addLongPressGestureRecognizer{ 403 | 404 | UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; 405 | longPress.minimumPressDuration = self.activeEditingModeTimeInterval?_activeEditingModeTimeInterval:2.0f; 406 | [self addGestureRecognizer:longPress]; 407 | self.longGestureRecognizer = longPress; 408 | 409 | } 410 | 411 | - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{ 412 | 413 | } 414 | 415 | - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{ 416 | 417 | } 418 | 419 | 420 | - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)recognizer{ 421 | 422 | BOOL isSystemVersionEqualOrGreaterThen9_0 = NO; 423 | self.isEqualOrGreaterThan9_0 = isSystemVersionEqualOrGreaterThen9_0 = [UIDevice.currentDevice.systemVersion compare:@"9.0" options:NSNumericSearch] != NSOrderedAscending; 424 | [self handleEditingMode:recognizer]; 425 | 426 | } 427 | 428 | - (void)handleEditingMode:(UILongPressGestureRecognizer *)recognizer{ 429 | 430 | switch (recognizer.state) { 431 | case UIGestureRecognizerStateBegan: { 432 | [self handleEditingMoveWhenGestureBegan:recognizer]; 433 | break; 434 | } 435 | case UIGestureRecognizerStateChanged: { 436 | [self handleEditingMoveWhenGestureChanged:recognizer]; 437 | break; 438 | } 439 | case UIGestureRecognizerStateEnded: { 440 | [self handleEditingMoveWhenGestureEnded:recognizer]; 441 | break; 442 | } 443 | default: { 444 | [self handleEditingMoveWhenGestureCanceledOrFailed:recognizer]; 445 | break; 446 | } 447 | } 448 | 449 | } 450 | 451 | - (void)handleEditingMoveWhenGestureBegan:(UILongPressGestureRecognizer *)recognizer{ 452 | 453 | CGPoint pressPoint = [recognizer locationInView:self.collectionView]; 454 | NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:pressPoint]; 455 | SPBaseCell *cell = (SPBaseCell *)[_collectionView cellForItemAtIndexPath:selectIndexPath]; 456 | self.activeIndexPath = selectIndexPath; 457 | self.sourceIndexPath = selectIndexPath; 458 | self.activeCell = cell; 459 | self.activeCell.selected = YES; 460 | 461 | self.centerOffset = CGPointMake(pressPoint.x - cell.center.x, pressPoint.y - cell.center.y); 462 | 463 | if (_isEqualOrGreaterThan9_0) { 464 | [self.collectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath]; 465 | }else{ 466 | self.snapViewForActiveCell = [cell snapshotViewAfterScreenUpdates:YES]; 467 | self.snapViewForActiveCell.frame = cell.frame; 468 | cell.hidden = YES; 469 | [self.collectionView addSubview:self.snapViewForActiveCell]; 470 | } 471 | 472 | } 473 | 474 | - (void)handleEditingMoveWhenGestureChanged:(UILongPressGestureRecognizer *)recognizer{ 475 | 476 | CGPoint pressPoint = [recognizer locationInView:self.collectionView]; 477 | if (_isEqualOrGreaterThan9_0) { 478 | [self.collectionView updateInteractiveMovementTargetPosition:pressPoint]; 479 | }else{ 480 | _snapViewForActiveCell.center = CGPointMake(pressPoint.x - _centerOffset.x, pressPoint.y-_centerOffset.y); 481 | [self handleExchangeOperation]; 482 | [self detectEdge]; 483 | } 484 | 485 | } 486 | 487 | - (void)handleExchangeOperation{ 488 | 489 | for (SPBaseCell *cell in self.collectionView.visibleCells) 490 | { 491 | NSIndexPath *currentIndexPath = [_collectionView indexPathForCell:cell]; 492 | if ([_collectionView indexPathForCell:cell] == self.activeIndexPath) continue; 493 | 494 | CGFloat space_x = fabs(_snapViewForActiveCell.center.x - cell.center.x); 495 | CGFloat space_y = fabs(_snapViewForActiveCell.center.y - cell.center.y); 496 | // CGFloat space = sqrtf(powf(space_x, 2) + powf(space_y, 2)); 497 | CGFloat size_x = cell.bounds.size.width; 498 | CGFloat size_y = cell.bounds.size.height; 499 | 500 | if (currentIndexPath.item > self.activeIndexPath.item) 501 | { 502 | [self.activeCells addObject:cell]; 503 | } 504 | 505 | if (space_x < size_x/2.0 && space_y < size_y/2.0) 506 | { 507 | [self handleCellExchangeWithSourceIndexPath:self.activeIndexPath destinationIndexPath:currentIndexPath]; 508 | self.activeIndexPath = currentIndexPath; 509 | } 510 | } 511 | 512 | } 513 | 514 | - (void)handleDatasourceExchangeWithSourceIndexPath:(NSIndexPath *)sourceIndexPath destinationIndexPath:(NSIndexPath *)destinationIndexPath{ 515 | 516 | NSMutableArray *tempArr = [self.datas mutableCopy]; 517 | 518 | NSInteger activeRange = destinationIndexPath.item - sourceIndexPath.item; 519 | BOOL moveForward = activeRange > 0; 520 | NSInteger originIndex = 0; 521 | NSInteger targetIndex = 0; 522 | 523 | for (NSInteger i = 1; i <= labs(activeRange); i ++) { 524 | 525 | NSInteger moveDirection = moveForward?1:-1; 526 | originIndex = sourceIndexPath.item + i*moveDirection; 527 | targetIndex = originIndex - 1*moveDirection; 528 | 529 | [tempArr exchangeObjectAtIndex:originIndex withObjectAtIndex:targetIndex]; 530 | 531 | } 532 | self.datas = [tempArr copy]; 533 | NSLog(@"##### %@ #####",self.datas); 534 | } 535 | 536 | - (void)handleCellExchangeWithSourceIndexPath:(NSIndexPath *)sourceIndexPath destinationIndexPath:(NSIndexPath *)destinationIndexPath{ 537 | 538 | NSInteger activeRange = destinationIndexPath.item - sourceIndexPath.item; 539 | BOOL moveForward = activeRange > 0; 540 | NSInteger originIndex = 0; 541 | NSInteger targetIndex = 0; 542 | 543 | for (NSInteger i = 1; i <= labs(activeRange); i ++) { 544 | 545 | NSInteger moveDirection = moveForward?1:-1; 546 | originIndex = sourceIndexPath.item + i*moveDirection; 547 | targetIndex = originIndex - 1*moveDirection; 548 | 549 | if (!_isEqualOrGreaterThan9_0) { 550 | CGFloat time = 0.25 - 0.11*fabs(self.changeRatio); 551 | NSLog(@"time:%f",time); 552 | [UIView beginAnimations:nil context:nil]; 553 | [UIView setAnimationDuration:time]; 554 | [_collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:originIndex inSection:sourceIndexPath.section] toIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:sourceIndexPath.section]]; 555 | [UIView commitAnimations]; 556 | 557 | NSLog(@"---> exchange %ld to %ld",(long)originIndex,(long)targetIndex); 558 | 559 | } 560 | 561 | 562 | } 563 | 564 | } 565 | 566 | - (void)handleEditingMoveWhenGestureEnded:(UILongPressGestureRecognizer *)recognizer{ 567 | 568 | if (_isEqualOrGreaterThan9_0) { 569 | self.activeCell.selected = NO; 570 | [self.collectionView endInteractiveMovement]; 571 | }else{ 572 | 573 | [self.snapViewForActiveCell removeFromSuperview]; 574 | self.activeCell.selected = NO; 575 | self.activeCell.hidden = NO; 576 | 577 | [self handleDatasourceExchangeWithSourceIndexPath:self.sourceIndexPath destinationIndexPath:self.activeIndexPath]; 578 | [self invalidateCADisplayLink]; 579 | self.edgeIntersectionOffset = 0; 580 | self.changeRatio = 0; 581 | 582 | } 583 | 584 | } 585 | 586 | - (void)handleEditingMoveWhenGestureCanceledOrFailed:(UILongPressGestureRecognizer *)recognizer{ 587 | 588 | if (_isEqualOrGreaterThan9_0) { 589 | self.activeCell.selected = NO; 590 | [self.collectionView cancelInteractiveMovement]; 591 | }else{ 592 | [UIView animateWithDuration:0.25f animations:^{ 593 | self.snapViewForActiveCell.center = self.activeCell.center; 594 | } completion:^(BOOL finished) { 595 | [self.snapViewForActiveCell removeFromSuperview]; 596 | self.activeCell.selected = NO; 597 | self.activeCell.hidden = NO; 598 | }]; 599 | 600 | [self invalidateCADisplayLink]; 601 | self.edgeIntersectionOffset = 0; 602 | self.changeRatio = 0; 603 | } 604 | 605 | } 606 | 607 | 608 | static CGFloat edgeRange = 10; 609 | static CGFloat velocityRatio = 5; 610 | - (void)detectEdge{ 611 | 612 | CGFloat baseOffset = 2; 613 | 614 | CGPoint snapView_minPoint = self.snapViewForActiveCell.frame.origin; 615 | CGFloat snapView_max_x = CGRectGetMaxX(_snapViewForActiveCell.frame); 616 | CGFloat snapView_max_y = CGRectGetMaxY(_snapViewForActiveCell.frame); 617 | 618 | // left 619 | if (snapView_minPoint.x - self.collectionView.contentOffset.x < edgeRange && 620 | self.collectionView.contentOffset.x > 0){ 621 | 622 | CGFloat intersection_x = edgeRange - (snapView_minPoint.x - self.collectionView.contentOffset.x); 623 | intersection_x = intersection_x < 2*edgeRange?2*edgeRange:intersection_x; 624 | self.changeRatio = intersection_x/(2*edgeRange); 625 | baseOffset = baseOffset * -1 - _changeRatio* baseOffset *velocityRatio; 626 | self.edgeIntersectionOffset = floorf(baseOffset); 627 | self.dragDirection = SPDragDirectionLeft; 628 | [self setupCADisplayLink]; 629 | NSLog(@"Drag left - vertical offset:%f",self.edgeIntersectionOffset); 630 | NSLog(@"CollectionView offset_X:%f",self.collectionView.contentOffset.x); 631 | 632 | } 633 | 634 | // up 635 | else if (snapView_minPoint.y - self.collectionView.contentOffset.y < edgeRange && 636 | self.collectionView.contentOffset.y > 0){ 637 | 638 | CGFloat intersection_y = edgeRange - (snapView_minPoint.y - self.collectionView.contentOffset.y); 639 | intersection_y = intersection_y > 2*edgeRange?2*edgeRange:intersection_y; 640 | self.changeRatio = intersection_y/(2*edgeRange); 641 | baseOffset = baseOffset * -1 - _changeRatio* baseOffset *velocityRatio; 642 | self.edgeIntersectionOffset = floorf(baseOffset); 643 | self.dragDirection = SPDragDirectionUp; 644 | [self setupCADisplayLink]; 645 | NSLog(@"Drag up - vertical offset:%f",self.edgeIntersectionOffset); 646 | NSLog(@"CollectionView offset_Y:%f",self.collectionView.contentOffset.y); 647 | 648 | } 649 | 650 | // right 651 | else if (snapView_max_x + edgeRange > self.collectionView.contentOffset.x + self.collectionView.bounds.size.width && self.collectionView.contentOffset.x + self.collectionView.bounds.size.width < self.collectionView.contentSize.width){ 652 | 653 | CGFloat intersection_x = edgeRange - (self.collectionView.contentOffset.x + self.collectionView.bounds.size.width - snapView_max_x); 654 | intersection_x = intersection_x > 2*edgeRange ? 2*edgeRange:intersection_x; 655 | self.changeRatio = intersection_x/(2*edgeRange); 656 | baseOffset = baseOffset + _changeRatio * baseOffset * velocityRatio; 657 | self.edgeIntersectionOffset = floorf(baseOffset); 658 | self.dragDirection = SPDragDirectionRight; 659 | [self setupCADisplayLink]; 660 | NSLog(@"Drag right - vertical offset:%f",self.edgeIntersectionOffset); 661 | NSLog(@"CollectionView offset_X:%f",self.collectionView.contentOffset.x); 662 | 663 | } 664 | 665 | // down 666 | else if (snapView_max_y + edgeRange > self.collectionView.contentOffset.y + self.collectionView.bounds.size.height && self.collectionView.contentOffset.y + self.collectionView.bounds.size.height < self.collectionView.contentSize.height){ 667 | 668 | CGFloat intersection_y = edgeRange - (self.collectionView.contentOffset.y + self.collectionView.bounds.size.height - snapView_max_y); 669 | intersection_y = intersection_y > 2*edgeRange ? 2*edgeRange:intersection_y; 670 | self.changeRatio = intersection_y/(2*edgeRange); 671 | baseOffset = baseOffset + _changeRatio* baseOffset * velocityRatio; 672 | self.edgeIntersectionOffset = floorf(baseOffset); 673 | self.dragDirection = SPDragDirectionDown; 674 | [self setupCADisplayLink]; 675 | NSLog(@"Drag down - vertical offset:%f",self.edgeIntersectionOffset); 676 | NSLog(@"CollectionView offset_Y:%f",self.collectionView.contentOffset.y); 677 | 678 | } 679 | 680 | // default 681 | else{ 682 | 683 | self.changeRatio = 0; 684 | 685 | if (self.displayLink) 686 | { 687 | [self invalidateCADisplayLink]; 688 | } 689 | } 690 | 691 | } 692 | 693 | - (void)handleEdgeIntersection{ 694 | 695 | [self handleExchangeOperation]; 696 | 697 | switch (_scrollDirection) { 698 | case SPEasyScrollDirectionHorizontal: 699 | { 700 | if (self.collectionView.contentOffset.x + self.inset.left < 0 && 701 | self.dragDirection == SPDragDirectionLeft){ 702 | return; 703 | } 704 | if (self.collectionView.contentOffset.x > 705 | self.collectionView.contentSize.width - (self.collectionView.bounds.size.width - self.inset.left) && 706 | self.dragDirection == SPDragDirectionRight){ 707 | return; 708 | } 709 | 710 | [self.collectionView setContentOffset:CGPointMake(_collectionView.contentOffset.x + self.edgeIntersectionOffset, _collectionView.contentOffset.y) animated:NO]; 711 | self.snapViewForActiveCell.center = CGPointMake(_snapViewForActiveCell.center.x + self.edgeIntersectionOffset, _snapViewForActiveCell.center.y); 712 | } 713 | break; 714 | case SPEasyScrollDirectionVertical: 715 | { 716 | 717 | if (self.collectionView.contentOffset.y + self.inset.top< 0 && 718 | self.dragDirection == SPDragDirectionUp) { 719 | return; 720 | } 721 | if (self.collectionView.contentOffset.y > 722 | self.collectionView.contentSize.height - (self.collectionView.bounds.size.height - self.inset.top) && 723 | self.dragDirection == SPDragDirectionDown) { 724 | return; 725 | } 726 | 727 | [self.collectionView setContentOffset:CGPointMake(_collectionView.contentOffset.x, _collectionView.contentOffset.y + self.edgeIntersectionOffset) animated:NO]; 728 | self.snapViewForActiveCell.center = CGPointMake(_snapViewForActiveCell.center.x, _snapViewForActiveCell.center.y + self.edgeIntersectionOffset); 729 | } 730 | break; 731 | } 732 | 733 | } 734 | 735 | #pragma mark - cycle scroll actions 736 | - (void)autoScroll{ 737 | 738 | if (!_totalItemCount) return; 739 | NSInteger currentIndex = [self currentIndex]; 740 | NSInteger nextIndex = [self nextIndexWithCurrentIndex:currentIndex]; 741 | [self scroll2Index:nextIndex]; 742 | 743 | } 744 | 745 | - (void)scroll2Index:(NSInteger)index{ 746 | 747 | [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:index?YES:NO]; 748 | 749 | } 750 | 751 | - (NSInteger)nextIndexWithCurrentIndex:(NSInteger)index{ 752 | 753 | if (index == _totalItemCount - 1) { 754 | return 0; 755 | }else{ 756 | return index + 1; 757 | } 758 | 759 | } 760 | 761 | - (NSInteger)currentIndex{ 762 | 763 | if (_collectionView.frame.size.width == 0 || _collectionView.frame.size.height == 0) { 764 | return 0; 765 | } 766 | 767 | int index = 0; 768 | if (_layout.scrollDirection == UICollectionViewScrollDirectionHorizontal) { 769 | index = (_collectionView.contentOffset.x + _layout.itemSize.width * 0.5) / _layout.itemSize.width; 770 | } else { 771 | index = (_collectionView.contentOffset.y + _layout.itemSize.height * 0.5) / _layout.itemSize.height; 772 | } 773 | 774 | return MAX(0, index); 775 | } 776 | 777 | #pragma mark - datasoure 778 | - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 779 | return 1; 780 | } 781 | 782 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 783 | 784 | return _totalItemCount; 785 | } 786 | 787 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 788 | 789 | SPBaseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentifier forIndexPath:indexPath]; 790 | // if (self.sp_datas.count) { 791 | // cell.data = collectionView.sp_datas[_needAutoScroll?[self getRealShownIndex:indexPath.item]:indexPath.item]; 792 | // }else{ 793 | cell.data = self.datas[_needAutoScroll?[self getRealShownIndex:indexPath.item]:indexPath.item]; 794 | // } 795 | 796 | return cell; 797 | 798 | } 799 | 800 | - (NSInteger)getRealShownIndex:(NSInteger)index{ 801 | return index%_datas.count; 802 | } 803 | 804 | - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ 805 | if ([kind isEqualToString:UICollectionElementKindSectionFooter]) { 806 | SPBaseReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:SectionFooterIdentifier forIndexPath:indexPath]; 807 | view.datas = self.sectionDatas[indexPath.section]; 808 | return view; 809 | }else{ 810 | SPBaseReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:SectionHeaderIdentifier forIndexPath:indexPath]; 811 | view.datas = self.sectionDatas[indexPath.section]; 812 | return view; 813 | } 814 | } 815 | 816 | #pragma mark - delegate 817 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 818 | 819 | self.selectIndex?self.selectIndex([self getRealShownIndex:indexPath.item]):nil; 820 | if ([self.delegate respondsToSelector:@selector(easyCollectionView:didSelectItemAtIndex:)]) { 821 | [self.delegate easyCollectionView:collectionView didSelectItemAtIndex:[self getRealShownIndex:indexPath.item]]; 822 | } 823 | 824 | } 825 | 826 | - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ 827 | 828 | BOOL canChange = self.datas.count > sourceIndexPath.item && self.datas.count > destinationIndexPath.item; 829 | if (canChange) { 830 | [self handleDatasourceExchangeWithSourceIndexPath:sourceIndexPath destinationIndexPath:destinationIndexPath]; 831 | } 832 | 833 | } 834 | 835 | - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 836 | 837 | if (!self.datas.count) return; 838 | _pageControl.currentPage = [self getRealShownIndex:[self currentIndex]]; 839 | 840 | } 841 | 842 | 843 | - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ 844 | 845 | } 846 | 847 | - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 848 | if (_needAutoScroll) [self invalidateTimer]; 849 | } 850 | 851 | -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 852 | if (_needAutoScroll) [self setupTimer]; 853 | } 854 | 855 | - (void)willMoveToSuperview:(UIView *)newSuperview{ 856 | if (!newSuperview) { 857 | [self invalidateTimer]; 858 | } 859 | } 860 | 861 | - (void)dealloc{ 862 | _collectionView.delegate = nil; 863 | _collectionView.dataSource = nil; 864 | } 865 | 866 | @end 867 | --------------------------------------------------------------------------------