├── HorizontalTableView.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── zhangping.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── HorizontalTableView.xcscheme │ └── xcschememanagement.plist ├── HorizontalTableView ├── 1.plist ├── 2.plist ├── 3.plist ├── AppDelegate.h ├── AppDelegate.m ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── HMShop.h ├── HMShop.m ├── HMShopCell.h ├── HMShopCell.m ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── Lib │ ├── MJExtension │ │ ├── MJArgument.h │ │ ├── MJArgument.m │ │ ├── MJConst.h │ │ ├── MJConst.m │ │ ├── MJExtension.h │ │ ├── MJFoundation.h │ │ ├── MJFoundation.m │ │ ├── MJIvar.h │ │ ├── MJIvar.m │ │ ├── MJMember.h │ │ ├── MJMember.m │ │ ├── MJMethod.h │ │ ├── MJMethod.m │ │ ├── MJType.h │ │ ├── MJType.m │ │ ├── MJTypeEncoding.h │ │ ├── MJTypeEncoding.m │ │ ├── NSObject+MJCoding.h │ │ ├── NSObject+MJCoding.m │ │ ├── NSObject+MJKeyValue.h │ │ ├── NSObject+MJKeyValue.m │ │ ├── NSObject+MJMember.h │ │ └── NSObject+MJMember.m │ ├── MJRefresh │ │ ├── MJRefresh.bundle │ │ │ └── arrow@2x.png │ │ ├── MJRefresh.h │ │ ├── MJRefreshBaseView.h │ │ ├── MJRefreshBaseView.m │ │ ├── MJRefreshConst.h │ │ ├── MJRefreshConst.m │ │ ├── MJRefreshFooterView.h │ │ ├── MJRefreshFooterView.m │ │ ├── MJRefreshHeaderView.h │ │ ├── MJRefreshHeaderView.m │ │ ├── UIScrollView+Extension.h │ │ ├── UIScrollView+Extension.m │ │ ├── UIScrollView+MJRefresh.h │ │ ├── UIScrollView+MJRefresh.m │ │ ├── UIView+Extension.h │ │ └── UIView+Extension.m │ └── SDWebImage │ │ ├── MKAnnotationView+WebCache.h │ │ ├── MKAnnotationView+WebCache.m │ │ ├── NSData+ImageContentType.h │ │ ├── NSData+ImageContentType.m │ │ ├── SDImageCache.h │ │ ├── SDImageCache.m │ │ ├── SDWebImageCompat.h │ │ ├── SDWebImageCompat.m │ │ ├── SDWebImageDecoder.h │ │ ├── SDWebImageDecoder.m │ │ ├── SDWebImageDownloader.h │ │ ├── SDWebImageDownloader.m │ │ ├── SDWebImageDownloaderOperation.h │ │ ├── SDWebImageDownloaderOperation.m │ │ ├── SDWebImageManager.h │ │ ├── SDWebImageManager.m │ │ ├── SDWebImageOperation.h │ │ ├── SDWebImagePrefetcher.h │ │ ├── SDWebImagePrefetcher.m │ │ ├── UIButton+WebCache.h │ │ ├── UIButton+WebCache.m │ │ ├── UIImage+GIF.h │ │ ├── UIImage+GIF.m │ │ ├── UIImage+MultiFormat.h │ │ ├── UIImage+MultiFormat.m │ │ ├── UIImage+WebP.h │ │ ├── UIImage+WebP.m │ │ ├── UIImageView+HighlightedWebCache.h │ │ ├── UIImageView+HighlightedWebCache.m │ │ ├── UIImageView+WebCache.h │ │ ├── UIImageView+WebCache.m │ │ ├── UIView+WebCacheOperation.h │ │ └── UIView+WebCacheOperation.m ├── ViewController.h ├── ViewController.m ├── ZPHorizontalTableView │ ├── ZPHTableView.h │ ├── ZPHTableView.m │ ├── ZPHTableViewCell.h │ └── ZPHTableViewCell.m └── main.m └── HorizontalTableViewTests ├── HorizontalTableViewTests.m └── Info.plist /HorizontalTableView.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /HorizontalTableView.xcodeproj/xcuserdata/zhangping.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /HorizontalTableView.xcodeproj/xcuserdata/zhangping.xcuserdatad/xcschemes/HorizontalTableView.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 29 | 35 | 36 | 37 | 38 | 39 | 44 | 45 | 47 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | 66 | 75 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 94 | 100 | 101 | 102 | 103 | 105 | 106 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /HorizontalTableView.xcodeproj/xcuserdata/zhangping.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | HorizontalTableView.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 6DCB068D1ABA653D00B56778 16 | 17 | primary 18 | 19 | 20 | 6DCB06A61ABA653D00B56778 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /HorizontalTableView/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. 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 | -------------------------------------------------------------------------------- /HorizontalTableView/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. 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 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // 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. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // 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. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // 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. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /HorizontalTableView/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /HorizontalTableView/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 | -------------------------------------------------------------------------------- /HorizontalTableView/HMShop.h: -------------------------------------------------------------------------------- 1 | // 2 | // HMShop.h 3 | // 06-瀑布流 4 | // 5 | // Created by apple on 14-7-28. 6 | // Copyright (c) 2014年 heima. All rights reserved. 7 | // 8 | #import 9 | #import 10 | 11 | @interface HMShop : NSObject 12 | @property (nonatomic, assign) CGFloat w; 13 | @property (nonatomic, assign) CGFloat h; 14 | @property (nonatomic, copy) NSString *img; 15 | @property (nonatomic, copy) NSString *price; 16 | @end 17 | -------------------------------------------------------------------------------- /HorizontalTableView/HMShop.m: -------------------------------------------------------------------------------- 1 | // 2 | // HMShop.m 3 | // 06-瀑布流 4 | // 5 | // Created by apple on 14-7-28. 6 | // Copyright (c) 2014年 heima. All rights reserved. 7 | // 8 | 9 | #import "HMShop.h" 10 | 11 | @implementation HMShop 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /HorizontalTableView/HMShopCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // HMShopCell.h 3 | // 06-瀑布流 4 | // 5 | // Created by apple on 14-7-28. 6 | // Copyright (c) 2014年 heima. All rights reserved. 7 | // 8 | 9 | #import "ZPHTableViewCell.h" 10 | @class ZPHTableView, HMShop; 11 | 12 | @interface HMShopCell : ZPHTableViewCell 13 | + (instancetype)cellWithWaterflowView:(ZPHTableView *)htableView; 14 | 15 | @property (nonatomic, strong) HMShop *shop; 16 | @end 17 | -------------------------------------------------------------------------------- /HorizontalTableView/HMShopCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // HMShopCell.m 3 | // 06-瀑布流 4 | // 5 | // Created by apple on 14-7-28. 6 | // Copyright (c) 2014年 heima. All rights reserved. 7 | // 8 | 9 | #import "HMShopCell.h" 10 | #import "ZPHTableView.h" 11 | #import "UIImageView+WebCache.h" 12 | #import "HMShop.h" 13 | 14 | @interface HMShopCell() 15 | @property (weak, nonatomic) UIImageView *imageView; 16 | @property (weak, nonatomic) UILabel *priceLabel; 17 | @end 18 | 19 | @implementation HMShopCell 20 | 21 | 22 | + (instancetype)cellWithWaterflowView:(ZPHTableView *)htableView 23 | { 24 | static NSString *ID = @"SHOP"; 25 | HMShopCell *cell = [htableView dequeueReusableCellWithIdentifier:ID]; 26 | if (cell == nil) { 27 | cell = [[HMShopCell alloc] init]; 28 | cell.identifier = ID; 29 | } 30 | return cell; 31 | } 32 | 33 | - (id)initWithFrame:(CGRect)frame 34 | { 35 | self = [super initWithFrame:frame]; 36 | if (self) { 37 | 38 | UIImageView *imageView = [[UIImageView alloc] init]; 39 | [self addSubview:imageView]; 40 | self.imageView = imageView; 41 | 42 | UILabel *priceLabel = [[UILabel alloc] init]; 43 | priceLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]; 44 | priceLabel.textAlignment = NSTextAlignmentCenter; 45 | priceLabel.textColor = [UIColor whiteColor]; 46 | [self addSubview:priceLabel]; 47 | self.priceLabel = priceLabel; 48 | } 49 | return self; 50 | } 51 | 52 | - (void)setShop:(HMShop *)shop 53 | { 54 | _shop = shop; 55 | 56 | self.priceLabel.text = shop.price; 57 | [self.imageView sd_setImageWithURL:[NSURL URLWithString:shop.img] placeholderImage:[UIImage imageNamed:@"loading"]]; 58 | } 59 | 60 | - (void)layoutSubviews 61 | { 62 | [super layoutSubviews]; 63 | 64 | self.imageView.frame = self.bounds; 65 | 66 | CGFloat priceX = 0; 67 | CGFloat priceH = 25; 68 | CGFloat priceY = self.bounds.size.height - priceH; 69 | CGFloat priceW = self.bounds.size.width; 70 | self.priceLabel.frame = CGRectMake(priceX, priceY, priceW, priceH); 71 | } 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /HorizontalTableView/Images.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 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /HorizontalTableView/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | hk.com.dycx.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJArgument.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJArgument.h 3 | // ItcastWeibo 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 包装一个方法参数 8 | 9 | #import 10 | /** 11 | * 包装一个方法参数 12 | */ 13 | @interface MJArgument : NSObject 14 | /** 参数的索引 */ 15 | @property (nonatomic, assign) int index; 16 | /** 参数类型 */ 17 | @property (nonatomic, copy) NSString *type; 18 | @end 19 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJArgument.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJArgument.m 3 | // ItcastWeibo 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "MJArgument.h" 10 | #import "MJExtension.h" 11 | 12 | @implementation MJArgument 13 | MJLogAllIvrs 14 | @end 15 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJConst.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MJConst__H__ 3 | #define __MJConst__H__ 4 | 5 | #ifdef DEBUG // 调试状态 6 | // 打开LOG功能 7 | #define MJLog(...) NSLog(__VA_ARGS__) 8 | // 打开断言功能 9 | #define MJAssert(condition, desc) NSAssert(condition, @"\n报错文件:%@\n报错行数:第%d行\n报错方法:%s\n错误描述:%@", [NSString stringWithUTF8String:__FILE__], __LINE__, __FUNCTION__, desc) 10 | #define MJAssertParamNotNil(param) MJAssert(param, [[NSString stringWithFormat:@#param] stringByAppendingString:@"参数不能为nil"]) 11 | #else // 发布状态 12 | // 关闭LOG功能 13 | #define MJLog(...) 14 | // 关闭断言功能 15 | #define MJAssert(condition, desc) 16 | #define MJAssertParamNotNil(param) 17 | #endif 18 | 19 | // 颜色 20 | #define MJColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] 21 | 22 | // 随机色 23 | #define MJRandomColor MJColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256)) 24 | 25 | #endif -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJConst.m: -------------------------------------------------------------------------------- 1 | #ifndef __MJConst__M__ 2 | #define __MJConst__M__ 3 | 4 | 5 | 6 | 7 | #endif -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJExtension.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJExtension.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "MJTypeEncoding.h" 10 | #import "NSObject+MJCoding.h" 11 | #import "NSObject+MJMember.h" 12 | #import "NSObject+MJKeyValue.h" 13 | 14 | #define MJLogAllIvrs \ 15 | - (NSString *)description \ 16 | { \ 17 | return [self keyValues].description; \ 18 | } 19 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJFoundation.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJFoundation.h 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 14/7/16. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MJFoundation : NSObject 12 | + (BOOL)isClassFromFoundation:(Class)c; 13 | @end 14 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJFoundation.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJFoundation.m 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 14/7/16. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "MJFoundation.h" 10 | #import "MJConst.h" 11 | 12 | static NSArray *_foundationClasses; 13 | 14 | @implementation MJFoundation 15 | 16 | + (void)initialize 17 | { 18 | _foundationClasses = @[@"NSArray",@"NSAutoreleasePool",@"NSBundle",@"NSByteOrder",@"NSCalendar",@"NSCharacterSet",@"NSCoder",@"NSData",@"NSDate",@"NSDateFormatter",@"NSDecimal",@"NSDecimalNumber",@"NSDictionary",@"NSEnumerator",@"NSError",@"NSException",@"NSFileHandle",@"NSFileManager",@"NSFormatter",@"NSHashTable",@"NSHTTPCookie",@"NSHTTPCookieStorage",@"NSIndexPath",@"NSIndexSet",@"NSInvocation",@"NSJSONSerialization",@"NSKeyValueCoding",@"NSKeyValueObserving",@"NSKeyedArchiver",@"NSLocale",@"NSLock",@"NSMapTable",@"NSMethodSignature",@"NSNotification",@"NSNotificationQueue",@"NSNull",@"NSNumberFormatter",@"NSObject",@"NSOperation",@"NSOrderedSet",@"NSOrthography",@"NSPathUtilities",@"NSPointerArray",@"NSPointerFunctions",@"NSPort",@"NSProcessInfo",@"NSPropertyList",@"NSProxy",@"NSRange",@"NSRegularExpression",@"NSRunLoop",@"NSScanner",@"NSSet",@"NSSortDescriptor",@"NSStream",@"NSString",@"NSTextCheckingResult",@"NSThread",@"NSTimeZone",@"NSTimer",@"NSURL",@"NSURLAuthenticationChallenge",@"NSURLCache",@"NSURLConnection",@"NSURLCredential",@"NSURLCredentialStorage",@"NSURLError",@"NSURLProtectionSpace",@"NSURLProtocol",@"NSURLRequest",@"NSURLResponse",@"NSUserDefaults",@"NSValue",@"NSValueTransformer",@"NSXMLParser",@"NSZone"]; 19 | } 20 | 21 | + (BOOL)isClassFromFoundation:(Class)c 22 | { 23 | MJAssertParamNotNil(c); 24 | return [_foundationClasses containsObject:NSStringFromClass(c)]; 25 | } 26 | @end 27 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJIvar.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJIvar.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 包装一个成员变量 8 | 9 | #import "MJMember.h" 10 | @class MJType; 11 | 12 | /** 13 | * 包装一个成员变量 14 | */ 15 | @interface MJIvar : MJMember 16 | /** 成员变量 */ 17 | @property (nonatomic, assign) Ivar ivar; 18 | /** 成员属性名 */ 19 | @property (nonatomic, copy, readonly) NSString *propertyName; 20 | /** 成员变量的值 */ 21 | @property (nonatomic) id value; 22 | /** 成员变量的类型 */ 23 | @property (nonatomic, strong, readonly) MJType *type; 24 | 25 | /** 26 | * 初始化 27 | * 28 | * @param ivar 成员变量 29 | * @param srcObject 哪个对象的成员变量 30 | * 31 | * @return 初始化好的对象 32 | */ 33 | - (instancetype)initWithIvar:(Ivar)ivar srcObject:(id)srcObject; 34 | @end 35 | 36 | /** 37 | * 遍历成员变量用的block 38 | * 39 | * @param ivar 成员变量的包装对象 40 | * @param stop YES代表停止遍历,NO代表继续遍历 41 | */ 42 | typedef void (^MJIvarsBlock)(MJIvar *ivar, BOOL *stop); -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJIvar.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJIvar.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "MJIvar.h" 10 | #import "MJTypeEncoding.h" 11 | #import "MJConst.h" 12 | 13 | @implementation MJIvar 14 | /** 15 | * 初始化 16 | * 17 | * @param ivar 成员变量 18 | * @param srcObject 哪个对象的成员变量 19 | * 20 | * @return 初始化好的对象 21 | */ 22 | - (instancetype)initWithIvar:(Ivar)ivar srcObject:(id)srcObject 23 | { 24 | if (self = [super initWithSrcObject:srcObject]) { 25 | self.ivar = ivar; 26 | } 27 | return self; 28 | } 29 | 30 | /** 31 | * 设置成员变量 32 | */ 33 | - (void)setIvar:(Ivar)ivar 34 | { 35 | _ivar = ivar; 36 | 37 | MJAssertParamNotNil(ivar); 38 | 39 | // 1.成员变量名 40 | _name = [NSString stringWithUTF8String:ivar_getName(ivar)]; 41 | 42 | // 2.属性名 43 | if ([_name hasPrefix:@"_"]) { 44 | _propertyName = [_name stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""]; 45 | } else { 46 | _propertyName = _name; 47 | } 48 | 49 | // 3.成员变量的类型符 50 | NSString *code = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)]; 51 | _type = [[MJType alloc] initWithCode:code]; 52 | } 53 | 54 | /** 55 | * 获得成员变量的值 56 | */ 57 | - (id)value 58 | { 59 | if (_type.KVCDisabled) return [NSNull null]; 60 | return [_srcObject valueForKey:_propertyName]; 61 | } 62 | 63 | /** 64 | * 设置成员变量的值 65 | */ 66 | - (void)setValue:(id)value 67 | { 68 | if (_type.KVCDisabled) return; 69 | [_srcObject setValue:value forKey:_propertyName]; 70 | } 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJMember.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJMember.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import "MJType.h" 12 | #import "MJArgument.h" 13 | 14 | @interface MJMember : NSObject 15 | { 16 | __weak id _srcObject; 17 | NSString *_name; 18 | } 19 | /** 成员来源于哪个类(可能是父类) */ 20 | @property (nonatomic, assign) Class srcClass; 21 | /** 成员来源类是否是Foundation框架的 */ 22 | @property (nonatomic, readonly, getter = isSrcClassFromFoundation) BOOL srcClassFromFoundation; 23 | 24 | /** 成员来源于哪个对象 */ 25 | @property (nonatomic, weak, readonly) id srcObject; 26 | 27 | /** 成员名 */ 28 | @property (nonatomic, copy, readonly) NSString *name; 29 | 30 | /** 31 | * 初始化 32 | * 33 | * @param srcObject 来源于哪个对象 34 | * 35 | * @return 初始化好的对象 36 | */ 37 | - (instancetype)initWithSrcObject:(id)srcObject; 38 | @end 39 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJMember.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJMember.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "MJMember.h" 10 | #import "MJExtension.h" 11 | #import "MJFoundation.h" 12 | #import "MJConst.h" 13 | 14 | @implementation MJMember 15 | 16 | 17 | /** 18 | * 初始化 19 | * 20 | * @param srcObject 来源于哪个对象 21 | * 22 | * @return 初始化好的对象 23 | */ 24 | - (instancetype)initWithSrcObject:(id)srcObject 25 | { 26 | if (self = [super init]) { 27 | _srcObject = srcObject; 28 | } 29 | return self; 30 | } 31 | 32 | - (void)setSrcClass:(Class)srcClass 33 | { 34 | _srcClass = srcClass; 35 | 36 | MJAssertParamNotNil(srcClass); 37 | 38 | _srcClassFromFoundation = [MJFoundation isClassFromFoundation:srcClass]; 39 | } 40 | 41 | MJLogAllIvrs 42 | @end 43 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJMethod.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJMethod.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 包装一个方法 8 | 9 | #import "MJMember.h" 10 | /** 11 | * 包装一个方法 12 | */ 13 | @interface MJMethod : MJMember 14 | /** 方法 */ 15 | @property (nonatomic, assign) Method method; 16 | /** 方法名 */ 17 | @property (nonatomic, assign, readonly) SEL selector; 18 | /** 所有的参数(都是MJArgument对象) */ 19 | @property (nonatomic, strong, readonly) NSArray *arguments; 20 | /** 返回值类型 */ 21 | @property (nonatomic, copy, readonly) NSString *returnType; 22 | /** 23 | * 初始化 24 | * 25 | * @param method 方法 26 | * @param srcObject 哪个对象的方法 27 | * 28 | * @return 初始化好的对象 29 | */ 30 | - (instancetype)initWithMethod:(Method)method srcObject:(id)srcObject; 31 | @end 32 | 33 | /** 34 | * 遍历方法用的block 35 | * 36 | * @param method 方法的包装对象 37 | * @param stop YES代表停止遍历,NO代表继续遍历 38 | */ 39 | typedef void (^MJMethodsBlock)(MJMethod *method, BOOL *stop); -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJMethod.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJMethod.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "MJMethod.h" 10 | #import "MJConst.h" 11 | 12 | @implementation MJMethod 13 | /** 14 | * 初始化 15 | * 16 | * @param method 方法 17 | * @param srcObject 哪个对象的方法 18 | * 19 | * @return 初始化好的对象 20 | */ 21 | - (instancetype)initWithMethod:(Method)method srcObject:(id)srcObject 22 | { 23 | if (self = [super initWithSrcObject:srcObject]) { 24 | self.method = method; 25 | } 26 | return self; 27 | } 28 | 29 | /** 30 | * 设置方法 31 | */ 32 | - (void)setMethod:(Method)method 33 | { 34 | _method = method; 35 | 36 | MJAssertParamNotNil(method); 37 | 38 | // 1.方法选择器 39 | _selector = method_getName(method); 40 | _name = NSStringFromSelector(self.selector); 41 | 42 | // 2.参数 43 | int step = 2; // 跳过前面的2个参数 44 | int argsCount = method_getNumberOfArguments(method); 45 | NSMutableArray *args = [NSMutableArray arrayWithCapacity:argsCount - step]; 46 | for (int i = step; i 10 | /** 11 | * 包装一种类型 12 | */ 13 | @interface MJType : NSObject 14 | /** 类型标识符 */ 15 | @property (nonatomic, copy) NSString *code; 16 | 17 | /** 对象类型(如果是基本数据类型,此值为nil) */ 18 | @property (nonatomic, assign, readonly) Class typeClass; 19 | 20 | /** 类型是否来自于Foundation框架,比如NSString、NSArray */ 21 | @property (nonatomic, readonly, getter = isFromFoundation) BOOL fromFoundation; 22 | /** 类型是否不支持KVC */ 23 | @property (nonatomic, readonly, getter = isKVCDisabled) BOOL KVCDisabled; 24 | 25 | /** 26 | * 初始化一个类型对象 27 | * 28 | * @param code 类型标识符 29 | */ 30 | - (instancetype)initWithCode:(NSString *)code; 31 | @end -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJType.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJType.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "MJType.h" 10 | #import "MJExtension.h" 11 | #import "MJFoundation.h" 12 | #import "MJConst.h" 13 | 14 | @implementation MJType 15 | 16 | - (instancetype)initWithCode:(NSString *)code 17 | { 18 | if (self = [super init]) { 19 | self.code = code; 20 | } 21 | return self; 22 | } 23 | 24 | /** 类型标识符 */ 25 | - (void)setCode:(NSString *)code 26 | { 27 | _code = code; 28 | 29 | MJAssertParamNotNil(code); 30 | 31 | if (code.length == 0 || [code isEqualToString:MJTypeSEL] || 32 | [code isEqualToString:MJTypeIvar] || 33 | [code isEqualToString:MJTypeMethod]) { 34 | _KVCDisabled = YES; 35 | } else if ([code hasPrefix:@"@"] && code.length > 3) { 36 | // 去掉@"和",截取中间的类型名称 37 | _code = [code substringFromIndex:2]; 38 | _code = [_code substringToIndex:_code.length - 1]; 39 | _typeClass = NSClassFromString(_code); 40 | 41 | _fromFoundation = [MJFoundation isClassFromFoundation:_typeClass]; 42 | } 43 | } 44 | 45 | MJLogAllIvrs 46 | @end 47 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJTypeEncoding.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJTypeEncoding.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | /** 9 | * 类型(属性类型) 10 | */ 11 | extern NSString *const MJTypeInt; 12 | extern NSString *const MJTypeFloat; 13 | extern NSString *const MJTypeDouble; 14 | extern NSString *const MJTypeLong; 15 | extern NSString *const MJTypeLongLong; 16 | extern NSString *const MJTypeChar; 17 | extern NSString *const MJTypeBOOL; 18 | extern NSString *const MJTypePointer; 19 | 20 | extern NSString *const MJTypeIvar; 21 | extern NSString *const MJTypeMethod; 22 | extern NSString *const MJTypeBlock; 23 | extern NSString *const MJTypeClass; 24 | extern NSString *const MJTypeSEL; 25 | extern NSString *const MJTypeId; 26 | 27 | /** 28 | * 返回值类型 29 | */ 30 | extern NSString *const MJReturnTypeVoid; 31 | extern NSString *const MJReturnTypeObject; 32 | 33 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/MJTypeEncoding.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJTypeEncoding.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | /** 9 | * 成员变量类型(属性类型) 10 | */ 11 | #import 12 | NSString *const MJTypeInt = @"i"; 13 | NSString *const MJTypeFloat = @"f"; 14 | NSString *const MJTypeDouble = @"d"; 15 | NSString *const MJTypeLong = @"q"; 16 | NSString *const MJTypeLongLong = @"q"; 17 | NSString *const MJTypeChar = @"c"; 18 | NSString *const MJTypeBOOL = @"c"; 19 | NSString *const MJTypePointer = @"*"; 20 | 21 | NSString *const MJTypeIvar = @"^{objc_ivar=}"; 22 | NSString *const MJTypeMethod = @"^{objc_method=}"; 23 | NSString *const MJTypeBlock = @"@?"; 24 | NSString *const MJTypeClass = @"#"; 25 | NSString *const MJTypeSEL = @":"; 26 | NSString *const MJTypeId = @"@"; 27 | 28 | /** 29 | * 返回值类型(如果是unsigned,就是大写) 30 | */ 31 | NSString *const MJReturnTypeVoid = @"v"; 32 | NSString *const MJReturnTypeObject = @"@"; 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/NSObject+MJCoding.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJCoding.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSObject (MJCoding) 12 | /** 13 | * 解码(从文件中解析对象) 14 | */ 15 | - (void)decode:(NSCoder *)decoder; 16 | /** 17 | * 编码(将对象写入文件中) 18 | */ 19 | - (void)encode:(NSCoder *)encoder; 20 | @end 21 | 22 | /** 23 | 归档的实现 24 | */ 25 | #define MJCodingImplementation \ 26 | - (id)initWithCoder:(NSCoder *)decoder \ 27 | { \ 28 | if (self = [super init]) { \ 29 | [self decode:decoder]; \ 30 | } \ 31 | return self; \ 32 | } \ 33 | \ 34 | - (void)encodeWithCoder:(NSCoder *)encoder \ 35 | { \ 36 | [self encode:encoder]; \ 37 | } -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/NSObject+MJCoding.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJCoding.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "NSObject+MJCoding.h" 10 | #import "NSObject+MJMember.h" 11 | 12 | @implementation NSObject (MJCoding) 13 | /** 14 | * 编码(将对象写入文件中) 15 | */ 16 | - (void)encode:(NSCoder *)encoder 17 | { 18 | [self enumerateIvarsWithBlock:^(MJIvar *ivar, BOOL *stop) { 19 | if (ivar.isSrcClassFromFoundation) return; 20 | [encoder encodeObject:ivar.value forKey:ivar.name]; 21 | }]; 22 | } 23 | 24 | /** 25 | * 解码(从文件中解析对象) 26 | */ 27 | - (void)decode:(NSCoder *)decoder 28 | { 29 | [self enumerateIvarsWithBlock:^(MJIvar *ivar, BOOL *stop) { 30 | if (ivar.isSrcClassFromFoundation) return; 31 | ivar.value = [decoder decodeObjectForKey:ivar.name]; 32 | }]; 33 | } 34 | @end 35 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/NSObject+MJKeyValue.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJKeyValue.h 3 | // MJExtension 4 | // 5 | // Created by mj on 13-8-24. 6 | // Copyright (c) 2013年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | * KeyValue协议 13 | */ 14 | @protocol MJKeyValue 15 | @optional 16 | /** 17 | * 将属性名换为其他key去字典中取值 18 | * 19 | * @return 字典中的key是属性名,value是从字典中取值用的key 20 | */ 21 | - (NSDictionary *)replacedKeyFromPropertyName; 22 | 23 | /** 24 | * 数组中需要转换的模型类 25 | * 26 | * @return 字典中的key是数组属性名,value是数组中存放模型的Class 27 | */ 28 | - (NSDictionary *)objectClassInArray; 29 | 30 | /** 31 | * 当字典转模型完毕时调用 32 | */ 33 | - (void)keyValuesDidFinishConvertingToObject; 34 | 35 | /** 36 | * 当模型转字典完毕时调用 37 | */ 38 | - (void)objectDidFinishConvertingToKeyValues; 39 | @end 40 | 41 | @interface NSObject (MJKeyValue) 42 | /** 43 | * 将字典的键值对转成模型属性 44 | * @param keyValues 字典 45 | */ 46 | - (void)setKeyValues:(NSDictionary *)keyValues; 47 | 48 | /** 49 | * 将模型转成字典 50 | * @return 字典 51 | */ 52 | - (NSDictionary *)keyValues; 53 | 54 | /** 55 | * 通过模型数组来创建一个字典数组 56 | * @param objectArray 模型数组 57 | * @return 字典数组 58 | */ 59 | + (NSArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray; 60 | 61 | #pragma mark - 字典转模型 62 | /** 63 | * 通过JSON数据来创建一个模型 64 | * @param data JSON数据 65 | * @return 新建的对象 66 | */ 67 | + (instancetype)objectWithJSONData:(NSData *)data; 68 | 69 | /** 70 | * 通过字典来创建一个模型 71 | * @param keyValues 字典 72 | * @return 新建的对象 73 | */ 74 | + (instancetype)objectWithKeyValues:(NSDictionary *)keyValues; 75 | 76 | /** 77 | * 通过plist来创建一个模型 78 | * @param filename 文件名(仅限于mainBundle中的文件) 79 | * @return 新建的对象 80 | */ 81 | + (instancetype)objectWithFilename:(NSString *)filename; 82 | 83 | /** 84 | * 通过plist来创建一个模型 85 | * @param file 文件全路径 86 | * @return 新建的对象 87 | */ 88 | + (instancetype)objectWithFile:(NSString *)file; 89 | 90 | #pragma mark - 字典数组转模型数组 91 | /** 92 | * 通过JSON数据来创建一个模型数组 93 | * @param data JSON数据 94 | * @return 新建的对象 95 | */ 96 | + (NSArray *)objectArrayWithJSONData:(NSData *)data; 97 | 98 | /** 99 | * 通过字典数组来创建一个模型数组 100 | * @param keyValuesArray 字典数组 101 | * @return 模型数组 102 | */ 103 | + (NSArray *)objectArrayWithKeyValuesArray:(NSArray *)keyValuesArray; 104 | 105 | /** 106 | * 通过plist来创建一个模型数组 107 | * @param filename 文件名(仅限于mainBundle中的文件) 108 | * @return 模型数组 109 | */ 110 | + (NSArray *)objectArrayWithFilename:(NSString *)filename; 111 | 112 | /** 113 | * 通过plist来创建一个模型数组 114 | * @param file 文件全路径 115 | * @return 模型数组 116 | */ 117 | + (NSArray *)objectArrayWithFile:(NSString *)file; 118 | @end 119 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/NSObject+MJKeyValue.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJKeyValue.m 3 | // MJExtension 4 | // 5 | // Created by mj on 13-8-24. 6 | // Copyright (c) 2013年 itcast. All rights reserved. 7 | // 8 | 9 | #import "NSObject+MJKeyValue.h" 10 | #import "NSObject+MJMember.h" 11 | #import "MJConst.h" 12 | 13 | @implementation NSObject (MJKeyValue) 14 | #pragma mark - 公共方法 15 | #pragma mark - 字典转模型 16 | /** 17 | * 通过JSON数据来创建一个模型 18 | * @param data JSON数据 19 | * @return 新建的对象 20 | */ 21 | + (instancetype)objectWithJSONData:(NSData *)data 22 | { 23 | MJAssertParamNotNil(data); 24 | 25 | NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 26 | return [self objectWithKeyValues:dict]; 27 | } 28 | 29 | /** 30 | * 通过字典来创建一个模型 31 | * @param keyValues 字典 32 | * @return 新建的对象 33 | */ 34 | + (instancetype)objectWithKeyValues:(NSDictionary *)keyValues 35 | { 36 | NSString *desc = [NSString stringWithFormat:@"keyValues is not a NSDictionary - keyValues参数不是一个字典, keyValues is a %@ - keyValues参数是一个%@", keyValues.class, keyValues.class]; 37 | MJAssert([keyValues isKindOfClass:[NSDictionary class]], desc); 38 | 39 | id model = [[self alloc] init]; 40 | [model setKeyValues:keyValues]; 41 | return model; 42 | } 43 | 44 | /** 45 | * 通过plist来创建一个模型 46 | * @param filename 文件名(仅限于mainBundle中的文件) 47 | * @return 新建的对象 48 | */ 49 | + (instancetype)objectWithFilename:(NSString *)filename 50 | { 51 | MJAssertParamNotNil(filename); 52 | NSString *file = [[NSBundle mainBundle] pathForResource:filename ofType:nil]; 53 | return [self objectWithFile:file]; 54 | } 55 | 56 | /** 57 | * 通过plist来创建一个模型 58 | * @param file 文件全路径 59 | * @return 新建的对象 60 | */ 61 | + (instancetype)objectWithFile:(NSString *)file 62 | { 63 | MJAssertParamNotNil(file); 64 | NSDictionary *keyValues = [NSDictionary dictionaryWithContentsOfFile:file]; 65 | return [self objectWithKeyValues:keyValues]; 66 | } 67 | 68 | /** 69 | * 将字典的键值对转成模型属性 70 | * @param keyValues 字典 71 | */ 72 | - (void)setKeyValues:(NSDictionary *)keyValues 73 | { 74 | NSString *desc = [NSString stringWithFormat:@"keyValues is not a NSDictionary - keyValues参数不是一个字典, keyValues is a %@ - keyValues参数是一个%@", keyValues.class, keyValues.class]; 75 | MJAssert([keyValues isKindOfClass:[NSDictionary class]], desc); 76 | 77 | [self enumerateIvarsWithBlock:^(MJIvar *ivar, BOOL *stop) { 78 | // 来自Foundation框架的成员变量,直接返回 79 | if (ivar.isSrcClassFromFoundation) return; 80 | 81 | // 1.取出属性值 82 | NSString *key = [self keyWithPropertyName:ivar.propertyName]; 83 | id value = keyValues[key]; 84 | if (!value || [value isKindOfClass:[NSNull class]]) return; 85 | 86 | // 2.如果是模型属性 87 | if (ivar.type.typeClass && !ivar.type.isFromFoundation) { 88 | value = [ivar.type.typeClass objectWithKeyValues:value]; 89 | } else if ([self respondsToSelector:@selector(objectClassInArray)]) { 90 | // 3.字典数组-->模型数组 91 | Class objectClass = self.objectClassInArray[ivar.propertyName]; 92 | if (objectClass) { 93 | value = [objectClass objectArrayWithKeyValuesArray:value]; 94 | } 95 | } 96 | 97 | // 4.赋值 98 | ivar.value = value; 99 | }]; 100 | 101 | // 转换完毕 102 | if ([self respondsToSelector:@selector(keyValuesDidFinishConvertingToObject)]) { 103 | [self keyValuesDidFinishConvertingToObject]; 104 | } 105 | } 106 | 107 | /** 108 | * 将模型转成字典 109 | * @return 字典 110 | */ 111 | - (NSDictionary *)keyValues 112 | { 113 | NSMutableDictionary *keyValues = [NSMutableDictionary dictionary]; 114 | 115 | [self enumerateIvarsWithBlock:^(MJIvar *ivar, BOOL *stop) { 116 | if (ivar.isSrcClassFromFoundation) return; 117 | 118 | // 1.取出属性值 119 | id value = ivar.value; 120 | if (!value) return; 121 | 122 | // 2.如果是模型属性 123 | if (ivar.type.typeClass && !ivar.type.isFromFoundation) { 124 | value = [value keyValues]; 125 | } else if ([self respondsToSelector:@selector(objectClassInArray)]) { 126 | // 3.处理数组里面有模型的情况 127 | Class objectClass = self.objectClassInArray[ivar.propertyName]; 128 | if (objectClass) { 129 | value = [objectClass keyValuesArrayWithObjectArray:value]; 130 | } 131 | } 132 | 133 | // 4.赋值 134 | NSString *key = [self keyWithPropertyName:ivar.propertyName]; 135 | keyValues[key] = value; 136 | }]; 137 | 138 | // 转换完毕 139 | if ([self respondsToSelector:@selector(objectDidFinishConvertingToKeyValues)]) { 140 | [self objectDidFinishConvertingToKeyValues]; 141 | } 142 | 143 | return keyValues; 144 | } 145 | 146 | /** 147 | * 通过JSON数据来创建一个模型数组 148 | * @param data JSON数据 149 | * @return 新建的对象 150 | */ 151 | + (NSArray *)objectArrayWithJSONData:(NSData *)data 152 | { 153 | MJAssertParamNotNil(data); 154 | 155 | NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 156 | return [self objectArrayWithKeyValuesArray:array]; 157 | } 158 | 159 | /** 160 | * 通过模型数组来创建一个字典数组 161 | * @param objectArray 模型数组 162 | * @return 字典数组 163 | */ 164 | + (NSArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray 165 | { 166 | // 0.判断真实性 167 | NSString *desc = [NSString stringWithFormat:@"objectArray is not a NSArray - objectArray不是一个数组, objectArray is a %@ - objectArray参数是一个%@", objectArray.class, objectArray.class]; 168 | MJAssert([objectArray isKindOfClass:[NSArray class]], desc); 169 | 170 | // 1.过滤 171 | if (![objectArray isKindOfClass:[NSArray class]]) return objectArray; 172 | if (![[objectArray lastObject] isKindOfClass:self]) return objectArray; 173 | 174 | // 2.创建数组 175 | NSMutableArray *keyValuesArray = [NSMutableArray array]; 176 | for (id object in objectArray) { 177 | [keyValuesArray addObject:[object keyValues]]; 178 | } 179 | return keyValuesArray; 180 | } 181 | 182 | #pragma mark - 字典数组转模型数组 183 | /** 184 | * 通过字典数组来创建一个模型数组 185 | * @param keyValuesArray 字典数组 186 | * @return 模型数组 187 | */ 188 | + (NSArray *)objectArrayWithKeyValuesArray:(NSArray *)keyValuesArray 189 | { 190 | // 1.判断真实性 191 | NSString *desc = [NSString stringWithFormat:@"keyValuesArray is not a keyValuesArray - keyValuesArray不是一个数组, keyValuesArray is a %@ - keyValuesArray参数是一个%@", keyValuesArray.class, keyValuesArray.class]; 192 | MJAssert([keyValuesArray isKindOfClass:[NSArray class]], desc); 193 | 194 | // 2.创建数组 195 | NSMutableArray *modelArray = [NSMutableArray array]; 196 | 197 | // 3.遍历 198 | for (NSDictionary *keyValues in keyValuesArray) { 199 | if (![keyValues isKindOfClass:[NSDictionary class]]) continue; 200 | 201 | id model = [self objectWithKeyValues:keyValues]; 202 | [modelArray addObject:model]; 203 | } 204 | 205 | return modelArray; 206 | } 207 | 208 | /** 209 | * 通过plist来创建一个模型数组 210 | * @param filename 文件名(仅限于mainBundle中的文件) 211 | * @return 模型数组 212 | */ 213 | + (NSArray *)objectArrayWithFilename:(NSString *)filename 214 | { 215 | MJAssertParamNotNil(filename); 216 | NSString *file = [[NSBundle mainBundle] pathForResource:filename ofType:nil]; 217 | return [self objectArrayWithFile:file]; 218 | } 219 | 220 | /** 221 | * 通过plist来创建一个模型数组 222 | * @param file 文件全路径 223 | * @return 模型数组 224 | */ 225 | + (NSArray *)objectArrayWithFile:(NSString *)file 226 | { 227 | MJAssertParamNotNil(file); 228 | NSArray *keyValuesArray = [NSArray arrayWithContentsOfFile:file]; 229 | return [self objectArrayWithKeyValuesArray:keyValuesArray]; 230 | } 231 | 232 | #pragma mark - 私有方法 233 | /** 234 | * 根据属性名获得对应的key 235 | * 236 | * @param propertyName 属性名 237 | * 238 | * @return 字典的key 239 | */ 240 | - (NSString *)keyWithPropertyName:(NSString *)propertyName 241 | { 242 | MJAssertParamNotNil(propertyName); 243 | NSString *key = nil; 244 | // 1.查看有没有需要替换的key 245 | if ([self respondsToSelector:@selector(replacedKeyFromPropertyName)]) { 246 | key = self.replacedKeyFromPropertyName[propertyName]; 247 | } 248 | // 2.用属性名作为key 249 | if (!key) key = propertyName; 250 | 251 | return key; 252 | } 253 | @end 254 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/NSObject+MJMember.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJMember.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "MJIvar.h" 11 | #import "MJMethod.h" 12 | 13 | /** 14 | * 遍历所有类的block(父类) 15 | */ 16 | typedef void (^MJClassesBlock)(Class c, BOOL *stop); 17 | 18 | @interface NSObject (MJMember) 19 | 20 | /** 21 | * 遍历所有的成员变量 22 | */ 23 | - (void)enumerateIvarsWithBlock:(MJIvarsBlock)block; 24 | 25 | /** 26 | * 遍历所有的方法 27 | */ 28 | - (void)enumerateMethodsWithBlock:(MJMethodsBlock)block; 29 | 30 | /** 31 | * 遍历所有的类 32 | */ 33 | - (void)enumerateClassesWithBlock:(MJClassesBlock)block; 34 | @end 35 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJExtension/NSObject+MJMember.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJMember.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "NSObject+MJMember.h" 10 | 11 | 12 | @implementation NSObject (MJMember) 13 | 14 | /** 15 | * 遍历所有的成员变量 16 | */ 17 | - (void)enumerateIvarsWithBlock:(MJIvarsBlock)block 18 | { 19 | [self enumerateClassesWithBlock:^(__unsafe_unretained Class c, BOOL *stop) { 20 | // 1.获得所有的成员变量 21 | unsigned int outCount = 0; 22 | Ivar *ivars = class_copyIvarList(c, &outCount); 23 | 24 | // 2.遍历每一个成员变量 25 | for (int i = 0; i [self.tableView headerBeginRefreshing]; 22 | 2> [self.tableView footerBeginRefreshing]; 23 | 24 | 6.结束刷新 25 | 1> [self.tableView headerEndRefreshing]; 26 | 2> [self.tableView footerEndRefreshing]; 27 | */ -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/MJRefreshBaseView.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefreshBaseView.h 3 | // MJRefresh 4 | // 5 | // Created by mj on 13-3-4. 6 | // Copyright (c) 2013年 itcast. All rights reserved. 7 | 8 | #import 9 | 10 | @class MJRefreshBaseView; 11 | 12 | #pragma mark - 控件的刷新状态 13 | typedef enum { 14 | MJRefreshStatePulling = 1, // 松开就可以进行刷新的状态 15 | MJRefreshStateNormal = 2, // 普通状态 16 | MJRefreshStateRefreshing = 3, // 正在刷新中的状态 17 | MJRefreshStateWillRefreshing = 4 18 | } MJRefreshState; 19 | 20 | #pragma mark - 控件的类型 21 | typedef enum { 22 | MJRefreshViewTypeHeader = -1, // 头部控件 23 | MJRefreshViewTypeFooter = 1 // 尾部控件 24 | } MJRefreshViewType; 25 | 26 | /** 27 | 类的声明 28 | */ 29 | @interface MJRefreshBaseView : UIView 30 | #pragma mark - 父控件 31 | @property (nonatomic, weak, readonly) UIScrollView *scrollView; 32 | @property (nonatomic, assign, readonly) UIEdgeInsets scrollViewOriginalInset; 33 | 34 | #pragma mark - 内部的控件 35 | @property (nonatomic, weak, readonly) UILabel *statusLabel; 36 | @property (nonatomic, weak, readonly) UIImageView *arrowImage; 37 | @property (nonatomic, weak, readonly) UIActivityIndicatorView *activityView; 38 | 39 | #pragma mark - 回调 40 | /** 41 | * 开始进入刷新状态的监听器 42 | */ 43 | @property (weak, nonatomic) id beginRefreshingTaget; 44 | /** 45 | * 开始进入刷新状态的监听方法 46 | */ 47 | @property (assign, nonatomic) SEL beginRefreshingAction; 48 | /** 49 | * 开始进入刷新状态就会调用 50 | */ 51 | @property (nonatomic, copy) void (^beginRefreshingCallback)(); 52 | 53 | #pragma mark - 刷新相关 54 | /** 55 | * 是否正在刷新 56 | */ 57 | @property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing; 58 | /** 59 | * 开始刷新 60 | */ 61 | - (void)beginRefreshing; 62 | /** 63 | * 结束刷新 64 | */ 65 | - (void)endRefreshing; 66 | 67 | #pragma mark - 交给子类去实现 和 调用 68 | @property (assign, nonatomic) MJRefreshState state; 69 | @end -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/MJRefreshBaseView.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefreshBaseView.m 3 | // MJRefresh 4 | // 5 | // Created by mj on 13-3-4. 6 | // Copyright (c) 2013年 itcast. All rights reserved. 7 | // 8 | 9 | #import "MJRefreshBaseView.h" 10 | #import "MJRefreshConst.h" 11 | #import "UIView+Extension.h" 12 | #import "UIScrollView+Extension.h" 13 | #import 14 | 15 | @interface MJRefreshBaseView() 16 | { 17 | __weak UILabel *_statusLabel; 18 | __weak UIImageView *_arrowImage; 19 | __weak UIActivityIndicatorView *_activityView; 20 | } 21 | @end 22 | 23 | @implementation MJRefreshBaseView 24 | #pragma mark - 控件初始化 25 | /** 26 | * 状态标签 27 | */ 28 | - (UILabel *)statusLabel 29 | { 30 | if (!_statusLabel) { 31 | UILabel *statusLabel = [[UILabel alloc] init]; 32 | statusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; 33 | statusLabel.font = [UIFont boldSystemFontOfSize:13]; 34 | statusLabel.textColor = MJRefreshLabelTextColor; 35 | statusLabel.backgroundColor = [UIColor clearColor]; 36 | statusLabel.textAlignment = NSTextAlignmentCenter; 37 | [self addSubview:_statusLabel = statusLabel]; 38 | } 39 | return _statusLabel; 40 | } 41 | 42 | /** 43 | * 箭头图片 44 | */ 45 | - (UIImageView *)arrowImage 46 | { 47 | if (!_arrowImage) { 48 | UIImageView *arrowImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MJRefreshSrcName(@"arrow.png")]]; 49 | arrowImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 50 | [self addSubview:_arrowImage = arrowImage]; 51 | } 52 | return _arrowImage; 53 | } 54 | 55 | /** 56 | * 状态标签 57 | */ 58 | - (UIActivityIndicatorView *)activityView 59 | { 60 | if (!_activityView) { 61 | UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 62 | activityView.bounds = self.arrowImage.bounds; 63 | activityView.autoresizingMask = self.arrowImage.autoresizingMask; 64 | [self addSubview:_activityView = activityView]; 65 | } 66 | return _activityView; 67 | } 68 | 69 | #pragma mark - 初始化方法 70 | - (instancetype)initWithFrame:(CGRect)frame { 71 | frame.size.height = MJRefreshViewHeight; 72 | if (self = [super initWithFrame:frame]) { 73 | // 1.自己的属性 74 | self.autoresizingMask = UIViewAutoresizingFlexibleWidth; 75 | self.backgroundColor = [UIColor clearColor]; 76 | 77 | // 2.设置默认状态 78 | self.state = MJRefreshStateNormal; 79 | } 80 | return self; 81 | } 82 | 83 | - (void)layoutSubviews 84 | { 85 | [super layoutSubviews]; 86 | 87 | // 1.箭头 88 | CGFloat arrowX = self.width * 0.5 - 100; 89 | self.arrowImage.center = CGPointMake(arrowX, self.height * 0.5); 90 | 91 | // 2.指示器 92 | self.activityView.center = self.arrowImage.center; 93 | } 94 | 95 | - (void)willMoveToSuperview:(UIView *)newSuperview 96 | { 97 | [super willMoveToSuperview:newSuperview]; 98 | 99 | // 旧的父控件 100 | [self.superview removeObserver:self forKeyPath:MJRefreshContentOffset context:nil]; 101 | 102 | if (newSuperview) { // 新的父控件 103 | [newSuperview addObserver:self forKeyPath:MJRefreshContentOffset options:NSKeyValueObservingOptionNew context:nil]; 104 | 105 | // 设置宽度 106 | self.width = newSuperview.width; 107 | // 设置位置 108 | self.x = 0; 109 | 110 | // 记录UIScrollView 111 | _scrollView = (UIScrollView *)newSuperview; 112 | // 记录UIScrollView最开始的contentInset 113 | _scrollViewOriginalInset = _scrollView.contentInset; 114 | } 115 | } 116 | 117 | #pragma mark - 显示到屏幕上 118 | - (void)drawRect:(CGRect)rect 119 | { 120 | if (self.state == MJRefreshStateWillRefreshing) { 121 | self.state = MJRefreshStateRefreshing; 122 | } 123 | } 124 | 125 | #pragma mark - 刷新相关 126 | #pragma mark 是否正在刷新 127 | - (BOOL)isRefreshing 128 | { 129 | return MJRefreshStateRefreshing == self.state; 130 | } 131 | 132 | #pragma mark 开始刷新 133 | - (void)beginRefreshing 134 | { 135 | if (self.window) { 136 | self.state = MJRefreshStateRefreshing; 137 | } else { 138 | #warning 不能调用set方法 139 | _state = MJRefreshStateWillRefreshing; 140 | [super setNeedsDisplay]; 141 | } 142 | } 143 | 144 | #pragma mark 结束刷新 145 | - (void)endRefreshing 146 | { 147 | double delayInSeconds = 0.3; 148 | dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 149 | dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 150 | self.state = MJRefreshStateNormal; 151 | }); 152 | } 153 | 154 | #pragma mark - 设置状态 155 | - (void)setState:(MJRefreshState)state 156 | { 157 | // 0.存储当前的contentInset 158 | if (self.state != MJRefreshStateRefreshing) { 159 | _scrollViewOriginalInset = self.scrollView.contentInset; 160 | } 161 | 162 | // 1.一样的就直接返回 163 | if (self.state == state) return; 164 | 165 | // 2.根据状态执行不同的操作 166 | switch (state) { 167 | case MJRefreshStateNormal: // 普通状态 168 | { 169 | // 显示箭头 170 | self.arrowImage.hidden = NO; 171 | 172 | // 停止转圈圈 173 | [self.activityView stopAnimating]; 174 | break; 175 | } 176 | 177 | case MJRefreshStatePulling: 178 | break; 179 | 180 | case MJRefreshStateRefreshing: 181 | { 182 | // 开始转圈圈 183 | [self.activityView startAnimating]; 184 | // 隐藏箭头 185 | self.arrowImage.hidden = YES; 186 | 187 | // 回调 188 | if ([self.beginRefreshingTaget respondsToSelector:self.beginRefreshingAction]) { 189 | objc_msgSend(self.beginRefreshingTaget, self.beginRefreshingAction, self); 190 | } 191 | 192 | if (self.beginRefreshingCallback) { 193 | self.beginRefreshingCallback(); 194 | } 195 | break; 196 | } 197 | default: 198 | break; 199 | } 200 | 201 | // 3.存储状态 202 | _state = state; 203 | } 204 | @end -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/MJRefreshConst.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefreshConst.h 3 | // MJRefresh 4 | // 5 | // Created by mj on 14-1-3. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | #import 9 | 10 | #ifdef DEBUG 11 | #define MJLog(...) NSLog(__VA_ARGS__) 12 | #else 13 | #define MJLog(...) 14 | #endif 15 | 16 | #define MJColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] 17 | // 文字颜色 18 | #define MJRefreshLabelTextColor MJColor(150, 150, 150) 19 | 20 | extern const CGFloat MJRefreshViewHeight; 21 | extern const CGFloat MJRefreshFastAnimationDuration; 22 | extern const CGFloat MJRefreshSlowAnimationDuration; 23 | 24 | extern NSString *const MJRefreshBundleName; 25 | #define MJRefreshSrcName(file) [MJRefreshBundleName stringByAppendingPathComponent:file] 26 | 27 | extern NSString *const MJRefreshFooterPullToRefresh; 28 | extern NSString *const MJRefreshFooterReleaseToRefresh; 29 | extern NSString *const MJRefreshFooterRefreshing; 30 | 31 | extern NSString *const MJRefreshHeaderPullToRefresh; 32 | extern NSString *const MJRefreshHeaderReleaseToRefresh; 33 | extern NSString *const MJRefreshHeaderRefreshing; 34 | extern NSString *const MJRefreshHeaderTimeKey; 35 | 36 | extern NSString *const MJRefreshContentOffset; 37 | extern NSString *const MJRefreshContentSize; -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/MJRefreshConst.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefreshConst.m 3 | // MJRefresh 4 | // 5 | // Created by mj on 14-1-3. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | const CGFloat MJRefreshViewHeight = 64.0; 12 | const CGFloat MJRefreshFastAnimationDuration = 0.25; 13 | const CGFloat MJRefreshSlowAnimationDuration = 0.4; 14 | 15 | NSString *const MJRefreshBundleName = @"MJRefresh.bundle"; 16 | 17 | NSString *const MJRefreshFooterPullToRefresh = @"上拉可以加载更多数据"; 18 | NSString *const MJRefreshFooterReleaseToRefresh = @"松开立即加载更多数据"; 19 | NSString *const MJRefreshFooterRefreshing = @"MJ哥正在帮你加载数据..."; 20 | 21 | NSString *const MJRefreshHeaderPullToRefresh = @"下拉可以刷新"; 22 | NSString *const MJRefreshHeaderReleaseToRefresh = @"松开立即刷新"; 23 | NSString *const MJRefreshHeaderRefreshing = @"MJ哥正在帮你刷新..."; 24 | NSString *const MJRefreshHeaderTimeKey = @"MJRefreshHeaderView"; 25 | 26 | NSString *const MJRefreshContentOffset = @"contentOffset"; 27 | NSString *const MJRefreshContentSize = @"contentSize"; -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/MJRefreshFooterView.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefreshTableFooterView.h 3 | // MJRefresh 4 | // 5 | // Created by mj on 13-2-26. 6 | // Copyright (c) 2013年 itcast. All rights reserved. 7 | // 上拉加载更多 8 | 9 | #import "MJRefreshBaseView.h" 10 | 11 | @interface MJRefreshFooterView : MJRefreshBaseView 12 | + (instancetype)footer; 13 | @end -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/MJRefreshFooterView.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefreshFooterView.m 3 | // MJRefresh 4 | // 5 | // Created by mj on 13-2-26. 6 | // Copyright (c) 2013年 itcast. All rights reserved. 7 | // 上拉加载更多 8 | 9 | #import "MJRefreshFooterView.h" 10 | #import "MJRefreshConst.h" 11 | #import "UIView+Extension.h" 12 | #import "UIScrollView+Extension.h" 13 | 14 | @interface MJRefreshFooterView() 15 | @property (assign, nonatomic) int lastRefreshCount; 16 | @end 17 | 18 | @implementation MJRefreshFooterView 19 | 20 | + (instancetype)footer 21 | { 22 | return [[MJRefreshFooterView alloc] init]; 23 | } 24 | 25 | - (void)layoutSubviews 26 | { 27 | [super layoutSubviews]; 28 | 29 | self.statusLabel.frame = self.bounds; 30 | } 31 | 32 | - (void)willMoveToSuperview:(UIView *)newSuperview 33 | { 34 | [super willMoveToSuperview:newSuperview]; 35 | 36 | // 旧的父控件 37 | [self.superview removeObserver:self forKeyPath:MJRefreshContentSize context:nil]; 38 | 39 | if (newSuperview) { // 新的父控件 40 | // 监听 41 | [newSuperview addObserver:self forKeyPath:MJRefreshContentSize options:NSKeyValueObservingOptionNew context:nil]; 42 | 43 | // 重新调整frame 44 | [self adjustFrameWithContentSize]; 45 | } 46 | } 47 | 48 | #pragma mark 重写调整frame 49 | - (void)adjustFrameWithContentSize 50 | { 51 | // 内容的高度 52 | CGFloat contentHeight = self.scrollView.contentSizeHeight; 53 | // 表格的高度 54 | CGFloat scrollHeight = self.scrollView.height - self.scrollViewOriginalInset.top - self.scrollViewOriginalInset.bottom; 55 | // 设置位置和尺寸 56 | self.y = MAX(contentHeight, scrollHeight); 57 | } 58 | 59 | #pragma mark 监听UIScrollView的属性 60 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 61 | { 62 | // 不能跟用户交互,直接返回 63 | if (!self.userInteractionEnabled || self.alpha <= 0.01 || self.hidden) return; 64 | 65 | if ([MJRefreshContentSize isEqualToString:keyPath]) { 66 | // 调整frame 67 | [self adjustFrameWithContentSize]; 68 | } else if ([MJRefreshContentOffset isEqualToString:keyPath]) { 69 | #warning 这个返回一定要放这个位置 70 | // 如果正在刷新,直接返回 71 | if (self.state == MJRefreshStateRefreshing) return; 72 | 73 | // 调整状态 74 | [self adjustStateWithContentOffset]; 75 | } 76 | } 77 | 78 | /** 79 | * 调整状态 80 | */ 81 | - (void)adjustStateWithContentOffset 82 | { 83 | // 当前的contentOffset 84 | CGFloat currentOffsetY = self.scrollView.contentOffsetY; 85 | // 尾部控件刚好出现的offsetY 86 | CGFloat happenOffsetY = [self happenOffsetY]; 87 | 88 | // 如果是向下滚动到看不见尾部控件,直接返回 89 | if (currentOffsetY <= happenOffsetY) return; 90 | 91 | if (self.scrollView.isDragging) { 92 | // 普通 和 即将刷新 的临界点 93 | CGFloat normal2pullingOffsetY = happenOffsetY + self.height; 94 | 95 | if (self.state == MJRefreshStateNormal && currentOffsetY > normal2pullingOffsetY) { 96 | // 转为即将刷新状态 97 | self.state = MJRefreshStatePulling; 98 | } else if (self.state == MJRefreshStatePulling && currentOffsetY <= normal2pullingOffsetY) { 99 | // 转为普通状态 100 | self.state = MJRefreshStateNormal; 101 | } 102 | } else if (self.state == MJRefreshStatePulling) {// 即将刷新 && 手松开 103 | // 开始刷新 104 | self.state = MJRefreshStateRefreshing; 105 | } 106 | } 107 | 108 | #pragma mark - 状态相关 109 | #pragma mark 设置状态 110 | - (void)setState:(MJRefreshState)state 111 | { 112 | // 1.一样的就直接返回 113 | if (self.state == state) return; 114 | 115 | // 2.保存旧状态 116 | MJRefreshState oldState = self.state; 117 | 118 | // 3.调用父类方法 119 | [super setState:state]; 120 | 121 | // 4.根据状态来设置属性 122 | switch (state) 123 | { 124 | case MJRefreshStateNormal: 125 | { 126 | // 设置文字 127 | self.statusLabel.text = MJRefreshFooterPullToRefresh; 128 | 129 | // 刷新完毕 130 | if (MJRefreshStateRefreshing == oldState) { 131 | self.arrowImage.transform = CGAffineTransformMakeRotation(M_PI); 132 | [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{ 133 | self.scrollView.contentInsetBottom = self.scrollViewOriginalInset.bottom; 134 | }]; 135 | } else { 136 | // 执行动画 137 | [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{ 138 | self.arrowImage.transform = CGAffineTransformMakeRotation(M_PI); 139 | }]; 140 | } 141 | 142 | CGFloat deltaH = [self heightForContentBreakView]; 143 | int currentCount = [self totalDataCountInScrollView]; 144 | // 刚刷新完毕 145 | if (MJRefreshStateRefreshing == oldState && deltaH > 0 && currentCount != self.lastRefreshCount) { 146 | self.scrollView.contentOffsetY = self.scrollView.contentOffsetY; 147 | } 148 | break; 149 | } 150 | 151 | case MJRefreshStatePulling: 152 | { 153 | // 设置文字 154 | self.statusLabel.text = MJRefreshFooterReleaseToRefresh; 155 | 156 | [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{ 157 | self.arrowImage.transform = CGAffineTransformIdentity; 158 | }]; 159 | break; 160 | } 161 | 162 | case MJRefreshStateRefreshing: 163 | { 164 | // 设置文字 165 | self.statusLabel.text = MJRefreshFooterRefreshing; 166 | 167 | // 记录刷新前的数量 168 | self.lastRefreshCount = [self totalDataCountInScrollView]; 169 | 170 | [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{ 171 | CGFloat bottom = self.height + self.scrollViewOriginalInset.bottom; 172 | CGFloat deltaH = [self heightForContentBreakView]; 173 | if (deltaH < 0) { // 如果内容高度小于view的高度 174 | bottom -= deltaH; 175 | } 176 | self.scrollView.contentInsetBottom = bottom; 177 | }]; 178 | break; 179 | } 180 | 181 | default: 182 | break; 183 | } 184 | } 185 | 186 | - (int)totalDataCountInScrollView 187 | { 188 | int totalCount = 0; 189 | if ([self.scrollView isKindOfClass:[UITableView class]]) { 190 | UITableView *tableView = (UITableView *)self.scrollView; 191 | 192 | for (int section = 0; section 0) { 220 | return deltaH - self.scrollViewOriginalInset.top; 221 | } else { 222 | return - self.scrollViewOriginalInset.top; 223 | } 224 | } 225 | @end -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/MJRefreshHeaderView.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefreshHeaderView.h 3 | // MJRefresh 4 | // 5 | // Created by mj on 13-2-26. 6 | // Copyright (c) 2013年 itcast. All rights reserved. 7 | // 下拉刷新 8 | 9 | #import "MJRefreshBaseView.h" 10 | 11 | @interface MJRefreshHeaderView : MJRefreshBaseView 12 | + (instancetype)header; 13 | @end -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/MJRefreshHeaderView.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefreshHeaderView.m 3 | // MJRefresh 4 | // 5 | // Created by mj on 13-2-26. 6 | // Copyright (c) 2013年 itcast. All rights reserved. 7 | // 下拉刷新 8 | 9 | #import "MJRefreshConst.h" 10 | #import "MJRefreshHeaderView.h" 11 | #import "UIView+Extension.h" 12 | #import "UIScrollView+Extension.h" 13 | 14 | @interface MJRefreshHeaderView() 15 | // 最后的更新时间 16 | @property (nonatomic, strong) NSDate *lastUpdateTime; 17 | @property (nonatomic, weak) UILabel *lastUpdateTimeLabel; 18 | @end 19 | 20 | @implementation MJRefreshHeaderView 21 | #pragma mark - 控件初始化 22 | /** 23 | * 时间标签 24 | */ 25 | - (UILabel *)lastUpdateTimeLabel 26 | { 27 | if (!_lastUpdateTimeLabel) { 28 | // 1.创建控件 29 | UILabel *lastUpdateTimeLabel = [[UILabel alloc] init]; 30 | lastUpdateTimeLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; 31 | lastUpdateTimeLabel.font = [UIFont boldSystemFontOfSize:12]; 32 | lastUpdateTimeLabel.textColor = MJRefreshLabelTextColor; 33 | lastUpdateTimeLabel.backgroundColor = [UIColor clearColor]; 34 | lastUpdateTimeLabel.textAlignment = NSTextAlignmentCenter; 35 | [self addSubview:_lastUpdateTimeLabel = lastUpdateTimeLabel]; 36 | 37 | // 2.加载时间 38 | self.lastUpdateTime = [[NSUserDefaults standardUserDefaults] objectForKey:MJRefreshHeaderTimeKey]; 39 | } 40 | return _lastUpdateTimeLabel; 41 | } 42 | 43 | + (instancetype)header 44 | { 45 | return [[MJRefreshHeaderView alloc] init]; 46 | } 47 | 48 | - (void)layoutSubviews 49 | { 50 | [super layoutSubviews]; 51 | 52 | CGFloat statusX = 0; 53 | CGFloat statusY = 0; 54 | CGFloat statusHeight = self.height * 0.5; 55 | CGFloat statusWidth = self.width; 56 | // 1.状态标签 57 | self.statusLabel.frame = CGRectMake(statusX, statusY, statusWidth, statusHeight); 58 | 59 | // 2.时间标签 60 | CGFloat lastUpdateY = statusHeight; 61 | CGFloat lastUpdateX = 0; 62 | CGFloat lastUpdateHeight = statusHeight; 63 | CGFloat lastUpdateWidth = statusWidth; 64 | self.lastUpdateTimeLabel.frame = CGRectMake(lastUpdateX, lastUpdateY, lastUpdateWidth, lastUpdateHeight); 65 | } 66 | 67 | - (void)willMoveToSuperview:(UIView *)newSuperview 68 | { 69 | [super willMoveToSuperview:newSuperview]; 70 | 71 | // 设置自己的位置和尺寸 72 | self.y = - self.height; 73 | } 74 | 75 | #pragma mark - 状态相关 76 | #pragma mark 设置最后的更新时间 77 | - (void)setLastUpdateTime:(NSDate *)lastUpdateTime 78 | { 79 | _lastUpdateTime = lastUpdateTime; 80 | 81 | // 1.归档 82 | [[NSUserDefaults standardUserDefaults] setObject:lastUpdateTime forKey:MJRefreshHeaderTimeKey]; 83 | [[NSUserDefaults standardUserDefaults] synchronize]; 84 | 85 | // 2.更新时间 86 | [self updateTimeLabel]; 87 | } 88 | 89 | #pragma mark 更新时间字符串 90 | - (void)updateTimeLabel 91 | { 92 | if (!self.lastUpdateTime) return; 93 | 94 | // 1.获得年月日 95 | NSCalendar *calendar = [NSCalendar currentCalendar]; 96 | NSUInteger unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit |NSMinuteCalendarUnit; 97 | NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:_lastUpdateTime]; 98 | NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]]; 99 | 100 | // 2.格式化日期 101 | NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 102 | if ([cmp1 day] == [cmp2 day]) { // 今天 103 | formatter.dateFormat = @"今天 HH:mm"; 104 | } else if ([cmp1 year] == [cmp2 year]) { // 今年 105 | formatter.dateFormat = @"MM-dd HH:mm"; 106 | } else { 107 | formatter.dateFormat = @"yyyy-MM-dd HH:mm"; 108 | } 109 | NSString *time = [formatter stringFromDate:self.lastUpdateTime]; 110 | 111 | // 3.显示日期 112 | self.lastUpdateTimeLabel.text = [NSString stringWithFormat:@"最后更新:%@", time]; 113 | } 114 | 115 | #pragma mark - 监听UIScrollView的contentOffset属性 116 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 117 | { 118 | // 不能跟用户交互就直接返回 119 | if (!self.userInteractionEnabled || self.alpha <= 0.01 || self.hidden) return; 120 | 121 | // 如果正在刷新,直接返回 122 | if (self.state == MJRefreshStateRefreshing) return; 123 | 124 | if ([MJRefreshContentOffset isEqualToString:keyPath]) { 125 | [self adjustStateWithContentOffset]; 126 | } 127 | } 128 | 129 | /** 130 | * 调整状态 131 | */ 132 | - (void)adjustStateWithContentOffset 133 | { 134 | // 当前的contentOffset 135 | CGFloat currentOffsetY = self.scrollView.contentOffsetY; 136 | // 头部控件刚好出现的offsetY 137 | CGFloat happenOffsetY = - self.scrollViewOriginalInset.top; 138 | 139 | // 如果是向上滚动到看不见头部控件,直接返回 140 | if (currentOffsetY >= happenOffsetY) return; 141 | 142 | if (self.scrollView.isDragging) { 143 | // 普通 和 即将刷新 的临界点 144 | CGFloat normal2pullingOffsetY = happenOffsetY - self.height; 145 | 146 | if (self.state == MJRefreshStateNormal && currentOffsetY < normal2pullingOffsetY) { 147 | // 转为即将刷新状态 148 | self.state = MJRefreshStatePulling; 149 | } else if (self.state == MJRefreshStatePulling && currentOffsetY >= normal2pullingOffsetY) { 150 | // 转为普通状态 151 | self.state = MJRefreshStateNormal; 152 | } 153 | } else if (self.state == MJRefreshStatePulling) {// 即将刷新 && 手松开 154 | // 开始刷新 155 | self.state = MJRefreshStateRefreshing; 156 | } 157 | } 158 | 159 | #pragma mark 设置状态 160 | - (void)setState:(MJRefreshState)state 161 | { 162 | // 1.一样的就直接返回 163 | if (self.state == state) return; 164 | 165 | // 2.保存旧状态 166 | MJRefreshState oldState = self.state; 167 | 168 | // 3.调用父类方法 169 | [super setState:state]; 170 | 171 | // 4.根据状态执行不同的操作 172 | switch (state) { 173 | case MJRefreshStateNormal: // 下拉可以刷新 174 | { 175 | // 设置文字 176 | self.statusLabel.text = MJRefreshHeaderPullToRefresh; 177 | 178 | // 刷新完毕 179 | if (MJRefreshStateRefreshing == oldState) { 180 | self.arrowImage.transform = CGAffineTransformIdentity; 181 | // 保存刷新时间 182 | self.lastUpdateTime = [NSDate date]; 183 | 184 | [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{ 185 | self.scrollView.contentInsetTop = self.scrollViewOriginalInset.top; 186 | }]; 187 | } else { 188 | // 执行动画 189 | [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{ 190 | self.arrowImage.transform = CGAffineTransformIdentity; 191 | }]; 192 | } 193 | break; 194 | } 195 | 196 | case MJRefreshStatePulling: // 松开可立即刷新 197 | { 198 | // 设置文字 199 | self.statusLabel.text = MJRefreshHeaderReleaseToRefresh; 200 | // 执行动画 201 | [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{ 202 | self.arrowImage.transform = CGAffineTransformMakeRotation(M_PI); 203 | }]; 204 | break; 205 | } 206 | 207 | case MJRefreshStateRefreshing: // 正在刷新中 208 | { 209 | // 设置文字 210 | self.statusLabel.text = MJRefreshHeaderRefreshing; 211 | 212 | // 执行动画 213 | [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{ 214 | // 1.增加滚动区域 215 | CGFloat top = self.scrollViewOriginalInset.top + self.height; 216 | self.scrollView.contentInsetTop = top; 217 | 218 | // 2.设置滚动位置 219 | self.scrollView.contentOffsetY = - top; 220 | }]; 221 | break; 222 | } 223 | 224 | default: 225 | break; 226 | } 227 | } 228 | @end -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/UIScrollView+Extension.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIScrollView+Extension.h 3 | // MJRefreshExample 4 | // 5 | // Created by MJ Lee on 14-5-28. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIScrollView (Extension) 12 | @property (assign, nonatomic) CGFloat contentInsetTop; 13 | @property (assign, nonatomic) CGFloat contentInsetBottom; 14 | @property (assign, nonatomic) CGFloat contentInsetLeft; 15 | @property (assign, nonatomic) CGFloat contentInsetRight; 16 | 17 | @property (assign, nonatomic) CGFloat contentOffsetX; 18 | @property (assign, nonatomic) CGFloat contentOffsetY; 19 | 20 | @property (assign, nonatomic) CGFloat contentSizeWidth; 21 | @property (assign, nonatomic) CGFloat contentSizeHeight; 22 | @end 23 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/UIScrollView+Extension.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIScrollView+Extension.m 3 | // MJRefreshExample 4 | // 5 | // Created by MJ Lee on 14-5-28. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "UIScrollView+Extension.h" 10 | 11 | @implementation UIScrollView (Extension) 12 | - (void)setContentInsetTop:(CGFloat)contentInsetTop 13 | { 14 | UIEdgeInsets inset = self.contentInset; 15 | inset.top = contentInsetTop; 16 | self.contentInset = inset; 17 | } 18 | 19 | - (CGFloat)contentInsetTop 20 | { 21 | return self.contentInset.top; 22 | } 23 | 24 | - (void)setContentInsetBottom:(CGFloat)contentInsetBottom 25 | { 26 | UIEdgeInsets inset = self.contentInset; 27 | inset.bottom = contentInsetBottom; 28 | self.contentInset = inset; 29 | } 30 | 31 | - (CGFloat)contentInsetBottom 32 | { 33 | return self.contentInset.bottom; 34 | } 35 | 36 | - (void)setContentInsetLeft:(CGFloat)contentInsetLeft 37 | { 38 | UIEdgeInsets inset = self.contentInset; 39 | inset.left = contentInsetLeft; 40 | self.contentInset = inset; 41 | } 42 | 43 | - (CGFloat)contentInsetLeft 44 | { 45 | return self.contentInset.left; 46 | } 47 | 48 | - (void)setContentInsetRight:(CGFloat)contentInsetRight 49 | { 50 | UIEdgeInsets inset = self.contentInset; 51 | inset.right = contentInsetRight; 52 | self.contentInset = inset; 53 | } 54 | 55 | - (CGFloat)contentInsetRight 56 | { 57 | return self.contentInset.right; 58 | } 59 | 60 | - (void)setContentOffsetX:(CGFloat)contentOffsetX 61 | { 62 | CGPoint offset = self.contentOffset; 63 | offset.x = contentOffsetX; 64 | self.contentOffset = offset; 65 | } 66 | 67 | - (CGFloat)contentOffsetX 68 | { 69 | return self.contentOffset.x; 70 | } 71 | 72 | - (void)setContentOffsetY:(CGFloat)contentOffsetY 73 | { 74 | CGPoint offset = self.contentOffset; 75 | offset.y = contentOffsetY; 76 | self.contentOffset = offset; 77 | } 78 | 79 | - (CGFloat)contentOffsetY 80 | { 81 | return self.contentOffset.y; 82 | } 83 | 84 | - (void)setContentSizeWidth:(CGFloat)contentSizeWidth 85 | { 86 | CGSize size = self.contentSize; 87 | size.width = contentSizeWidth; 88 | self.contentSize = size; 89 | } 90 | 91 | - (CGFloat)contentSizeWidth 92 | { 93 | return self.contentSize.width; 94 | } 95 | 96 | - (void)setContentSizeHeight:(CGFloat)contentSizeHeight 97 | { 98 | CGSize size = self.contentSize; 99 | size.height = contentSizeHeight; 100 | self.contentSize = size; 101 | } 102 | 103 | - (CGFloat)contentSizeHeight 104 | { 105 | return self.contentSize.height; 106 | } 107 | @end 108 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/UIScrollView+MJRefresh.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIScrollView+MJRefresh.h 3 | // MJRefreshExample 4 | // 5 | // Created by MJ Lee on 14-5-28. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIScrollView (MJRefresh) 12 | #pragma mark - 下拉刷新 13 | /** 14 | * 添加一个下拉刷新头部控件 15 | * 16 | * @param callback 回调 17 | */ 18 | - (void)addHeaderWithCallback:(void (^)())callback; 19 | 20 | /** 21 | * 添加一个下拉刷新头部控件 22 | * 23 | * @param target 目标 24 | * @param action 回调方法 25 | */ 26 | - (void)addHeaderWithTarget:(id)target action:(SEL)action; 27 | 28 | /** 29 | * 移除下拉刷新头部控件 30 | */ 31 | - (void)removeHeader; 32 | 33 | /** 34 | * 主动让下拉刷新头部控件进入刷新状态 35 | */ 36 | - (void)headerBeginRefreshing; 37 | 38 | /** 39 | * 让下拉刷新头部控件停止刷新状态 40 | */ 41 | - (void)headerEndRefreshing; 42 | 43 | /** 44 | * 下拉刷新头部控件的可见性 45 | */ 46 | @property (nonatomic, assign, getter = isHeaderHidden) BOOL headerHidden; 47 | 48 | #pragma mark - 上拉刷新 49 | /** 50 | * 添加一个上拉刷新尾部控件 51 | * 52 | * @param callback 回调 53 | */ 54 | - (void)addFooterWithCallback:(void (^)())callback; 55 | 56 | /** 57 | * 添加一个上拉刷新尾部控件 58 | * 59 | * @param target 目标 60 | * @param action 回调方法 61 | */ 62 | - (void)addFooterWithTarget:(id)target action:(SEL)action; 63 | 64 | /** 65 | * 移除上拉刷新尾部控件 66 | */ 67 | - (void)removeFooter; 68 | 69 | /** 70 | * 主动让上拉刷新尾部控件进入刷新状态 71 | */ 72 | - (void)footerBeginRefreshing; 73 | 74 | /** 75 | * 让上拉刷新尾部控件停止刷新状态 76 | */ 77 | - (void)footerEndRefreshing; 78 | 79 | /** 80 | * 下拉刷新头部控件的可见性 81 | */ 82 | @property (nonatomic, assign, getter = isFooterHidden) BOOL footerHidden; 83 | @end 84 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/UIScrollView+MJRefresh.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIScrollView+MJRefresh.m 3 | // MJRefreshExample 4 | // 5 | // Created by MJ Lee on 14-5-28. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "UIScrollView+MJRefresh.h" 10 | #import "MJRefreshHeaderView.h" 11 | #import "MJRefreshFooterView.h" 12 | #import 13 | 14 | @interface UIScrollView() 15 | @property (weak, nonatomic) MJRefreshHeaderView *header; 16 | @property (weak, nonatomic) MJRefreshFooterView *footer; 17 | @end 18 | 19 | 20 | @implementation UIScrollView (MJRefresh) 21 | 22 | #pragma mark - 运行时相关 23 | static char MJRefreshHeaderViewKey; 24 | static char MJRefreshFooterViewKey; 25 | 26 | - (void)setHeader:(MJRefreshHeaderView *)header { 27 | [self willChangeValueForKey:@"MJRefreshHeaderViewKey"]; 28 | objc_setAssociatedObject(self, &MJRefreshHeaderViewKey, 29 | header, 30 | OBJC_ASSOCIATION_ASSIGN); 31 | [self didChangeValueForKey:@"MJRefreshHeaderViewKey"]; 32 | } 33 | 34 | - (MJRefreshHeaderView *)header { 35 | return objc_getAssociatedObject(self, &MJRefreshHeaderViewKey); 36 | } 37 | 38 | - (void)setFooter:(MJRefreshFooterView *)footer { 39 | [self willChangeValueForKey:@"MJRefreshFooterViewKey"]; 40 | objc_setAssociatedObject(self, &MJRefreshFooterViewKey, 41 | footer, 42 | OBJC_ASSOCIATION_ASSIGN); 43 | [self didChangeValueForKey:@"MJRefreshFooterViewKey"]; 44 | } 45 | 46 | - (MJRefreshFooterView *)footer { 47 | return objc_getAssociatedObject(self, &MJRefreshFooterViewKey); 48 | } 49 | 50 | #pragma mark - 下拉刷新 51 | /** 52 | * 添加一个下拉刷新头部控件 53 | * 54 | * @param callback 回调 55 | */ 56 | - (void)addHeaderWithCallback:(void (^)())callback 57 | { 58 | // 1.创建新的header 59 | if (!self.header) { 60 | MJRefreshHeaderView *header = [MJRefreshHeaderView header]; 61 | [self addSubview:header]; 62 | self.header = header; 63 | } 64 | 65 | // 2.设置block回调 66 | self.header.beginRefreshingCallback = callback; 67 | } 68 | 69 | /** 70 | * 添加一个下拉刷新头部控件 71 | * 72 | * @param target 目标 73 | * @param action 回调方法 74 | */ 75 | - (void)addHeaderWithTarget:(id)target action:(SEL)action 76 | { 77 | // 1.创建新的header 78 | if (!self.header) { 79 | MJRefreshHeaderView *header = [MJRefreshHeaderView header]; 80 | [self addSubview:header]; 81 | self.header = header; 82 | } 83 | 84 | // 2.设置目标和回调方法 85 | self.header.beginRefreshingTaget = target; 86 | self.header.beginRefreshingAction = action; 87 | } 88 | 89 | /** 90 | * 移除下拉刷新头部控件 91 | */ 92 | - (void)removeHeader 93 | { 94 | [self.header removeFromSuperview]; 95 | self.header = nil; 96 | } 97 | 98 | /** 99 | * 主动让下拉刷新头部控件进入刷新状态 100 | */ 101 | - (void)headerBeginRefreshing 102 | { 103 | [self.header beginRefreshing]; 104 | } 105 | 106 | /** 107 | * 让下拉刷新头部控件停止刷新状态 108 | */ 109 | - (void)headerEndRefreshing 110 | { 111 | [self.header endRefreshing]; 112 | } 113 | 114 | /** 115 | * 下拉刷新头部控件的可见性 116 | */ 117 | - (void)setHeaderHidden:(BOOL)hidden 118 | { 119 | self.header.hidden = hidden; 120 | } 121 | 122 | - (BOOL)isHeaderHidden 123 | { 124 | return self.header.isHidden; 125 | } 126 | 127 | #pragma mark - 上拉刷新 128 | /** 129 | * 添加一个上拉刷新尾部控件 130 | * 131 | * @param callback 回调 132 | */ 133 | - (void)addFooterWithCallback:(void (^)())callback 134 | { 135 | // 1.创建新的footer 136 | if (!self.footer) { 137 | MJRefreshFooterView *footer = [MJRefreshFooterView footer]; 138 | [self addSubview:footer]; 139 | self.footer = footer; 140 | } 141 | 142 | // 2.设置block回调 143 | self.footer.beginRefreshingCallback = callback; 144 | } 145 | 146 | /** 147 | * 添加一个上拉刷新尾部控件 148 | * 149 | * @param target 目标 150 | * @param action 回调方法 151 | */ 152 | - (void)addFooterWithTarget:(id)target action:(SEL)action 153 | { 154 | // 1.创建新的footer 155 | if (!self.footer) { 156 | MJRefreshFooterView *footer = [MJRefreshFooterView footer]; 157 | [self addSubview:footer]; 158 | self.footer = footer; 159 | } 160 | 161 | // 2.设置目标和回调方法 162 | self.footer.beginRefreshingTaget = target; 163 | self.footer.beginRefreshingAction = action; 164 | } 165 | 166 | /** 167 | * 移除上拉刷新尾部控件 168 | */ 169 | - (void)removeFooter 170 | { 171 | [self.footer removeFromSuperview]; 172 | self.footer = nil; 173 | } 174 | 175 | /** 176 | * 主动让上拉刷新尾部控件进入刷新状态 177 | */ 178 | - (void)footerBeginRefreshing 179 | { 180 | [self.footer beginRefreshing]; 181 | } 182 | 183 | /** 184 | * 让上拉刷新尾部控件停止刷新状态 185 | */ 186 | - (void)footerEndRefreshing 187 | { 188 | [self.footer endRefreshing]; 189 | } 190 | 191 | /** 192 | * 下拉刷新头部控件的可见性 193 | */ 194 | - (void)setFooterHidden:(BOOL)hidden 195 | { 196 | self.footer.hidden = hidden; 197 | } 198 | 199 | - (BOOL)isFooterHidden 200 | { 201 | return self.footer.isHidden; 202 | } 203 | @end 204 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/UIView+Extension.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+Extension.h 3 | // MJRefreshExample 4 | // 5 | // Created by MJ Lee on 14-5-28. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIView (Extension) 12 | @property (assign, nonatomic) CGFloat x; 13 | @property (assign, nonatomic) CGFloat y; 14 | @property (assign, nonatomic) CGFloat width; 15 | @property (assign, nonatomic) CGFloat height; 16 | @property (assign, nonatomic) CGSize size; 17 | @property (assign, nonatomic) CGPoint origin; 18 | @end 19 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/MJRefresh/UIView+Extension.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+Extension.m 3 | // MJRefreshExample 4 | // 5 | // Created by MJ Lee on 14-5-28. 6 | // Copyright (c) 2014年 itcast. All rights reserved. 7 | // 8 | 9 | #import "UIView+Extension.h" 10 | 11 | @implementation UIView (Extension) 12 | - (void)setX:(CGFloat)x 13 | { 14 | CGRect frame = self.frame; 15 | frame.origin.x = x; 16 | self.frame = frame; 17 | } 18 | 19 | - (CGFloat)x 20 | { 21 | return self.frame.origin.x; 22 | } 23 | 24 | - (void)setY:(CGFloat)y 25 | { 26 | CGRect frame = self.frame; 27 | frame.origin.y = y; 28 | self.frame = frame; 29 | } 30 | 31 | - (CGFloat)y 32 | { 33 | return self.frame.origin.y; 34 | } 35 | 36 | - (void)setWidth:(CGFloat)width 37 | { 38 | CGRect frame = self.frame; 39 | frame.size.width = width; 40 | self.frame = frame; 41 | } 42 | 43 | - (CGFloat)width 44 | { 45 | return self.frame.size.width; 46 | } 47 | 48 | - (void)setHeight:(CGFloat)height 49 | { 50 | CGRect frame = self.frame; 51 | frame.size.height = height; 52 | self.frame = frame; 53 | } 54 | 55 | - (CGFloat)height 56 | { 57 | return self.frame.size.height; 58 | } 59 | 60 | - (void)setSize:(CGSize)size 61 | { 62 | CGRect frame = self.frame; 63 | frame.size = size; 64 | self.frame = frame; 65 | } 66 | 67 | - (CGSize)size 68 | { 69 | return self.frame.size; 70 | } 71 | 72 | - (void)setOrigin:(CGPoint)origin 73 | { 74 | CGRect frame = self.frame; 75 | frame.origin = origin; 76 | self.frame = frame; 77 | } 78 | 79 | - (CGPoint)origin 80 | { 81 | return self.frame.origin; 82 | } 83 | @end 84 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/MKAnnotationView+WebCache.h: -------------------------------------------------------------------------------- 1 | // 2 | // MKAnnotationView+WebCache.h 3 | // SDWebImage 4 | // 5 | // Created by Olivier Poitrey on 14/03/12. 6 | // Copyright (c) 2012 Dailymotion. All rights reserved. 7 | // 8 | 9 | #import "MapKit/MapKit.h" 10 | #import "SDWebImageManager.h" 11 | 12 | /** 13 | * Integrates SDWebImage async downloading and caching of remote images with MKAnnotationView. 14 | */ 15 | @interface MKAnnotationView (WebCache) 16 | 17 | /** 18 | * Get the current image URL. 19 | * 20 | * Note that because of the limitations of categories this property can get out of sync 21 | * if you use sd_setImage: directly. 22 | */ 23 | - (NSURL *)sd_imageURL; 24 | 25 | /** 26 | * Set the imageView `image` with an `url`. 27 | * 28 | * The download is asynchronous and cached. 29 | * 30 | * @param url The url for the image. 31 | */ 32 | - (void)sd_setImageWithURL:(NSURL *)url; 33 | 34 | /** 35 | * Set the imageView `image` with an `url` and a placeholder. 36 | * 37 | * The download is asynchronous and cached. 38 | * 39 | * @param url The url for the image. 40 | * @param placeholder The image to be set initially, until the image request finishes. 41 | * @see sd_setImageWithURL:placeholderImage:options: 42 | */ 43 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; 44 | 45 | /** 46 | * Set the imageView `image` with an `url`, placeholder and custom options. 47 | * 48 | * The download is asynchronous and cached. 49 | * 50 | * @param url The url for the image. 51 | * @param placeholder The image to be set initially, until the image request finishes. 52 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 53 | */ 54 | 55 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options; 56 | 57 | /** 58 | * Set the imageView `image` with an `url`. 59 | * 60 | * The download is asynchronous and cached. 61 | * 62 | * @param url The url for the image. 63 | * @param completedBlock A block called when operation has been completed. This block has no return value 64 | * and takes the requested UIImage as first parameter. In case of error the image parameter 65 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 66 | * indicating if the image was retrived from the local cache or from the network. 67 | * The forth parameter is the original image url. 68 | */ 69 | - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock; 70 | 71 | /** 72 | * Set the imageView `image` with an `url`, placeholder. 73 | * 74 | * The download is asynchronous and cached. 75 | * 76 | * @param url The url for the image. 77 | * @param placeholder The image to be set initially, until the image request finishes. 78 | * @param completedBlock A block called when operation has been completed. This block has no return value 79 | * and takes the requested UIImage as first parameter. In case of error the image parameter 80 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 81 | * indicating if the image was retrived from the local cache or from the network. 82 | * The forth parameter is the original image url. 83 | */ 84 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock; 85 | 86 | /** 87 | * Set the imageView `image` with an `url`, placeholder and custom options. 88 | * 89 | * The download is asynchronous and cached. 90 | * 91 | * @param url The url for the image. 92 | * @param placeholder The image to be set initially, until the image request finishes. 93 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 94 | * @param completedBlock A block called when operation has been completed. This block has no return value 95 | * and takes the requested UIImage as first parameter. In case of error the image parameter 96 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 97 | * indicating if the image was retrived from the local cache or from the network. 98 | * The forth parameter is the original image url. 99 | */ 100 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock; 101 | 102 | /** 103 | * Cancel the current download 104 | */ 105 | - (void)sd_cancelCurrentImageLoad; 106 | 107 | @end 108 | 109 | 110 | @interface MKAnnotationView (WebCacheDeprecated) 111 | 112 | - (NSURL *)imageURL __deprecated_msg("Use `sd_imageURL`"); 113 | 114 | - (void)setImageWithURL:(NSURL *)url __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:`"); 115 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:`"); 116 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options:`"); 117 | 118 | - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:completed:`"); 119 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:completed:`"); 120 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options:completed:`"); 121 | 122 | - (void)cancelCurrentImageLoad __deprecated_msg("Use `sd_cancelCurrentImageLoad`"); 123 | 124 | @end 125 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/MKAnnotationView+WebCache.m: -------------------------------------------------------------------------------- 1 | // 2 | // MKAnnotationView+WebCache.m 3 | // SDWebImage 4 | // 5 | // Created by Olivier Poitrey on 14/03/12. 6 | // Copyright (c) 2012 Dailymotion. All rights reserved. 7 | // 8 | 9 | #import "MKAnnotationView+WebCache.h" 10 | #import "objc/runtime.h" 11 | #import "UIView+WebCacheOperation.h" 12 | 13 | static char imageURLKey; 14 | 15 | @implementation MKAnnotationView (WebCache) 16 | 17 | - (NSURL *)sd_imageURL { 18 | return objc_getAssociatedObject(self, &imageURLKey); 19 | } 20 | 21 | - (void)sd_setImageWithURL:(NSURL *)url { 22 | [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:nil]; 23 | } 24 | 25 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { 26 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:nil]; 27 | } 28 | 29 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { 30 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:nil]; 31 | } 32 | 33 | - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock { 34 | [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:completedBlock]; 35 | } 36 | 37 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock { 38 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:completedBlock]; 39 | } 40 | 41 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock { 42 | [self sd_cancelCurrentImageLoad]; 43 | 44 | objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 45 | self.image = placeholder; 46 | 47 | if (url) { 48 | __weak MKAnnotationView *wself = self; 49 | id operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 50 | if (!wself) return; 51 | dispatch_main_sync_safe(^{ 52 | __strong MKAnnotationView *sself = wself; 53 | if (!sself) return; 54 | if (image) { 55 | sself.image = image; 56 | } 57 | if (completedBlock && finished) { 58 | completedBlock(image, error, cacheType, url); 59 | } 60 | }); 61 | }]; 62 | [self sd_setImageLoadOperation:operation forKey:@"MKAnnotationViewImage"]; 63 | } else { 64 | dispatch_main_async_safe(^{ 65 | NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}]; 66 | if (completedBlock) { 67 | completedBlock(nil, error, SDImageCacheTypeNone, url); 68 | } 69 | }); 70 | } 71 | } 72 | 73 | - (void)sd_cancelCurrentImageLoad { 74 | [self sd_cancelImageLoadOperationWithKey:@"MKAnnotationViewImage"]; 75 | } 76 | 77 | @end 78 | 79 | 80 | @implementation MKAnnotationView (WebCacheDeprecated) 81 | 82 | - (NSURL *)imageURL { 83 | return [self sd_imageURL]; 84 | } 85 | 86 | - (void)setImageWithURL:(NSURL *)url { 87 | [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:nil]; 88 | } 89 | 90 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { 91 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:nil]; 92 | } 93 | 94 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { 95 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:nil]; 96 | } 97 | 98 | - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock { 99 | [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 100 | if (completedBlock) { 101 | completedBlock(image, error, cacheType); 102 | } 103 | }]; 104 | } 105 | 106 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock { 107 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 108 | if (completedBlock) { 109 | completedBlock(image, error, cacheType); 110 | } 111 | }]; 112 | } 113 | 114 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { 115 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 116 | if (completedBlock) { 117 | completedBlock(image, error, cacheType); 118 | } 119 | }]; 120 | } 121 | 122 | - (void)cancelCurrentImageLoad { 123 | [self sd_cancelCurrentImageLoad]; 124 | } 125 | 126 | @end 127 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/NSData+ImageContentType.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Fabrice Aneche on 06/01/14. 3 | // Copyright (c) 2014 Dailymotion. All rights reserved. 4 | // 5 | 6 | #import 7 | 8 | @interface NSData (ImageContentType) 9 | 10 | /** 11 | * Compute the content type for an image data 12 | * 13 | * @param data the input data 14 | * 15 | * @return the content type as string (i.e. image/jpeg, image/gif) 16 | */ 17 | + (NSString *)sd_contentTypeForImageData:(NSData *)data; 18 | 19 | @end 20 | 21 | 22 | @interface NSData (ImageContentTypeDeprecated) 23 | 24 | + (NSString *)contentTypeForImageData:(NSData *)data __deprecated_msg("Use `sd_contentTypeForImageData:`"); 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/NSData+ImageContentType.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Fabrice Aneche on 06/01/14. 3 | // Copyright (c) 2014 Dailymotion. All rights reserved. 4 | // 5 | 6 | #import "NSData+ImageContentType.h" 7 | 8 | 9 | @implementation NSData (ImageContentType) 10 | 11 | + (NSString *)sd_contentTypeForImageData:(NSData *)data { 12 | uint8_t c; 13 | [data getBytes:&c length:1]; 14 | switch (c) { 15 | case 0xFF: 16 | return @"image/jpeg"; 17 | case 0x89: 18 | return @"image/png"; 19 | case 0x47: 20 | return @"image/gif"; 21 | case 0x49: 22 | case 0x4D: 23 | return @"image/tiff"; 24 | case 0x52: 25 | // R as RIFF for WEBP 26 | if ([data length] < 12) { 27 | return nil; 28 | } 29 | 30 | NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding]; 31 | if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) { 32 | return @"image/webp"; 33 | } 34 | 35 | return nil; 36 | } 37 | return nil; 38 | } 39 | 40 | @end 41 | 42 | 43 | @implementation NSData (ImageContentTypeDeprecated) 44 | 45 | + (NSString *)contentTypeForImageData:(NSData *)data { 46 | return [self sd_contentTypeForImageData:data]; 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDImageCache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import 10 | #import "SDWebImageCompat.h" 11 | 12 | typedef NS_ENUM(NSInteger, SDImageCacheType) { 13 | /** 14 | * The image wasn't available the SDWebImage caches, but was downloaded from the web. 15 | */ 16 | SDImageCacheTypeNone, 17 | /** 18 | * The image was obtained from the disk cache. 19 | */ 20 | SDImageCacheTypeDisk, 21 | /** 22 | * The image was obtained from the memory cache. 23 | */ 24 | SDImageCacheTypeMemory 25 | }; 26 | 27 | typedef void(^SDWebImageQueryCompletedBlock)(UIImage *image, SDImageCacheType cacheType); 28 | 29 | typedef void(^SDWebImageCheckCacheCompletionBlock)(BOOL isInCache); 30 | 31 | typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger totalSize); 32 | 33 | /** 34 | * SDImageCache maintains a memory cache and an optional disk cache. Disk cache write operations are performed 35 | * asynchronous so it doesn’t add unnecessary latency to the UI. 36 | */ 37 | @interface SDImageCache : NSObject 38 | 39 | /** 40 | * The maximum "total cost" of the in-memory image cache. The cost function is the number of pixels held in memory. 41 | */ 42 | @property (assign, nonatomic) NSUInteger maxMemoryCost; 43 | 44 | /** 45 | * The maximum length of time to keep an image in the cache, in seconds 46 | */ 47 | @property (assign, nonatomic) NSInteger maxCacheAge; 48 | 49 | /** 50 | * The maximum size of the cache, in bytes. 51 | */ 52 | @property (assign, nonatomic) NSUInteger maxCacheSize; 53 | 54 | /** 55 | * Returns global shared cache instance 56 | * 57 | * @return SDImageCache global instance 58 | */ 59 | + (SDImageCache *)sharedImageCache; 60 | 61 | /** 62 | * Init a new cache store with a specific namespace 63 | * 64 | * @param ns The namespace to use for this cache store 65 | */ 66 | - (id)initWithNamespace:(NSString *)ns; 67 | 68 | /** 69 | * Add a read-only cache path to search for images pre-cached by SDImageCache 70 | * Useful if you want to bundle pre-loaded images with your app 71 | * 72 | * @param path The path to use for this read-only cache path 73 | */ 74 | - (void)addReadOnlyCachePath:(NSString *)path; 75 | 76 | /** 77 | * Store an image into memory and disk cache at the given key. 78 | * 79 | * @param image The image to store 80 | * @param key The unique image cache key, usually it's image absolute URL 81 | */ 82 | - (void)storeImage:(UIImage *)image forKey:(NSString *)key; 83 | 84 | /** 85 | * Store an image into memory and optionally disk cache at the given key. 86 | * 87 | * @param image The image to store 88 | * @param key The unique image cache key, usually it's image absolute URL 89 | * @param toDisk Store the image to disk cache if YES 90 | */ 91 | - (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk; 92 | 93 | /** 94 | * Store an image into memory and optionally disk cache at the given key. 95 | * 96 | * @param image The image to store 97 | * @param recalculate BOOL indicates if imageData can be used or a new data should be constructed from the UIImage 98 | * @param imageData The image data as returned by the server, this representation will be used for disk storage 99 | * instead of converting the given image object into a storable/compressed image format in order 100 | * to save quality and CPU 101 | * @param key The unique image cache key, usually it's image absolute URL 102 | * @param toDisk Store the image to disk cache if YES 103 | */ 104 | - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk; 105 | 106 | /** 107 | * Query the disk cache asynchronously. 108 | * 109 | * @param key The unique key used to store the wanted image 110 | */ 111 | - (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock; 112 | 113 | /** 114 | * Query the memory cache synchronously. 115 | * 116 | * @param key The unique key used to store the wanted image 117 | */ 118 | - (UIImage *)imageFromMemoryCacheForKey:(NSString *)key; 119 | 120 | /** 121 | * Query the disk cache synchronously after checking the memory cache. 122 | * 123 | * @param key The unique key used to store the wanted image 124 | */ 125 | - (UIImage *)imageFromDiskCacheForKey:(NSString *)key; 126 | 127 | /** 128 | * Remove the image from memory and disk cache synchronously 129 | * 130 | * @param key The unique image cache key 131 | */ 132 | - (void)removeImageForKey:(NSString *)key; 133 | 134 | 135 | /** 136 | * Remove the image from memory and disk cache synchronously 137 | * 138 | * @param key The unique image cache key 139 | * @param completionBlock An block that should be executed after the image has been removed (optional) 140 | */ 141 | - (void)removeImageForKey:(NSString *)key withCompletion:(SDWebImageNoParamsBlock)completion; 142 | 143 | /** 144 | * Remove the image from memory and optionally disk cache synchronously 145 | * 146 | * @param key The unique image cache key 147 | * @param fromDisk Also remove cache entry from disk if YES 148 | */ 149 | - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk; 150 | 151 | /** 152 | * Remove the image from memory and optionally disk cache synchronously 153 | * 154 | * @param key The unique image cache key 155 | * @param fromDisk Also remove cache entry from disk if YES 156 | * @param completionBlock An block that should be executed after the image has been removed (optional) 157 | */ 158 | - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(SDWebImageNoParamsBlock)completion; 159 | 160 | /** 161 | * Clear all memory cached images 162 | */ 163 | - (void)clearMemory; 164 | 165 | /** 166 | * Clear all disk cached images. Non-blocking method - returns immediately. 167 | * @param completionBlock An block that should be executed after cache expiration completes (optional) 168 | */ 169 | - (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion; 170 | 171 | /** 172 | * Clear all disk cached images 173 | * @see clearDiskOnCompletion: 174 | */ 175 | - (void)clearDisk; 176 | 177 | /** 178 | * Remove all expired cached image from disk. Non-blocking method - returns immediately. 179 | * @param completionBlock An block that should be executed after cache expiration completes (optional) 180 | */ 181 | - (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock; 182 | 183 | /** 184 | * Remove all expired cached image from disk 185 | * @see cleanDiskWithCompletionBlock: 186 | */ 187 | - (void)cleanDisk; 188 | 189 | /** 190 | * Get the size used by the disk cache 191 | */ 192 | - (NSUInteger)getSize; 193 | 194 | /** 195 | * Get the number of images in the disk cache 196 | */ 197 | - (NSUInteger)getDiskCount; 198 | 199 | /** 200 | * Asynchronously calculate the disk cache's size. 201 | */ 202 | - (void)calculateSizeWithCompletionBlock:(SDWebImageCalculateSizeBlock)completionBlock; 203 | 204 | /** 205 | * Async check if image exists in disk cache already (does not load the image) 206 | * 207 | * @param key the key describing the url 208 | * @param completionBlock the block to be executed when the check is done. 209 | * @note the completion block will be always executed on the main queue 210 | */ 211 | - (void)diskImageExistsWithKey:(NSString *)key completion:(SDWebImageCheckCacheCompletionBlock)completionBlock; 212 | 213 | /** 214 | * Check if image exists in disk cache already (does not load the image) 215 | * 216 | * @param key the key describing the url 217 | * 218 | * @return YES if an image exists for the given key 219 | */ 220 | - (BOOL)diskImageExistsWithKey:(NSString *)key; 221 | 222 | /** 223 | * Get the cache path for a certain key (needs the cache path root folder) 224 | * 225 | * @param key the key (can be obtained from url using cacheKeyForURL) 226 | * @param path the cach path root folder 227 | * 228 | * @return the cache path 229 | */ 230 | - (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path; 231 | 232 | /** 233 | * Get the default cache path for a certain key 234 | * 235 | * @param key the key (can be obtained from url using cacheKeyForURL) 236 | * 237 | * @return the default cache path 238 | */ 239 | - (NSString *)defaultCachePathForKey:(NSString *)key; 240 | 241 | @end 242 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImageCompat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * (c) Jamie Pinkham 5 | * 6 | * For the full copyright and license information, please view the LICENSE 7 | * file that was distributed with this source code. 8 | */ 9 | 10 | #import 11 | 12 | #ifdef __OBJC_GC__ 13 | #error SDWebImage does not support Objective-C Garbage Collection 14 | #endif 15 | 16 | #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0 17 | #error SDWebImage doesn't support Deployement Target version < 5.0 18 | #endif 19 | 20 | #if !TARGET_OS_IPHONE 21 | #import 22 | #ifndef UIImage 23 | #define UIImage NSImage 24 | #endif 25 | #ifndef UIImageView 26 | #define UIImageView NSImageView 27 | #endif 28 | #else 29 | 30 | #import 31 | 32 | #endif 33 | 34 | #ifndef NS_ENUM 35 | #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type 36 | #endif 37 | 38 | #ifndef NS_OPTIONS 39 | #define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type 40 | #endif 41 | 42 | #if OS_OBJECT_USE_OBJC 43 | #undef SDDispatchQueueRelease 44 | #undef SDDispatchQueueSetterSementics 45 | #define SDDispatchQueueRelease(q) 46 | #define SDDispatchQueueSetterSementics strong 47 | #else 48 | #undef SDDispatchQueueRelease 49 | #undef SDDispatchQueueSetterSementics 50 | #define SDDispatchQueueRelease(q) (dispatch_release(q)) 51 | #define SDDispatchQueueSetterSementics assign 52 | #endif 53 | 54 | extern UIImage *SDScaledImageForKey(NSString *key, UIImage *image); 55 | 56 | typedef void(^SDWebImageNoParamsBlock)(); 57 | 58 | #define dispatch_main_sync_safe(block)\ 59 | if ([NSThread isMainThread]) {\ 60 | block();\ 61 | } else {\ 62 | dispatch_sync(dispatch_get_main_queue(), block);\ 63 | } 64 | 65 | #define dispatch_main_async_safe(block)\ 66 | if ([NSThread isMainThread]) {\ 67 | block();\ 68 | } else {\ 69 | dispatch_async(dispatch_get_main_queue(), block);\ 70 | } 71 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImageCompat.m: -------------------------------------------------------------------------------- 1 | // 2 | // SDWebImageCompat.m 3 | // SDWebImage 4 | // 5 | // Created by Olivier Poitrey on 11/12/12. 6 | // Copyright (c) 2012 Dailymotion. All rights reserved. 7 | // 8 | 9 | #import "SDWebImageCompat.h" 10 | 11 | #if !__has_feature(objc_arc) 12 | #error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag 13 | #endif 14 | 15 | inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image) { 16 | if (!image) { 17 | return nil; 18 | } 19 | 20 | if ([image.images count] > 0) { 21 | NSMutableArray *scaledImages = [NSMutableArray array]; 22 | 23 | for (UIImage *tempImage in image.images) { 24 | [scaledImages addObject:SDScaledImageForKey(key, tempImage)]; 25 | } 26 | 27 | return [UIImage animatedImageWithImages:scaledImages duration:image.duration]; 28 | } 29 | else { 30 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 31 | CGFloat scale = 1.0; 32 | if (key.length >= 8) { 33 | // Search @2x. at the end of the string, before a 3 to 4 extension length (only if key len is 8 or more @2x. + 4 len ext) 34 | NSRange range = [key rangeOfString:@"@2x." options:0 range:NSMakeRange(key.length - 8, 5)]; 35 | if (range.location != NSNotFound) { 36 | scale = 2.0; 37 | } 38 | } 39 | 40 | UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation]; 41 | image = scaledImage; 42 | } 43 | return image; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImageDecoder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * Created by james on 9/28/11. 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | #import 12 | #import "SDWebImageCompat.h" 13 | 14 | @interface UIImage (ForceDecode) 15 | 16 | + (UIImage *)decodedImageWithImage:(UIImage *)image; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImageDecoder.m: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * Created by james on 9/28/11. 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | #import "SDWebImageDecoder.h" 12 | 13 | @implementation UIImage (ForceDecode) 14 | 15 | + (UIImage *)decodedImageWithImage:(UIImage *)image { 16 | if (image.images) { 17 | // Do not decode animated images 18 | return image; 19 | } 20 | 21 | CGImageRef imageRef = image.CGImage; 22 | CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); 23 | CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize}; 24 | 25 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 26 | CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 27 | 28 | int infoMask = (bitmapInfo & kCGBitmapAlphaInfoMask); 29 | BOOL anyNonAlpha = (infoMask == kCGImageAlphaNone || 30 | infoMask == kCGImageAlphaNoneSkipFirst || 31 | infoMask == kCGImageAlphaNoneSkipLast); 32 | 33 | // CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB. 34 | // https://developer.apple.com/library/mac/#qa/qa1037/_index.html 35 | if (infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1) { 36 | // Unset the old alpha info. 37 | bitmapInfo &= ~kCGBitmapAlphaInfoMask; 38 | 39 | // Set noneSkipFirst. 40 | bitmapInfo |= kCGImageAlphaNoneSkipFirst; 41 | } 42 | // Some PNGs tell us they have alpha but only 3 components. Odd. 43 | else if (!anyNonAlpha && CGColorSpaceGetNumberOfComponents(colorSpace) == 3) { 44 | // Unset the old alpha info. 45 | bitmapInfo &= ~kCGBitmapAlphaInfoMask; 46 | bitmapInfo |= kCGImageAlphaPremultipliedFirst; 47 | } 48 | 49 | // It calculates the bytes-per-row based on the bitsPerComponent and width arguments. 50 | CGContextRef context = CGBitmapContextCreate(NULL, 51 | imageSize.width, 52 | imageSize.height, 53 | CGImageGetBitsPerComponent(imageRef), 54 | 0, 55 | colorSpace, 56 | bitmapInfo); 57 | CGColorSpaceRelease(colorSpace); 58 | 59 | // If failed, return undecompressed image 60 | if (!context) return image; 61 | 62 | CGContextDrawImage(context, imageRect, imageRef); 63 | CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); 64 | 65 | CGContextRelease(context); 66 | 67 | UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation]; 68 | CGImageRelease(decompressedImageRef); 69 | return decompressedImage; 70 | } 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImageDownloader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import 10 | #import "SDWebImageCompat.h" 11 | #import "SDWebImageOperation.h" 12 | 13 | typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) { 14 | SDWebImageDownloaderLowPriority = 1 << 0, 15 | SDWebImageDownloaderProgressiveDownload = 1 << 1, 16 | 17 | /** 18 | * By default, request prevent the of NSURLCache. With this flag, NSURLCache 19 | * is used with default policies. 20 | */ 21 | SDWebImageDownloaderUseNSURLCache = 1 << 2, 22 | 23 | /** 24 | * Call completion block with nil image/imageData if the image was read from NSURLCache 25 | * (to be combined with `SDWebImageDownloaderUseNSURLCache`). 26 | */ 27 | 28 | SDWebImageDownloaderIgnoreCachedResponse = 1 << 3, 29 | /** 30 | * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for 31 | * extra time in background to let the request finish. If the background task expires the operation will be cancelled. 32 | */ 33 | 34 | SDWebImageDownloaderContinueInBackground = 1 << 4, 35 | 36 | /** 37 | * Handles cookies stored in NSHTTPCookieStore by setting 38 | * NSMutableURLRequest.HTTPShouldHandleCookies = YES; 39 | */ 40 | SDWebImageDownloaderHandleCookies = 1 << 5, 41 | 42 | /** 43 | * Enable to allow untrusted SSL ceriticates. 44 | * Useful for testing purposes. Use with caution in production. 45 | */ 46 | SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6, 47 | 48 | /** 49 | * Put the image in the high priority queue. 50 | */ 51 | SDWebImageDownloaderHighPriority = 1 << 7, 52 | 53 | 54 | }; 55 | 56 | typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) { 57 | /** 58 | * Default value. All download operations will execute in queue style (first-in-first-out). 59 | */ 60 | SDWebImageDownloaderFIFOExecutionOrder, 61 | 62 | /** 63 | * All download operations will execute in stack style (last-in-first-out). 64 | */ 65 | SDWebImageDownloaderLIFOExecutionOrder 66 | }; 67 | 68 | extern NSString *const SDWebImageDownloadStartNotification; 69 | extern NSString *const SDWebImageDownloadStopNotification; 70 | 71 | typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize); 72 | 73 | typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, NSError *error, BOOL finished); 74 | 75 | typedef NSDictionary *(^SDWebImageDownloaderHeadersFilterBlock)(NSURL *url, NSDictionary *headers); 76 | 77 | /** 78 | * Asynchronous downloader dedicated and optimized for image loading. 79 | */ 80 | @interface SDWebImageDownloader : NSObject 81 | 82 | @property (assign, nonatomic) NSInteger maxConcurrentDownloads; 83 | 84 | /** 85 | * Shows the current amount of downloads that still need to be downloaded 86 | */ 87 | 88 | @property (readonly, nonatomic) NSUInteger currentDownloadCount; 89 | 90 | 91 | /** 92 | * The timeout value (in seconds) for the download operation. Default: 15.0. 93 | */ 94 | @property (assign, nonatomic) NSTimeInterval downloadTimeout; 95 | 96 | 97 | /** 98 | * Changes download operations execution order. Default value is `SDWebImageDownloaderFIFOExecutionOrder`. 99 | */ 100 | @property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder; 101 | 102 | /** 103 | * Singleton method, returns the shared instance 104 | * 105 | * @return global shared instance of downloader class 106 | */ 107 | + (SDWebImageDownloader *)sharedDownloader; 108 | 109 | /** 110 | * Set username 111 | */ 112 | @property (strong, nonatomic) NSString *username; 113 | 114 | /** 115 | * Set password 116 | */ 117 | @property (strong, nonatomic) NSString *password; 118 | 119 | /** 120 | * Set filter to pick headers for downloading image HTTP request. 121 | * 122 | * This block will be invoked for each downloading image request, returned 123 | * NSDictionary will be used as headers in corresponding HTTP request. 124 | */ 125 | @property (nonatomic, copy) SDWebImageDownloaderHeadersFilterBlock headersFilter; 126 | 127 | /** 128 | * Set a value for a HTTP header to be appended to each download HTTP request. 129 | * 130 | * @param value The value for the header field. Use `nil` value to remove the header. 131 | * @param field The name of the header field to set. 132 | */ 133 | - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field; 134 | 135 | /** 136 | * Returns the value of the specified HTTP header field. 137 | * 138 | * @return The value associated with the header field field, or `nil` if there is no corresponding header field. 139 | */ 140 | - (NSString *)valueForHTTPHeaderField:(NSString *)field; 141 | 142 | /** 143 | * Creates a SDWebImageDownloader async downloader instance with a given URL 144 | * 145 | * The delegate will be informed when the image is finish downloaded or an error has happen. 146 | * 147 | * @see SDWebImageDownloaderDelegate 148 | * 149 | * @param url The URL to the image to download 150 | * @param options The options to be used for this download 151 | * @param progressBlock A block called repeatedly while the image is downloading 152 | * @param completedBlock A block called once the download is completed. 153 | * If the download succeeded, the image parameter is set, in case of error, 154 | * error parameter is set with the error. The last parameter is always YES 155 | * if SDWebImageDownloaderProgressiveDownload isn't use. With the 156 | * SDWebImageDownloaderProgressiveDownload option, this block is called 157 | * repeatedly with the partial image object and the finished argument set to NO 158 | * before to be called a last time with the full image and finished argument 159 | * set to YES. In case of error, the finished argument is always YES. 160 | * 161 | * @return A cancellable SDWebImageOperation 162 | */ 163 | - (id )downloadImageWithURL:(NSURL *)url 164 | options:(SDWebImageDownloaderOptions)options 165 | progress:(SDWebImageDownloaderProgressBlock)progressBlock 166 | completed:(SDWebImageDownloaderCompletedBlock)completedBlock; 167 | 168 | /** 169 | * Sets the download queue suspension state 170 | */ 171 | - (void)setSuspended:(BOOL)suspended; 172 | 173 | @end 174 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImageDownloaderOperation.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import 10 | #import "SDWebImageDownloader.h" 11 | #import "SDWebImageOperation.h" 12 | 13 | @interface SDWebImageDownloaderOperation : NSOperation 14 | 15 | /** 16 | * The request used by the operation's connection. 17 | */ 18 | @property (strong, nonatomic, readonly) NSURLRequest *request; 19 | 20 | /** 21 | * Whether the URL connection should consult the credential storage for authenticating the connection. `YES` by default. 22 | * 23 | * This is the value that is returned in the `NSURLConnectionDelegate` method `-connectionShouldUseCredentialStorage:`. 24 | */ 25 | @property (nonatomic, assign) BOOL shouldUseCredentialStorage; 26 | 27 | /** 28 | * The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`. 29 | * 30 | * This will be overridden by any shared credentials that exist for the username or password of the request URL, if present. 31 | */ 32 | @property (nonatomic, strong) NSURLCredential *credential; 33 | 34 | /** 35 | * The SDWebImageDownloaderOptions for the receiver. 36 | */ 37 | @property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options; 38 | 39 | /** 40 | * Initializes a `SDWebImageDownloaderOperation` object 41 | * 42 | * @see SDWebImageDownloaderOperation 43 | * 44 | * @param request the URL request 45 | * @param options downloader options 46 | * @param progressBlock the block executed when a new chunk of data arrives. 47 | * @note the progress block is executed on a background queue 48 | * @param completedBlock the block executed when the download is done. 49 | * @note the completed block is executed on the main queue for success. If errors are found, there is a chance the block will be executed on a background queue 50 | * @param cancelBlock the block executed if the download (operation) is cancelled 51 | * 52 | * @return the initialized instance 53 | */ 54 | - (id)initWithRequest:(NSURLRequest *)request 55 | options:(SDWebImageDownloaderOptions)options 56 | progress:(SDWebImageDownloaderProgressBlock)progressBlock 57 | completed:(SDWebImageDownloaderCompletedBlock)completedBlock 58 | cancelled:(SDWebImageNoParamsBlock)cancelBlock; 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImageManager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import "SDWebImageCompat.h" 10 | #import "SDWebImageOperation.h" 11 | #import "SDWebImageDownloader.h" 12 | #import "SDImageCache.h" 13 | 14 | typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { 15 | /** 16 | * By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying. 17 | * This flag disable this blacklisting. 18 | */ 19 | SDWebImageRetryFailed = 1 << 0, 20 | 21 | /** 22 | * By default, image downloads are started during UI interactions, this flags disable this feature, 23 | * leading to delayed download on UIScrollView deceleration for instance. 24 | */ 25 | SDWebImageLowPriority = 1 << 1, 26 | 27 | /** 28 | * This flag disables on-disk caching 29 | */ 30 | SDWebImageCacheMemoryOnly = 1 << 2, 31 | 32 | /** 33 | * This flag enables progressive download, the image is displayed progressively during download as a browser would do. 34 | * By default, the image is only displayed once completely downloaded. 35 | */ 36 | SDWebImageProgressiveDownload = 1 << 3, 37 | 38 | /** 39 | * Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed. 40 | * The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation. 41 | * This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics. 42 | * If a cached image is refreshed, the completion block is called once with the cached image and again with the final image. 43 | * 44 | * Use this flag only if you can't make your URLs static with embeded cache busting parameter. 45 | */ 46 | SDWebImageRefreshCached = 1 << 4, 47 | 48 | /** 49 | * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for 50 | * extra time in background to let the request finish. If the background task expires the operation will be cancelled. 51 | */ 52 | SDWebImageContinueInBackground = 1 << 5, 53 | 54 | /** 55 | * Handles cookies stored in NSHTTPCookieStore by setting 56 | * NSMutableURLRequest.HTTPShouldHandleCookies = YES; 57 | */ 58 | SDWebImageHandleCookies = 1 << 6, 59 | 60 | /** 61 | * Enable to allow untrusted SSL ceriticates. 62 | * Useful for testing purposes. Use with caution in production. 63 | */ 64 | SDWebImageAllowInvalidSSLCertificates = 1 << 7, 65 | 66 | /** 67 | * By default, image are loaded in the order they were queued. This flag move them to 68 | * the front of the queue and is loaded immediately instead of waiting for the current queue to be loaded (which 69 | * could take a while). 70 | */ 71 | SDWebImageHighPriority = 1 << 8, 72 | 73 | /** 74 | * By default, placeholder images are loaded while the image is loading. This flag will delay the loading 75 | * of the placeholder image until after the image has finished loading. 76 | */ 77 | SDWebImageDelayPlaceholder = 1 << 9 78 | }; 79 | 80 | typedef void(^SDWebImageCompletionBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL); 81 | 82 | typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL); 83 | 84 | typedef NSString *(^SDWebImageCacheKeyFilterBlock)(NSURL *url); 85 | 86 | 87 | @class SDWebImageManager; 88 | 89 | @protocol SDWebImageManagerDelegate 90 | 91 | @optional 92 | 93 | /** 94 | * Controls which image should be downloaded when the image is not found in the cache. 95 | * 96 | * @param imageManager The current `SDWebImageManager` 97 | * @param imageURL The url of the image to be downloaded 98 | * 99 | * @return Return NO to prevent the downloading of the image on cache misses. If not implemented, YES is implied. 100 | */ 101 | - (BOOL)imageManager:(SDWebImageManager *)imageManager shouldDownloadImageForURL:(NSURL *)imageURL; 102 | 103 | /** 104 | * Allows to transform the image immediately after it has been downloaded and just before to cache it on disk and memory. 105 | * NOTE: This method is called from a global queue in order to not to block the main thread. 106 | * 107 | * @param imageManager The current `SDWebImageManager` 108 | * @param image The image to transform 109 | * @param imageURL The url of the image to transform 110 | * 111 | * @return The transformed image object. 112 | */ 113 | - (UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL; 114 | 115 | @end 116 | 117 | /** 118 | * The SDWebImageManager is the class behind the UIImageView+WebCache category and likes. 119 | * It ties the asynchronous downloader (SDWebImageDownloader) with the image cache store (SDImageCache). 120 | * You can use this class directly to benefit from web image downloading with caching in another context than 121 | * a UIView. 122 | * 123 | * Here is a simple example of how to use SDWebImageManager: 124 | * 125 | * @code 126 | 127 | SDWebImageManager *manager = [SDWebImageManager sharedManager]; 128 | [manager downloadWithURL:imageURL 129 | options:0 130 | progress:nil 131 | completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 132 | if (image) { 133 | // do something with image 134 | } 135 | }]; 136 | 137 | * @endcode 138 | */ 139 | @interface SDWebImageManager : NSObject 140 | 141 | @property (weak, nonatomic) id delegate; 142 | 143 | @property (strong, nonatomic, readonly) SDImageCache *imageCache; 144 | @property (strong, nonatomic, readonly) SDWebImageDownloader *imageDownloader; 145 | 146 | /** 147 | * The cache filter is a block used each time SDWebImageManager need to convert an URL into a cache key. This can 148 | * be used to remove dynamic part of an image URL. 149 | * 150 | * The following example sets a filter in the application delegate that will remove any query-string from the 151 | * URL before to use it as a cache key: 152 | * 153 | * @code 154 | 155 | [[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url) { 156 | url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path]; 157 | return [url absoluteString]; 158 | }]; 159 | 160 | * @endcode 161 | */ 162 | @property (copy) SDWebImageCacheKeyFilterBlock cacheKeyFilter; 163 | 164 | /** 165 | * Returns global SDWebImageManager instance. 166 | * 167 | * @return SDWebImageManager shared instance 168 | */ 169 | + (SDWebImageManager *)sharedManager; 170 | 171 | /** 172 | * Downloads the image at the given URL if not present in cache or return the cached version otherwise. 173 | * 174 | * @param url The URL to the image 175 | * @param options A mask to specify options to use for this request 176 | * @param progressBlock A block called while image is downloading 177 | * @param completedBlock A block called when operation has been completed. 178 | * 179 | * This parameter is required. 180 | * 181 | * This block has no return value and takes the requested UIImage as first parameter. 182 | * In case of error the image parameter is nil and the second parameter may contain an NSError. 183 | * 184 | * The third parameter is an `SDImageCacheType` enum indicating if the image was retrived from the local cache 185 | * or from the memory cache or from the network. 186 | * 187 | * The last parameter is set to NO when the SDWebImageProgressiveDownload option is used and the image is 188 | * downloading. This block is thus called repetidly with a partial image. When image is fully downloaded, the 189 | * block is called a last time with the full image and the last parameter set to YES. 190 | * 191 | * @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation 192 | */ 193 | - (id )downloadImageWithURL:(NSURL *)url 194 | options:(SDWebImageOptions)options 195 | progress:(SDWebImageDownloaderProgressBlock)progressBlock 196 | completed:(SDWebImageCompletionWithFinishedBlock)completedBlock; 197 | 198 | /** 199 | * Saves image to cache for given URL 200 | * 201 | * @param image The image to cache 202 | * @param url The URL to the image 203 | * 204 | */ 205 | 206 | - (void)saveImageToCache:(UIImage *)image forURL:(NSURL *)url; 207 | 208 | /** 209 | * Cancel all current opreations 210 | */ 211 | - (void)cancelAll; 212 | 213 | /** 214 | * Check one or more operations running 215 | */ 216 | - (BOOL)isRunning; 217 | 218 | /** 219 | * Check if image has already been cached 220 | * 221 | * @param url image url 222 | * 223 | * @return if the image was already cached 224 | */ 225 | - (BOOL)cachedImageExistsForURL:(NSURL *)url; 226 | 227 | /** 228 | * Check if image has already been cached on disk only 229 | * 230 | * @param url image url 231 | * 232 | * @return if the image was already cached (disk only) 233 | */ 234 | - (BOOL)diskImageExistsForURL:(NSURL *)url; 235 | 236 | /** 237 | * Async check if image has already been cached 238 | * 239 | * @param url image url 240 | * @param completionBlock the block to be executed when the check is finished 241 | * 242 | * @note the completion block is always executed on the main queue 243 | */ 244 | - (void)cachedImageExistsForURL:(NSURL *)url 245 | completion:(SDWebImageCheckCacheCompletionBlock)completionBlock; 246 | 247 | /** 248 | * Async check if image has already been cached on disk only 249 | * 250 | * @param url image url 251 | * @param completionBlock the block to be executed when the check is finished 252 | * 253 | * @note the completion block is always executed on the main queue 254 | */ 255 | - (void)diskImageExistsForURL:(NSURL *)url 256 | completion:(SDWebImageCheckCacheCompletionBlock)completionBlock; 257 | 258 | 259 | /** 260 | *Return the cache key for a given URL 261 | */ 262 | - (NSString *)cacheKeyForURL:(NSURL *)url; 263 | 264 | @end 265 | 266 | 267 | #pragma mark - Deprecated 268 | 269 | typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType) __deprecated_msg("Block type deprecated. Use `SDWebImageCompletionBlock`"); 270 | typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) __deprecated_msg("Block type deprecated. Use `SDWebImageCompletionWithFinishedBlock`"); 271 | 272 | 273 | @interface SDWebImageManager (Deprecated) 274 | 275 | /** 276 | * Downloads the image at the given URL if not present in cache or return the cached version otherwise. 277 | * 278 | * @deprecated This method has been deprecated. Use `downloadImageWithURL:options:progress:completed:` 279 | */ 280 | - (id )downloadWithURL:(NSURL *)url 281 | options:(SDWebImageOptions)options 282 | progress:(SDWebImageDownloaderProgressBlock)progressBlock 283 | completed:(SDWebImageCompletedWithFinishedBlock)completedBlock __deprecated_msg("Method deprecated. Use `downloadImageWithURL:options:progress:completed:`"); 284 | 285 | @end 286 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImageOperation.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import 10 | 11 | @protocol SDWebImageOperation 12 | 13 | - (void)cancel; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImagePrefetcher.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import 10 | #import "SDWebImageManager.h" 11 | 12 | @class SDWebImagePrefetcher; 13 | 14 | @protocol SDWebImagePrefetcherDelegate 15 | 16 | @optional 17 | 18 | /** 19 | * Called when an image was prefetched. 20 | * 21 | * @param imagePrefetcher The current image prefetcher 22 | * @param imageURL The image url that was prefetched 23 | * @param finishedCount The total number of images that were prefetched (successful or not) 24 | * @param totalCount The total number of images that were to be prefetched 25 | */ 26 | - (void)imagePrefetcher:(SDWebImagePrefetcher *)imagePrefetcher didPrefetchURL:(NSURL *)imageURL finishedCount:(NSUInteger)finishedCount totalCount:(NSUInteger)totalCount; 27 | 28 | /** 29 | * Called when all images are prefetched. 30 | * @param imagePrefetcher The current image prefetcher 31 | * @param totalCount The total number of images that were prefetched (whether successful or not) 32 | * @param skippedCount The total number of images that were skipped 33 | */ 34 | - (void)imagePrefetcher:(SDWebImagePrefetcher *)imagePrefetcher didFinishWithTotalCount:(NSUInteger)totalCount skippedCount:(NSUInteger)skippedCount; 35 | 36 | @end 37 | 38 | typedef void(^SDWebImagePrefetcherProgressBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls); 39 | typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls); 40 | 41 | /** 42 | * Prefetch some URLs in the cache for future use. Images are downloaded in low priority. 43 | */ 44 | @interface SDWebImagePrefetcher : NSObject 45 | 46 | /** 47 | * The web image manager 48 | */ 49 | @property (strong, nonatomic, readonly) SDWebImageManager *manager; 50 | 51 | /** 52 | * Maximum number of URLs to prefetch at the same time. Defaults to 3. 53 | */ 54 | @property (nonatomic, assign) NSUInteger maxConcurrentDownloads; 55 | 56 | /** 57 | * SDWebImageOptions for prefetcher. Defaults to SDWebImageLowPriority. 58 | */ 59 | @property (nonatomic, assign) SDWebImageOptions options; 60 | 61 | @property (weak, nonatomic) id delegate; 62 | 63 | /** 64 | * Return the global image prefetcher instance. 65 | */ 66 | + (SDWebImagePrefetcher *)sharedImagePrefetcher; 67 | 68 | /** 69 | * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching, 70 | * currently one image is downloaded at a time, 71 | * and skips images for failed downloads and proceed to the next image in the list 72 | * 73 | * @param urls list of URLs to prefetch 74 | */ 75 | - (void)prefetchURLs:(NSArray *)urls; 76 | 77 | /** 78 | * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching, 79 | * currently one image is downloaded at a time, 80 | * and skips images for failed downloads and proceed to the next image in the list 81 | * 82 | * @param urls list of URLs to prefetch 83 | * @param progressBlock block to be called when progress updates; 84 | * first parameter is the number of completed (successful or not) requests, 85 | * second parameter is the total number of images originally requested to be prefetched 86 | * @param completionBlock block to be called when prefetching is completed 87 | * first param is the number of completed (successful or not) requests, 88 | * second parameter is the number of skipped requests 89 | */ 90 | - (void)prefetchURLs:(NSArray *)urls progress:(SDWebImagePrefetcherProgressBlock)progressBlock completed:(SDWebImagePrefetcherCompletionBlock)completionBlock; 91 | 92 | /** 93 | * Remove and cancel queued list 94 | */ 95 | - (void)cancelPrefetching; 96 | 97 | 98 | @end 99 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/SDWebImagePrefetcher.m: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import "SDWebImagePrefetcher.h" 10 | 11 | #if !defined(DEBUG) && !defined (SD_VERBOSE) 12 | #define NSLog(...) 13 | #endif 14 | 15 | @interface SDWebImagePrefetcher () 16 | 17 | @property (strong, nonatomic) SDWebImageManager *manager; 18 | @property (strong, nonatomic) NSArray *prefetchURLs; 19 | @property (assign, nonatomic) NSUInteger requestedCount; 20 | @property (assign, nonatomic) NSUInteger skippedCount; 21 | @property (assign, nonatomic) NSUInteger finishedCount; 22 | @property (assign, nonatomic) NSTimeInterval startedTime; 23 | @property (copy, nonatomic) SDWebImagePrefetcherCompletionBlock completionBlock; 24 | @property (copy, nonatomic) SDWebImagePrefetcherProgressBlock progressBlock; 25 | 26 | @end 27 | 28 | @implementation SDWebImagePrefetcher 29 | 30 | + (SDWebImagePrefetcher *)sharedImagePrefetcher { 31 | static dispatch_once_t once; 32 | static id instance; 33 | dispatch_once(&once, ^{ 34 | instance = [self new]; 35 | }); 36 | return instance; 37 | } 38 | 39 | - (id)init { 40 | if ((self = [super init])) { 41 | _manager = [SDWebImageManager new]; 42 | _options = SDWebImageLowPriority; 43 | self.maxConcurrentDownloads = 3; 44 | } 45 | return self; 46 | } 47 | 48 | - (void)setMaxConcurrentDownloads:(NSUInteger)maxConcurrentDownloads { 49 | self.manager.imageDownloader.maxConcurrentDownloads = maxConcurrentDownloads; 50 | } 51 | 52 | - (NSUInteger)maxConcurrentDownloads { 53 | return self.manager.imageDownloader.maxConcurrentDownloads; 54 | } 55 | 56 | - (void)startPrefetchingAtIndex:(NSUInteger)index { 57 | if (index >= self.prefetchURLs.count) return; 58 | self.requestedCount++; 59 | [self.manager downloadImageWithURL:self.prefetchURLs[index] options:self.options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 60 | if (!finished) return; 61 | self.finishedCount++; 62 | 63 | if (image) { 64 | if (self.progressBlock) { 65 | self.progressBlock(self.finishedCount,[self.prefetchURLs count]); 66 | } 67 | NSLog(@"Prefetched %@ out of %@", @(self.finishedCount), @(self.prefetchURLs.count)); 68 | } 69 | else { 70 | if (self.progressBlock) { 71 | self.progressBlock(self.finishedCount,[self.prefetchURLs count]); 72 | } 73 | NSLog(@"Prefetched %@ out of %@ (Failed)", @(self.finishedCount), @(self.prefetchURLs.count)); 74 | 75 | // Add last failed 76 | self.skippedCount++; 77 | } 78 | if ([self.delegate respondsToSelector:@selector(imagePrefetcher:didPrefetchURL:finishedCount:totalCount:)]) { 79 | [self.delegate imagePrefetcher:self 80 | didPrefetchURL:self.prefetchURLs[index] 81 | finishedCount:self.finishedCount 82 | totalCount:self.prefetchURLs.count 83 | ]; 84 | } 85 | 86 | if (self.prefetchURLs.count > self.requestedCount) { 87 | dispatch_async(dispatch_get_main_queue(), ^{ 88 | [self startPrefetchingAtIndex:self.requestedCount]; 89 | }); 90 | } 91 | else if (self.finishedCount == self.requestedCount) { 92 | [self reportStatus]; 93 | if (self.completionBlock) { 94 | self.completionBlock(self.finishedCount, self.skippedCount); 95 | self.completionBlock = nil; 96 | } 97 | } 98 | }]; 99 | } 100 | 101 | - (void)reportStatus { 102 | NSUInteger total = [self.prefetchURLs count]; 103 | NSLog(@"Finished prefetching (%@ successful, %@ skipped, timeElasped %.2f)", @(total - self.skippedCount), @(self.skippedCount), CFAbsoluteTimeGetCurrent() - self.startedTime); 104 | if ([self.delegate respondsToSelector:@selector(imagePrefetcher:didFinishWithTotalCount:skippedCount:)]) { 105 | [self.delegate imagePrefetcher:self 106 | didFinishWithTotalCount:(total - self.skippedCount) 107 | skippedCount:self.skippedCount 108 | ]; 109 | } 110 | } 111 | 112 | - (void)prefetchURLs:(NSArray *)urls { 113 | [self prefetchURLs:urls progress:nil completed:nil]; 114 | } 115 | 116 | - (void)prefetchURLs:(NSArray *)urls progress:(SDWebImagePrefetcherProgressBlock)progressBlock completed:(SDWebImagePrefetcherCompletionBlock)completionBlock { 117 | [self cancelPrefetching]; // Prevent duplicate prefetch request 118 | self.startedTime = CFAbsoluteTimeGetCurrent(); 119 | self.prefetchURLs = urls; 120 | self.completionBlock = completionBlock; 121 | self.progressBlock = progressBlock; 122 | 123 | // Starts prefetching from the very first image on the list with the max allowed concurrency 124 | NSUInteger listCount = self.prefetchURLs.count; 125 | for (NSUInteger i = 0; i < self.maxConcurrentDownloads && self.requestedCount < listCount; i++) { 126 | [self startPrefetchingAtIndex:i]; 127 | } 128 | } 129 | 130 | - (void)cancelPrefetching { 131 | self.prefetchURLs = nil; 132 | self.skippedCount = 0; 133 | self.requestedCount = 0; 134 | self.finishedCount = 0; 135 | [self.manager cancelAll]; 136 | } 137 | 138 | @end 139 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImage+GIF.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+GIF.h 3 | // LBGIFImage 4 | // 5 | // Created by Laurin Brandner on 06.01.12. 6 | // Copyright (c) 2012 __MyCompanyName__. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIImage (GIF) 12 | 13 | + (UIImage *)sd_animatedGIFNamed:(NSString *)name; 14 | 15 | + (UIImage *)sd_animatedGIFWithData:(NSData *)data; 16 | 17 | - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImage+GIF.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+GIF.m 3 | // LBGIFImage 4 | // 5 | // Created by Laurin Brandner on 06.01.12. 6 | // Copyright (c) 2012 __MyCompanyName__. All rights reserved. 7 | // 8 | 9 | #import "UIImage+GIF.h" 10 | #import 11 | 12 | @implementation UIImage (GIF) 13 | 14 | + (UIImage *)sd_animatedGIFWithData:(NSData *)data { 15 | if (!data) { 16 | return nil; 17 | } 18 | 19 | CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); 20 | 21 | size_t count = CGImageSourceGetCount(source); 22 | 23 | UIImage *animatedImage; 24 | 25 | if (count <= 1) { 26 | animatedImage = [[UIImage alloc] initWithData:data]; 27 | } 28 | else { 29 | NSMutableArray *images = [NSMutableArray array]; 30 | 31 | NSTimeInterval duration = 0.0f; 32 | 33 | for (size_t i = 0; i < count; i++) { 34 | CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); 35 | 36 | duration += [self sd_frameDurationAtIndex:i source:source]; 37 | 38 | [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]]; 39 | 40 | CGImageRelease(image); 41 | } 42 | 43 | if (!duration) { 44 | duration = (1.0f / 10.0f) * count; 45 | } 46 | 47 | animatedImage = [UIImage animatedImageWithImages:images duration:duration]; 48 | } 49 | 50 | CFRelease(source); 51 | 52 | return animatedImage; 53 | } 54 | 55 | + (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source { 56 | float frameDuration = 0.1f; 57 | CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil); 58 | NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties; 59 | NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary]; 60 | 61 | NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime]; 62 | if (delayTimeUnclampedProp) { 63 | frameDuration = [delayTimeUnclampedProp floatValue]; 64 | } 65 | else { 66 | 67 | NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime]; 68 | if (delayTimeProp) { 69 | frameDuration = [delayTimeProp floatValue]; 70 | } 71 | } 72 | 73 | // Many annoying ads specify a 0 duration to make an image flash as quickly as possible. 74 | // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify 75 | // a duration of <= 10 ms. See and 76 | // for more information. 77 | 78 | if (frameDuration < 0.011f) { 79 | frameDuration = 0.100f; 80 | } 81 | 82 | CFRelease(cfFrameProperties); 83 | return frameDuration; 84 | } 85 | 86 | + (UIImage *)sd_animatedGIFNamed:(NSString *)name { 87 | CGFloat scale = [UIScreen mainScreen].scale; 88 | 89 | if (scale > 1.0f) { 90 | NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"]; 91 | 92 | NSData *data = [NSData dataWithContentsOfFile:retinaPath]; 93 | 94 | if (data) { 95 | return [UIImage sd_animatedGIFWithData:data]; 96 | } 97 | 98 | NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; 99 | 100 | data = [NSData dataWithContentsOfFile:path]; 101 | 102 | if (data) { 103 | return [UIImage sd_animatedGIFWithData:data]; 104 | } 105 | 106 | return [UIImage imageNamed:name]; 107 | } 108 | else { 109 | NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; 110 | 111 | NSData *data = [NSData dataWithContentsOfFile:path]; 112 | 113 | if (data) { 114 | return [UIImage sd_animatedGIFWithData:data]; 115 | } 116 | 117 | return [UIImage imageNamed:name]; 118 | } 119 | } 120 | 121 | - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size { 122 | if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) { 123 | return self; 124 | } 125 | 126 | CGSize scaledSize = size; 127 | CGPoint thumbnailPoint = CGPointZero; 128 | 129 | CGFloat widthFactor = size.width / self.size.width; 130 | CGFloat heightFactor = size.height / self.size.height; 131 | CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor : heightFactor; 132 | scaledSize.width = self.size.width * scaleFactor; 133 | scaledSize.height = self.size.height * scaleFactor; 134 | 135 | if (widthFactor > heightFactor) { 136 | thumbnailPoint.y = (size.height - scaledSize.height) * 0.5; 137 | } 138 | else if (widthFactor < heightFactor) { 139 | thumbnailPoint.x = (size.width - scaledSize.width) * 0.5; 140 | } 141 | 142 | NSMutableArray *scaledImages = [NSMutableArray array]; 143 | 144 | UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 145 | 146 | for (UIImage *image in self.images) { 147 | [image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)]; 148 | UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 149 | 150 | [scaledImages addObject:newImage]; 151 | } 152 | 153 | UIGraphicsEndImageContext(); 154 | 155 | return [UIImage animatedImageWithImages:scaledImages duration:self.duration]; 156 | } 157 | 158 | @end 159 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImage+MultiFormat.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+MultiFormat.h 3 | // SDWebImage 4 | // 5 | // Created by Olivier Poitrey on 07/06/13. 6 | // Copyright (c) 2013 Dailymotion. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIImage (MultiFormat) 12 | 13 | + (UIImage *)sd_imageWithData:(NSData *)data; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImage+MultiFormat.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+MultiFormat.m 3 | // SDWebImage 4 | // 5 | // Created by Olivier Poitrey on 07/06/13. 6 | // Copyright (c) 2013 Dailymotion. All rights reserved. 7 | // 8 | 9 | #import "UIImage+MultiFormat.h" 10 | #import "UIImage+GIF.h" 11 | #import "NSData+ImageContentType.h" 12 | #import 13 | 14 | #ifdef SD_WEBP 15 | #import "UIImage+WebP.h" 16 | #endif 17 | 18 | @implementation UIImage (MultiFormat) 19 | 20 | + (UIImage *)sd_imageWithData:(NSData *)data { 21 | UIImage *image; 22 | NSString *imageContentType = [NSData sd_contentTypeForImageData:data]; 23 | if ([imageContentType isEqualToString:@"image/gif"]) { 24 | image = [UIImage sd_animatedGIFWithData:data]; 25 | } 26 | #ifdef SD_WEBP 27 | else if ([imageContentType isEqualToString:@"image/webp"]) 28 | { 29 | image = [UIImage sd_imageWithWebPData:data]; 30 | } 31 | #endif 32 | else { 33 | image = [[UIImage alloc] initWithData:data]; 34 | UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data]; 35 | if (orientation != UIImageOrientationUp) { 36 | image = [UIImage imageWithCGImage:image.CGImage 37 | scale:image.scale 38 | orientation:orientation]; 39 | } 40 | } 41 | 42 | 43 | return image; 44 | } 45 | 46 | 47 | +(UIImageOrientation)sd_imageOrientationFromImageData:(NSData *)imageData { 48 | UIImageOrientation result = UIImageOrientationUp; 49 | CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL); 50 | if (imageSource) { 51 | CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL); 52 | if (properties) { 53 | CFTypeRef val; 54 | int exifOrientation; 55 | val = CFDictionaryGetValue(properties, kCGImagePropertyOrientation); 56 | if (val) { 57 | CFNumberGetValue(val, kCFNumberIntType, &exifOrientation); 58 | result = [self sd_exifOrientationToiOSOrientation:exifOrientation]; 59 | } // else - if it's not set it remains at up 60 | CFRelease((CFTypeRef) properties); 61 | } else { 62 | //NSLog(@"NO PROPERTIES, FAIL"); 63 | } 64 | CFRelease(imageSource); 65 | } 66 | return result; 67 | } 68 | 69 | #pragma mark EXIF orientation tag converter 70 | // Convert an EXIF image orientation to an iOS one. 71 | // reference see here: http://sylvana.net/jpegcrop/exif_orientation.html 72 | + (UIImageOrientation) sd_exifOrientationToiOSOrientation:(int)exifOrientation { 73 | UIImageOrientation orientation = UIImageOrientationUp; 74 | switch (exifOrientation) { 75 | case 1: 76 | orientation = UIImageOrientationUp; 77 | break; 78 | 79 | case 3: 80 | orientation = UIImageOrientationDown; 81 | break; 82 | 83 | case 8: 84 | orientation = UIImageOrientationLeft; 85 | break; 86 | 87 | case 6: 88 | orientation = UIImageOrientationRight; 89 | break; 90 | 91 | case 2: 92 | orientation = UIImageOrientationUpMirrored; 93 | break; 94 | 95 | case 4: 96 | orientation = UIImageOrientationDownMirrored; 97 | break; 98 | 99 | case 5: 100 | orientation = UIImageOrientationLeftMirrored; 101 | break; 102 | 103 | case 7: 104 | orientation = UIImageOrientationRightMirrored; 105 | break; 106 | default: 107 | break; 108 | } 109 | return orientation; 110 | } 111 | 112 | 113 | 114 | @end 115 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImage+WebP.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+WebP.h 3 | // SDWebImage 4 | // 5 | // Created by Olivier Poitrey on 07/06/13. 6 | // Copyright (c) 2013 Dailymotion. All rights reserved. 7 | // 8 | 9 | #ifdef SD_WEBP 10 | 11 | #import 12 | 13 | // Fix for issue #416 Undefined symbols for architecture armv7 since WebP introduction when deploying to device 14 | void WebPInitPremultiplyNEON(void); 15 | 16 | void WebPInitUpsamplersNEON(void); 17 | 18 | void VP8DspInitNEON(void); 19 | 20 | @interface UIImage (WebP) 21 | 22 | + (UIImage *)sd_imageWithWebPData:(NSData *)data; 23 | 24 | @end 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImage+WebP.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+WebP.m 3 | // SDWebImage 4 | // 5 | // Created by Olivier Poitrey on 07/06/13. 6 | // Copyright (c) 2013 Dailymotion. All rights reserved. 7 | // 8 | 9 | #ifdef SD_WEBP 10 | #import "UIImage+WebP.h" 11 | #import "webp/decode.h" 12 | 13 | // Callback for CGDataProviderRelease 14 | static void FreeImageData(void *info, const void *data, size_t size) 15 | { 16 | free((void *)data); 17 | } 18 | 19 | @implementation UIImage (WebP) 20 | 21 | + (UIImage *)sd_imageWithWebPData:(NSData *)data { 22 | WebPDecoderConfig config; 23 | if (!WebPInitDecoderConfig(&config)) { 24 | return nil; 25 | } 26 | 27 | config.output.colorspace = MODE_rgbA; 28 | config.options.use_threads = 1; 29 | 30 | // Decode the WebP image data into a RGBA value array. 31 | if (WebPDecode(data.bytes, data.length, &config) != VP8_STATUS_OK) { 32 | return nil; 33 | } 34 | 35 | int width = config.input.width; 36 | int height = config.input.height; 37 | if (config.options.use_scaling) { 38 | width = config.options.scaled_width; 39 | height = config.options.scaled_height; 40 | } 41 | 42 | // Construct a UIImage from the decoded RGBA value array. 43 | CGDataProviderRef provider = 44 | CGDataProviderCreateWithData(NULL, config.output.u.RGBA.rgba, config.output.u.RGBA.size, FreeImageData); 45 | CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 46 | CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast; 47 | CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 48 | CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); 49 | 50 | CGColorSpaceRelease(colorSpaceRef); 51 | CGDataProviderRelease(provider); 52 | 53 | UIImage *image = [[UIImage alloc] initWithCGImage:imageRef]; 54 | CGImageRelease(imageRef); 55 | 56 | return image; 57 | } 58 | 59 | @end 60 | 61 | #if !COCOAPODS 62 | // Functions to resolve some undefined symbols when using WebP and force_load flag 63 | void WebPInitPremultiplyNEON(void) {} 64 | void WebPInitUpsamplersNEON(void) {} 65 | void VP8DspInitNEON(void) {} 66 | #endif 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImageView+HighlightedWebCache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import 10 | #import "SDWebImageCompat.h" 11 | #import "SDWebImageManager.h" 12 | 13 | /** 14 | * Integrates SDWebImage async downloading and caching of remote images with UIImageView for highlighted state. 15 | */ 16 | @interface UIImageView (HighlightedWebCache) 17 | 18 | /** 19 | * Set the imageView `highlightedImage` with an `url`. 20 | * 21 | * The download is asynchronous and cached. 22 | * 23 | * @param url The url for the image. 24 | */ 25 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url; 26 | 27 | /** 28 | * Set the imageView `highlightedImage` with an `url` and custom options. 29 | * 30 | * The download is asynchronous and cached. 31 | * 32 | * @param url The url for the image. 33 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 34 | */ 35 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options; 36 | 37 | /** 38 | * Set the imageView `highlightedImage` with an `url`. 39 | * 40 | * The download is asynchronous and cached. 41 | * 42 | * @param url The url for the image. 43 | * @param completedBlock A block called when operation has been completed. This block has no return value 44 | * and takes the requested UIImage as first parameter. In case of error the image parameter 45 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 46 | * indicating if the image was retrived from the local cache or from the network. 47 | * The forth parameter is the original image url. 48 | */ 49 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock; 50 | 51 | /** 52 | * Set the imageView `highlightedImage` with an `url` and custom options. 53 | * 54 | * The download is asynchronous and cached. 55 | * 56 | * @param url The url for the image. 57 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 58 | * @param completedBlock A block called when operation has been completed. This block has no return value 59 | * and takes the requested UIImage as first parameter. In case of error the image parameter 60 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 61 | * indicating if the image was retrived from the local cache or from the network. 62 | * The forth parameter is the original image url. 63 | */ 64 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock; 65 | 66 | /** 67 | * Set the imageView `highlightedImage` with an `url` and custom options. 68 | * 69 | * The download is asynchronous and cached. 70 | * 71 | * @param url The url for the image. 72 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 73 | * @param progressBlock A block called while image is downloading 74 | * @param completedBlock A block called when operation has been completed. This block has no return value 75 | * and takes the requested UIImage as first parameter. In case of error the image parameter 76 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 77 | * indicating if the image was retrived from the local cache or from the network. 78 | * The forth parameter is the original image url. 79 | */ 80 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock; 81 | 82 | /** 83 | * Cancel the current download 84 | */ 85 | - (void)sd_cancelCurrentHighlightedImageLoad; 86 | 87 | @end 88 | 89 | 90 | @interface UIImageView (HighlightedWebCacheDeprecated) 91 | 92 | - (void)setHighlightedImageWithURL:(NSURL *)url __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:`"); 93 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:options:`"); 94 | - (void)setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:completed:`"); 95 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:options:completed:`"); 96 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:options:progress:completed:`"); 97 | 98 | - (void)cancelCurrentHighlightedImageLoad __deprecated_msg("Use `sd_cancelCurrentHighlightedImageLoad`"); 99 | 100 | @end 101 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImageView+HighlightedWebCache.m: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import "UIImageView+HighlightedWebCache.h" 10 | #import "UIView+WebCacheOperation.h" 11 | 12 | #define UIImageViewHighlightedWebCacheOperationKey @"highlightedImage" 13 | 14 | @implementation UIImageView (HighlightedWebCache) 15 | 16 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url { 17 | [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:nil]; 18 | } 19 | 20 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options { 21 | [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:nil]; 22 | } 23 | 24 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock { 25 | [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:completedBlock]; 26 | } 27 | 28 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock { 29 | [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:completedBlock]; 30 | } 31 | 32 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock { 33 | [self sd_cancelCurrentHighlightedImageLoad]; 34 | 35 | if (url) { 36 | __weak UIImageView *wself = self; 37 | id operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 38 | if (!wself) return; 39 | dispatch_main_sync_safe (^ 40 | { 41 | if (!wself) return; 42 | if (image) { 43 | wself.highlightedImage = image; 44 | [wself setNeedsLayout]; 45 | } 46 | if (completedBlock && finished) { 47 | completedBlock(image, error, cacheType, url); 48 | } 49 | }); 50 | }]; 51 | [self sd_setImageLoadOperation:operation forKey:UIImageViewHighlightedWebCacheOperationKey]; 52 | } else { 53 | dispatch_main_async_safe(^{ 54 | NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}]; 55 | if (completedBlock) { 56 | completedBlock(nil, error, SDImageCacheTypeNone, url); 57 | } 58 | }); 59 | } 60 | } 61 | 62 | - (void)sd_cancelCurrentHighlightedImageLoad { 63 | [self sd_cancelImageLoadOperationWithKey:UIImageViewHighlightedWebCacheOperationKey]; 64 | } 65 | 66 | @end 67 | 68 | 69 | @implementation UIImageView (HighlightedWebCacheDeprecated) 70 | 71 | - (void)setHighlightedImageWithURL:(NSURL *)url { 72 | [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:nil]; 73 | } 74 | 75 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options { 76 | [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:nil]; 77 | } 78 | 79 | - (void)setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock { 80 | [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 81 | if (completedBlock) { 82 | completedBlock(image, error, cacheType); 83 | } 84 | }]; 85 | } 86 | 87 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { 88 | [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 89 | if (completedBlock) { 90 | completedBlock(image, error, cacheType); 91 | } 92 | }]; 93 | } 94 | 95 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { 96 | [self sd_setHighlightedImageWithURL:url options:0 progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 97 | if (completedBlock) { 98 | completedBlock(image, error, cacheType); 99 | } 100 | }]; 101 | } 102 | 103 | - (void)cancelCurrentHighlightedImageLoad { 104 | [self sd_cancelCurrentHighlightedImageLoad]; 105 | } 106 | 107 | @end 108 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImageView+WebCache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import "SDWebImageCompat.h" 10 | #import "SDWebImageManager.h" 11 | 12 | /** 13 | * Integrates SDWebImage async downloading and caching of remote images with UIImageView. 14 | * 15 | * Usage with a UITableViewCell sub-class: 16 | * 17 | * @code 18 | 19 | #import 20 | 21 | ... 22 | 23 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 24 | { 25 | static NSString *MyIdentifier = @"MyIdentifier"; 26 | 27 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 28 | 29 | if (cell == nil) { 30 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] 31 | autorelease]; 32 | } 33 | 34 | // Here we use the provided sd_setImageWithURL: method to load the web image 35 | // Ensure you use a placeholder image otherwise cells will be initialized with no image 36 | [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"] 37 | placeholderImage:[UIImage imageNamed:@"placeholder"]]; 38 | 39 | cell.textLabel.text = @"My Text"; 40 | return cell; 41 | } 42 | 43 | * @endcode 44 | */ 45 | @interface UIImageView (WebCache) 46 | 47 | /** 48 | * Get the current image URL. 49 | * 50 | * Note that because of the limitations of categories this property can get out of sync 51 | * if you use sd_setImage: directly. 52 | */ 53 | - (NSURL *)sd_imageURL; 54 | 55 | /** 56 | * Set the imageView `image` with an `url`. 57 | * 58 | * The download is asynchronous and cached. 59 | * 60 | * @param url The url for the image. 61 | */ 62 | - (void)sd_setImageWithURL:(NSURL *)url; 63 | 64 | /** 65 | * Set the imageView `image` with an `url` and a placeholder. 66 | * 67 | * The download is asynchronous and cached. 68 | * 69 | * @param url The url for the image. 70 | * @param placeholder The image to be set initially, until the image request finishes. 71 | * @see sd_setImageWithURL:placeholderImage:options: 72 | */ 73 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; 74 | 75 | /** 76 | * Set the imageView `image` with an `url`, placeholder and custom options. 77 | * 78 | * The download is asynchronous and cached. 79 | * 80 | * @param url The url for the image. 81 | * @param placeholder The image to be set initially, until the image request finishes. 82 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 83 | */ 84 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options; 85 | 86 | /** 87 | * Set the imageView `image` with an `url`. 88 | * 89 | * The download is asynchronous and cached. 90 | * 91 | * @param url The url for the image. 92 | * @param completedBlock A block called when operation has been completed. This block has no return value 93 | * and takes the requested UIImage as first parameter. In case of error the image parameter 94 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 95 | * indicating if the image was retrived from the local cache or from the network. 96 | * The forth parameter is the original image url. 97 | */ 98 | - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock; 99 | 100 | /** 101 | * Set the imageView `image` with an `url`, placeholder. 102 | * 103 | * The download is asynchronous and cached. 104 | * 105 | * @param url The url for the image. 106 | * @param placeholder The image to be set initially, until the image request finishes. 107 | * @param completedBlock A block called when operation has been completed. This block has no return value 108 | * and takes the requested UIImage as first parameter. In case of error the image parameter 109 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 110 | * indicating if the image was retrived from the local cache or from the network. 111 | * The forth parameter is the original image url. 112 | */ 113 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock; 114 | 115 | /** 116 | * Set the imageView `image` with an `url`, placeholder and custom options. 117 | * 118 | * The download is asynchronous and cached. 119 | * 120 | * @param url The url for the image. 121 | * @param placeholder The image to be set initially, until the image request finishes. 122 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 123 | * @param completedBlock A block called when operation has been completed. This block has no return value 124 | * and takes the requested UIImage as first parameter. In case of error the image parameter 125 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 126 | * indicating if the image was retrived from the local cache or from the network. 127 | * The forth parameter is the original image url. 128 | */ 129 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock; 130 | 131 | /** 132 | * Set the imageView `image` with an `url`, placeholder and custom options. 133 | * 134 | * The download is asynchronous and cached. 135 | * 136 | * @param url The url for the image. 137 | * @param placeholder The image to be set initially, until the image request finishes. 138 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 139 | * @param progressBlock A block called while image is downloading 140 | * @param completedBlock A block called when operation has been completed. This block has no return value 141 | * and takes the requested UIImage as first parameter. In case of error the image parameter 142 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 143 | * indicating if the image was retrived from the local cache or from the network. 144 | * The forth parameter is the original image url. 145 | */ 146 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock; 147 | 148 | /** 149 | * Set the imageView `image` with an `url` and a optionaly placeholder image. 150 | * 151 | * The download is asynchronous and cached. 152 | * 153 | * @param url The url for the image. 154 | * @param placeholder The image to be set initially, until the image request finishes. 155 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. 156 | * @param progressBlock A block called while image is downloading 157 | * @param completedBlock A block called when operation has been completed. This block has no return value 158 | * and takes the requested UIImage as first parameter. In case of error the image parameter 159 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean 160 | * indicating if the image was retrived from the local cache or from the network. 161 | * The forth parameter is the original image url. 162 | */ 163 | - (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock; 164 | 165 | /** 166 | * Download an array of images and starts them in an animation loop 167 | * 168 | * @param arrayOfURLs An array of NSURL 169 | */ 170 | - (void)sd_setAnimationImagesWithURLs:(NSArray *)arrayOfURLs; 171 | 172 | /** 173 | * Cancel the current download 174 | */ 175 | - (void)sd_cancelCurrentImageLoad; 176 | 177 | - (void)sd_cancelCurrentAnimationImagesLoad; 178 | 179 | @end 180 | 181 | 182 | @interface UIImageView (WebCacheDeprecated) 183 | 184 | - (NSURL *)imageURL __deprecated_msg("Use `sd_imageURL`"); 185 | 186 | - (void)setImageWithURL:(NSURL *)url __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:`"); 187 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:`"); 188 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options`"); 189 | 190 | - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:completed:`"); 191 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:completed:`"); 192 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options:completed:`"); 193 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options:progress:completed:`"); 194 | 195 | - (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs __deprecated_msg("Use `sd_setAnimationImagesWithURLs:`"); 196 | 197 | - (void)cancelCurrentArrayLoad __deprecated_msg("Use `sd_cancelCurrentAnimationImagesLoad`"); 198 | 199 | - (void)cancelCurrentImageLoad __deprecated_msg("Use `sd_cancelCurrentImageLoad`"); 200 | 201 | @end 202 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIImageView+WebCache.m: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import "UIImageView+WebCache.h" 10 | #import "objc/runtime.h" 11 | #import "UIView+WebCacheOperation.h" 12 | 13 | static char imageURLKey; 14 | 15 | @implementation UIImageView (WebCache) 16 | 17 | - (void)sd_setImageWithURL:(NSURL *)url { 18 | [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil]; 19 | } 20 | 21 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { 22 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil]; 23 | } 24 | 25 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { 26 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil]; 27 | } 28 | 29 | - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock { 30 | [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock]; 31 | } 32 | 33 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock { 34 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock]; 35 | } 36 | 37 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock { 38 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock]; 39 | } 40 | 41 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock { 42 | [self sd_cancelCurrentImageLoad]; 43 | objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 44 | 45 | if (!(options & SDWebImageDelayPlaceholder)) { 46 | self.image = placeholder; 47 | } 48 | 49 | if (url) { 50 | __weak UIImageView *wself = self; 51 | id operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 52 | if (!wself) return; 53 | dispatch_main_sync_safe(^{ 54 | if (!wself) return; 55 | if (image) { 56 | wself.image = image; 57 | [wself setNeedsLayout]; 58 | } else { 59 | if ((options & SDWebImageDelayPlaceholder)) { 60 | wself.image = placeholder; 61 | [wself setNeedsLayout]; 62 | } 63 | } 64 | if (completedBlock && finished) { 65 | completedBlock(image, error, cacheType, url); 66 | } 67 | }); 68 | }]; 69 | [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"]; 70 | } else { 71 | dispatch_main_async_safe(^{ 72 | NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}]; 73 | if (completedBlock) { 74 | completedBlock(nil, error, SDImageCacheTypeNone, url); 75 | } 76 | }); 77 | } 78 | } 79 | 80 | - (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock { 81 | NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url]; 82 | UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key]; 83 | 84 | [self sd_setImageWithURL:url placeholderImage:lastPreviousCachedImage ?: placeholder options:options progress:progressBlock completed:completedBlock]; 85 | } 86 | 87 | - (NSURL *)sd_imageURL { 88 | return objc_getAssociatedObject(self, &imageURLKey); 89 | } 90 | 91 | - (void)sd_setAnimationImagesWithURLs:(NSArray *)arrayOfURLs { 92 | [self sd_cancelCurrentAnimationImagesLoad]; 93 | __weak UIImageView *wself = self; 94 | 95 | NSMutableArray *operationsArray = [[NSMutableArray alloc] init]; 96 | 97 | for (NSURL *logoImageURL in arrayOfURLs) { 98 | id operation = [SDWebImageManager.sharedManager downloadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 99 | if (!wself) return; 100 | dispatch_main_sync_safe(^{ 101 | __strong UIImageView *sself = wself; 102 | [sself stopAnimating]; 103 | if (sself && image) { 104 | NSMutableArray *currentImages = [[sself animationImages] mutableCopy]; 105 | if (!currentImages) { 106 | currentImages = [[NSMutableArray alloc] init]; 107 | } 108 | [currentImages addObject:image]; 109 | 110 | sself.animationImages = currentImages; 111 | [sself setNeedsLayout]; 112 | } 113 | [sself startAnimating]; 114 | }); 115 | }]; 116 | [operationsArray addObject:operation]; 117 | } 118 | 119 | [self sd_setImageLoadOperation:[NSArray arrayWithArray:operationsArray] forKey:@"UIImageViewAnimationImages"]; 120 | } 121 | 122 | - (void)sd_cancelCurrentImageLoad { 123 | [self sd_cancelImageLoadOperationWithKey:@"UIImageViewImageLoad"]; 124 | } 125 | 126 | - (void)sd_cancelCurrentAnimationImagesLoad { 127 | [self sd_cancelImageLoadOperationWithKey:@"UIImageViewAnimationImages"]; 128 | } 129 | 130 | @end 131 | 132 | 133 | @implementation UIImageView (WebCacheDeprecated) 134 | 135 | - (NSURL *)imageURL { 136 | return [self sd_imageURL]; 137 | } 138 | 139 | - (void)setImageWithURL:(NSURL *)url { 140 | [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil]; 141 | } 142 | 143 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { 144 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil]; 145 | } 146 | 147 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { 148 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil]; 149 | } 150 | 151 | - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock { 152 | [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 153 | if (completedBlock) { 154 | completedBlock(image, error, cacheType); 155 | } 156 | }]; 157 | } 158 | 159 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock { 160 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 161 | if (completedBlock) { 162 | completedBlock(image, error, cacheType); 163 | } 164 | }]; 165 | } 166 | 167 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { 168 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 169 | if (completedBlock) { 170 | completedBlock(image, error, cacheType); 171 | } 172 | }]; 173 | } 174 | 175 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { 176 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 177 | if (completedBlock) { 178 | completedBlock(image, error, cacheType); 179 | } 180 | }]; 181 | } 182 | 183 | - (void)cancelCurrentArrayLoad { 184 | [self sd_cancelCurrentAnimationImagesLoad]; 185 | } 186 | 187 | - (void)cancelCurrentImageLoad { 188 | [self sd_cancelCurrentImageLoad]; 189 | } 190 | 191 | - (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs { 192 | [self sd_setAnimationImagesWithURLs:arrayOfURLs]; 193 | } 194 | 195 | @end 196 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIView+WebCacheOperation.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import 10 | #import "SDWebImageManager.h" 11 | 12 | @interface UIView (WebCacheOperation) 13 | 14 | /** 15 | * Set the image load operation (storage in a UIView based dictionary) 16 | * 17 | * @param operation the operation 18 | * @param key key for storing the operation 19 | */ 20 | - (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key; 21 | 22 | /** 23 | * Cancel all operations for the current UIView and key 24 | * 25 | * @param key key for identifying the operations 26 | */ 27 | - (void)sd_cancelImageLoadOperationWithKey:(NSString *)key; 28 | 29 | /** 30 | * Just remove the operations corresponding to the current UIView and key without cancelling them 31 | * 32 | * @param key key for identifying the operations 33 | */ 34 | - (void)sd_removeImageLoadOperationWithKey:(NSString *)key; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /HorizontalTableView/Lib/SDWebImage/UIView+WebCacheOperation.m: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDWebImage package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import "UIView+WebCacheOperation.h" 10 | #import "objc/runtime.h" 11 | 12 | static char loadOperationKey; 13 | 14 | @implementation UIView (WebCacheOperation) 15 | 16 | - (NSMutableDictionary *)operationDictionary { 17 | NSMutableDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey); 18 | if (operations) { 19 | return operations; 20 | } 21 | operations = [NSMutableDictionary dictionary]; 22 | objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 23 | return operations; 24 | } 25 | 26 | - (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key { 27 | [self sd_cancelImageLoadOperationWithKey:key]; 28 | NSMutableDictionary *operationDictionary = [self operationDictionary]; 29 | [operationDictionary setObject:operation forKey:key]; 30 | } 31 | 32 | - (void)sd_cancelImageLoadOperationWithKey:(NSString *)key { 33 | // Cancel in progress downloader from queue 34 | NSMutableDictionary *operationDictionary = [self operationDictionary]; 35 | id operations = [operationDictionary objectForKey:key]; 36 | if (operations) { 37 | if ([operations isKindOfClass:[NSArray class]]) { 38 | for (id operation in operations) { 39 | if (operation) { 40 | [operation cancel]; 41 | } 42 | } 43 | } else if ([operations conformsToProtocol:@protocol(SDWebImageOperation)]){ 44 | [(id) operations cancel]; 45 | } 46 | [operationDictionary removeObjectForKey:key]; 47 | } 48 | } 49 | 50 | - (void)sd_removeImageLoadOperationWithKey:(NSString *)key { 51 | NSMutableDictionary *operationDictionary = [self operationDictionary]; 52 | [operationDictionary removeObjectForKey:key]; 53 | } 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /HorizontalTableView/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /HorizontalTableView/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "ZPHTableView.h" 11 | #import "ZPHTableViewCell.h" 12 | #import "HMShop.h" 13 | #import "HMShopCell.h" 14 | #import "MJExtension.h" 15 | #import "MJRefresh.h" 16 | 17 | static NSUInteger const loopNumber = 50; 18 | 19 | @interface ViewController () 20 | @property (weak, nonatomic) ZPHTableView *htableView; 21 | @property (nonatomic, strong)NSMutableArray *shops; 22 | @end 23 | 24 | @implementation ViewController 25 | - (NSMutableArray *)shops { 26 | if (_shops == nil) { 27 | self.shops = [NSMutableArray array]; 28 | NSArray *shops = [HMShop objectArrayWithFilename:@"2.plist"]; 29 | [self.shops addObjectsFromArray:shops]; 30 | } 31 | return _shops; 32 | } 33 | 34 | - (void)viewDidLoad { 35 | [super viewDidLoad]; 36 | ZPHTableView *htableView = [[ZPHTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)]; 37 | self.htableView = htableView; 38 | self.htableView.delegate = self; 39 | self.htableView.dataSource = self; 40 | self.htableView.backgroundColor = [UIColor redColor]; 41 | 42 | [self.view addSubview:htableView]; 43 | [self.htableView addHeaderWithTarget:self action:@selector(loadNewShops)]; 44 | [self.htableView addFooterWithTarget:self action:@selector(loadMoreShops)]; 45 | [self.htableView showItemAtIndex:self.shops.count * loopNumber / 2]; 46 | } 47 | 48 | - (void)loadNewShops { 49 | static dispatch_once_t onceToken; 50 | dispatch_once(&onceToken, ^{ 51 | NSArray *newShops = [HMShop objectArrayWithFilename:@"1.plist"]; 52 | NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newShops.count)]; 53 | [self.shops insertObjects:newShops atIndexes:set]; 54 | 55 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 56 | [self.htableView reloadData]; 57 | [self.htableView headerEndRefreshing]; 58 | }); 59 | }); 60 | } 61 | 62 | - (void)loadMoreShops { 63 | static dispatch_once_t onceToken; 64 | dispatch_once(&onceToken, ^{ 65 | NSArray *moreShops = [HMShop objectArrayWithFilename:@"3.plist"]; 66 | [self.shops addObjectsFromArray:moreShops]; 67 | 68 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 69 | [self.htableView reloadData]; 70 | [self.htableView headerEndRefreshing]; 71 | }); 72 | }); 73 | } 74 | 75 | /** 总共有多少个条目 */ 76 | - (NSUInteger)numberOfCellInHTableView:(ZPHTableView *)htableView{ 77 | return self.shops.count * loopNumber; 78 | } 79 | 80 | /** 一页要显示多少个 */ 81 | //- (NSUInteger)numberOfCellInOnePageWithHTableView:(ZPHTableView *)htableView { 82 | // return 1; 83 | //} 84 | 85 | - (ZPHTableViewCell *)htableView:(ZPHTableView *)htableView cellAtIndex:(NSUInteger)index { 86 | HMShopCell *cell = [HMShopCell cellWithWaterflowView:htableView]; 87 | cell.shop = self.shops[index % self.shops.count]; 88 | 89 | cell.backgroundColor = [UIColor grayColor]; 90 | return cell; 91 | } 92 | 93 | @end 94 | -------------------------------------------------------------------------------- /HorizontalTableView/ZPHorizontalTableView/ZPHTableView.h: -------------------------------------------------------------------------------- 1 | // 2 | // ZPHTableView.h 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @class ZPHTableView, ZPHTableViewCell; 12 | 13 | typedef enum { 14 | ZPHTableViewMarginTypeTop, 15 | ZPHTableViewMarginTypeBottom, 16 | ZPHTableViewMarginTypeLeft, 17 | ZPHTableViewMarginTypeRight, 18 | ZPHTableViewMarginTypeColumn, // 每一列 19 | ZPHTableViewMarginTypeRow, // 每一行 20 | } ZPHTableViewMarginType; 21 | 22 | /** 数据源 */ 23 | @protocol ZPHTableViewDataSource 24 | @required 25 | /** 一共有多少个数据 */ 26 | - (NSUInteger)numberOfCellInHTableView:(ZPHTableView *)htableView; 27 | 28 | /** 一行多少个条目 */ 29 | - (NSUInteger)numberOfCellInOnePageWithHTableView:(ZPHTableView *)htableView; 30 | 31 | /** 返回index位置对应的cell */ 32 | - (ZPHTableViewCell *)htableView:(ZPHTableView *)htableView cellAtIndex:(NSUInteger)index; 33 | @end 34 | 35 | /** 代理方法 */ 36 | @protocol ZPHtableViewDelegate 37 | @optional 38 | /** 第index位置cell对应的宽度 */ 39 | - (CGFloat)htableView:(ZPHTableView *)htableView widthAtIndex:(NSUInteger)index; 40 | 41 | /** 选中第index位置的cell */ 42 | - (void)htableView:(ZPHTableView *)htableView didSelectAtIndex:(NSUInteger)index; 43 | 44 | /** 返回间距 */ 45 | - (CGFloat)htableview:(ZPHTableView *)htableView marginForType:(ZPHTableViewMarginType)type; 46 | @end 47 | 48 | /** 水平滚动tableView */ 49 | @interface ZPHTableView : UIScrollView 50 | 51 | /** 数据源 */ 52 | @property (nonatomic, weak) id dataSource; 53 | 54 | /** 代理 */ 55 | @property (nonatomic, weak) id delegate; 56 | 57 | /** 刷新数据(只要调用这个方法,会重新向数据源和代理发送请求,请求数据)*/ 58 | - (void)reloadData; 59 | 60 | /** 根据标识去缓存池查找可循环利用的cell */ 61 | - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; 62 | 63 | /** cell的高度 */ 64 | - (CGFloat)cellHeight; 65 | 66 | /** 显示第Index个条目 */ 67 | - (void)showItemAtIndex:(NSUInteger)index; 68 | @end 69 | -------------------------------------------------------------------------------- /HorizontalTableView/ZPHorizontalTableView/ZPHTableView.m: -------------------------------------------------------------------------------- 1 | // 2 | // ZPHTableView.m 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. All rights reserved. 7 | // 8 | 9 | #import "ZPHTableView.h" 10 | #import "ZPHTableViewCell.h" 11 | 12 | static CGFloat const ZPHTableViewDefaultNumberOfCell = 3; 13 | static CGFloat const ZPHTableViewDefaultMargin = 0; 14 | static NSUInteger const ZPHTableViewDefaultNumberOfMoreCheckout = 40; 15 | 16 | @interface ZPHTableView () 17 | /** 所有cell的frame数据 */ 18 | @property (nonatomic, strong)NSMutableArray *cellFrames; 19 | /** 正在展示的cell */ 20 | @property (nonatomic, strong)NSMutableDictionary *displayingCells; 21 | /** 正在展示的cell的index */ 22 | @property (nonatomic, strong)NSMutableArray *displayingCellIndexs; 23 | 24 | /** 缓存池(用Set,存放离开屏幕的cell) */ 25 | @property (nonatomic, strong)NSMutableSet *reusableCells; 26 | 27 | @end 28 | 29 | @implementation ZPHTableView 30 | #pragma 初始化 31 | - (NSMutableArray *)cellFrames { 32 | if (_cellFrames == nil) { 33 | self.cellFrames = [NSMutableArray array]; 34 | } 35 | return _cellFrames; 36 | } 37 | 38 | - (NSMutableDictionary *)displayingCells { 39 | if (_displayingCells == nil) { 40 | self.displayingCells = [NSMutableDictionary dictionary]; 41 | } 42 | return _displayingCells; 43 | } 44 | 45 | - (NSMutableArray *)displayingCellIndexs { 46 | if (_displayingCellIndexs == nil) { 47 | self.displayingCellIndexs = [NSMutableArray array]; 48 | } 49 | return _displayingCellIndexs; 50 | } 51 | 52 | - (NSMutableSet *)reusableCells { 53 | if (_reusableCells == nil) { 54 | self.reusableCells = [NSMutableSet set]; 55 | } 56 | return _reusableCells; 57 | } 58 | 59 | - (void)willMoveToSuperview:(UIView *)newSuperview { 60 | [self reloadData]; 61 | } 62 | 63 | - (instancetype)initWithFrame:(CGRect)frame { 64 | if (self = [super initWithFrame:frame]) { 65 | self.decelerationRate = UIScrollViewDecelerationRateFast; 66 | } 67 | return self; 68 | } 69 | 70 | /** 加载数据 */ 71 | - (void)reloadData { 72 | // NSLog(@"reloadData"); 73 | //清除之前数据 74 | [self.displayingCells.allValues makeObjectsPerformSelector:@selector(removeFromSuperview)]; 75 | [self.displayingCells removeAllObjects]; 76 | [self.cellFrames removeAllObjects]; 77 | [self.reusableCells removeAllObjects]; 78 | 79 | //目标:算出所有cell的frame 80 | NSUInteger numberOfCells = [self.dataSource numberOfCellInHTableView:self]; 81 | CGFloat cellHeight = [self cellHeight]; 82 | 83 | CGFloat cellMarginLeft = [self marginForType:ZPHTableViewMarginTypeLeft]; 84 | CGFloat cellMarginRight = [self marginForType:ZPHTableViewMarginTypeRight]; 85 | CGFloat cellMarginColum = [self marginForType:ZPHTableViewMarginTypeColumn]; 86 | CGFloat cellMarginTop = [self marginForType:ZPHTableViewMarginTypeTop]; 87 | 88 | for (int i = 0; i < numberOfCells; i++) { 89 | CGFloat cellWidth = [self widthAtIndex:i]; 90 | CGFloat cellX = cellMarginLeft + (cellWidth + cellMarginColum) * i; 91 | CGFloat cellY = cellMarginTop; 92 | CGRect frame = CGRectMake(cellX, cellY, cellWidth, cellHeight); 93 | [self.cellFrames addObject:[NSValue valueWithCGRect:frame]]; 94 | } 95 | CGRect maxFrame = [self.cellFrames.lastObject CGRectValue]; 96 | CGFloat contentMaxX = CGRectGetMaxX(maxFrame); 97 | self.contentSize = CGSizeMake(contentMaxX + cellMarginRight, cellHeight); 98 | } 99 | CGFloat lastOffestX = 0; 100 | /** 当UIScrollView滚动的时候会调用这个方法 */ 101 | - (void)layoutSubviews { 102 | [super layoutSubviews]; 103 | 104 | CGFloat currentOffestX = self.contentOffset.x; 105 | CGFloat offest = ABS(currentOffestX - lastOffestX); 106 | 107 | int count = offest / [self widthAtIndex:0]; 108 | if (count > 25) { 109 | NSLog(@"scroll so fastest offest count = %d", count); 110 | } 111 | 112 | lastOffestX = currentOffestX; 113 | 114 | // 向数据源索要对应位置的cell 115 | NSUInteger numberOfCells = self.cellFrames.count; 116 | 117 | NSUInteger minDisplayingIndex = [self.displayingCellIndexs.firstObject unsignedIntValue]; 118 | NSUInteger maxDisplayingIndex = [self.displayingCellIndexs.lastObject unsignedIntValue]; 119 | 120 | if (self.displayingCellIndexs.count == 0) { //根据contentOffest.x算现在该显示哪个cell 121 | // TODO: 1 122 | minDisplayingIndex = [self indexFromContentOffestX]; 123 | maxDisplayingIndex = minDisplayingIndex + [self numberOfCellInOnePage]; 124 | NSLog(@"displayingCells.count == 0 minDisplayingIndex = %zd", minDisplayingIndex); 125 | } 126 | 127 | int startIndex = minDisplayingIndex - ZPHTableViewDefaultNumberOfMoreCheckout; 128 | NSUInteger endIndex = maxDisplayingIndex + ZPHTableViewDefaultNumberOfMoreCheckout; 129 | 130 | if (startIndex < 0) { 131 | startIndex = 0; 132 | } 133 | 134 | if (endIndex >= numberOfCells) { 135 | endIndex = numberOfCells; 136 | } 137 | 138 | 139 | for (int i = startIndex; i < endIndex; i++) { 140 | // 取出i位置的frame 141 | CGRect cellFrame = [self.cellFrames[i] CGRectValue]; 142 | 143 | // 优先从字典中取出i位置的cell 144 | ZPHTableViewCell *cell = self.displayingCells[@(i)]; 145 | if ([self isInScreen:cellFrame]) { 146 | if (cell == nil) { 147 | cell = [self.dataSource htableView:self cellAtIndex:i]; 148 | cell.frame = cellFrame; 149 | self.displayingCells[@(i)] = cell; 150 | [self.displayingCellIndexs addObject:[NSNumber numberWithInt:i]]; 151 | [self addSubview:cell]; 152 | } 153 | } else { 154 | if (cell) { 155 | [cell removeFromSuperview]; 156 | [self.displayingCells removeObjectForKey:@(i)]; 157 | [self.displayingCellIndexs removeObject:[NSNumber numberWithInt:i]]; 158 | [self.reusableCells addObject:cell]; 159 | } 160 | } 161 | } 162 | } 163 | 164 | - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier { 165 | __block ZPHTableViewCell *reusableCell = nil; 166 | [self.reusableCells enumerateObjectsUsingBlock:^(ZPHTableViewCell *cell, BOOL *stop) { 167 | if ([cell.identifier isEqualToString:identifier]) { 168 | reusableCell = cell; 169 | *stop = YES; 170 | } 171 | }]; 172 | 173 | if (reusableCell) { 174 | [self.reusableCells removeObject:reusableCell]; 175 | } 176 | return reusableCell; 177 | } 178 | 179 | - (NSUInteger)indexFromContentOffestX { 180 | NSUInteger index = self.contentOffset.x / [self widthAtIndex:0]; 181 | return index; 182 | } 183 | 184 | /** 判断一个frame有无显示在屏幕上 */ 185 | - (BOOL)isInScreen:(CGRect)frame { 186 | return (CGRectGetMaxX(frame) > self.contentOffset.x) && 187 | (CGRectGetMinX(frame) < self.contentOffset.x + self.bounds.size.width); 188 | } 189 | 190 | /** cell的宽度 */ 191 | - (CGFloat)widthAtIndex:(NSUInteger)index { 192 | if ([self.delegate respondsToSelector:@selector(htableView:widthAtIndex:)]) { 193 | return [self.delegate htableView:self widthAtIndex:index]; 194 | } else { 195 | CGFloat marginLeft = [self marginForType:ZPHTableViewMarginTypeLeft]; 196 | CGFloat marginRight = [self marginForType:ZPHTableViewMarginTypeRight]; 197 | return (self.bounds.size.width - marginLeft - marginRight) / [self numberOfCellInOnePage]; 198 | } 199 | } 200 | 201 | /** cell的高度 */ 202 | - (CGFloat)cellHeight { 203 | CGFloat marginTop = [self marginForType:ZPHTableViewMarginTypeTop]; 204 | CGFloat marginBottom = [self marginForType:ZPHTableViewMarginTypeBottom]; 205 | return self.bounds.size.height - marginTop - marginBottom; 206 | } 207 | 208 | /** 获取间距 */ 209 | - (CGFloat)marginForType:(ZPHTableViewMarginType)type { 210 | if ([self.delegate respondsToSelector:@selector(htableview:marginForType:)]) { 211 | return [self.delegate htableview:self marginForType:type]; 212 | } else { 213 | if (ZPHTableViewMarginTypeColumn == type) { 214 | return 0; 215 | } else { 216 | return ZPHTableViewDefaultMargin; 217 | } 218 | } 219 | } 220 | 221 | /** 一页显示多少个 */ 222 | - (NSUInteger)numberOfCellInOnePage { 223 | if ([self.dataSource respondsToSelector:@selector(numberOfCellInOnePageWithHTableView:)]) { 224 | return [self.dataSource numberOfCellInOnePageWithHTableView:self]; 225 | } else { 226 | return ZPHTableViewDefaultNumberOfCell; 227 | } 228 | } 229 | 230 | - (void)showItemAtIndex:(NSUInteger)index { 231 | if (self.cellFrames.count < index) { 232 | NSLog(@"error cellFrames.count = 0"); 233 | return; 234 | } 235 | CGFloat offestX = 0; 236 | for (int i = 0; i <= index; i++) { 237 | CGRect frame = [self.cellFrames[i] CGRectValue]; 238 | offestX += frame.size.width; 239 | } 240 | CGPoint half = CGPointMake(offestX, 0); 241 | [self setContentOffset:half animated:NO]; 242 | } 243 | @end 244 | -------------------------------------------------------------------------------- /HorizontalTableView/ZPHorizontalTableView/ZPHTableViewCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // ZPHTableViewCell.h 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ZPHTableViewCell : UIView 12 | @property (nonatomic, copy) NSString *identifier; 13 | @end 14 | -------------------------------------------------------------------------------- /HorizontalTableView/ZPHorizontalTableView/ZPHTableViewCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // ZPHTableViewCell.m 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. All rights reserved. 7 | // 8 | 9 | #import "ZPHTableViewCell.h" 10 | 11 | @implementation ZPHTableViewCell 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 | -------------------------------------------------------------------------------- /HorizontalTableView/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // HorizontalTableView 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. 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 | -------------------------------------------------------------------------------- /HorizontalTableViewTests/HorizontalTableViewTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // HorizontalTableViewTests.m 3 | // HorizontalTableViewTests 4 | // 5 | // Created by zhangping on 15/3/19. 6 | // Copyright (c) 2015年 dycx. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface HorizontalTableViewTests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation HorizontalTableViewTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown { 24 | // Put teardown code here. This method is called after the invocation of each test method in the class. 25 | [super tearDown]; 26 | } 27 | 28 | - (void)testExample { 29 | // This is an example of a functional test case. 30 | XCTAssert(YES, @"Pass"); 31 | } 32 | 33 | - (void)testPerformanceExample { 34 | // This is an example of a performance test case. 35 | [self measureBlock:^{ 36 | // Put the code you want to measure the time of here. 37 | }]; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /HorizontalTableViewTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | hk.com.dycx.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | --------------------------------------------------------------------------------