├── README.md ├── SDDemo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcuserdata │ │ └── as_one.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── as_one.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ └── xcschememanagement.plist └── SDDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets ├── AppIcon.appiconset │ ├── Contents.json │ ├── Icon-120.png │ ├── Icon-121.png │ ├── Icon-180.png │ ├── Icon-87.png │ ├── Icon-Small@2x.png │ └── Icon-Spotlight-40@2x.png ├── BaiduImage │ ├── Contents.json │ ├── a.imageset │ │ ├── Contents.json │ │ └── a.png │ ├── b.imageset │ │ ├── Contents.json │ │ └── b.png │ ├── d.imageset │ │ ├── Contents.json │ │ └── d.png │ ├── e.imageset │ │ ├── Contents.json │ │ └── e.png │ ├── h.imageset │ │ ├── Contents.json │ │ └── h.png │ ├── j.imageset │ │ ├── Contents.json │ │ └── j.png │ └── phil.imageset │ │ ├── Contents.json │ │ └── phil.png └── Contents.json ├── Base.lproj └── LaunchScreen.storyboard ├── Demo ├── Controller │ ├── BaiduTableViewController.h │ ├── BaiduTableViewController.m │ ├── SDAnimationControllerViewController.h │ ├── SDAnimationControllerViewController.m │ ├── SDItemLayoutViewController.h │ └── SDItemLayoutViewController.m ├── Model │ ├── FDFeedEntity.h │ └── FDFeedEntity.m └── View │ ├── FDFeedCell.h │ ├── FDFeedCell.m │ └── FDFeedCell.xib ├── Info.plist ├── Layout ├── UITableView+SDIndexPathHeightCache.h ├── UITableView+SDIndexPathHeightCache.m ├── UITableView+SDTemplateLayoutCell.h └── UITableView+SDTemplateLayoutCell.m ├── Lib └── Masonry │ ├── MASCompositeConstraint.h │ ├── MASCompositeConstraint.m │ ├── MASConstraint+Private.h │ ├── MASConstraint.h │ ├── MASConstraint.m │ ├── MASConstraintMaker.h │ ├── MASConstraintMaker.m │ ├── MASLayoutConstraint.h │ ├── MASLayoutConstraint.m │ ├── MASUtilities.h │ ├── MASViewAttribute.h │ ├── MASViewAttribute.m │ ├── MASViewConstraint.h │ ├── MASViewConstraint.m │ ├── Masonry.h │ ├── NSArray+MASAdditions.h │ ├── NSArray+MASAdditions.m │ ├── NSArray+MASShorthandAdditions.h │ ├── NSLayoutConstraint+MASDebugAdditions.h │ ├── NSLayoutConstraint+MASDebugAdditions.m │ ├── View+MASAdditions.h │ ├── View+MASAdditions.m │ ├── View+MASShorthandAdditions.h │ ├── ViewController+MASAdditions.h │ └── ViewController+MASAdditions.m ├── Resource ├── baidu.json ├── demo.gif ├── item.png └── qq.png ├── SDMacro.h ├── SDPrefixHeader.pch ├── TableViewController.h ├── TableViewController.m ├── Tools ├── HTTPManager.h └── HTTPManager.m ├── listView.gif └── main.m /README.md: -------------------------------------------------------------------------------- 1 | ## 一.SDDemo包含 2 | 3 | #####二.更新动态 4 | * 流畅的图文混排 5 | * 递归算法实现iOS补位动画 6 | * 一个UIView中Item的自排算法 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | > 流畅的图文混排说明 15 | 16 | 流畅的图文混排说明:一个简单的图文混排,图片大小自适应,限制最大宽度,使用indexPath缓存行高。支持cell编辑插入、收藏、点赞、置顶、删除功能。 17 | 缓存行高功能,能够支持90%以上的需求,如果数据计算较为复杂,可以对数据预处理+缓存行高,就相对较好了。 18 | 如果有什么问题,可以建issue,我会及时回复。 19 | 20 | ``` 21 | Containts the following function: 22 | 1. How to whrit a fluent listView; 23 | 2. How to edit the tableViewCell; 24 | 3. How to make the imageView size fit imageSize; 25 | 4. How to use the xib; 26 | 5. How to use the constain; 27 | ``` 28 | 29 | 30 | ``` 31 | //  以下是部分图文混排的代码 32 | 33 | // 插入一行 34 | - (void)InsertRow { 35 | if (self.feedEntitySections.count == 0) { 36 | [self InsertSection]; 37 | } else { 38 | [self.feedEntitySections[0] insertObject:self.randomEntity atIndex:0]; 39 | NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 40 | [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 41 | } 42 | } 43 | // 插入一组 44 | - (void)InsertSection { 45 | [self.feedEntitySections insertObject:@[self.randomEntity].mutableCopy atIndex:0]; 46 | [self.tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; 47 | } 48 | // 删除一组 49 | - (void)deleteSection { 50 | if (self.feedEntitySections.count>0) { 51 | [self.feedEntitySections removeObjectAtIndex:0]; 52 | [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; 53 | } 54 | } 55 | // 删除指定行 56 | - (void)deleteRowWithIndexPath:(NSIndexPath *)indexPath { 57 | if (!indexPath) return; 58 | NSArray *feeds = self.feedEntitySections[indexPath.section]; 59 | if (feeds.count > 1) { 60 | [self.feedEntitySections[indexPath.section] removeObjectAtIndex:indexPath.row]; 61 | [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic]; 62 | } else { 63 | [self.feedEntitySections removeObject:self.feedEntitySections[indexPath.section]]; 64 | [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]; 65 | } 66 | 67 | } 68 | 69 | // 置顶 70 | - (void)moveWithIndexPath:(NSIndexPath *)indexPath { 71 | if (!indexPath) return; 72 | NSArray *sections = self.feedEntitySections[indexPath.section]; 73 | if (sections.count > 1) { 74 | [self.feedEntitySections[0] insertObject:sections[indexPath.row] atIndex:0]; 75 | [self.feedEntitySections[indexPath.section] removeObjectAtIndex:indexPath.row + 1]; 76 | [self.tableView reloadData]; 77 | } else { 78 | [self.feedEntitySections[0] insertObject:sections[indexPath.row] atIndex:0]; 79 | [self.feedEntitySections removeObjectAtIndex:indexPath.section + 1]; 80 | [self.tableView reloadData]; 81 | } 82 | } 83 | 84 | 85 | ``` 86 | 87 | 88 | -------------------------------------------------------------------------------- /SDDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SDDemo.xcodeproj/project.xcworkspace/xcuserdata/as_one.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo.xcodeproj/project.xcworkspace/xcuserdata/as_one.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SDDemo.xcodeproj/xcuserdata/as_one.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /SDDemo.xcodeproj/xcuserdata/as_one.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SDDemo.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SDDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. 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 | -------------------------------------------------------------------------------- /SDDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "TableViewController.h" 11 | 12 | @interface AppDelegate () 13 | 14 | @end 15 | 16 | @implementation AppDelegate 17 | 18 | 19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | 21 | self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 22 | 23 | self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[TableViewController new]]; 24 | 25 | [self.window makeKeyAndVisible]; 26 | 27 | return YES; 28 | } 29 | 30 | 31 | - (void)applicationWillResignActive:(UIApplication *)application { 32 | // 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. 33 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 34 | } 35 | 36 | 37 | - (void)applicationDidEnterBackground:(UIApplication *)application { 38 | // 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. 39 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 40 | } 41 | 42 | 43 | - (void)applicationWillEnterForeground:(UIApplication *)application { 44 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 45 | } 46 | 47 | 48 | - (void)applicationDidBecomeActive:(UIApplication *)application { 49 | // 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. 50 | } 51 | 52 | 53 | - (void)applicationWillTerminate:(UIApplication *)application { 54 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 55 | } 56 | 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "size" : "29x29", 20 | "idiom" : "iphone", 21 | "filename" : "Icon-87.png", 22 | "scale" : "3x" 23 | }, 24 | { 25 | "idiom" : "iphone", 26 | "size" : "40x40", 27 | "scale" : "2x" 28 | }, 29 | { 30 | "idiom" : "iphone", 31 | "size" : "40x40", 32 | "scale" : "3x" 33 | }, 34 | { 35 | "idiom" : "iphone", 36 | "size" : "60x60", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "idiom" : "iphone", 41 | "size" : "60x60", 42 | "scale" : "3x" 43 | }, 44 | { 45 | "idiom" : "ipad", 46 | "size" : "20x20", 47 | "scale" : "1x" 48 | }, 49 | { 50 | "idiom" : "ipad", 51 | "size" : "20x20", 52 | "scale" : "2x" 53 | }, 54 | { 55 | "idiom" : "ipad", 56 | "size" : "29x29", 57 | "scale" : "1x" 58 | }, 59 | { 60 | "size" : "29x29", 61 | "idiom" : "ipad", 62 | "filename" : "Icon-Small@2x.png", 63 | "scale" : "2x" 64 | }, 65 | { 66 | "idiom" : "ipad", 67 | "size" : "40x40", 68 | "scale" : "1x" 69 | }, 70 | { 71 | "idiom" : "ipad", 72 | "size" : "40x40", 73 | "scale" : "2x" 74 | }, 75 | { 76 | "idiom" : "ipad", 77 | "size" : "76x76", 78 | "scale" : "1x" 79 | }, 80 | { 81 | "idiom" : "ipad", 82 | "size" : "76x76", 83 | "scale" : "2x" 84 | }, 85 | { 86 | "idiom" : "ipad", 87 | "size" : "83.5x83.5", 88 | "scale" : "2x" 89 | }, 90 | { 91 | "idiom" : "ios-marketing", 92 | "size" : "1024x1024", 93 | "scale" : "1x" 94 | }, 95 | { 96 | "size" : "60x60", 97 | "idiom" : "car", 98 | "filename" : "Icon-120.png", 99 | "scale" : "2x" 100 | }, 101 | { 102 | "size" : "60x60", 103 | "idiom" : "car", 104 | "filename" : "Icon-180.png", 105 | "scale" : "3x" 106 | }, 107 | { 108 | "size" : "24x24", 109 | "idiom" : "watch", 110 | "scale" : "2x", 111 | "role" : "notificationCenter", 112 | "subtype" : "38mm" 113 | }, 114 | { 115 | "size" : "27.5x27.5", 116 | "idiom" : "watch", 117 | "scale" : "2x", 118 | "role" : "notificationCenter", 119 | "subtype" : "42mm" 120 | }, 121 | { 122 | "size" : "29x29", 123 | "idiom" : "watch", 124 | "role" : "companionSettings", 125 | "scale" : "2x" 126 | }, 127 | { 128 | "size" : "29x29", 129 | "idiom" : "watch", 130 | "role" : "companionSettings", 131 | "scale" : "3x" 132 | }, 133 | { 134 | "size" : "40x40", 135 | "idiom" : "watch", 136 | "filename" : "Icon-Spotlight-40@2x.png", 137 | "scale" : "2x", 138 | "role" : "appLauncher", 139 | "subtype" : "38mm" 140 | }, 141 | { 142 | "size" : "44x44", 143 | "idiom" : "watch", 144 | "scale" : "2x", 145 | "role" : "appLauncher", 146 | "subtype" : "40mm" 147 | }, 148 | { 149 | "size" : "50x50", 150 | "idiom" : "watch", 151 | "scale" : "2x", 152 | "role" : "appLauncher", 153 | "subtype" : "44mm" 154 | }, 155 | { 156 | "size" : "86x86", 157 | "idiom" : "watch", 158 | "scale" : "2x", 159 | "role" : "quickLook", 160 | "subtype" : "38mm" 161 | }, 162 | { 163 | "size" : "98x98", 164 | "idiom" : "watch", 165 | "scale" : "2x", 166 | "role" : "quickLook", 167 | "subtype" : "42mm" 168 | }, 169 | { 170 | "size" : "108x108", 171 | "idiom" : "watch", 172 | "scale" : "2x", 173 | "role" : "quickLook", 174 | "subtype" : "44mm" 175 | }, 176 | { 177 | "idiom" : "watch-marketing", 178 | "size" : "1024x1024", 179 | "scale" : "1x" 180 | }, 181 | { 182 | "size" : "60x60", 183 | "idiom" : "car", 184 | "filename" : "Icon-121.png", 185 | "unassigned" : true, 186 | "scale" : "2x" 187 | } 188 | ], 189 | "info" : { 190 | "version" : 1, 191 | "author" : "xcode" 192 | } 193 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-120.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-121.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-121.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-180.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-87.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-87.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-Small@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-Small@2x.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-Spotlight-40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/AppIcon.appiconset/Icon-Spotlight-40@2x.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/a.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "a.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/a.imageset/a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/BaiduImage/a.imageset/a.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/b.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "b.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/b.imageset/b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/BaiduImage/b.imageset/b.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/d.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "d.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/d.imageset/d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/BaiduImage/d.imageset/d.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/e.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "e.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/e.imageset/e.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/BaiduImage/e.imageset/e.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/h.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "h.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/h.imageset/h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/BaiduImage/h.imageset/h.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/j.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "j.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/j.imageset/j.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/BaiduImage/j.imageset/j.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/phil.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "phil.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/BaiduImage/phil.imageset/phil.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Assets.xcassets/BaiduImage/phil.imageset/phil.png -------------------------------------------------------------------------------- /SDDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SDDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /SDDemo/Demo/Controller/BaiduTableViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // xuelinTableViewController.h 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SDTableViewController : UITableViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /SDDemo/Demo/Controller/BaiduTableViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // xuelinTableViewController.m 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | 8 | #import "BaiduTableViewController.h" 9 | #import "FDFeedEntity.h" 10 | #import "FDFeedCell.h" 11 | #import "UITableView+SDTemplateLayoutCell.h" 12 | 13 | @interface SDTableViewController () 14 | @property (nonatomic, copy) NSArray *prototypeEntitiesFromJSON; 15 | @property (nonatomic, strong) NSMutableArray *feedEntitySections; // 2d array 16 | 17 | @end 18 | 19 | @implementation SDTableViewController 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | 24 | self.navigationItem.title = @"小憩"; 25 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Actions" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)]; 26 | 27 | [self.tableView registerNib:[UINib nibWithNibName:@"FDFeedCell" bundle:nil] forCellReuseIdentifier:@"FDFeedCell"]; 28 | 29 | // 加载数据 30 | [self buildTestDataThen:^{ 31 | self.feedEntitySections = @[].mutableCopy; 32 | [self.feedEntitySections addObject:self.prototypeEntitiesFromJSON.mutableCopy]; 33 | 34 | [self.tableView reloadData]; 35 | }]; 36 | 37 | // 刷新控件 38 | UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; 39 | self.tableView.refreshControl = refreshControl; 40 | [refreshControl addTarget:self action:@selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged]; 41 | refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"]; 42 | 43 | } 44 | 45 | #pragma mark - Table view data source 46 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 47 | return self.feedEntitySections.count; 48 | } 49 | 50 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 51 | return [self.feedEntitySections[section] count]; 52 | } 53 | 54 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 55 | FDFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FDFeedCell"]; 56 | [self configerCell:cell atIndexPath:indexPath]; 57 | return cell; 58 | } 59 | 60 | 61 | - (void)configerCell:(FDFeedCell *)cell atIndexPath:(NSIndexPath *)indexPath { 62 | cell.sd_enforceFrameLayout = NO; 63 | cell.entity = self.feedEntitySections[indexPath.section][indexPath.row]; 64 | } 65 | 66 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 67 | return [tableView sd_heightForCellWithIdentifier:@"FDFeedCell" cacheByIndexPath:indexPath configuration:^(id cell) { 68 | [self configerCell:cell atIndexPath:indexPath]; 69 | }]; 70 | } 71 | 72 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 73 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; 74 | } 75 | 76 | // 左划动作 77 | - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 78 | UITableViewRowAction *actionOne = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { 79 | [self deleteRowWithIndexPath:indexPath]; 80 | 81 | }]; 82 | UITableViewRowAction *actionTwo = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"🌹top" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { 83 | [self moveWithIndexPath:indexPath]; 84 | }]; 85 | UITableViewRowAction *actionThree = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"👍" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { 86 | FDFeedCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 87 | cell.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0]; 88 | }]; 89 | 90 | actionTwo.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0]; 91 | actionThree.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0]; 92 | 93 | return @[actionOne,actionTwo,actionThree]; 94 | } 95 | 96 | 97 | #pragma mark - loadData 98 | - (void)buildTestDataThen:(void (^)(void))then { 99 | // Simulate an async request 100 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 101 | 102 | // Data from `data.json` 103 | NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:@"baidu" ofType:@"json"]; 104 | NSData *data = [NSData dataWithContentsOfFile:dataFilePath]; 105 | NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; 106 | NSArray *feedDicts = rootDict[@"feed"]; 107 | 108 | // Convert to `FDFeedEntity` 109 | NSMutableArray *entities = @[].mutableCopy; 110 | [feedDicts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 111 | [entities addObject:[[FDFeedEntity alloc] initWithDictionary:obj]]; 112 | }]; 113 | self.prototypeEntitiesFromJSON = entities; 114 | 115 | // Callback 116 | dispatch_async(dispatch_get_main_queue(), ^{ 117 | !then ?: then(); 118 | }); 119 | }); 120 | } 121 | // 下拉加载数据 122 | - (void)refreshControlAction:(UIRefreshControl *)sender { 123 | [sender beginRefreshing]; 124 | if (sender.refreshing) 125 | sender.attributedTitle = [[NSAttributedString alloc]initWithString:@"努力加载中..."]; 126 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 127 | [self.feedEntitySections removeAllObjects]; 128 | [self.feedEntitySections addObject:self.prototypeEntitiesFromJSON.mutableCopy]; 129 | [self.tableView reloadData]; 130 | [sender endRefreshing]; 131 | }); 132 | } 133 | 134 | #pragma mark - rightButtonAction 135 | - (void)rightButtonAction { 136 | [[[UIActionSheet alloc] initWithTitle:@"Actions" 137 | delegate:self 138 | cancelButtonTitle:@"Cancel" 139 | destructiveButtonTitle:nil 140 | otherButtonTitles:@"Insert a row",@"Insert a section",@"Delete a section", nil] showInView:self.view]; 141 | } 142 | 143 | - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 144 | SEL selectors[] = { 145 | @selector(InsertRow), 146 | @selector(InsertSection), 147 | @selector(deleteSection), 148 | }; 149 | NSLog(@"%ld----%ld-------%ld",buttonIndex,sizeof(selectors),sizeof(SEL)); 150 | if (buttonIndex < sizeof(selectors) / sizeof(SEL)) { 151 | // 方法名 -> 方法id -> 方法内存地址 -> 根据方法地址调用方法 152 | void(*imp)(id, SEL) = (typeof(imp))[self methodForSelector:selectors[buttonIndex]]; 153 | imp(self, selectors[buttonIndex]); 154 | } 155 | 156 | } 157 | 158 | // 随机插入数据 159 | - (FDFeedEntity *)randomEntity { 160 | NSUInteger randomNumber = arc4random_uniform((int32_t)self.prototypeEntitiesFromJSON.count); 161 | FDFeedEntity *randomEntity = self.prototypeEntitiesFromJSON[randomNumber]; 162 | return randomEntity; 163 | } 164 | 165 | // 插入一行 166 | - (void)InsertRow { 167 | if (self.feedEntitySections.count == 0) { 168 | [self InsertSection]; 169 | } else { 170 | [self.feedEntitySections[0] insertObject:self.randomEntity atIndex:0]; 171 | NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 172 | [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 173 | } 174 | } 175 | // 插入一组 176 | - (void)InsertSection { 177 | [self.feedEntitySections insertObject:@[self.randomEntity].mutableCopy atIndex:0]; 178 | [self.tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; 179 | } 180 | // 删除一组 181 | - (void)deleteSection { 182 | if (self.feedEntitySections.count>0) { 183 | [self.feedEntitySections removeObjectAtIndex:0]; 184 | [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; 185 | } 186 | } 187 | // 删除指定行 188 | - (void)deleteRowWithIndexPath:(NSIndexPath *)indexPath { 189 | if (!indexPath) return; 190 | NSArray *feeds = self.feedEntitySections[indexPath.section]; 191 | if (feeds.count > 1) { 192 | [self.feedEntitySections[indexPath.section] removeObjectAtIndex:indexPath.row]; 193 | [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic]; 194 | } else { 195 | [self.feedEntitySections removeObject:self.feedEntitySections[indexPath.section]]; 196 | [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]; 197 | } 198 | 199 | } 200 | 201 | // 置顶 202 | - (void)moveWithIndexPath:(NSIndexPath *)indexPath { 203 | if (!indexPath) return; 204 | NSArray *sections = self.feedEntitySections[indexPath.section]; 205 | if (sections.count > 1) { 206 | [self.feedEntitySections[0] insertObject:sections[indexPath.row] atIndex:0]; 207 | [self.feedEntitySections[indexPath.section] removeObjectAtIndex:indexPath.row + 1]; 208 | [self.tableView reloadData]; 209 | } else { 210 | [self.feedEntitySections[0] insertObject:sections[indexPath.row] atIndex:0]; 211 | [self.feedEntitySections removeObjectAtIndex:indexPath.section + 1]; 212 | [self.tableView reloadData]; 213 | } 214 | } 215 | 216 | @end 217 | -------------------------------------------------------------------------------- /SDDemo/Demo/Controller/SDAnimationControllerViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // SDAnimationControllerViewController.h 3 | // SDDemo 4 | // 5 | // Created by xuelin on 2018/12/2. 6 | // Copyright © 2018 xuelin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface SDAnimationControllerViewController : UIViewController 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /SDDemo/Demo/Controller/SDAnimationControllerViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // SDAnimationControllerViewController.m 3 | // SDDemo 4 | // 5 | // Created by xuelin on 2018/12/2. 6 | // Copyright © 2018 xuelin. All rights reserved. 7 | // 8 | 9 | #import "SDAnimationControllerViewController.h" 10 | 11 | static NSInteger const AnimationCount = 4; 12 | 13 | @interface SDAnimationControllerViewController () 14 | // 从右向左动画相关 15 | @property (nonatomic, strong) NSMutableArray *rightBtns; 16 | @property (nonatomic, strong) NSMutableArray *rightFrames; 17 | @property (nonatomic, strong) UIView *rightBackView; 18 | 19 | // 从底部想上动画相关 20 | @property (nonatomic, strong) NSMutableArray *bottomBtns; 21 | @property (nonatomic, strong) NSMutableArray *bottomFrames; 22 | @property (nonatomic, strong) UIView *bottomBackView; 23 | @end 24 | 25 | @implementation SDAnimationControllerViewController 26 | 27 | - (void)viewDidLoad { 28 | [super viewDidLoad]; 29 | 30 | self.view.backgroundColor = [UIColor whiteColor]; 31 | 32 | _rightBtns = @[].mutableCopy; 33 | _rightFrames = @[].mutableCopy; 34 | _bottomBtns = @[].mutableCopy; 35 | _bottomFrames = @[].mutableCopy; 36 | 37 | // 从右侧开始向左进行动画 38 | [self setAnimationViewFromRight]; 39 | 40 | // 从底部向上进行动画 41 | [self setAnimationViewFromBottom]; 42 | } 43 | 44 | // 从底部向上进行动画 45 | - (void)setAnimationViewFromBottom { 46 | UIView *backview = [[UIView alloc] initWithFrame:CGRectMake(0, 80, kScreenWidth, 210)]; 47 | [self.view addSubview:backview]; 48 | self.bottomBackView = backview; 49 | backview.backgroundColor = [UIColor orangeColor]; 50 | 51 | CGFloat buttonHeight = 210 / 3; 52 | for (int i = 0; i < AnimationCount; i++) { 53 | CGFloat y = buttonHeight * i; 54 | UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, y, kScreenWidth, buttonHeight)]; 55 | [self.bottomBtns addObject:button]; 56 | [backview addSubview:button]; 57 | [button addTarget:self action:@selector(bottomAction:) forControlEvents:UIControlEventTouchUpInside]; 58 | button.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0]; 59 | [backview addSubview:button]; 60 | button.tag = i; 61 | [self.bottomFrames addObject:[NSValue valueWithCGRect:button.frame]]; 62 | [button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal]; 63 | } 64 | backview.clipsToBounds = YES; 65 | } 66 | 67 | - (void)bottomAction:(UIButton *)button { 68 | button.hidden = YES; 69 | [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ 70 | [self bottonChangeFrame:button]; 71 | button.alpha = 0; 72 | } completion:^(BOOL finished) { 73 | button.frame = self.bottomFrames.lastObject.CGRectValue; 74 | button.hidden = NO; 75 | [self bottomReloadTagAndFrame:button]; 76 | [UIView animateWithDuration:0.5 animations:^{ 77 | button.alpha = 1; 78 | }]; 79 | }]; 80 | } 81 | 82 | - (void)bottonChangeFrame:(UIButton *)button { 83 | [self bottomFrameChange:button tag:button.tag]; 84 | } 85 | 86 | - (void)bottomFrameChange:(UIButton *)button tag:(NSInteger)tag { 87 | if (tag == 0) { 88 | tag++; 89 | } 90 | self.bottomBtns[tag].frame = self.bottomFrames[tag - 1].CGRectValue; 91 | if (tag < 3 && tag >= 1) { 92 | tag++; 93 | [self bottomFrameChange:self.bottomBtns[tag] tag:tag]; 94 | } 95 | } 96 | 97 | - (void)bottomReloadTagAndFrame:(UIButton *)button { 98 | UIButton *tempButton = button; 99 | [self.bottomBtns removeObject:button]; 100 | [self.bottomBtns addObject:tempButton]; 101 | [self.bottomBtns enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 102 | obj.tag = idx; 103 | }]; 104 | } 105 | 106 | 107 | 108 | // 从右侧开始向左进行动画 109 | - (void)setAnimationViewFromRight { 110 | UIView *backview = [[UIView alloc] initWithFrame:CGRectMake(0, 360, kScreenWidth, 120)]; 111 | [self.view addSubview:backview]; 112 | self.rightBackView = backview; 113 | backview.backgroundColor = [UIColor orangeColor]; 114 | 115 | CGFloat itemSpace = 15; 116 | CGFloat buttonWidth = (kScreenWidth - itemSpace * 4) / 3; 117 | 118 | for (int i = 0; i < AnimationCount; i++) { 119 | CGFloat x = itemSpace + (buttonWidth + itemSpace) * i; 120 | UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, buttonWidth, 80)]; 121 | [self.rightBtns addObject:button]; 122 | [backview addSubview:button]; 123 | [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 124 | button.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0]; 125 | [backview addSubview:button]; 126 | button.tag = i; 127 | [self.rightFrames addObject:[NSValue valueWithCGRect:button.frame]]; 128 | [button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal]; 129 | } 130 | } 131 | 132 | - (void)action:(UIButton *)button { 133 | button.hidden = YES; 134 | [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ 135 | [self changeFrame:button]; 136 | button.alpha = 0; 137 | } completion:^(BOOL finished) { 138 | button.frame = self.rightFrames.lastObject.CGRectValue; 139 | button.hidden = NO; 140 | [self reloadTagAndFrame:button]; 141 | [UIView animateWithDuration:0.5 animations:^{ 142 | button.alpha = 1; 143 | }]; 144 | }]; 145 | } 146 | 147 | - (void)changeFrame:(UIButton *)button { 148 | [self frameChange:button tag:button.tag]; 149 | } 150 | 151 | - (void)frameChange:(UIButton *)button tag:(NSInteger)tag { 152 | if (tag == 0) { 153 | tag++; 154 | } 155 | self.rightBtns[tag].frame = self.rightFrames[tag - 1].CGRectValue; 156 | if (tag < 3 && tag >= 1) { 157 | tag++; 158 | [self frameChange:self.rightBtns[tag] tag:tag]; 159 | } 160 | } 161 | 162 | - (void)reloadTagAndFrame:(UIButton *)button { 163 | UIButton *tempButton = button; 164 | [self.rightBtns removeObject:button]; 165 | [self.rightBtns addObject:tempButton]; 166 | [self.rightBtns enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 167 | obj.tag = idx; 168 | }]; 169 | } 170 | 171 | - (BOOL)shouldAutorotate { 172 | return NO; 173 | } 174 | 175 | @end 176 | -------------------------------------------------------------------------------- /SDDemo/Demo/Controller/SDItemLayoutViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // SDItemLayoutViewController.h 3 | // SDDemo 4 | // 5 | // Created by xuelin on 2019/2/27. 6 | // Copyright © 2019 xuelin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface SDItemLayoutViewController : UIViewController 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /SDDemo/Demo/Controller/SDItemLayoutViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // SDItemLayoutViewController.m 3 | // SDDemo 4 | // 5 | // Created by xuelin on 2019/2/27. 6 | // Copyright © 2019 xuelin. All rights reserved. 7 | // 8 | 9 | #import "SDItemLayoutViewController.h" 10 | 11 | 12 | static const NSInteger SortTitleItemColumn = 5; 13 | static const NSInteger SortTitleRowHeight = 48.0; 14 | 15 | @interface SDItemLayoutViewController () 16 | @property (nonatomic, strong) UIView *sortView; 17 | @property (nonatomic, strong) NSArray *sortTitleArray; 18 | @property (nonatomic, strong) NSArray *typeButtonArray; 19 | @end 20 | 21 | @implementation SDItemLayoutViewController 22 | 23 | - (void)viewDidLoad { 24 | [super viewDidLoad]; 25 | self.view.backgroundColor = [UIColor whiteColor]; 26 | 27 | [self.view addSubview:self.sortView]; 28 | [self.sortView mas_makeConstraints:^(MASConstraintMaker *make) { 29 | make.left.right.equalTo(self.view); 30 | make.center.equalTo(self.view); 31 | make.height.equalTo(@([self sortViewHeight])); 32 | }]; 33 | 34 | } 35 | 36 | - (void)typeButtonAction:(UIButton *)button { 37 | 38 | } 39 | 40 | - (UIView *)sortView { 41 | if (!_sortView) { 42 | UIView *sortView = [[UIView alloc] init]; 43 | sortView.backgroundColor = [UIColor lightGrayColor]; 44 | _sortView = sortView; 45 | 46 | CGFloat itemSpace = 11.4; 47 | CGFloat leftMargin = 15; 48 | CGFloat itemWidth = (kScreenWidth - leftMargin * 2 - itemSpace * 4) / SortTitleItemColumn; 49 | NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:3]; 50 | for (int i = 0; i < self.sortTitleArray.count; i ++) { 51 | UIButton *button = [[UIButton alloc] init]; 52 | button.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0]; 53 | 54 | [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 55 | [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted]; 56 | button.titleLabel.font = [UIFont systemFontOfSize:13]; 57 | button.layer.cornerRadius = 12.5; 58 | button.layer.masksToBounds = YES; 59 | button.tag = i; 60 | [button setTitle:self.sortTitleArray[i] forState:UIControlStateNormal]; 61 | if (i == 0) { 62 | button.selected = YES; 63 | } 64 | [button addTarget:self action:@selector(typeButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 65 | [sortView addSubview:button]; 66 | 67 | [button mas_makeConstraints:^(MASConstraintMaker *make) { 68 | make.height.equalTo(@25); 69 | make.width.equalTo(@(itemWidth)); 70 | make.top.equalTo(sortView).offset((int)(button.tag / 5.0) * (25 + 15) + 15); 71 | make.left.equalTo(sortView).offset(button.tag % 5 * (itemWidth + itemSpace) + leftMargin); 72 | }]; 73 | 74 | [buttonArray addObject:button]; 75 | } 76 | _typeButtonArray = [buttonArray copy]; 77 | } 78 | return _sortView; 79 | } 80 | 81 | - (CGFloat)sortViewHeight { 82 | CGFloat itemSpace = 11.4; 83 | CGFloat leftMargin = 15; 84 | CGFloat itemWidth = (kScreenWidth - leftMargin * 2 - itemSpace * 4) / SortTitleItemColumn; 85 | CGFloat oneRowH = SortTitleRowHeight; 86 | CGFloat itemsW = itemWidth * self.sortTitleArray.count + itemSpace * (self.sortTitleArray.count - 1) + leftMargin * 2; 87 | NSInteger row = (NSInteger)ceilf(itemsW / kScreenWidth); 88 | CGFloat sumH = oneRowH * row; 89 | return sumH; 90 | } 91 | 92 | - (NSArray *)sortTitleArray { 93 | if (!_sortTitleArray) { 94 | _sortTitleArray = @[@"全部", @"买入", @"增持", @"减持", @"卖出", @"预警", @"A股", @"H股", @"美股", @"币种", @"添加", @"其他"]; 95 | } 96 | return _sortTitleArray; 97 | } 98 | 99 | @end 100 | -------------------------------------------------------------------------------- /SDDemo/Demo/Model/FDFeedEntity.h: -------------------------------------------------------------------------------- 1 | // 2 | // FDFeedEntity.h 3 | // Demo 4 | // 5 | // Created by sunnyxx on 15/4/16. 6 | // Copyright (c) 2015年 forkingdog. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface FDFeedEntity : NSObject 13 | 14 | - (instancetype)initWithDictionary:(NSDictionary *)dictionary; 15 | 16 | @property (nonatomic, copy, readonly) NSString *identifier; 17 | @property (nonatomic, copy, readonly) NSString *title; 18 | @property (nonatomic, copy, readonly) NSString *content; 19 | @property (nonatomic, copy, readonly) NSString *username; 20 | @property (nonatomic, copy, readonly) NSString *time; 21 | @property (nonatomic, copy, readonly) NSString *imageName; 22 | @property (nonatomic, strong) NSNumber *height; 23 | @end 24 | -------------------------------------------------------------------------------- /SDDemo/Demo/Model/FDFeedEntity.m: -------------------------------------------------------------------------------- 1 | // 2 | // FDFeedEntity.m 3 | // Demo 4 | // 5 | // Created by sunnyxx on 15/4/16. 6 | // Copyright (c) 2015年 forkingdog. All rights reserved. 7 | // 8 | 9 | #import "FDFeedEntity.h" 10 | 11 | @implementation FDFeedEntity 12 | 13 | - (instancetype)initWithDictionary:(NSDictionary *)dictionary 14 | { 15 | self = super.init; 16 | if (self) { 17 | _identifier = [self uniqueIdentifier]; 18 | _title = dictionary[@"title"]; 19 | _content = dictionary[@"content"]; 20 | _username = dictionary[@"username"]; 21 | _time = dictionary[@"time"]; 22 | _imageName = dictionary[@"imageName"]; 23 | _height = dictionary[@"height"]; 24 | } 25 | return self; 26 | } 27 | 28 | - (NSString *)uniqueIdentifier 29 | { 30 | static NSInteger counter = 0; 31 | return [NSString stringWithFormat:@"unique-id-%@", @(counter++)]; 32 | } 33 | 34 | - (void)setValue:(id)value forUndefinedKey:(NSString *)key {}; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /SDDemo/Demo/View/FDFeedCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // FDFeedCell.h 3 | // Demo 4 | // 5 | // Created by sunnyxx on 15/4/17. 6 | // Copyright (c) 2015年 forkingdog. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "FDFeedEntity.h" 11 | 12 | @interface FDFeedCell : UITableViewCell 13 | 14 | @property (nonatomic, strong) FDFeedEntity *entity; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /SDDemo/Demo/View/FDFeedCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // FDFeedCell.m 3 | // Demo 4 | // 5 | // Created by sunnyxx on 15/4/17. 6 | // Copyright (c) 2015年 forkingdog. All rights reserved. 7 | // 8 | 9 | #import "FDFeedCell.h" 10 | 11 | @interface FDFeedCell () 12 | 13 | @property (nonatomic, weak) IBOutlet UILabel *titleLabel; 14 | @property (nonatomic, weak) IBOutlet UILabel *contentLabel; 15 | @property (nonatomic, weak) IBOutlet UIImageView *contentImageView; 16 | @property (nonatomic, weak) IBOutlet UILabel *usernameLabel; 17 | @property (nonatomic, weak) IBOutlet UILabel *timeLabel; 18 | @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstain; 19 | 20 | @end 21 | 22 | @implementation FDFeedCell 23 | 24 | - (void)awakeFromNib 25 | { 26 | [super awakeFromNib]; 27 | 28 | // Fix the bug in iOS7 - initial constraints warning 29 | self.contentView.bounds = [UIScreen mainScreen].bounds; 30 | } 31 | 32 | - (void)setEntity:(FDFeedEntity *)entity 33 | { 34 | _entity = entity; 35 | 36 | self.titleLabel.text = entity.title; 37 | self.contentLabel.text = entity.content; 38 | self.contentImageView.image = entity.imageName.length > 0 ? [UIImage imageNamed:entity.imageName] : nil; 39 | self.usernameLabel.text = entity.username; 40 | self.timeLabel.text = entity.time; 41 | // self.heightConstain.constant = self.entity.height.floatValue; 42 | } 43 | 44 | // If you are not using auto layout, override this method, enable it by setting 45 | // "fd_enforceFrameLayout" to YES. 46 | - (CGSize)sizeThatFits:(CGSize)size { 47 | CGFloat totalHeight = 0; 48 | totalHeight += [self.titleLabel sizeThatFits:size].height; 49 | totalHeight += [self.contentLabel sizeThatFits:size].height; 50 | totalHeight += [self.contentImageView sizeThatFits:size].height; 51 | totalHeight += [self.usernameLabel sizeThatFits:size].height; 52 | totalHeight += 40; // margins 53 | return CGSizeMake(size.width, totalHeight); 54 | } 55 | 56 | - (void)prepareForReuse { 57 | [super prepareForReuse]; 58 | self.backgroundColor = [UIColor whiteColor]; 59 | } 60 | 61 | @end 62 | -------------------------------------------------------------------------------- /SDDemo/Demo/View/FDFeedCell.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 27 | 33 | 39 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /SDDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | NSAppTransportSecurity 24 | 25 | NSAllowsArbitraryLoads 26 | 27 | 28 | UILaunchStoryboardName 29 | LaunchScreen 30 | UIRequiredDeviceCapabilities 31 | 32 | armv7 33 | 34 | UISupportedInterfaceOrientations 35 | 36 | UIInterfaceOrientationPortrait 37 | UIInterfaceOrientationLandscapeLeft 38 | UIInterfaceOrientationLandscapeRight 39 | 40 | UISupportedInterfaceOrientations~ipad 41 | 42 | UIInterfaceOrientationPortrait 43 | UIInterfaceOrientationPortraitUpsideDown 44 | UIInterfaceOrientationLandscapeLeft 45 | UIInterfaceOrientationLandscapeRight 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /SDDemo/Layout/UITableView+SDIndexPathHeightCache.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITableView+SDIndexPathHeightCache.h 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import 10 | @interface SDIndexPathHeightCache : NSObject 11 | 12 | // Enable automatically if you're using index path driven height cache 13 | @property (nonatomic, assign) BOOL automaticallyInvalidateEnabled; 14 | 15 | // Height cache 16 | - (BOOL)existsHeightAtIndexPath:(NSIndexPath *)indexPath; 17 | - (void)cacheHeight:(CGFloat)height byIndexPath:(NSIndexPath *)indexPath; 18 | - (CGFloat)heightForIndexPath:(NSIndexPath *)indexPath; 19 | - (void)invalidateHeightAtIndexPath:(NSIndexPath *)indexPath; 20 | - (void)invalidateAllHeightCache; 21 | 22 | @end 23 | 24 | @interface UITableView (SDIndexPathHeightCache) 25 | /// Height cache by index path. Generally, you don't need to use it directly. 26 | @property (nonatomic, strong, readonly) SDIndexPathHeightCache *sd_indexPathHeightCache; 27 | @end 28 | 29 | @interface UITableView (SDIndexPathHeightCacheInvalidation) 30 | /// Call this method when you want to reload data but don't want to invalidate 31 | /// all height cache by index path, for example, load more data at the bottom of 32 | /// table view. 33 | - (void)sd_reloadDataWithoutInvalidateIndexPathHeightCache; 34 | @end 35 | -------------------------------------------------------------------------------- /SDDemo/Layout/UITableView+SDIndexPathHeightCache.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITableView+SDIndexPathHeightCache.m 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import "UITableView+SDIndexPathHeightCache.h" 10 | #import 11 | 12 | typedef NSMutableArray *> SDIndexPathHeightsBySection; 13 | 14 | @interface SDIndexPathHeightCache () 15 | @property (nonatomic, strong) SDIndexPathHeightsBySection *heightsBySectionForPortrait; 16 | @property (nonatomic, strong) SDIndexPathHeightsBySection *heightsBySectionForLandscape; 17 | @end 18 | 19 | @implementation SDIndexPathHeightCache 20 | 21 | - (instancetype)init { 22 | self = [super init]; 23 | if (self) { 24 | _heightsBySectionForPortrait = [NSMutableArray array]; 25 | _heightsBySectionForLandscape = [NSMutableArray array]; 26 | } 27 | return self; 28 | } 29 | 30 | - (SDIndexPathHeightsBySection *)heightsBySectionForCurrentOrientation { 31 | return UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation) ? self.heightsBySectionForPortrait: self.heightsBySectionForLandscape; 32 | } 33 | 34 | - (void)enumerateAllOrientationsUsingBlock:(void (^)(SDIndexPathHeightsBySection *heightsBySection))block { 35 | block(self.heightsBySectionForPortrait); 36 | block(self.heightsBySectionForLandscape); 37 | } 38 | 39 | - (BOOL)existsHeightAtIndexPath:(NSIndexPath *)indexPath { 40 | [self buildCachesAtIndexPathsIfNeeded:@[indexPath]]; 41 | NSNumber *number = self.heightsBySectionForCurrentOrientation[indexPath.section][indexPath.row]; 42 | return ![number isEqualToNumber:@-1]; 43 | } 44 | 45 | - (void)cacheHeight:(CGFloat)height byIndexPath:(NSIndexPath *)indexPath { 46 | self.automaticallyInvalidateEnabled = YES; 47 | [self buildCachesAtIndexPathsIfNeeded:@[indexPath]]; 48 | self.heightsBySectionForCurrentOrientation[indexPath.section][indexPath.row] = @(height); 49 | } 50 | 51 | - (CGFloat)heightForIndexPath:(NSIndexPath *)indexPath { 52 | [self buildCachesAtIndexPathsIfNeeded:@[indexPath]]; 53 | NSNumber *number = self.heightsBySectionForCurrentOrientation[indexPath.section][indexPath.row]; 54 | #if CGFLOAT_IS_DOUBLE 55 | return number.doubleValue; 56 | #else 57 | return number.floatValue; 58 | #endif 59 | } 60 | 61 | - (void)invalidateHeightAtIndexPath:(NSIndexPath *)indexPath { 62 | [self buildCachesAtIndexPathsIfNeeded:@[indexPath]]; 63 | [self enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 64 | heightsBySection[indexPath.section][indexPath.row] = @-1; 65 | }]; 66 | } 67 | 68 | - (void)invalidateAllHeightCache { 69 | [self enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 70 | [heightsBySection removeAllObjects]; 71 | }]; 72 | } 73 | 74 | - (void)buildCachesAtIndexPathsIfNeeded:(NSArray *)indexPaths { 75 | // Build every section array or row array which is smaller than given index path. 76 | [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 77 | [self buildSectionsIfNeeded:indexPath.section]; 78 | [self buildRowsIfNeeded:indexPath.row inExistSection:indexPath.section]; 79 | }]; 80 | } 81 | 82 | - (void)buildSectionsIfNeeded:(NSInteger)targetSection { 83 | [self enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 84 | for (NSInteger section = 0; section <= targetSection; ++section) { 85 | if (section >= heightsBySection.count) { 86 | heightsBySection[section] = [NSMutableArray array]; 87 | } 88 | } 89 | }]; 90 | } 91 | 92 | - (void)buildRowsIfNeeded:(NSInteger)targetRow inExistSection:(NSInteger)section { 93 | [self enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 94 | NSMutableArray *heightsByRow = heightsBySection[section]; 95 | for (NSInteger row = 0; row <= targetRow; ++row) { 96 | if (row >= heightsByRow.count) { 97 | heightsByRow[row] = @-1; 98 | } 99 | } 100 | }]; 101 | } 102 | 103 | @end 104 | 105 | @implementation UITableView (SDIndexPathHeightCache) 106 | 107 | - (SDIndexPathHeightCache *)sd_indexPathHeightCache { 108 | SDIndexPathHeightCache *cache = objc_getAssociatedObject(self, _cmd); 109 | if (!cache) { 110 | cache = [SDIndexPathHeightCache new]; 111 | objc_setAssociatedObject(self, _cmd, cache, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 112 | } 113 | return cache; 114 | } 115 | 116 | @end 117 | 118 | // We just forward primary call, in crash report, top most method in stack maybe sd's, 119 | // but it's really not our bug, you should check whether your table view's data source and 120 | // displaying cells are not matched when reloading. 121 | static void __SD_TEMPLATE_LAYOUT_CELL_PRIMARY_CALL_IF_CRASH_NOT_OUR_BUG__(void (^callout)(void)) { 122 | callout(); 123 | } 124 | #define SDPrimaryCall(...) do {__SD_TEMPLATE_LAYOUT_CELL_PRIMARY_CALL_IF_CRASH_NOT_OUR_BUG__(^{__VA_ARGS__});} while(0) 125 | 126 | @implementation UITableView (sdIndexPathHeightCacheInvalidation) 127 | 128 | - (void)sd_reloadDataWithoutInvalidateIndexPathHeightCache { 129 | SDPrimaryCall([self sd_reloadData];); 130 | } 131 | 132 | + (void)load { 133 | // All methods that trigger height cache's invalidation 134 | SEL selectors[] = { 135 | @selector(reloadData), 136 | @selector(insertSections:withRowAnimation:), 137 | @selector(deleteSections:withRowAnimation:), 138 | @selector(reloadSections:withRowAnimation:), 139 | @selector(moveSection:toSection:), 140 | @selector(insertRowsAtIndexPaths:withRowAnimation:), 141 | @selector(deleteRowsAtIndexPaths:withRowAnimation:), 142 | @selector(reloadRowsAtIndexPaths:withRowAnimation:), 143 | @selector(moveRowAtIndexPath:toIndexPath:) 144 | }; 145 | 146 | for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) { 147 | SEL originalSelector = selectors[index]; 148 | SEL swizzledSelector = NSSelectorFromString([@"sd_" stringByAppendingString:NSStringFromSelector(originalSelector)]); 149 | Method originalMethod = class_getInstanceMethod(self, originalSelector); 150 | Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector); 151 | method_exchangeImplementations(originalMethod, swizzledMethod); 152 | } 153 | } 154 | 155 | - (void)sd_reloadData { 156 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 157 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 158 | [heightsBySection removeAllObjects]; 159 | }]; 160 | } 161 | SDPrimaryCall([self sd_reloadData];); 162 | } 163 | 164 | - (void)sd_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { 165 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 166 | [sections enumerateIndexesUsingBlock:^(NSUInteger section, BOOL *stop) { 167 | [self.sd_indexPathHeightCache buildSectionsIfNeeded:section]; 168 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 169 | [heightsBySection insertObject:[NSMutableArray array] atIndex:section]; 170 | }]; 171 | }]; 172 | } 173 | SDPrimaryCall([self sd_insertSections:sections withRowAnimation:animation];); 174 | } 175 | 176 | - (void)sd_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { 177 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 178 | [sections enumerateIndexesUsingBlock:^(NSUInteger section, BOOL *stop) { 179 | [self.sd_indexPathHeightCache buildSectionsIfNeeded:section]; 180 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 181 | [heightsBySection removeObjectAtIndex:section]; 182 | }]; 183 | }]; 184 | } 185 | SDPrimaryCall([self sd_deleteSections:sections withRowAnimation:animation];); 186 | } 187 | 188 | - (void)sd_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { 189 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 190 | [sections enumerateIndexesUsingBlock: ^(NSUInteger section, BOOL *stop) { 191 | [self.sd_indexPathHeightCache buildSectionsIfNeeded:section]; 192 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 193 | [heightsBySection[section] removeAllObjects]; 194 | }]; 195 | 196 | }]; 197 | } 198 | SDPrimaryCall([self sd_reloadSections:sections withRowAnimation:animation];); 199 | } 200 | 201 | - (void)sd_moveSection:(NSInteger)section toSection:(NSInteger)newSection { 202 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 203 | [self.sd_indexPathHeightCache buildSectionsIfNeeded:section]; 204 | [self.sd_indexPathHeightCache buildSectionsIfNeeded:newSection]; 205 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 206 | [heightsBySection exchangeObjectAtIndex:section withObjectAtIndex:newSection]; 207 | }]; 208 | } 209 | SDPrimaryCall([self sd_moveSection:section toSection:newSection];); 210 | } 211 | 212 | - (void)sd_insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { 213 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 214 | [self.sd_indexPathHeightCache buildCachesAtIndexPathsIfNeeded:indexPaths]; 215 | [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 216 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 217 | [heightsBySection[indexPath.section] insertObject:@-1 atIndex:indexPath.row]; 218 | }]; 219 | }]; 220 | } 221 | SDPrimaryCall([self sd_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];); 222 | } 223 | 224 | - (void)sd_deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { 225 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 226 | [self.sd_indexPathHeightCache buildCachesAtIndexPathsIfNeeded:indexPaths]; 227 | 228 | NSMutableDictionary *mutableIndexSetsToRemove = [NSMutableDictionary dictionary]; 229 | [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 230 | NSMutableIndexSet *mutableIndexSet = mutableIndexSetsToRemove[@(indexPath.section)]; 231 | if (!mutableIndexSet) { 232 | mutableIndexSet = [NSMutableIndexSet indexSet]; 233 | mutableIndexSetsToRemove[@(indexPath.section)] = mutableIndexSet; 234 | } 235 | [mutableIndexSet addIndex:indexPath.row]; 236 | }]; 237 | 238 | [mutableIndexSetsToRemove enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, NSIndexSet *indexSet, BOOL *stop) { 239 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 240 | [heightsBySection[key.integerValue] removeObjectsAtIndexes:indexSet]; 241 | }]; 242 | }]; 243 | } 244 | SDPrimaryCall([self sd_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];); 245 | } 246 | 247 | - (void)sd_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { 248 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 249 | [self.sd_indexPathHeightCache buildCachesAtIndexPathsIfNeeded:indexPaths]; 250 | [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 251 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 252 | heightsBySection[indexPath.section][indexPath.row] = @-1; 253 | }]; 254 | }]; 255 | } 256 | SDPrimaryCall([self sd_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];); 257 | } 258 | 259 | - (void)sd_moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { 260 | if (self.sd_indexPathHeightCache.automaticallyInvalidateEnabled) { 261 | [self.sd_indexPathHeightCache buildCachesAtIndexPathsIfNeeded:@[sourceIndexPath, destinationIndexPath]]; 262 | [self.sd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(SDIndexPathHeightsBySection *heightsBySection) { 263 | NSMutableArray *sourceRows = heightsBySection[sourceIndexPath.section]; 264 | NSMutableArray *destinationRows = heightsBySection[destinationIndexPath.section]; 265 | NSNumber *sourceValue = sourceRows[sourceIndexPath.row]; 266 | NSNumber *destinationValue = destinationRows[destinationIndexPath.row]; 267 | sourceRows[sourceIndexPath.row] = destinationValue; 268 | destinationRows[destinationIndexPath.row] = sourceValue; 269 | }]; 270 | } 271 | SDPrimaryCall([self sd_moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];); 272 | } 273 | 274 | @end 275 | -------------------------------------------------------------------------------- /SDDemo/Layout/UITableView+SDTemplateLayoutCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITableView+SDTemplateLayoutCell.h 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UITableView (SDTemplateLayoutCell) 12 | /// Access to internal template layout cell for given reuse identifier. 13 | /// Generally, you don't need to know these template layout cells. 14 | /// 15 | /// @param identifier Reuse identifier for cell which must be registered. 16 | /// 17 | - (__kindof UITableViewCell *)sd_templateCellForReuseIdentifier:(NSString *)identifier; 18 | 19 | /// Returns height of cell of type specifed by a reuse identifier and configured 20 | /// by the configuration block. 21 | /// 22 | /// The cell would be layed out on a fixed-width, vertically expanding basis with 23 | /// respect to its dynamic content, using auto layout. Thus, it is imperative that 24 | /// the cell was set up to be self-satisfied, i.e. its content always determines 25 | /// its height given the width is equal to the tableview's. 26 | /// 27 | /// @param identifier A string identifier for retrieving and maintaining template 28 | /// cells with system's "-dequeueReusableCellWithIdentifier:" call. 29 | /// @param configuration An optional block for configuring and providing content 30 | /// to the template cell. The configuration should be minimal for scrolling 31 | /// performance yet sufficient for calculating cell's height. 32 | /// 33 | - (CGFloat)sd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id cell))configuration; 34 | 35 | /// This method does what "-fd_heightForCellWithIdentifier:configuration" does, and 36 | /// calculated height will be cached by its index path, returns a cached height 37 | /// when needed. Therefore lots of extra height calculations could be saved. 38 | /// 39 | /// No need to worry about invalidating cached heights when data source changes, it 40 | /// will be done automatically when you call "-reloadData" or any method that triggers 41 | /// UITableView's reloading. 42 | /// 43 | /// @param indexPath where this cell's height cache belongs. 44 | /// 45 | - (CGFloat)sd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(id cell))configuration; 46 | 47 | @end 48 | 49 | 50 | @interface UITableView (SDTemplateLayoutHeaderFooterView) 51 | 52 | /// Returns header or footer view's height that registered in table view with reuse identifier. 53 | /// 54 | /// Use it after calling "-[UITableView registerNib/Class:forHeaderFooterViewReuseIdentifier]", 55 | /// same with "-fd_heightForCellWithIdentifier:configuration:", it will call "-sizeThatFits:" for 56 | /// subclass of UITableViewHeaderFooterView which is not using Auto Layout. 57 | /// 58 | - (CGFloat)sd_heightForHeaderFooterViewWithIdentifier:(NSString *)identifier configuration:(void (^)(id headerFooterView))configuration; 59 | 60 | @end 61 | 62 | @interface UITableViewCell (SDTemplateLayoutCell) 63 | 64 | /// Indicate this is a template layout cell for calculation only. 65 | /// You may need this when there are non-UI side effects when configure a cell. 66 | /// Like: 67 | /// - (void)configureCell:(FooCell *)cell atIndexPath:(NSIndexPath *)indexPath { 68 | /// cell.entity = [self entityAtIndexPath:indexPath]; 69 | /// if (!cell.fd_isTemplateLayoutCell) { 70 | /// [self notifySomething]; // non-UI side effects 71 | /// } 72 | /// } 73 | /// 74 | @property (nonatomic, assign) BOOL sd_isTemplateLayoutCell; 75 | 76 | /// Enable to enforce this template layout cell to use "frame layout" rather than "auto layout", 77 | /// and will ask cell's height by calling "-sizeThatFits:", so you must override this method. 78 | /// Use this property only when you want to manually control this template layout cell's height 79 | /// calculation mode, default to NO. 80 | /// 81 | @property (nonatomic, assign) BOOL sd_enforceFrameLayout; 82 | 83 | @end 84 | 85 | -------------------------------------------------------------------------------- /SDDemo/Layout/UITableView+SDTemplateLayoutCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITableView+SDTemplateLayoutCell.m 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import "UITableView+SDTemplateLayoutCell.h" 10 | #import "UITableView+SDIndexPathHeightCache.h" 11 | #import 12 | 13 | @implementation UITableView (SDTemplateLayoutCell) 14 | 15 | - (CGFloat)sd_systemFittingHeightForConfiguratedCell:(UITableViewCell *)cell { 16 | CGFloat contentViewWidth = CGRectGetWidth(self.frame); 17 | 18 | // If a cell has accessory view or system accessory type, its content view's width is smaller 19 | // than cell's by some fixed values. 20 | if (cell.accessoryView) { 21 | contentViewWidth -= 16 + CGRectGetWidth(cell.accessoryView.frame); 22 | } else { 23 | static const CGFloat systemAccessoryWidths[] = { 24 | [UITableViewCellAccessoryNone] = 0, 25 | [UITableViewCellAccessoryDisclosureIndicator] = 34, 26 | [UITableViewCellAccessoryDetailDisclosureButton] = 68, 27 | [UITableViewCellAccessoryCheckmark] = 40, 28 | [UITableViewCellAccessoryDetailButton] = 48 29 | }; 30 | contentViewWidth -= systemAccessoryWidths[cell.accessoryType]; 31 | } 32 | 33 | // If not using auto layout, you have to override "-sizeThatFits:" to provide a fitting size by yourself. 34 | // This is the same height calculation passes used in iOS8 self-sizing cell's implementation. 35 | // 36 | // 1. Try "- systemLayoutSizeFittingSize:" first. (skip this step if 'sd_enforceFrameLayout' set to YES.) 37 | // 2. Warning once if step 1 still returns 0 when using AutoLayout 38 | // 3. Try "- sizeThatFits:" if step 1 returns 0 39 | // 4. Use a valid height or default row height (44) if not exist one 40 | 41 | CGFloat fittingHeight = 0; 42 | 43 | if (!cell.sd_enforceFrameLayout && contentViewWidth > 0) { 44 | // Add a hard width constraint to make dynamic content views (like labels) expand vertically instead 45 | // of growing horizontally, in a flow-layout manner. 46 | NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:contentViewWidth]; 47 | 48 | // [bug fix] after iOS 10.3, Auto Layout engine will add an additional 0 width constraint onto cell's content view, to avoid that, we add constraints to content view's left, right, top and bottom. 49 | static BOOL isSystemVersionEqualOrGreaterThen10_3 = NO; 50 | static dispatch_once_t onceToken; 51 | dispatch_once(&onceToken, ^{ 52 | isSystemVersionEqualOrGreaterThen10_3 = UIDevice.currentDevice.systemVersion.floatValue >= 10.3; 53 | }); 54 | 55 | NSArray *edgeConstraints; 56 | if (isSystemVersionEqualOrGreaterThen10_3) { 57 | // To avoid confilicts, make width constraint softer than required (1000) 58 | widthFenceConstraint.priority = UILayoutPriorityRequired - 1; 59 | 60 | // Build edge constraints 61 | NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]; 62 | NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]; 63 | NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]; 64 | NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]; 65 | edgeConstraints = @[leftConstraint, rightConstraint, topConstraint, bottomConstraint]; 66 | [cell addConstraints:edgeConstraints]; 67 | } 68 | 69 | [cell.contentView addConstraint:widthFenceConstraint]; 70 | 71 | // Auto layout engine does its math 72 | fittingHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 73 | 74 | // Clean-ups 75 | [cell.contentView removeConstraint:widthFenceConstraint]; 76 | if (isSystemVersionEqualOrGreaterThen10_3) { 77 | [cell removeConstraints:edgeConstraints]; 78 | } 79 | } 80 | 81 | if (fittingHeight == 0) { 82 | #if DEBUG 83 | // Warn if using AutoLayout but get zero height. 84 | if (cell.contentView.constraints.count > 0) { 85 | if (!objc_getAssociatedObject(self, _cmd)) { 86 | NSLog(@"[SDTemplateLayoutCell] Warning once only: Cannot get a proper cell height (now 0) from '- systemFittingSize:'(AutoLayout). You should check how constraints are built in cell, making it into 'self-sizing' cell."); 87 | objc_setAssociatedObject(self, _cmd, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 88 | } 89 | } 90 | #endif 91 | // Try '- sizeThatFits:' for frame layout. 92 | // Note: fitting height should not include separator view. 93 | fittingHeight = [cell sizeThatFits:CGSizeMake(contentViewWidth, 0)].height; 94 | } 95 | 96 | // Still zero height after all above. 97 | if (fittingHeight == 0) { 98 | // Use default row height. 99 | fittingHeight = 44; 100 | } 101 | 102 | // Add 1px extra space for separator line if needed, simulating default UITableViewCell. 103 | if (self.separatorStyle != UITableViewCellSeparatorStyleNone) { 104 | fittingHeight += 1.0 / [UIScreen mainScreen].scale; 105 | } 106 | 107 | return fittingHeight; 108 | } 109 | 110 | - (__kindof UITableViewCell *)sd_templateCellForReuseIdentifier:(NSString *)identifier { 111 | NSAssert(identifier.length > 0, @"Expect a valid identifier - %@", identifier); 112 | 113 | NSMutableDictionary *templateCellsByIdentifiers = objc_getAssociatedObject(self, _cmd); 114 | if (!templateCellsByIdentifiers) { 115 | templateCellsByIdentifiers = @{}.mutableCopy; 116 | objc_setAssociatedObject(self, _cmd, templateCellsByIdentifiers, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 117 | } 118 | 119 | UITableViewCell *templateCell = templateCellsByIdentifiers[identifier]; 120 | 121 | if (!templateCell) { 122 | templateCell = [self dequeueReusableCellWithIdentifier:identifier]; 123 | NSAssert(templateCell != nil, @"Cell must be registered to table view for identifier - %@", identifier); 124 | templateCell.sd_isTemplateLayoutCell = YES; 125 | templateCell.contentView.translatesAutoresizingMaskIntoConstraints = NO; 126 | templateCellsByIdentifiers[identifier] = templateCell; 127 | } 128 | 129 | return templateCell; 130 | } 131 | 132 | - (CGFloat)sd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id cell))configuration { 133 | if (!identifier) { 134 | return 0; 135 | } 136 | 137 | UITableViewCell *templateLayoutCell = [self sd_templateCellForReuseIdentifier:identifier]; 138 | 139 | // Manually calls to ensure consistent behavior with actual cells. (that are displayed on screen) 140 | [templateLayoutCell prepareForReuse]; 141 | 142 | // Customize and provide content for our template cell. 143 | if (configuration) { 144 | configuration(templateLayoutCell); 145 | } 146 | 147 | return [self sd_systemFittingHeightForConfiguratedCell:templateLayoutCell]; 148 | } 149 | 150 | - (CGFloat)sd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(id cell))configuration { 151 | if (!identifier || !indexPath) { 152 | return 0; 153 | } 154 | 155 | // Hit cache 156 | if ([self.sd_indexPathHeightCache existsHeightAtIndexPath:indexPath]) { 157 | return [self.sd_indexPathHeightCache heightForIndexPath:indexPath]; 158 | } 159 | 160 | CGFloat height = [self sd_heightForCellWithIdentifier:identifier configuration:configuration]; 161 | [self.sd_indexPathHeightCache cacheHeight:height byIndexPath:indexPath]; 162 | 163 | return height; 164 | } 165 | 166 | @end 167 | 168 | @implementation UITableView (SDTemplateLayoutHeaderFooterView) 169 | 170 | - (__kindof UITableViewHeaderFooterView *)sd_templateHeaderFooterViewForReuseIdentifier:(NSString *)identifier { 171 | NSAssert(identifier.length > 0, @"Expect a valid identifier - %@", identifier); 172 | 173 | NSMutableDictionary *templateHeaderFooterViews = objc_getAssociatedObject(self, _cmd); 174 | if (!templateHeaderFooterViews) { 175 | templateHeaderFooterViews = @{}.mutableCopy; 176 | objc_setAssociatedObject(self, _cmd, templateHeaderFooterViews, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 177 | } 178 | 179 | UITableViewHeaderFooterView *templateHeaderFooterView = templateHeaderFooterViews[identifier]; 180 | 181 | if (!templateHeaderFooterView) { 182 | templateHeaderFooterView = [self dequeueReusableHeaderFooterViewWithIdentifier:identifier]; 183 | NSAssert(templateHeaderFooterView != nil, @"HeaderFooterView must be registered to table view for identifier - %@", identifier); 184 | templateHeaderFooterView.contentView.translatesAutoresizingMaskIntoConstraints = NO; 185 | templateHeaderFooterViews[identifier] = templateHeaderFooterView; 186 | } 187 | 188 | return templateHeaderFooterView; 189 | } 190 | 191 | - (CGFloat)sd_heightForHeaderFooterViewWithIdentifier:(NSString *)identifier configuration:(void (^)(id))configuration { 192 | UITableViewHeaderFooterView *templateHeaderFooterView = [self sd_templateHeaderFooterViewForReuseIdentifier:identifier]; 193 | 194 | NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:templateHeaderFooterView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:CGRectGetWidth(self.frame)]; 195 | [templateHeaderFooterView addConstraint:widthFenceConstraint]; 196 | CGFloat fittingHeight = [templateHeaderFooterView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 197 | [templateHeaderFooterView removeConstraint:widthFenceConstraint]; 198 | 199 | if (fittingHeight == 0) { 200 | fittingHeight = [templateHeaderFooterView sizeThatFits:CGSizeMake(CGRectGetWidth(self.frame), 0)].height; 201 | } 202 | 203 | return fittingHeight; 204 | } 205 | 206 | @end 207 | 208 | @implementation UITableViewCell (sdTemplateLayoutCell) 209 | 210 | - (BOOL)sd_isTemplateLayoutCell { 211 | return [objc_getAssociatedObject(self, _cmd) boolValue]; 212 | } 213 | 214 | - (void)setSd_isTemplateLayoutCell:(BOOL)isTemplateLayoutCell { 215 | objc_setAssociatedObject(self, @selector(sd_isTemplateLayoutCell), @(isTemplateLayoutCell), OBJC_ASSOCIATION_RETAIN); 216 | } 217 | 218 | - (BOOL)sd_enforceFrameLayout { 219 | return [objc_getAssociatedObject(self, _cmd) boolValue]; 220 | } 221 | 222 | - (void)setSd_enforceFrameLayout:(BOOL)enforceFrameLayout { 223 | objc_setAssociatedObject(self, @selector(sd_enforceFrameLayout), @(enforceFrameLayout), OBJC_ASSOCIATION_RETAIN); 224 | } 225 | 226 | @end 227 | 228 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASCompositeConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASCompositeConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | #import "MASUtilities.h" 11 | 12 | /** 13 | * A group of MASConstraint objects 14 | */ 15 | @interface MASCompositeConstraint : MASConstraint 16 | 17 | /** 18 | * Creates a composite with a predefined array of children 19 | * 20 | * @param children child MASConstraints 21 | * 22 | * @return a composite constraint 23 | */ 24 | - (id)initWithChildren:(NSArray *)children; 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASCompositeConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASCompositeConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASCompositeConstraint.h" 10 | #import "MASConstraint+Private.h" 11 | 12 | @interface MASCompositeConstraint () 13 | 14 | @property (nonatomic, strong) id mas_key; 15 | @property (nonatomic, strong) NSMutableArray *childConstraints; 16 | 17 | @end 18 | 19 | @implementation MASCompositeConstraint 20 | 21 | - (id)initWithChildren:(NSArray *)children { 22 | self = [super init]; 23 | if (!self) return nil; 24 | 25 | _childConstraints = [children mutableCopy]; 26 | for (MASConstraint *constraint in _childConstraints) { 27 | constraint.delegate = self; 28 | } 29 | 30 | return self; 31 | } 32 | 33 | #pragma mark - MASConstraintDelegate 34 | 35 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint { 36 | NSUInteger index = [self.childConstraints indexOfObject:constraint]; 37 | NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint); 38 | [self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint]; 39 | } 40 | 41 | - (MASConstraint *)constraint:(MASConstraint __unused *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 42 | id strongDelegate = self.delegate; 43 | MASConstraint *newConstraint = [strongDelegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 44 | newConstraint.delegate = self; 45 | [self.childConstraints addObject:newConstraint]; 46 | return newConstraint; 47 | } 48 | 49 | #pragma mark - NSLayoutConstraint multiplier proxies 50 | 51 | - (MASConstraint * (^)(CGFloat))multipliedBy { 52 | return ^id(CGFloat multiplier) { 53 | for (MASConstraint *constraint in self.childConstraints) { 54 | constraint.multipliedBy(multiplier); 55 | } 56 | return self; 57 | }; 58 | } 59 | 60 | - (MASConstraint * (^)(CGFloat))dividedBy { 61 | return ^id(CGFloat divider) { 62 | for (MASConstraint *constraint in self.childConstraints) { 63 | constraint.dividedBy(divider); 64 | } 65 | return self; 66 | }; 67 | } 68 | 69 | #pragma mark - MASLayoutPriority proxy 70 | 71 | - (MASConstraint * (^)(MASLayoutPriority))priority { 72 | return ^id(MASLayoutPriority priority) { 73 | for (MASConstraint *constraint in self.childConstraints) { 74 | constraint.priority(priority); 75 | } 76 | return self; 77 | }; 78 | } 79 | 80 | #pragma mark - NSLayoutRelation proxy 81 | 82 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { 83 | return ^id(id attr, NSLayoutRelation relation) { 84 | for (MASConstraint *constraint in self.childConstraints.copy) { 85 | constraint.equalToWithRelation(attr, relation); 86 | } 87 | return self; 88 | }; 89 | } 90 | 91 | #pragma mark - attribute chaining 92 | 93 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 94 | [self constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 95 | return self; 96 | } 97 | 98 | #pragma mark - Animator proxy 99 | 100 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 101 | 102 | - (MASConstraint *)animator { 103 | for (MASConstraint *constraint in self.childConstraints) { 104 | [constraint animator]; 105 | } 106 | return self; 107 | } 108 | 109 | #endif 110 | 111 | #pragma mark - debug helpers 112 | 113 | - (MASConstraint * (^)(id))key { 114 | return ^id(id key) { 115 | self.mas_key = key; 116 | int i = 0; 117 | for (MASConstraint *constraint in self.childConstraints) { 118 | constraint.key([NSString stringWithFormat:@"%@[%d]", key, i++]); 119 | } 120 | return self; 121 | }; 122 | } 123 | 124 | #pragma mark - NSLayoutConstraint constant setters 125 | 126 | - (void)setInsets:(MASEdgeInsets)insets { 127 | for (MASConstraint *constraint in self.childConstraints) { 128 | constraint.insets = insets; 129 | } 130 | } 131 | 132 | - (void)setInset:(CGFloat)inset { 133 | for (MASConstraint *constraint in self.childConstraints) { 134 | constraint.inset = inset; 135 | } 136 | } 137 | 138 | - (void)setOffset:(CGFloat)offset { 139 | for (MASConstraint *constraint in self.childConstraints) { 140 | constraint.offset = offset; 141 | } 142 | } 143 | 144 | - (void)setSizeOffset:(CGSize)sizeOffset { 145 | for (MASConstraint *constraint in self.childConstraints) { 146 | constraint.sizeOffset = sizeOffset; 147 | } 148 | } 149 | 150 | - (void)setCenterOffset:(CGPoint)centerOffset { 151 | for (MASConstraint *constraint in self.childConstraints) { 152 | constraint.centerOffset = centerOffset; 153 | } 154 | } 155 | 156 | #pragma mark - MASConstraint 157 | 158 | - (void)activate { 159 | for (MASConstraint *constraint in self.childConstraints) { 160 | [constraint activate]; 161 | } 162 | } 163 | 164 | - (void)deactivate { 165 | for (MASConstraint *constraint in self.childConstraints) { 166 | [constraint deactivate]; 167 | } 168 | } 169 | 170 | - (void)install { 171 | for (MASConstraint *constraint in self.childConstraints) { 172 | constraint.updateExisting = self.updateExisting; 173 | [constraint install]; 174 | } 175 | } 176 | 177 | - (void)uninstall { 178 | for (MASConstraint *constraint in self.childConstraints) { 179 | [constraint uninstall]; 180 | } 181 | } 182 | 183 | @end 184 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASConstraint+Private.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint+Private.h 3 | // Masonry 4 | // 5 | // Created by Nick Tymchenko on 29/04/14. 6 | // Copyright (c) 2014 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | 11 | @protocol MASConstraintDelegate; 12 | 13 | 14 | @interface MASConstraint () 15 | 16 | /** 17 | * Whether or not to check for an existing constraint instead of adding constraint 18 | */ 19 | @property (nonatomic, assign) BOOL updateExisting; 20 | 21 | /** 22 | * Usually MASConstraintMaker but could be a parent MASConstraint 23 | */ 24 | @property (nonatomic, weak) id delegate; 25 | 26 | /** 27 | * Based on a provided value type, is equal to calling: 28 | * NSNumber - setOffset: 29 | * NSValue with CGPoint - setPointOffset: 30 | * NSValue with CGSize - setSizeOffset: 31 | * NSValue with MASEdgeInsets - setInsets: 32 | */ 33 | - (void)setLayoutConstantWithValue:(NSValue *)value; 34 | 35 | @end 36 | 37 | 38 | @interface MASConstraint (Abstract) 39 | 40 | /** 41 | * Sets the constraint relation to given NSLayoutRelation 42 | * returns a block which accepts one of the following: 43 | * MASViewAttribute, UIView, NSValue, NSArray 44 | * see readme for more details. 45 | */ 46 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation; 47 | 48 | /** 49 | * Override to set a custom chaining behaviour 50 | */ 51 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute; 52 | 53 | @end 54 | 55 | 56 | @protocol MASConstraintDelegate 57 | 58 | /** 59 | * Notifies the delegate when the constraint needs to be replaced with another constraint. For example 60 | * A MASViewConstraint may turn into a MASCompositeConstraint when an array is passed to one of the equality blocks 61 | */ 62 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint; 63 | 64 | - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute; 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * Enables Constraints to be created with chainable syntax 13 | * Constraint can represent single NSLayoutConstraint (MASViewConstraint) 14 | * or a group of NSLayoutConstraints (MASComposisteConstraint) 15 | */ 16 | @interface MASConstraint : NSObject 17 | 18 | // Chaining Support 19 | 20 | /** 21 | * Modifies the NSLayoutConstraint constant, 22 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 23 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 24 | */ 25 | - (MASConstraint * (^)(MASEdgeInsets insets))insets; 26 | 27 | /** 28 | * Modifies the NSLayoutConstraint constant, 29 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 30 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 31 | */ 32 | - (MASConstraint * (^)(CGFloat inset))inset; 33 | 34 | /** 35 | * Modifies the NSLayoutConstraint constant, 36 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 37 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 38 | */ 39 | - (MASConstraint * (^)(CGSize offset))sizeOffset; 40 | 41 | /** 42 | * Modifies the NSLayoutConstraint constant, 43 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 44 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 45 | */ 46 | - (MASConstraint * (^)(CGPoint offset))centerOffset; 47 | 48 | /** 49 | * Modifies the NSLayoutConstraint constant 50 | */ 51 | - (MASConstraint * (^)(CGFloat offset))offset; 52 | 53 | /** 54 | * Modifies the NSLayoutConstraint constant based on a value type 55 | */ 56 | - (MASConstraint * (^)(NSValue *value))valueOffset; 57 | 58 | /** 59 | * Sets the NSLayoutConstraint multiplier property 60 | */ 61 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy; 62 | 63 | /** 64 | * Sets the NSLayoutConstraint multiplier to 1.0/dividedBy 65 | */ 66 | - (MASConstraint * (^)(CGFloat divider))dividedBy; 67 | 68 | /** 69 | * Sets the NSLayoutConstraint priority to a float or MASLayoutPriority 70 | */ 71 | - (MASConstraint * (^)(MASLayoutPriority priority))priority; 72 | 73 | /** 74 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityLow 75 | */ 76 | - (MASConstraint * (^)(void))priorityLow; 77 | 78 | /** 79 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium 80 | */ 81 | - (MASConstraint * (^)(void))priorityMedium; 82 | 83 | /** 84 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh 85 | */ 86 | - (MASConstraint * (^)(void))priorityHigh; 87 | 88 | /** 89 | * Sets the constraint relation to NSLayoutRelationEqual 90 | * returns a block which accepts one of the following: 91 | * MASViewAttribute, UIView, NSValue, NSArray 92 | * see readme for more details. 93 | */ 94 | - (MASConstraint * (^)(id attr))equalTo; 95 | 96 | /** 97 | * Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual 98 | * returns a block which accepts one of the following: 99 | * MASViewAttribute, UIView, NSValue, NSArray 100 | * see readme for more details. 101 | */ 102 | - (MASConstraint * (^)(id attr))greaterThanOrEqualTo; 103 | 104 | /** 105 | * Sets the constraint relation to NSLayoutRelationLessThanOrEqual 106 | * returns a block which accepts one of the following: 107 | * MASViewAttribute, UIView, NSValue, NSArray 108 | * see readme for more details. 109 | */ 110 | - (MASConstraint * (^)(id attr))lessThanOrEqualTo; 111 | 112 | /** 113 | * Optional semantic property which has no effect but improves the readability of constraint 114 | */ 115 | - (MASConstraint *)with; 116 | 117 | /** 118 | * Optional semantic property which has no effect but improves the readability of constraint 119 | */ 120 | - (MASConstraint *)and; 121 | 122 | /** 123 | * Creates a new MASCompositeConstraint with the called attribute and reciever 124 | */ 125 | - (MASConstraint *)left; 126 | - (MASConstraint *)top; 127 | - (MASConstraint *)right; 128 | - (MASConstraint *)bottom; 129 | - (MASConstraint *)leading; 130 | - (MASConstraint *)trailing; 131 | - (MASConstraint *)width; 132 | - (MASConstraint *)height; 133 | - (MASConstraint *)centerX; 134 | - (MASConstraint *)centerY; 135 | - (MASConstraint *)baseline; 136 | 137 | - (MASConstraint *)firstBaseline; 138 | - (MASConstraint *)lastBaseline; 139 | 140 | #if TARGET_OS_IPHONE || TARGET_OS_TV 141 | 142 | - (MASConstraint *)leftMargin; 143 | - (MASConstraint *)rightMargin; 144 | - (MASConstraint *)topMargin; 145 | - (MASConstraint *)bottomMargin; 146 | - (MASConstraint *)leadingMargin; 147 | - (MASConstraint *)trailingMargin; 148 | - (MASConstraint *)centerXWithinMargins; 149 | - (MASConstraint *)centerYWithinMargins; 150 | 151 | #endif 152 | 153 | 154 | /** 155 | * Sets the constraint debug name 156 | */ 157 | - (MASConstraint * (^)(id key))key; 158 | 159 | // NSLayoutConstraint constant Setters 160 | // for use outside of mas_updateConstraints/mas_makeConstraints blocks 161 | 162 | /** 163 | * Modifies the NSLayoutConstraint constant, 164 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 165 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 166 | */ 167 | - (void)setInsets:(MASEdgeInsets)insets; 168 | 169 | /** 170 | * Modifies the NSLayoutConstraint constant, 171 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 172 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 173 | */ 174 | - (void)setInset:(CGFloat)inset; 175 | 176 | /** 177 | * Modifies the NSLayoutConstraint constant, 178 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 179 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 180 | */ 181 | - (void)setSizeOffset:(CGSize)sizeOffset; 182 | 183 | /** 184 | * Modifies the NSLayoutConstraint constant, 185 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 186 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 187 | */ 188 | - (void)setCenterOffset:(CGPoint)centerOffset; 189 | 190 | /** 191 | * Modifies the NSLayoutConstraint constant 192 | */ 193 | - (void)setOffset:(CGFloat)offset; 194 | 195 | 196 | // NSLayoutConstraint Installation support 197 | 198 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 199 | /** 200 | * Whether or not to go through the animator proxy when modifying the constraint 201 | */ 202 | @property (nonatomic, copy, readonly) MASConstraint *animator; 203 | #endif 204 | 205 | /** 206 | * Activates an NSLayoutConstraint if it's supported by an OS. 207 | * Invokes install otherwise. 208 | */ 209 | - (void)activate; 210 | 211 | /** 212 | * Deactivates previously installed/activated NSLayoutConstraint. 213 | */ 214 | - (void)deactivate; 215 | 216 | /** 217 | * Creates a NSLayoutConstraint and adds it to the appropriate view. 218 | */ 219 | - (void)install; 220 | 221 | /** 222 | * Removes previously installed NSLayoutConstraint 223 | */ 224 | - (void)uninstall; 225 | 226 | @end 227 | 228 | 229 | /** 230 | * Convenience auto-boxing macros for MASConstraint methods. 231 | * 232 | * Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax. 233 | * A potential drawback of this is that the unprefixed macros will appear in global scope. 234 | */ 235 | #define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__))) 236 | #define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 237 | #define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 238 | 239 | #define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__))) 240 | 241 | 242 | #ifdef MAS_SHORTHAND_GLOBALS 243 | 244 | #define equalTo(...) mas_equalTo(__VA_ARGS__) 245 | #define greaterThanOrEqualTo(...) mas_greaterThanOrEqualTo(__VA_ARGS__) 246 | #define lessThanOrEqualTo(...) mas_lessThanOrEqualTo(__VA_ARGS__) 247 | 248 | #define offset(...) mas_offset(__VA_ARGS__) 249 | 250 | #endif 251 | 252 | 253 | @interface MASConstraint (AutoboxingSupport) 254 | 255 | /** 256 | * Aliases to corresponding relation methods (for shorthand macros) 257 | * Also needed to aid autocompletion 258 | */ 259 | - (MASConstraint * (^)(id attr))mas_equalTo; 260 | - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo; 261 | - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo; 262 | 263 | /** 264 | * A dummy method to aid autocompletion 265 | */ 266 | - (MASConstraint * (^)(id offset))mas_offset; 267 | 268 | @end 269 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.m 3 | // Masonry 4 | // 5 | // Created by Nick Tymchenko on 1/20/14. 6 | // 7 | 8 | #import "MASConstraint.h" 9 | #import "MASConstraint+Private.h" 10 | 11 | #define MASMethodNotImplemented() \ 12 | @throw [NSException exceptionWithName:NSInternalInconsistencyException \ 13 | reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)] \ 14 | userInfo:nil] 15 | 16 | @implementation MASConstraint 17 | 18 | #pragma mark - Init 19 | 20 | - (id)init { 21 | NSAssert(![self isMemberOfClass:[MASConstraint class]], @"MASConstraint is an abstract class, you should not instantiate it directly."); 22 | return [super init]; 23 | } 24 | 25 | #pragma mark - NSLayoutRelation proxies 26 | 27 | - (MASConstraint * (^)(id))equalTo { 28 | return ^id(id attribute) { 29 | return self.equalToWithRelation(attribute, NSLayoutRelationEqual); 30 | }; 31 | } 32 | 33 | - (MASConstraint * (^)(id))mas_equalTo { 34 | return ^id(id attribute) { 35 | return self.equalToWithRelation(attribute, NSLayoutRelationEqual); 36 | }; 37 | } 38 | 39 | - (MASConstraint * (^)(id))greaterThanOrEqualTo { 40 | return ^id(id attribute) { 41 | return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual); 42 | }; 43 | } 44 | 45 | - (MASConstraint * (^)(id))mas_greaterThanOrEqualTo { 46 | return ^id(id attribute) { 47 | return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual); 48 | }; 49 | } 50 | 51 | - (MASConstraint * (^)(id))lessThanOrEqualTo { 52 | return ^id(id attribute) { 53 | return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual); 54 | }; 55 | } 56 | 57 | - (MASConstraint * (^)(id))mas_lessThanOrEqualTo { 58 | return ^id(id attribute) { 59 | return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual); 60 | }; 61 | } 62 | 63 | #pragma mark - MASLayoutPriority proxies 64 | 65 | - (MASConstraint * (^)(void))priorityLow { 66 | return ^id{ 67 | self.priority(MASLayoutPriorityDefaultLow); 68 | return self; 69 | }; 70 | } 71 | 72 | - (MASConstraint * (^)(void))priorityMedium { 73 | return ^id{ 74 | self.priority(MASLayoutPriorityDefaultMedium); 75 | return self; 76 | }; 77 | } 78 | 79 | - (MASConstraint * (^)(void))priorityHigh { 80 | return ^id{ 81 | self.priority(MASLayoutPriorityDefaultHigh); 82 | return self; 83 | }; 84 | } 85 | 86 | #pragma mark - NSLayoutConstraint constant proxies 87 | 88 | - (MASConstraint * (^)(MASEdgeInsets))insets { 89 | return ^id(MASEdgeInsets insets){ 90 | self.insets = insets; 91 | return self; 92 | }; 93 | } 94 | 95 | - (MASConstraint * (^)(CGFloat))inset { 96 | return ^id(CGFloat inset){ 97 | self.inset = inset; 98 | return self; 99 | }; 100 | } 101 | 102 | - (MASConstraint * (^)(CGSize))sizeOffset { 103 | return ^id(CGSize offset) { 104 | self.sizeOffset = offset; 105 | return self; 106 | }; 107 | } 108 | 109 | - (MASConstraint * (^)(CGPoint))centerOffset { 110 | return ^id(CGPoint offset) { 111 | self.centerOffset = offset; 112 | return self; 113 | }; 114 | } 115 | 116 | - (MASConstraint * (^)(CGFloat))offset { 117 | return ^id(CGFloat offset){ 118 | self.offset = offset; 119 | return self; 120 | }; 121 | } 122 | 123 | - (MASConstraint * (^)(NSValue *value))valueOffset { 124 | return ^id(NSValue *offset) { 125 | NSAssert([offset isKindOfClass:NSValue.class], @"expected an NSValue offset, got: %@", offset); 126 | [self setLayoutConstantWithValue:offset]; 127 | return self; 128 | }; 129 | } 130 | 131 | - (MASConstraint * (^)(id offset))mas_offset { 132 | // Will never be called due to macro 133 | return nil; 134 | } 135 | 136 | #pragma mark - NSLayoutConstraint constant setter 137 | 138 | - (void)setLayoutConstantWithValue:(NSValue *)value { 139 | if ([value isKindOfClass:NSNumber.class]) { 140 | self.offset = [(NSNumber *)value doubleValue]; 141 | } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) { 142 | CGPoint point; 143 | [value getValue:&point]; 144 | self.centerOffset = point; 145 | } else if (strcmp(value.objCType, @encode(CGSize)) == 0) { 146 | CGSize size; 147 | [value getValue:&size]; 148 | self.sizeOffset = size; 149 | } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) { 150 | MASEdgeInsets insets; 151 | [value getValue:&insets]; 152 | self.insets = insets; 153 | } else { 154 | NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value); 155 | } 156 | } 157 | 158 | #pragma mark - Semantic properties 159 | 160 | - (MASConstraint *)with { 161 | return self; 162 | } 163 | 164 | - (MASConstraint *)and { 165 | return self; 166 | } 167 | 168 | #pragma mark - Chaining 169 | 170 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute __unused)layoutAttribute { 171 | MASMethodNotImplemented(); 172 | } 173 | 174 | - (MASConstraint *)left { 175 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 176 | } 177 | 178 | - (MASConstraint *)top { 179 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 180 | } 181 | 182 | - (MASConstraint *)right { 183 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 184 | } 185 | 186 | - (MASConstraint *)bottom { 187 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 188 | } 189 | 190 | - (MASConstraint *)leading { 191 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 192 | } 193 | 194 | - (MASConstraint *)trailing { 195 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 196 | } 197 | 198 | - (MASConstraint *)width { 199 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 200 | } 201 | 202 | - (MASConstraint *)height { 203 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 204 | } 205 | 206 | - (MASConstraint *)centerX { 207 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 208 | } 209 | 210 | - (MASConstraint *)centerY { 211 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 212 | } 213 | 214 | - (MASConstraint *)baseline { 215 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 216 | } 217 | 218 | - (MASConstraint *)firstBaseline { 219 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline]; 220 | } 221 | - (MASConstraint *)lastBaseline { 222 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline]; 223 | } 224 | 225 | #if TARGET_OS_IPHONE || TARGET_OS_TV 226 | 227 | - (MASConstraint *)leftMargin { 228 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 229 | } 230 | 231 | - (MASConstraint *)rightMargin { 232 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 233 | } 234 | 235 | - (MASConstraint *)topMargin { 236 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 237 | } 238 | 239 | - (MASConstraint *)bottomMargin { 240 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 241 | } 242 | 243 | - (MASConstraint *)leadingMargin { 244 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 245 | } 246 | 247 | - (MASConstraint *)trailingMargin { 248 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 249 | } 250 | 251 | - (MASConstraint *)centerXWithinMargins { 252 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 253 | } 254 | 255 | - (MASConstraint *)centerYWithinMargins { 256 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 257 | } 258 | 259 | #endif 260 | 261 | #pragma mark - Abstract 262 | 263 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy { MASMethodNotImplemented(); } 264 | 265 | - (MASConstraint * (^)(CGFloat divider))dividedBy { MASMethodNotImplemented(); } 266 | 267 | - (MASConstraint * (^)(MASLayoutPriority priority))priority { MASMethodNotImplemented(); } 268 | 269 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { MASMethodNotImplemented(); } 270 | 271 | - (MASConstraint * (^)(id key))key { MASMethodNotImplemented(); } 272 | 273 | - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); } 274 | 275 | - (void)setInset:(CGFloat __unused)inset { MASMethodNotImplemented(); } 276 | 277 | - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); } 278 | 279 | - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); } 280 | 281 | - (void)setOffset:(CGFloat __unused)offset { MASMethodNotImplemented(); } 282 | 283 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 284 | 285 | - (MASConstraint *)animator { MASMethodNotImplemented(); } 286 | 287 | #endif 288 | 289 | - (void)activate { MASMethodNotImplemented(); } 290 | 291 | - (void)deactivate { MASMethodNotImplemented(); } 292 | 293 | - (void)install { MASMethodNotImplemented(); } 294 | 295 | - (void)uninstall { MASMethodNotImplemented(); } 296 | 297 | @end 298 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASConstraintMaker.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraintMaker.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | #import "MASUtilities.h" 11 | 12 | typedef NS_OPTIONS(NSInteger, MASAttribute) { 13 | MASAttributeLeft = 1 << NSLayoutAttributeLeft, 14 | MASAttributeRight = 1 << NSLayoutAttributeRight, 15 | MASAttributeTop = 1 << NSLayoutAttributeTop, 16 | MASAttributeBottom = 1 << NSLayoutAttributeBottom, 17 | MASAttributeLeading = 1 << NSLayoutAttributeLeading, 18 | MASAttributeTrailing = 1 << NSLayoutAttributeTrailing, 19 | MASAttributeWidth = 1 << NSLayoutAttributeWidth, 20 | MASAttributeHeight = 1 << NSLayoutAttributeHeight, 21 | MASAttributeCenterX = 1 << NSLayoutAttributeCenterX, 22 | MASAttributeCenterY = 1 << NSLayoutAttributeCenterY, 23 | MASAttributeBaseline = 1 << NSLayoutAttributeBaseline, 24 | 25 | MASAttributeFirstBaseline = 1 << NSLayoutAttributeFirstBaseline, 26 | MASAttributeLastBaseline = 1 << NSLayoutAttributeLastBaseline, 27 | 28 | #if TARGET_OS_IPHONE || TARGET_OS_TV 29 | 30 | MASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin, 31 | MASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin, 32 | MASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin, 33 | MASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin, 34 | MASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin, 35 | MASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin, 36 | MASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins, 37 | MASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins, 38 | 39 | #endif 40 | 41 | }; 42 | 43 | /** 44 | * Provides factory methods for creating MASConstraints. 45 | * Constraints are collected until they are ready to be installed 46 | * 47 | */ 48 | @interface MASConstraintMaker : NSObject 49 | 50 | /** 51 | * The following properties return a new MASViewConstraint 52 | * with the first item set to the makers associated view and the appropriate MASViewAttribute 53 | */ 54 | @property (nonatomic, strong, readonly) MASConstraint *left; 55 | @property (nonatomic, strong, readonly) MASConstraint *top; 56 | @property (nonatomic, strong, readonly) MASConstraint *right; 57 | @property (nonatomic, strong, readonly) MASConstraint *bottom; 58 | @property (nonatomic, strong, readonly) MASConstraint *leading; 59 | @property (nonatomic, strong, readonly) MASConstraint *trailing; 60 | @property (nonatomic, strong, readonly) MASConstraint *width; 61 | @property (nonatomic, strong, readonly) MASConstraint *height; 62 | @property (nonatomic, strong, readonly) MASConstraint *centerX; 63 | @property (nonatomic, strong, readonly) MASConstraint *centerY; 64 | @property (nonatomic, strong, readonly) MASConstraint *baseline; 65 | 66 | @property (nonatomic, strong, readonly) MASConstraint *firstBaseline; 67 | @property (nonatomic, strong, readonly) MASConstraint *lastBaseline; 68 | 69 | #if TARGET_OS_IPHONE || TARGET_OS_TV 70 | 71 | @property (nonatomic, strong, readonly) MASConstraint *leftMargin; 72 | @property (nonatomic, strong, readonly) MASConstraint *rightMargin; 73 | @property (nonatomic, strong, readonly) MASConstraint *topMargin; 74 | @property (nonatomic, strong, readonly) MASConstraint *bottomMargin; 75 | @property (nonatomic, strong, readonly) MASConstraint *leadingMargin; 76 | @property (nonatomic, strong, readonly) MASConstraint *trailingMargin; 77 | @property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins; 78 | @property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins; 79 | 80 | #endif 81 | 82 | /** 83 | * Returns a block which creates a new MASCompositeConstraint with the first item set 84 | * to the makers associated view and children corresponding to the set bits in the 85 | * MASAttribute parameter. Combine multiple attributes via binary-or. 86 | */ 87 | @property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs); 88 | 89 | /** 90 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeEdges 91 | * which generates the appropriate MASViewConstraint children (top, left, bottom, right) 92 | * with the first item set to the makers associated view 93 | */ 94 | @property (nonatomic, strong, readonly) MASConstraint *edges; 95 | 96 | /** 97 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeSize 98 | * which generates the appropriate MASViewConstraint children (width, height) 99 | * with the first item set to the makers associated view 100 | */ 101 | @property (nonatomic, strong, readonly) MASConstraint *size; 102 | 103 | /** 104 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeCenter 105 | * which generates the appropriate MASViewConstraint children (centerX, centerY) 106 | * with the first item set to the makers associated view 107 | */ 108 | @property (nonatomic, strong, readonly) MASConstraint *center; 109 | 110 | /** 111 | * Whether or not to check for an existing constraint instead of adding constraint 112 | */ 113 | @property (nonatomic, assign) BOOL updateExisting; 114 | 115 | /** 116 | * Whether or not to remove existing constraints prior to installing 117 | */ 118 | @property (nonatomic, assign) BOOL removeExisting; 119 | 120 | /** 121 | * initialises the maker with a default view 122 | * 123 | * @param view any MASConstraint are created with this view as the first item 124 | * 125 | * @return a new MASConstraintMaker 126 | */ 127 | - (id)initWithView:(MAS_VIEW *)view; 128 | 129 | /** 130 | * Calls install method on any MASConstraints which have been created by this maker 131 | * 132 | * @return an array of all the installed MASConstraints 133 | */ 134 | - (NSArray *)install; 135 | 136 | - (MASConstraint * (^)(dispatch_block_t))group; 137 | 138 | @end 139 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASConstraintMaker.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraintMaker.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraintMaker.h" 10 | #import "MASViewConstraint.h" 11 | #import "MASCompositeConstraint.h" 12 | #import "MASConstraint+Private.h" 13 | #import "MASViewAttribute.h" 14 | #import "View+MASAdditions.h" 15 | 16 | @interface MASConstraintMaker () 17 | 18 | @property (nonatomic, weak) MAS_VIEW *view; 19 | @property (nonatomic, strong) NSMutableArray *constraints; 20 | 21 | @end 22 | 23 | @implementation MASConstraintMaker 24 | 25 | - (id)initWithView:(MAS_VIEW *)view { 26 | self = [super init]; 27 | if (!self) return nil; 28 | 29 | self.view = view; 30 | self.constraints = NSMutableArray.new; 31 | 32 | return self; 33 | } 34 | 35 | - (NSArray *)install { 36 | if (self.removeExisting) { 37 | NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view]; 38 | for (MASConstraint *constraint in installedConstraints) { 39 | [constraint uninstall]; 40 | } 41 | } 42 | NSArray *constraints = self.constraints.copy; 43 | for (MASConstraint *constraint in constraints) { 44 | constraint.updateExisting = self.updateExisting; 45 | [constraint install]; 46 | } 47 | [self.constraints removeAllObjects]; 48 | return constraints; 49 | } 50 | 51 | #pragma mark - MASConstraintDelegate 52 | 53 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint { 54 | NSUInteger index = [self.constraints indexOfObject:constraint]; 55 | NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint); 56 | [self.constraints replaceObjectAtIndex:index withObject:replacementConstraint]; 57 | } 58 | 59 | - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 60 | MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute]; 61 | MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute]; 62 | if ([constraint isKindOfClass:MASViewConstraint.class]) { 63 | //replace with composite constraint 64 | NSArray *children = @[constraint, newConstraint]; 65 | MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 66 | compositeConstraint.delegate = self; 67 | [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint]; 68 | return compositeConstraint; 69 | } 70 | if (!constraint) { 71 | newConstraint.delegate = self; 72 | [self.constraints addObject:newConstraint]; 73 | } 74 | return newConstraint; 75 | } 76 | 77 | - (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs { 78 | __unused MASAttribute anyAttribute = (MASAttributeLeft | MASAttributeRight | MASAttributeTop | MASAttributeBottom | MASAttributeLeading 79 | | MASAttributeTrailing | MASAttributeWidth | MASAttributeHeight | MASAttributeCenterX 80 | | MASAttributeCenterY | MASAttributeBaseline 81 | | MASAttributeFirstBaseline | MASAttributeLastBaseline 82 | #if TARGET_OS_IPHONE || TARGET_OS_TV 83 | | MASAttributeLeftMargin | MASAttributeRightMargin | MASAttributeTopMargin | MASAttributeBottomMargin 84 | | MASAttributeLeadingMargin | MASAttributeTrailingMargin | MASAttributeCenterXWithinMargins 85 | | MASAttributeCenterYWithinMargins 86 | #endif 87 | ); 88 | 89 | NSAssert((attrs & anyAttribute) != 0, @"You didn't pass any attribute to make.attributes(...)"); 90 | 91 | NSMutableArray *attributes = [NSMutableArray array]; 92 | 93 | if (attrs & MASAttributeLeft) [attributes addObject:self.view.mas_left]; 94 | if (attrs & MASAttributeRight) [attributes addObject:self.view.mas_right]; 95 | if (attrs & MASAttributeTop) [attributes addObject:self.view.mas_top]; 96 | if (attrs & MASAttributeBottom) [attributes addObject:self.view.mas_bottom]; 97 | if (attrs & MASAttributeLeading) [attributes addObject:self.view.mas_leading]; 98 | if (attrs & MASAttributeTrailing) [attributes addObject:self.view.mas_trailing]; 99 | if (attrs & MASAttributeWidth) [attributes addObject:self.view.mas_width]; 100 | if (attrs & MASAttributeHeight) [attributes addObject:self.view.mas_height]; 101 | if (attrs & MASAttributeCenterX) [attributes addObject:self.view.mas_centerX]; 102 | if (attrs & MASAttributeCenterY) [attributes addObject:self.view.mas_centerY]; 103 | if (attrs & MASAttributeBaseline) [attributes addObject:self.view.mas_baseline]; 104 | if (attrs & MASAttributeFirstBaseline) [attributes addObject:self.view.mas_firstBaseline]; 105 | if (attrs & MASAttributeLastBaseline) [attributes addObject:self.view.mas_lastBaseline]; 106 | 107 | #if TARGET_OS_IPHONE || TARGET_OS_TV 108 | 109 | if (attrs & MASAttributeLeftMargin) [attributes addObject:self.view.mas_leftMargin]; 110 | if (attrs & MASAttributeRightMargin) [attributes addObject:self.view.mas_rightMargin]; 111 | if (attrs & MASAttributeTopMargin) [attributes addObject:self.view.mas_topMargin]; 112 | if (attrs & MASAttributeBottomMargin) [attributes addObject:self.view.mas_bottomMargin]; 113 | if (attrs & MASAttributeLeadingMargin) [attributes addObject:self.view.mas_leadingMargin]; 114 | if (attrs & MASAttributeTrailingMargin) [attributes addObject:self.view.mas_trailingMargin]; 115 | if (attrs & MASAttributeCenterXWithinMargins) [attributes addObject:self.view.mas_centerXWithinMargins]; 116 | if (attrs & MASAttributeCenterYWithinMargins) [attributes addObject:self.view.mas_centerYWithinMargins]; 117 | 118 | #endif 119 | 120 | NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count]; 121 | 122 | for (MASViewAttribute *a in attributes) { 123 | [children addObject:[[MASViewConstraint alloc] initWithFirstViewAttribute:a]]; 124 | } 125 | 126 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 127 | constraint.delegate = self; 128 | [self.constraints addObject:constraint]; 129 | return constraint; 130 | } 131 | 132 | #pragma mark - standard Attributes 133 | 134 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 135 | return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute]; 136 | } 137 | 138 | - (MASConstraint *)left { 139 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 140 | } 141 | 142 | - (MASConstraint *)top { 143 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 144 | } 145 | 146 | - (MASConstraint *)right { 147 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 148 | } 149 | 150 | - (MASConstraint *)bottom { 151 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 152 | } 153 | 154 | - (MASConstraint *)leading { 155 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 156 | } 157 | 158 | - (MASConstraint *)trailing { 159 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 160 | } 161 | 162 | - (MASConstraint *)width { 163 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 164 | } 165 | 166 | - (MASConstraint *)height { 167 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 168 | } 169 | 170 | - (MASConstraint *)centerX { 171 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 172 | } 173 | 174 | - (MASConstraint *)centerY { 175 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 176 | } 177 | 178 | - (MASConstraint *)baseline { 179 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 180 | } 181 | 182 | - (MASConstraint *(^)(MASAttribute))attributes { 183 | return ^(MASAttribute attrs){ 184 | return [self addConstraintWithAttributes:attrs]; 185 | }; 186 | } 187 | 188 | - (MASConstraint *)firstBaseline { 189 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline]; 190 | } 191 | 192 | - (MASConstraint *)lastBaseline { 193 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline]; 194 | } 195 | 196 | #if TARGET_OS_IPHONE || TARGET_OS_TV 197 | 198 | - (MASConstraint *)leftMargin { 199 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 200 | } 201 | 202 | - (MASConstraint *)rightMargin { 203 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 204 | } 205 | 206 | - (MASConstraint *)topMargin { 207 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 208 | } 209 | 210 | - (MASConstraint *)bottomMargin { 211 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 212 | } 213 | 214 | - (MASConstraint *)leadingMargin { 215 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 216 | } 217 | 218 | - (MASConstraint *)trailingMargin { 219 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 220 | } 221 | 222 | - (MASConstraint *)centerXWithinMargins { 223 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 224 | } 225 | 226 | - (MASConstraint *)centerYWithinMargins { 227 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 228 | } 229 | 230 | #endif 231 | 232 | 233 | #pragma mark - composite Attributes 234 | 235 | - (MASConstraint *)edges { 236 | return [self addConstraintWithAttributes:MASAttributeTop | MASAttributeLeft | MASAttributeRight | MASAttributeBottom]; 237 | } 238 | 239 | - (MASConstraint *)size { 240 | return [self addConstraintWithAttributes:MASAttributeWidth | MASAttributeHeight]; 241 | } 242 | 243 | - (MASConstraint *)center { 244 | return [self addConstraintWithAttributes:MASAttributeCenterX | MASAttributeCenterY]; 245 | } 246 | 247 | #pragma mark - grouping 248 | 249 | - (MASConstraint *(^)(dispatch_block_t group))group { 250 | return ^id(dispatch_block_t group) { 251 | NSInteger previousCount = self.constraints.count; 252 | group(); 253 | 254 | NSArray *children = [self.constraints subarrayWithRange:NSMakeRange(previousCount, self.constraints.count - previousCount)]; 255 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 256 | constraint.delegate = self; 257 | return constraint; 258 | }; 259 | } 260 | 261 | @end 262 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASLayoutConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASLayoutConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * When you are debugging or printing the constraints attached to a view this subclass 13 | * makes it easier to identify which constraints have been created via Masonry 14 | */ 15 | @interface MASLayoutConstraint : NSLayoutConstraint 16 | 17 | /** 18 | * a key to associate with this constraint 19 | */ 20 | @property (nonatomic, strong) id mas_key; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASLayoutConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASLayoutConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASLayoutConstraint.h" 10 | 11 | @implementation MASLayoutConstraint 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASUtilities.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASUtilities.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 19/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | 13 | #if TARGET_OS_IPHONE || TARGET_OS_TV 14 | 15 | #import 16 | #define MAS_VIEW UIView 17 | #define MAS_VIEW_CONTROLLER UIViewController 18 | #define MASEdgeInsets UIEdgeInsets 19 | 20 | typedef UILayoutPriority MASLayoutPriority; 21 | static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired; 22 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh; 23 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500; 24 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow; 25 | static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel; 26 | 27 | #elif TARGET_OS_MAC 28 | 29 | #import 30 | #define MAS_VIEW NSView 31 | #define MASEdgeInsets NSEdgeInsets 32 | 33 | typedef NSLayoutPriority MASLayoutPriority; 34 | static const MASLayoutPriority MASLayoutPriorityRequired = NSLayoutPriorityRequired; 35 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = NSLayoutPriorityDefaultHigh; 36 | static const MASLayoutPriority MASLayoutPriorityDragThatCanResizeWindow = NSLayoutPriorityDragThatCanResizeWindow; 37 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 501; 38 | static const MASLayoutPriority MASLayoutPriorityWindowSizeStayPut = NSLayoutPriorityWindowSizeStayPut; 39 | static const MASLayoutPriority MASLayoutPriorityDragThatCannotResizeWindow = NSLayoutPriorityDragThatCannotResizeWindow; 40 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = NSLayoutPriorityDefaultLow; 41 | static const MASLayoutPriority MASLayoutPriorityFittingSizeCompression = NSLayoutPriorityFittingSizeCompression; 42 | 43 | #endif 44 | 45 | /** 46 | * Allows you to attach keys to objects matching the variable names passed. 47 | * 48 | * view1.mas_key = @"view1", view2.mas_key = @"view2"; 49 | * 50 | * is equivalent to: 51 | * 52 | * MASAttachKeys(view1, view2); 53 | */ 54 | #define MASAttachKeys(...) \ 55 | { \ 56 | NSDictionary *keyPairs = NSDictionaryOfVariableBindings(__VA_ARGS__); \ 57 | for (id key in keyPairs.allKeys) { \ 58 | id obj = keyPairs[key]; \ 59 | NSAssert([obj respondsToSelector:@selector(setMas_key:)], \ 60 | @"Cannot attach mas_key to %@", obj); \ 61 | [obj setMas_key:key]; \ 62 | } \ 63 | } 64 | 65 | /** 66 | * Used to create object hashes 67 | * Based on http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html 68 | */ 69 | #define MAS_NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger)) 70 | #define MAS_NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (MAS_NSUINT_BIT - howmuch))) 71 | 72 | /** 73 | * Given a scalar or struct value, wraps it in NSValue 74 | * Based on EXPObjectify: https://github.com/specta/expecta 75 | */ 76 | static inline id _MASBoxValue(const char *type, ...) { 77 | va_list v; 78 | va_start(v, type); 79 | id obj = nil; 80 | if (strcmp(type, @encode(id)) == 0) { 81 | id actual = va_arg(v, id); 82 | obj = actual; 83 | } else if (strcmp(type, @encode(CGPoint)) == 0) { 84 | CGPoint actual = (CGPoint)va_arg(v, CGPoint); 85 | obj = [NSValue value:&actual withObjCType:type]; 86 | } else if (strcmp(type, @encode(CGSize)) == 0) { 87 | CGSize actual = (CGSize)va_arg(v, CGSize); 88 | obj = [NSValue value:&actual withObjCType:type]; 89 | } else if (strcmp(type, @encode(MASEdgeInsets)) == 0) { 90 | MASEdgeInsets actual = (MASEdgeInsets)va_arg(v, MASEdgeInsets); 91 | obj = [NSValue value:&actual withObjCType:type]; 92 | } else if (strcmp(type, @encode(double)) == 0) { 93 | double actual = (double)va_arg(v, double); 94 | obj = [NSNumber numberWithDouble:actual]; 95 | } else if (strcmp(type, @encode(float)) == 0) { 96 | float actual = (float)va_arg(v, double); 97 | obj = [NSNumber numberWithFloat:actual]; 98 | } else if (strcmp(type, @encode(int)) == 0) { 99 | int actual = (int)va_arg(v, int); 100 | obj = [NSNumber numberWithInt:actual]; 101 | } else if (strcmp(type, @encode(long)) == 0) { 102 | long actual = (long)va_arg(v, long); 103 | obj = [NSNumber numberWithLong:actual]; 104 | } else if (strcmp(type, @encode(long long)) == 0) { 105 | long long actual = (long long)va_arg(v, long long); 106 | obj = [NSNumber numberWithLongLong:actual]; 107 | } else if (strcmp(type, @encode(short)) == 0) { 108 | short actual = (short)va_arg(v, int); 109 | obj = [NSNumber numberWithShort:actual]; 110 | } else if (strcmp(type, @encode(char)) == 0) { 111 | char actual = (char)va_arg(v, int); 112 | obj = [NSNumber numberWithChar:actual]; 113 | } else if (strcmp(type, @encode(bool)) == 0) { 114 | bool actual = (bool)va_arg(v, int); 115 | obj = [NSNumber numberWithBool:actual]; 116 | } else if (strcmp(type, @encode(unsigned char)) == 0) { 117 | unsigned char actual = (unsigned char)va_arg(v, unsigned int); 118 | obj = [NSNumber numberWithUnsignedChar:actual]; 119 | } else if (strcmp(type, @encode(unsigned int)) == 0) { 120 | unsigned int actual = (unsigned int)va_arg(v, unsigned int); 121 | obj = [NSNumber numberWithUnsignedInt:actual]; 122 | } else if (strcmp(type, @encode(unsigned long)) == 0) { 123 | unsigned long actual = (unsigned long)va_arg(v, unsigned long); 124 | obj = [NSNumber numberWithUnsignedLong:actual]; 125 | } else if (strcmp(type, @encode(unsigned long long)) == 0) { 126 | unsigned long long actual = (unsigned long long)va_arg(v, unsigned long long); 127 | obj = [NSNumber numberWithUnsignedLongLong:actual]; 128 | } else if (strcmp(type, @encode(unsigned short)) == 0) { 129 | unsigned short actual = (unsigned short)va_arg(v, unsigned int); 130 | obj = [NSNumber numberWithUnsignedShort:actual]; 131 | } 132 | va_end(v); 133 | return obj; 134 | } 135 | 136 | #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value)) 137 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASViewAttribute.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASViewAttribute.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * An immutable tuple which stores the view and the related NSLayoutAttribute. 13 | * Describes part of either the left or right hand side of a constraint equation 14 | */ 15 | @interface MASViewAttribute : NSObject 16 | 17 | /** 18 | * The view which the reciever relates to. Can be nil if item is not a view. 19 | */ 20 | @property (nonatomic, weak, readonly) MAS_VIEW *view; 21 | 22 | /** 23 | * The item which the reciever relates to. 24 | */ 25 | @property (nonatomic, weak, readonly) id item; 26 | 27 | /** 28 | * The attribute which the reciever relates to 29 | */ 30 | @property (nonatomic, assign, readonly) NSLayoutAttribute layoutAttribute; 31 | 32 | /** 33 | * Convenience initializer. 34 | */ 35 | - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute; 36 | 37 | /** 38 | * The designated initializer. 39 | */ 40 | - (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute; 41 | 42 | /** 43 | * Determine whether the layoutAttribute is a size attribute 44 | * 45 | * @return YES if layoutAttribute is equal to NSLayoutAttributeWidth or NSLayoutAttributeHeight 46 | */ 47 | - (BOOL)isSizeAttribute; 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASViewAttribute.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASViewAttribute.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewAttribute.h" 10 | 11 | @implementation MASViewAttribute 12 | 13 | - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute { 14 | self = [self initWithView:view item:view layoutAttribute:layoutAttribute]; 15 | return self; 16 | } 17 | 18 | - (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute { 19 | self = [super init]; 20 | if (!self) return nil; 21 | 22 | _view = view; 23 | _item = item; 24 | _layoutAttribute = layoutAttribute; 25 | 26 | return self; 27 | } 28 | 29 | - (BOOL)isSizeAttribute { 30 | return self.layoutAttribute == NSLayoutAttributeWidth 31 | || self.layoutAttribute == NSLayoutAttributeHeight; 32 | } 33 | 34 | - (BOOL)isEqual:(MASViewAttribute *)viewAttribute { 35 | if ([viewAttribute isKindOfClass:self.class]) { 36 | return self.view == viewAttribute.view 37 | && self.layoutAttribute == viewAttribute.layoutAttribute; 38 | } 39 | return [super isEqual:viewAttribute]; 40 | } 41 | 42 | - (NSUInteger)hash { 43 | return MAS_NSUINTROTATE([self.view hash], MAS_NSUINT_BIT / 2) ^ self.layoutAttribute; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASViewConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASViewConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewAttribute.h" 10 | #import "MASConstraint.h" 11 | #import "MASLayoutConstraint.h" 12 | #import "MASUtilities.h" 13 | 14 | /** 15 | * A single constraint. 16 | * Contains the attributes neccessary for creating a NSLayoutConstraint and adding it to the appropriate view 17 | */ 18 | @interface MASViewConstraint : MASConstraint 19 | 20 | /** 21 | * First item/view and first attribute of the NSLayoutConstraint 22 | */ 23 | @property (nonatomic, strong, readonly) MASViewAttribute *firstViewAttribute; 24 | 25 | /** 26 | * Second item/view and second attribute of the NSLayoutConstraint 27 | */ 28 | @property (nonatomic, strong, readonly) MASViewAttribute *secondViewAttribute; 29 | 30 | /** 31 | * initialises the MASViewConstraint with the first part of the equation 32 | * 33 | * @param firstViewAttribute view.mas_left, view.mas_width etc. 34 | * 35 | * @return a new view constraint 36 | */ 37 | - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute; 38 | 39 | /** 40 | * Returns all MASViewConstraints installed with this view as a first item. 41 | * 42 | * @param view A view to retrieve constraints for. 43 | * 44 | * @return An array of MASViewConstraints. 45 | */ 46 | + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view; 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/MASViewConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASViewConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewConstraint.h" 10 | #import "MASConstraint+Private.h" 11 | #import "MASCompositeConstraint.h" 12 | #import "MASLayoutConstraint.h" 13 | #import "View+MASAdditions.h" 14 | #import 15 | 16 | @interface MAS_VIEW (MASConstraints) 17 | 18 | @property (nonatomic, readonly) NSMutableSet *mas_installedConstraints; 19 | 20 | @end 21 | 22 | @implementation MAS_VIEW (MASConstraints) 23 | 24 | static char kInstalledConstraintsKey; 25 | 26 | - (NSMutableSet *)mas_installedConstraints { 27 | NSMutableSet *constraints = objc_getAssociatedObject(self, &kInstalledConstraintsKey); 28 | if (!constraints) { 29 | constraints = [NSMutableSet set]; 30 | objc_setAssociatedObject(self, &kInstalledConstraintsKey, constraints, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 31 | } 32 | return constraints; 33 | } 34 | 35 | @end 36 | 37 | 38 | @interface MASViewConstraint () 39 | 40 | @property (nonatomic, strong, readwrite) MASViewAttribute *secondViewAttribute; 41 | @property (nonatomic, weak) MAS_VIEW *installedView; 42 | @property (nonatomic, weak) MASLayoutConstraint *layoutConstraint; 43 | @property (nonatomic, assign) NSLayoutRelation layoutRelation; 44 | @property (nonatomic, assign) MASLayoutPriority layoutPriority; 45 | @property (nonatomic, assign) CGFloat layoutMultiplier; 46 | @property (nonatomic, assign) CGFloat layoutConstant; 47 | @property (nonatomic, assign) BOOL hasLayoutRelation; 48 | @property (nonatomic, strong) id mas_key; 49 | @property (nonatomic, assign) BOOL useAnimator; 50 | 51 | @end 52 | 53 | @implementation MASViewConstraint 54 | 55 | - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute { 56 | self = [super init]; 57 | if (!self) return nil; 58 | 59 | _firstViewAttribute = firstViewAttribute; 60 | self.layoutPriority = MASLayoutPriorityRequired; 61 | self.layoutMultiplier = 1; 62 | 63 | return self; 64 | } 65 | 66 | #pragma mark - NSCoping 67 | 68 | - (id)copyWithZone:(NSZone __unused *)zone { 69 | MASViewConstraint *constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:self.firstViewAttribute]; 70 | constraint.layoutConstant = self.layoutConstant; 71 | constraint.layoutRelation = self.layoutRelation; 72 | constraint.layoutPriority = self.layoutPriority; 73 | constraint.layoutMultiplier = self.layoutMultiplier; 74 | constraint.delegate = self.delegate; 75 | return constraint; 76 | } 77 | 78 | #pragma mark - Public 79 | 80 | + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view { 81 | return [view.mas_installedConstraints allObjects]; 82 | } 83 | 84 | #pragma mark - Private 85 | 86 | - (void)setLayoutConstant:(CGFloat)layoutConstant { 87 | _layoutConstant = layoutConstant; 88 | 89 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 90 | if (self.useAnimator) { 91 | [self.layoutConstraint.animator setConstant:layoutConstant]; 92 | } else { 93 | self.layoutConstraint.constant = layoutConstant; 94 | } 95 | #else 96 | self.layoutConstraint.constant = layoutConstant; 97 | #endif 98 | } 99 | 100 | - (void)setLayoutRelation:(NSLayoutRelation)layoutRelation { 101 | _layoutRelation = layoutRelation; 102 | self.hasLayoutRelation = YES; 103 | } 104 | 105 | - (BOOL)supportsActiveProperty { 106 | return [self.layoutConstraint respondsToSelector:@selector(isActive)]; 107 | } 108 | 109 | - (BOOL)isActive { 110 | BOOL active = YES; 111 | if ([self supportsActiveProperty]) { 112 | active = [self.layoutConstraint isActive]; 113 | } 114 | 115 | return active; 116 | } 117 | 118 | - (BOOL)hasBeenInstalled { 119 | return (self.layoutConstraint != nil) && [self isActive]; 120 | } 121 | 122 | - (void)setSecondViewAttribute:(id)secondViewAttribute { 123 | if ([secondViewAttribute isKindOfClass:NSValue.class]) { 124 | [self setLayoutConstantWithValue:secondViewAttribute]; 125 | } else if ([secondViewAttribute isKindOfClass:MAS_VIEW.class]) { 126 | _secondViewAttribute = [[MASViewAttribute alloc] initWithView:secondViewAttribute layoutAttribute:self.firstViewAttribute.layoutAttribute]; 127 | } else if ([secondViewAttribute isKindOfClass:MASViewAttribute.class]) { 128 | MASViewAttribute *attr = secondViewAttribute; 129 | if (attr.layoutAttribute == NSLayoutAttributeNotAnAttribute) { 130 | _secondViewAttribute = [[MASViewAttribute alloc] initWithView:attr.view item:attr.item layoutAttribute:self.firstViewAttribute.layoutAttribute];; 131 | } else { 132 | _secondViewAttribute = secondViewAttribute; 133 | } 134 | } else { 135 | NSAssert(NO, @"attempting to add unsupported attribute: %@", secondViewAttribute); 136 | } 137 | } 138 | 139 | #pragma mark - NSLayoutConstraint multiplier proxies 140 | 141 | - (MASConstraint * (^)(CGFloat))multipliedBy { 142 | return ^id(CGFloat multiplier) { 143 | NSAssert(!self.hasBeenInstalled, 144 | @"Cannot modify constraint multiplier after it has been installed"); 145 | 146 | self.layoutMultiplier = multiplier; 147 | return self; 148 | }; 149 | } 150 | 151 | 152 | - (MASConstraint * (^)(CGFloat))dividedBy { 153 | return ^id(CGFloat divider) { 154 | NSAssert(!self.hasBeenInstalled, 155 | @"Cannot modify constraint multiplier after it has been installed"); 156 | 157 | self.layoutMultiplier = 1.0/divider; 158 | return self; 159 | }; 160 | } 161 | 162 | #pragma mark - MASLayoutPriority proxy 163 | 164 | - (MASConstraint * (^)(MASLayoutPriority))priority { 165 | return ^id(MASLayoutPriority priority) { 166 | NSAssert(!self.hasBeenInstalled, 167 | @"Cannot modify constraint priority after it has been installed"); 168 | 169 | self.layoutPriority = priority; 170 | return self; 171 | }; 172 | } 173 | 174 | #pragma mark - NSLayoutRelation proxy 175 | 176 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { 177 | return ^id(id attribute, NSLayoutRelation relation) { 178 | if ([attribute isKindOfClass:NSArray.class]) { 179 | NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation"); 180 | NSMutableArray *children = NSMutableArray.new; 181 | for (id attr in attribute) { 182 | MASViewConstraint *viewConstraint = [self copy]; 183 | viewConstraint.layoutRelation = relation; 184 | viewConstraint.secondViewAttribute = attr; 185 | [children addObject:viewConstraint]; 186 | } 187 | MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 188 | compositeConstraint.delegate = self.delegate; 189 | [self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint]; 190 | return compositeConstraint; 191 | } else { 192 | NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation"); 193 | self.layoutRelation = relation; 194 | self.secondViewAttribute = attribute; 195 | return self; 196 | } 197 | }; 198 | } 199 | 200 | #pragma mark - Semantic properties 201 | 202 | - (MASConstraint *)with { 203 | return self; 204 | } 205 | 206 | - (MASConstraint *)and { 207 | return self; 208 | } 209 | 210 | #pragma mark - attribute chaining 211 | 212 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 213 | NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation"); 214 | 215 | return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 216 | } 217 | 218 | #pragma mark - Animator proxy 219 | 220 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 221 | 222 | - (MASConstraint *)animator { 223 | self.useAnimator = YES; 224 | return self; 225 | } 226 | 227 | #endif 228 | 229 | #pragma mark - debug helpers 230 | 231 | - (MASConstraint * (^)(id))key { 232 | return ^id(id key) { 233 | self.mas_key = key; 234 | return self; 235 | }; 236 | } 237 | 238 | #pragma mark - NSLayoutConstraint constant setters 239 | 240 | - (void)setInsets:(MASEdgeInsets)insets { 241 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 242 | switch (layoutAttribute) { 243 | case NSLayoutAttributeLeft: 244 | case NSLayoutAttributeLeading: 245 | self.layoutConstant = insets.left; 246 | break; 247 | case NSLayoutAttributeTop: 248 | self.layoutConstant = insets.top; 249 | break; 250 | case NSLayoutAttributeBottom: 251 | self.layoutConstant = -insets.bottom; 252 | break; 253 | case NSLayoutAttributeRight: 254 | case NSLayoutAttributeTrailing: 255 | self.layoutConstant = -insets.right; 256 | break; 257 | default: 258 | break; 259 | } 260 | } 261 | 262 | - (void)setInset:(CGFloat)inset { 263 | [self setInsets:(MASEdgeInsets){.top = inset, .left = inset, .bottom = inset, .right = inset}]; 264 | } 265 | 266 | - (void)setOffset:(CGFloat)offset { 267 | self.layoutConstant = offset; 268 | } 269 | 270 | - (void)setSizeOffset:(CGSize)sizeOffset { 271 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 272 | switch (layoutAttribute) { 273 | case NSLayoutAttributeWidth: 274 | self.layoutConstant = sizeOffset.width; 275 | break; 276 | case NSLayoutAttributeHeight: 277 | self.layoutConstant = sizeOffset.height; 278 | break; 279 | default: 280 | break; 281 | } 282 | } 283 | 284 | - (void)setCenterOffset:(CGPoint)centerOffset { 285 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 286 | switch (layoutAttribute) { 287 | case NSLayoutAttributeCenterX: 288 | self.layoutConstant = centerOffset.x; 289 | break; 290 | case NSLayoutAttributeCenterY: 291 | self.layoutConstant = centerOffset.y; 292 | break; 293 | default: 294 | break; 295 | } 296 | } 297 | 298 | #pragma mark - MASConstraint 299 | 300 | - (void)activate { 301 | [self install]; 302 | } 303 | 304 | - (void)deactivate { 305 | [self uninstall]; 306 | } 307 | 308 | - (void)install { 309 | if (self.hasBeenInstalled) { 310 | return; 311 | } 312 | 313 | if ([self supportsActiveProperty] && self.layoutConstraint) { 314 | self.layoutConstraint.active = YES; 315 | [self.firstViewAttribute.view.mas_installedConstraints addObject:self]; 316 | return; 317 | } 318 | 319 | MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item; 320 | NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute; 321 | MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item; 322 | NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute; 323 | 324 | // alignment attributes must have a secondViewAttribute 325 | // therefore we assume that is refering to superview 326 | // eg make.left.equalTo(@10) 327 | if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) { 328 | secondLayoutItem = self.firstViewAttribute.view.superview; 329 | secondLayoutAttribute = firstLayoutAttribute; 330 | } 331 | 332 | MASLayoutConstraint *layoutConstraint 333 | = [MASLayoutConstraint constraintWithItem:firstLayoutItem 334 | attribute:firstLayoutAttribute 335 | relatedBy:self.layoutRelation 336 | toItem:secondLayoutItem 337 | attribute:secondLayoutAttribute 338 | multiplier:self.layoutMultiplier 339 | constant:self.layoutConstant]; 340 | 341 | layoutConstraint.priority = self.layoutPriority; 342 | layoutConstraint.mas_key = self.mas_key; 343 | 344 | if (self.secondViewAttribute.view) { 345 | MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view]; 346 | NSAssert(closestCommonSuperview, 347 | @"couldn't find a common superview for %@ and %@", 348 | self.firstViewAttribute.view, self.secondViewAttribute.view); 349 | self.installedView = closestCommonSuperview; 350 | } else if (self.firstViewAttribute.isSizeAttribute) { 351 | self.installedView = self.firstViewAttribute.view; 352 | } else { 353 | self.installedView = self.firstViewAttribute.view.superview; 354 | } 355 | 356 | 357 | MASLayoutConstraint *existingConstraint = nil; 358 | if (self.updateExisting) { 359 | existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint]; 360 | } 361 | if (existingConstraint) { 362 | // just update the constant 363 | existingConstraint.constant = layoutConstraint.constant; 364 | self.layoutConstraint = existingConstraint; 365 | } else { 366 | [self.installedView addConstraint:layoutConstraint]; 367 | self.layoutConstraint = layoutConstraint; 368 | [firstLayoutItem.mas_installedConstraints addObject:self]; 369 | } 370 | } 371 | 372 | - (MASLayoutConstraint *)layoutConstraintSimilarTo:(MASLayoutConstraint *)layoutConstraint { 373 | // check if any constraints are the same apart from the only mutable property constant 374 | 375 | // go through constraints in reverse as we do not want to match auto-resizing or interface builder constraints 376 | // and they are likely to be added first. 377 | for (NSLayoutConstraint *existingConstraint in self.installedView.constraints.reverseObjectEnumerator) { 378 | if (![existingConstraint isKindOfClass:MASLayoutConstraint.class]) continue; 379 | if (existingConstraint.firstItem != layoutConstraint.firstItem) continue; 380 | if (existingConstraint.secondItem != layoutConstraint.secondItem) continue; 381 | if (existingConstraint.firstAttribute != layoutConstraint.firstAttribute) continue; 382 | if (existingConstraint.secondAttribute != layoutConstraint.secondAttribute) continue; 383 | if (existingConstraint.relation != layoutConstraint.relation) continue; 384 | if (existingConstraint.multiplier != layoutConstraint.multiplier) continue; 385 | if (existingConstraint.priority != layoutConstraint.priority) continue; 386 | 387 | return (id)existingConstraint; 388 | } 389 | return nil; 390 | } 391 | 392 | - (void)uninstall { 393 | if ([self supportsActiveProperty]) { 394 | self.layoutConstraint.active = NO; 395 | [self.firstViewAttribute.view.mas_installedConstraints removeObject:self]; 396 | return; 397 | } 398 | 399 | [self.installedView removeConstraint:self.layoutConstraint]; 400 | self.layoutConstraint = nil; 401 | self.installedView = nil; 402 | 403 | [self.firstViewAttribute.view.mas_installedConstraints removeObject:self]; 404 | } 405 | 406 | @end 407 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/Masonry.h: -------------------------------------------------------------------------------- 1 | // 2 | // Masonry.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for Masonry. 12 | FOUNDATION_EXPORT double MasonryVersionNumber; 13 | 14 | //! Project version string for Masonry. 15 | FOUNDATION_EXPORT const unsigned char MasonryVersionString[]; 16 | 17 | #import "MASUtilities.h" 18 | #import "View+MASAdditions.h" 19 | #import "View+MASShorthandAdditions.h" 20 | #import "ViewController+MASAdditions.h" 21 | #import "NSArray+MASAdditions.h" 22 | #import "NSArray+MASShorthandAdditions.h" 23 | #import "MASConstraint.h" 24 | #import "MASCompositeConstraint.h" 25 | #import "MASViewAttribute.h" 26 | #import "MASViewConstraint.h" 27 | #import "MASConstraintMaker.h" 28 | #import "MASLayoutConstraint.h" 29 | #import "NSLayoutConstraint+MASDebugAdditions.h" 30 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/NSArray+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASAdditions.h 3 | // 4 | // 5 | // Created by Daniel Hammond on 11/26/13. 6 | // 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | typedef NS_ENUM(NSUInteger, MASAxisType) { 14 | MASAxisTypeHorizontal, 15 | MASAxisTypeVertical 16 | }; 17 | 18 | @interface NSArray (MASAdditions) 19 | 20 | /** 21 | * Creates a MASConstraintMaker with each view in the callee. 22 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing on each view 23 | * 24 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 25 | * 26 | * @return Array of created MASConstraints 27 | */ 28 | - (NSArray *)mas_makeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 29 | 30 | /** 31 | * Creates a MASConstraintMaker with each view in the callee. 32 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 33 | * If an existing constraint exists then it will be updated instead. 34 | * 35 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 36 | * 37 | * @return Array of created/updated MASConstraints 38 | */ 39 | - (NSArray *)mas_updateConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 40 | 41 | /** 42 | * Creates a MASConstraintMaker with each view in the callee. 43 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 44 | * All constraints previously installed for the views will be removed. 45 | * 46 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 47 | * 48 | * @return Array of created/updated MASConstraints 49 | */ 50 | - (NSArray *)mas_remakeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 51 | 52 | /** 53 | * distribute with fixed spacing 54 | * 55 | * @param axisType which axis to distribute items along 56 | * @param fixedSpacing the spacing between each item 57 | * @param leadSpacing the spacing before the first item and the container 58 | * @param tailSpacing the spacing after the last item and the container 59 | */ 60 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; 61 | 62 | /** 63 | * distribute with fixed item size 64 | * 65 | * @param axisType which axis to distribute items along 66 | * @param fixedItemLength the fixed length of each item 67 | * @param leadSpacing the spacing before the first item and the container 68 | * @param tailSpacing the spacing after the last item and the container 69 | */ 70 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/NSArray+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASAdditions.m 3 | // 4 | // 5 | // Created by Daniel Hammond on 11/26/13. 6 | // 7 | // 8 | 9 | #import "NSArray+MASAdditions.h" 10 | #import "View+MASAdditions.h" 11 | 12 | @implementation NSArray (MASAdditions) 13 | 14 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block { 15 | NSMutableArray *constraints = [NSMutableArray array]; 16 | for (MAS_VIEW *view in self) { 17 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 18 | [constraints addObjectsFromArray:[view mas_makeConstraints:block]]; 19 | } 20 | return constraints; 21 | } 22 | 23 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block { 24 | NSMutableArray *constraints = [NSMutableArray array]; 25 | for (MAS_VIEW *view in self) { 26 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 27 | [constraints addObjectsFromArray:[view mas_updateConstraints:block]]; 28 | } 29 | return constraints; 30 | } 31 | 32 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block { 33 | NSMutableArray *constraints = [NSMutableArray array]; 34 | for (MAS_VIEW *view in self) { 35 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 36 | [constraints addObjectsFromArray:[view mas_remakeConstraints:block]]; 37 | } 38 | return constraints; 39 | } 40 | 41 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing { 42 | if (self.count < 2) { 43 | NSAssert(self.count>1,@"views to distribute need to bigger than one"); 44 | return; 45 | } 46 | 47 | MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews]; 48 | if (axisType == MASAxisTypeHorizontal) { 49 | MAS_VIEW *prev; 50 | for (int i = 0; i < self.count; i++) { 51 | MAS_VIEW *v = self[i]; 52 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 53 | if (prev) { 54 | make.width.equalTo(prev); 55 | make.left.equalTo(prev.mas_right).offset(fixedSpacing); 56 | if (i == self.count - 1) {//last one 57 | make.right.equalTo(tempSuperView).offset(-tailSpacing); 58 | } 59 | } 60 | else {//first one 61 | make.left.equalTo(tempSuperView).offset(leadSpacing); 62 | } 63 | 64 | }]; 65 | prev = v; 66 | } 67 | } 68 | else { 69 | MAS_VIEW *prev; 70 | for (int i = 0; i < self.count; i++) { 71 | MAS_VIEW *v = self[i]; 72 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 73 | if (prev) { 74 | make.height.equalTo(prev); 75 | make.top.equalTo(prev.mas_bottom).offset(fixedSpacing); 76 | if (i == self.count - 1) {//last one 77 | make.bottom.equalTo(tempSuperView).offset(-tailSpacing); 78 | } 79 | } 80 | else {//first one 81 | make.top.equalTo(tempSuperView).offset(leadSpacing); 82 | } 83 | 84 | }]; 85 | prev = v; 86 | } 87 | } 88 | } 89 | 90 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing { 91 | if (self.count < 2) { 92 | NSAssert(self.count>1,@"views to distribute need to bigger than one"); 93 | return; 94 | } 95 | 96 | MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews]; 97 | if (axisType == MASAxisTypeHorizontal) { 98 | MAS_VIEW *prev; 99 | for (int i = 0; i < self.count; i++) { 100 | MAS_VIEW *v = self[i]; 101 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 102 | make.width.equalTo(@(fixedItemLength)); 103 | if (prev) { 104 | if (i == self.count - 1) {//last one 105 | make.right.equalTo(tempSuperView).offset(-tailSpacing); 106 | } 107 | else { 108 | CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1)); 109 | make.right.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset); 110 | } 111 | } 112 | else {//first one 113 | make.left.equalTo(tempSuperView).offset(leadSpacing); 114 | } 115 | }]; 116 | prev = v; 117 | } 118 | } 119 | else { 120 | MAS_VIEW *prev; 121 | for (int i = 0; i < self.count; i++) { 122 | MAS_VIEW *v = self[i]; 123 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 124 | make.height.equalTo(@(fixedItemLength)); 125 | if (prev) { 126 | if (i == self.count - 1) {//last one 127 | make.bottom.equalTo(tempSuperView).offset(-tailSpacing); 128 | } 129 | else { 130 | CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1)); 131 | make.bottom.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset); 132 | } 133 | } 134 | else {//first one 135 | make.top.equalTo(tempSuperView).offset(leadSpacing); 136 | } 137 | }]; 138 | prev = v; 139 | } 140 | } 141 | } 142 | 143 | - (MAS_VIEW *)mas_commonSuperviewOfViews 144 | { 145 | MAS_VIEW *commonSuperview = nil; 146 | MAS_VIEW *previousView = nil; 147 | for (id object in self) { 148 | if ([object isKindOfClass:[MAS_VIEW class]]) { 149 | MAS_VIEW *view = (MAS_VIEW *)object; 150 | if (previousView) { 151 | commonSuperview = [view mas_closestCommonSuperview:commonSuperview]; 152 | } else { 153 | commonSuperview = view; 154 | } 155 | previousView = view; 156 | } 157 | } 158 | NSAssert(commonSuperview, @"Can't constrain views that do not share a common superview. Make sure that all the views in this array have been added into the same view hierarchy."); 159 | return commonSuperview; 160 | } 161 | 162 | @end 163 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/NSArray+MASShorthandAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASShorthandAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "NSArray+MASAdditions.h" 10 | 11 | #ifdef MAS_SHORTHAND 12 | 13 | /** 14 | * Shorthand array additions without the 'mas_' prefixes, 15 | * only enabled if MAS_SHORTHAND is defined 16 | */ 17 | @interface NSArray (MASShorthandAdditions) 18 | 19 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; 20 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; 21 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; 22 | 23 | @end 24 | 25 | @implementation NSArray (MASShorthandAdditions) 26 | 27 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *))block { 28 | return [self mas_makeConstraints:block]; 29 | } 30 | 31 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *))block { 32 | return [self mas_updateConstraints:block]; 33 | } 34 | 35 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *))block { 36 | return [self mas_remakeConstraints:block]; 37 | } 38 | 39 | @end 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/NSLayoutConstraint+MASDebugAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraint+MASDebugAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * makes debug and log output of NSLayoutConstraints more readable 13 | */ 14 | @interface NSLayoutConstraint (MASDebugAdditions) 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/NSLayoutConstraint+MASDebugAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraint+MASDebugAdditions.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "NSLayoutConstraint+MASDebugAdditions.h" 10 | #import "MASConstraint.h" 11 | #import "MASLayoutConstraint.h" 12 | 13 | @implementation NSLayoutConstraint (MASDebugAdditions) 14 | 15 | #pragma mark - description maps 16 | 17 | + (NSDictionary *)layoutRelationDescriptionsByValue { 18 | static dispatch_once_t once; 19 | static NSDictionary *descriptionMap; 20 | dispatch_once(&once, ^{ 21 | descriptionMap = @{ 22 | @(NSLayoutRelationEqual) : @"==", 23 | @(NSLayoutRelationGreaterThanOrEqual) : @">=", 24 | @(NSLayoutRelationLessThanOrEqual) : @"<=", 25 | }; 26 | }); 27 | return descriptionMap; 28 | } 29 | 30 | + (NSDictionary *)layoutAttributeDescriptionsByValue { 31 | static dispatch_once_t once; 32 | static NSDictionary *descriptionMap; 33 | dispatch_once(&once, ^{ 34 | descriptionMap = @{ 35 | @(NSLayoutAttributeTop) : @"top", 36 | @(NSLayoutAttributeLeft) : @"left", 37 | @(NSLayoutAttributeBottom) : @"bottom", 38 | @(NSLayoutAttributeRight) : @"right", 39 | @(NSLayoutAttributeLeading) : @"leading", 40 | @(NSLayoutAttributeTrailing) : @"trailing", 41 | @(NSLayoutAttributeWidth) : @"width", 42 | @(NSLayoutAttributeHeight) : @"height", 43 | @(NSLayoutAttributeCenterX) : @"centerX", 44 | @(NSLayoutAttributeCenterY) : @"centerY", 45 | @(NSLayoutAttributeBaseline) : @"baseline", 46 | @(NSLayoutAttributeFirstBaseline) : @"firstBaseline", 47 | @(NSLayoutAttributeLastBaseline) : @"lastBaseline", 48 | 49 | #if TARGET_OS_IPHONE || TARGET_OS_TV 50 | @(NSLayoutAttributeLeftMargin) : @"leftMargin", 51 | @(NSLayoutAttributeRightMargin) : @"rightMargin", 52 | @(NSLayoutAttributeTopMargin) : @"topMargin", 53 | @(NSLayoutAttributeBottomMargin) : @"bottomMargin", 54 | @(NSLayoutAttributeLeadingMargin) : @"leadingMargin", 55 | @(NSLayoutAttributeTrailingMargin) : @"trailingMargin", 56 | @(NSLayoutAttributeCenterXWithinMargins) : @"centerXWithinMargins", 57 | @(NSLayoutAttributeCenterYWithinMargins) : @"centerYWithinMargins", 58 | #endif 59 | 60 | }; 61 | 62 | }); 63 | return descriptionMap; 64 | } 65 | 66 | 67 | + (NSDictionary *)layoutPriorityDescriptionsByValue { 68 | static dispatch_once_t once; 69 | static NSDictionary *descriptionMap; 70 | dispatch_once(&once, ^{ 71 | #if TARGET_OS_IPHONE || TARGET_OS_TV 72 | descriptionMap = @{ 73 | @(MASLayoutPriorityDefaultHigh) : @"high", 74 | @(MASLayoutPriorityDefaultLow) : @"low", 75 | @(MASLayoutPriorityDefaultMedium) : @"medium", 76 | @(MASLayoutPriorityRequired) : @"required", 77 | @(MASLayoutPriorityFittingSizeLevel) : @"fitting size", 78 | }; 79 | #elif TARGET_OS_MAC 80 | descriptionMap = @{ 81 | @(MASLayoutPriorityDefaultHigh) : @"high", 82 | @(MASLayoutPriorityDragThatCanResizeWindow) : @"drag can resize window", 83 | @(MASLayoutPriorityDefaultMedium) : @"medium", 84 | @(MASLayoutPriorityWindowSizeStayPut) : @"window size stay put", 85 | @(MASLayoutPriorityDragThatCannotResizeWindow) : @"drag cannot resize window", 86 | @(MASLayoutPriorityDefaultLow) : @"low", 87 | @(MASLayoutPriorityFittingSizeCompression) : @"fitting size", 88 | @(MASLayoutPriorityRequired) : @"required", 89 | }; 90 | #endif 91 | }); 92 | return descriptionMap; 93 | } 94 | 95 | #pragma mark - description override 96 | 97 | + (NSString *)descriptionForObject:(id)obj { 98 | if ([obj respondsToSelector:@selector(mas_key)] && [obj mas_key]) { 99 | return [NSString stringWithFormat:@"%@:%@", [obj class], [obj mas_key]]; 100 | } 101 | return [NSString stringWithFormat:@"%@:%p", [obj class], obj]; 102 | } 103 | 104 | - (NSString *)description { 105 | NSMutableString *description = [[NSMutableString alloc] initWithString:@"<"]; 106 | 107 | [description appendString:[self.class descriptionForObject:self]]; 108 | 109 | [description appendFormat:@" %@", [self.class descriptionForObject:self.firstItem]]; 110 | if (self.firstAttribute != NSLayoutAttributeNotAnAttribute) { 111 | [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.firstAttribute)]]; 112 | } 113 | 114 | [description appendFormat:@" %@", self.class.layoutRelationDescriptionsByValue[@(self.relation)]]; 115 | 116 | if (self.secondItem) { 117 | [description appendFormat:@" %@", [self.class descriptionForObject:self.secondItem]]; 118 | } 119 | if (self.secondAttribute != NSLayoutAttributeNotAnAttribute) { 120 | [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.secondAttribute)]]; 121 | } 122 | 123 | if (self.multiplier != 1) { 124 | [description appendFormat:@" * %g", self.multiplier]; 125 | } 126 | 127 | if (self.secondAttribute == NSLayoutAttributeNotAnAttribute) { 128 | [description appendFormat:@" %g", self.constant]; 129 | } else { 130 | if (self.constant) { 131 | [description appendFormat:@" %@ %g", (self.constant < 0 ? @"-" : @"+"), ABS(self.constant)]; 132 | } 133 | } 134 | 135 | if (self.priority != MASLayoutPriorityRequired) { 136 | [description appendFormat:@" ^%@", self.class.layoutPriorityDescriptionsByValue[@(self.priority)] ?: [NSNumber numberWithDouble:self.priority]]; 137 | } 138 | 139 | [description appendString:@">"]; 140 | return description; 141 | } 142 | 143 | @end 144 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/View+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | /** 14 | * Provides constraint maker block 15 | * and convience methods for creating MASViewAttribute which are view + NSLayoutAttribute pairs 16 | */ 17 | @interface MAS_VIEW (MASAdditions) 18 | 19 | /** 20 | * following properties return a new MASViewAttribute with current view and appropriate NSLayoutAttribute 21 | */ 22 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_left; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_top; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_right; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom; 26 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leading; 27 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing; 28 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_width; 29 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_height; 30 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX; 31 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY; 32 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline; 33 | @property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr); 34 | 35 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_firstBaseline; 36 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_lastBaseline; 37 | 38 | #if TARGET_OS_IPHONE || TARGET_OS_TV 39 | 40 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin; 41 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin; 42 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin; 43 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin; 44 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin; 45 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin; 46 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins; 47 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins; 48 | 49 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuide NS_AVAILABLE_IOS(11.0); 50 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeading NS_AVAILABLE_IOS(11.0); 51 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTrailing NS_AVAILABLE_IOS(11.0); 52 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeft NS_AVAILABLE_IOS(11.0); 53 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideRight NS_AVAILABLE_IOS(11.0); 54 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTop NS_AVAILABLE_IOS(11.0); 55 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideBottom NS_AVAILABLE_IOS(11.0); 56 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideWidth NS_AVAILABLE_IOS(11.0); 57 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideHeight NS_AVAILABLE_IOS(11.0); 58 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterX NS_AVAILABLE_IOS(11.0); 59 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterY NS_AVAILABLE_IOS(11.0); 60 | 61 | #endif 62 | 63 | /** 64 | * a key to associate with this view 65 | */ 66 | @property (nonatomic, strong) id mas_key; 67 | 68 | /** 69 | * Finds the closest common superview between this view and another view 70 | * 71 | * @param view other view 72 | * 73 | * @return returns nil if common superview could not be found 74 | */ 75 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view; 76 | 77 | /** 78 | * Creates a MASConstraintMaker with the callee view. 79 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing 80 | * 81 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 82 | * 83 | * @return Array of created MASConstraints 84 | */ 85 | - (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 86 | 87 | /** 88 | * Creates a MASConstraintMaker with the callee view. 89 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 90 | * If an existing constraint exists then it will be updated instead. 91 | * 92 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 93 | * 94 | * @return Array of created/updated MASConstraints 95 | */ 96 | - (NSArray *)mas_updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 97 | 98 | /** 99 | * Creates a MASConstraintMaker with the callee view. 100 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 101 | * All constraints previously installed for the view will be removed. 102 | * 103 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 104 | * 105 | * @return Array of created/updated MASConstraints 106 | */ 107 | - (NSArray *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 108 | 109 | @end 110 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/View+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASAdditions.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "View+MASAdditions.h" 10 | #import 11 | 12 | @implementation MAS_VIEW (MASAdditions) 13 | 14 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { 15 | self.translatesAutoresizingMaskIntoConstraints = NO; 16 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 17 | block(constraintMaker); 18 | return [constraintMaker install]; 19 | } 20 | 21 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block { 22 | self.translatesAutoresizingMaskIntoConstraints = NO; 23 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 24 | constraintMaker.updateExisting = YES; 25 | block(constraintMaker); 26 | return [constraintMaker install]; 27 | } 28 | 29 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block { 30 | self.translatesAutoresizingMaskIntoConstraints = NO; 31 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 32 | constraintMaker.removeExisting = YES; 33 | block(constraintMaker); 34 | return [constraintMaker install]; 35 | } 36 | 37 | #pragma mark - NSLayoutAttribute properties 38 | 39 | - (MASViewAttribute *)mas_left { 40 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeft]; 41 | } 42 | 43 | - (MASViewAttribute *)mas_top { 44 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTop]; 45 | } 46 | 47 | - (MASViewAttribute *)mas_right { 48 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRight]; 49 | } 50 | 51 | - (MASViewAttribute *)mas_bottom { 52 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottom]; 53 | } 54 | 55 | - (MASViewAttribute *)mas_leading { 56 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeading]; 57 | } 58 | 59 | - (MASViewAttribute *)mas_trailing { 60 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailing]; 61 | } 62 | 63 | - (MASViewAttribute *)mas_width { 64 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeWidth]; 65 | } 66 | 67 | - (MASViewAttribute *)mas_height { 68 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeHeight]; 69 | } 70 | 71 | - (MASViewAttribute *)mas_centerX { 72 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterX]; 73 | } 74 | 75 | - (MASViewAttribute *)mas_centerY { 76 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterY]; 77 | } 78 | 79 | - (MASViewAttribute *)mas_baseline { 80 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBaseline]; 81 | } 82 | 83 | - (MASViewAttribute *(^)(NSLayoutAttribute))mas_attribute 84 | { 85 | return ^(NSLayoutAttribute attr) { 86 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:attr]; 87 | }; 88 | } 89 | 90 | - (MASViewAttribute *)mas_firstBaseline { 91 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeFirstBaseline]; 92 | } 93 | - (MASViewAttribute *)mas_lastBaseline { 94 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLastBaseline]; 95 | } 96 | 97 | #if TARGET_OS_IPHONE || TARGET_OS_TV 98 | 99 | - (MASViewAttribute *)mas_leftMargin { 100 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeftMargin]; 101 | } 102 | 103 | - (MASViewAttribute *)mas_rightMargin { 104 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRightMargin]; 105 | } 106 | 107 | - (MASViewAttribute *)mas_topMargin { 108 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTopMargin]; 109 | } 110 | 111 | - (MASViewAttribute *)mas_bottomMargin { 112 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottomMargin]; 113 | } 114 | 115 | - (MASViewAttribute *)mas_leadingMargin { 116 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeadingMargin]; 117 | } 118 | 119 | - (MASViewAttribute *)mas_trailingMargin { 120 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailingMargin]; 121 | } 122 | 123 | - (MASViewAttribute *)mas_centerXWithinMargins { 124 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 125 | } 126 | 127 | - (MASViewAttribute *)mas_centerYWithinMargins { 128 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 129 | } 130 | 131 | - (MASViewAttribute *)mas_safeAreaLayoutGuide { 132 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeNotAnAttribute]; 133 | } 134 | 135 | - (MASViewAttribute *)mas_safeAreaLayoutGuideLeading { 136 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeLeading]; 137 | } 138 | 139 | - (MASViewAttribute *)mas_safeAreaLayoutGuideTrailing { 140 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeTrailing]; 141 | } 142 | 143 | - (MASViewAttribute *)mas_safeAreaLayoutGuideLeft { 144 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeLeft]; 145 | } 146 | 147 | - (MASViewAttribute *)mas_safeAreaLayoutGuideRight { 148 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeRight]; 149 | } 150 | 151 | - (MASViewAttribute *)mas_safeAreaLayoutGuideTop { 152 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 153 | } 154 | 155 | - (MASViewAttribute *)mas_safeAreaLayoutGuideBottom { 156 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 157 | } 158 | 159 | - (MASViewAttribute *)mas_safeAreaLayoutGuideWidth { 160 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeWidth]; 161 | } 162 | 163 | - (MASViewAttribute *)mas_safeAreaLayoutGuideHeight { 164 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeHeight]; 165 | } 166 | 167 | - (MASViewAttribute *)mas_safeAreaLayoutGuideCenterX { 168 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeCenterX]; 169 | } 170 | 171 | - (MASViewAttribute *)mas_safeAreaLayoutGuideCenterY { 172 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeCenterY]; 173 | } 174 | 175 | #endif 176 | 177 | #pragma mark - associated properties 178 | 179 | - (id)mas_key { 180 | return objc_getAssociatedObject(self, @selector(mas_key)); 181 | } 182 | 183 | - (void)setMas_key:(id)key { 184 | objc_setAssociatedObject(self, @selector(mas_key), key, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 185 | } 186 | 187 | #pragma mark - heirachy 188 | 189 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view { 190 | MAS_VIEW *closestCommonSuperview = nil; 191 | 192 | MAS_VIEW *secondViewSuperview = view; 193 | while (!closestCommonSuperview && secondViewSuperview) { 194 | MAS_VIEW *firstViewSuperview = self; 195 | while (!closestCommonSuperview && firstViewSuperview) { 196 | if (secondViewSuperview == firstViewSuperview) { 197 | closestCommonSuperview = secondViewSuperview; 198 | } 199 | firstViewSuperview = firstViewSuperview.superview; 200 | } 201 | secondViewSuperview = secondViewSuperview.superview; 202 | } 203 | return closestCommonSuperview; 204 | } 205 | 206 | @end 207 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/View+MASShorthandAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASShorthandAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "View+MASAdditions.h" 10 | 11 | #ifdef MAS_SHORTHAND 12 | 13 | /** 14 | * Shorthand view additions without the 'mas_' prefixes, 15 | * only enabled if MAS_SHORTHAND is defined 16 | */ 17 | @interface MAS_VIEW (MASShorthandAdditions) 18 | 19 | @property (nonatomic, strong, readonly) MASViewAttribute *left; 20 | @property (nonatomic, strong, readonly) MASViewAttribute *top; 21 | @property (nonatomic, strong, readonly) MASViewAttribute *right; 22 | @property (nonatomic, strong, readonly) MASViewAttribute *bottom; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *leading; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *trailing; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *width; 26 | @property (nonatomic, strong, readonly) MASViewAttribute *height; 27 | @property (nonatomic, strong, readonly) MASViewAttribute *centerX; 28 | @property (nonatomic, strong, readonly) MASViewAttribute *centerY; 29 | @property (nonatomic, strong, readonly) MASViewAttribute *baseline; 30 | @property (nonatomic, strong, readonly) MASViewAttribute *(^attribute)(NSLayoutAttribute attr); 31 | 32 | @property (nonatomic, strong, readonly) MASViewAttribute *firstBaseline; 33 | @property (nonatomic, strong, readonly) MASViewAttribute *lastBaseline; 34 | 35 | #if TARGET_OS_IPHONE || TARGET_OS_TV 36 | 37 | @property (nonatomic, strong, readonly) MASViewAttribute *leftMargin; 38 | @property (nonatomic, strong, readonly) MASViewAttribute *rightMargin; 39 | @property (nonatomic, strong, readonly) MASViewAttribute *topMargin; 40 | @property (nonatomic, strong, readonly) MASViewAttribute *bottomMargin; 41 | @property (nonatomic, strong, readonly) MASViewAttribute *leadingMargin; 42 | @property (nonatomic, strong, readonly) MASViewAttribute *trailingMargin; 43 | @property (nonatomic, strong, readonly) MASViewAttribute *centerXWithinMargins; 44 | @property (nonatomic, strong, readonly) MASViewAttribute *centerYWithinMargins; 45 | 46 | #endif 47 | 48 | #if TARGET_OS_IPHONE || TARGET_OS_TV 49 | 50 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideLeading NS_AVAILABLE_IOS(11.0); 51 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideTrailing NS_AVAILABLE_IOS(11.0); 52 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideLeft NS_AVAILABLE_IOS(11.0); 53 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideRight NS_AVAILABLE_IOS(11.0); 54 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideTop NS_AVAILABLE_IOS(11.0); 55 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideBottom NS_AVAILABLE_IOS(11.0); 56 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideWidth NS_AVAILABLE_IOS(11.0); 57 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideHeight NS_AVAILABLE_IOS(11.0); 58 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideCenterX NS_AVAILABLE_IOS(11.0); 59 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideCenterY NS_AVAILABLE_IOS(11.0); 60 | 61 | #endif 62 | 63 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; 64 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; 65 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; 66 | 67 | @end 68 | 69 | #define MAS_ATTR_FORWARD(attr) \ 70 | - (MASViewAttribute *)attr { \ 71 | return [self mas_##attr]; \ 72 | } 73 | 74 | #define MAS_ATTR_FORWARD_AVAILABLE(attr, available) \ 75 | - (MASViewAttribute *)attr available { \ 76 | return [self mas_##attr]; \ 77 | } 78 | 79 | @implementation MAS_VIEW (MASShorthandAdditions) 80 | 81 | MAS_ATTR_FORWARD(top); 82 | MAS_ATTR_FORWARD(left); 83 | MAS_ATTR_FORWARD(bottom); 84 | MAS_ATTR_FORWARD(right); 85 | MAS_ATTR_FORWARD(leading); 86 | MAS_ATTR_FORWARD(trailing); 87 | MAS_ATTR_FORWARD(width); 88 | MAS_ATTR_FORWARD(height); 89 | MAS_ATTR_FORWARD(centerX); 90 | MAS_ATTR_FORWARD(centerY); 91 | MAS_ATTR_FORWARD(baseline); 92 | 93 | MAS_ATTR_FORWARD(firstBaseline); 94 | MAS_ATTR_FORWARD(lastBaseline); 95 | 96 | #if TARGET_OS_IPHONE || TARGET_OS_TV 97 | 98 | MAS_ATTR_FORWARD(leftMargin); 99 | MAS_ATTR_FORWARD(rightMargin); 100 | MAS_ATTR_FORWARD(topMargin); 101 | MAS_ATTR_FORWARD(bottomMargin); 102 | MAS_ATTR_FORWARD(leadingMargin); 103 | MAS_ATTR_FORWARD(trailingMargin); 104 | MAS_ATTR_FORWARD(centerXWithinMargins); 105 | MAS_ATTR_FORWARD(centerYWithinMargins); 106 | 107 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideLeading, NS_AVAILABLE_IOS(11.0)); 108 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideTrailing, NS_AVAILABLE_IOS(11.0)); 109 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideLeft, NS_AVAILABLE_IOS(11.0)); 110 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideRight, NS_AVAILABLE_IOS(11.0)); 111 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideTop, NS_AVAILABLE_IOS(11.0)); 112 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideBottom, NS_AVAILABLE_IOS(11.0)); 113 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideWidth, NS_AVAILABLE_IOS(11.0)); 114 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideHeight, NS_AVAILABLE_IOS(11.0)); 115 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideCenterX, NS_AVAILABLE_IOS(11.0)); 116 | MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideCenterY, NS_AVAILABLE_IOS(11.0)); 117 | 118 | #endif 119 | 120 | - (MASViewAttribute *(^)(NSLayoutAttribute))attribute { 121 | return [self mas_attribute]; 122 | } 123 | 124 | - (NSArray *)makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 125 | return [self mas_makeConstraints:block]; 126 | } 127 | 128 | - (NSArray *)updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 129 | return [self mas_updateConstraints:block]; 130 | } 131 | 132 | - (NSArray *)remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 133 | return [self mas_remakeConstraints:block]; 134 | } 135 | 136 | @end 137 | 138 | #endif 139 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/ViewController+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+MASAdditions.h 3 | // Masonry 4 | // 5 | // Created by Craig Siemens on 2015-06-23. 6 | // 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | #ifdef MAS_VIEW_CONTROLLER 14 | 15 | @interface MAS_VIEW_CONTROLLER (MASAdditions) 16 | 17 | /** 18 | * following properties return a new MASViewAttribute with appropriate UILayoutGuide and NSLayoutAttribute 19 | */ 20 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuide NS_DEPRECATED_IOS(8.0, 11.0); 21 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuide NS_DEPRECATED_IOS(8.0, 11.0); 22 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideTop NS_DEPRECATED_IOS(8.0, 11.0); 23 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideBottom NS_DEPRECATED_IOS(8.0, 11.0); 24 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideTop NS_DEPRECATED_IOS(8.0, 11.0); 25 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideBottom NS_DEPRECATED_IOS(8.0, 11.0); 26 | 27 | @end 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /SDDemo/Lib/Masonry/ViewController+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+MASAdditions.m 3 | // Masonry 4 | // 5 | // Created by Craig Siemens on 2015-06-23. 6 | // 7 | // 8 | 9 | #import "ViewController+MASAdditions.h" 10 | 11 | #ifdef MAS_VIEW_CONTROLLER 12 | 13 | @implementation MAS_VIEW_CONTROLLER (MASAdditions) 14 | 15 | #pragma clang diagnostic push 16 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 17 | 18 | - (MASViewAttribute *)mas_topLayoutGuide { 19 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 20 | } 21 | - (MASViewAttribute *)mas_topLayoutGuideTop { 22 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 23 | } 24 | - (MASViewAttribute *)mas_topLayoutGuideBottom { 25 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 26 | } 27 | 28 | - (MASViewAttribute *)mas_bottomLayoutGuide { 29 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 30 | } 31 | - (MASViewAttribute *)mas_bottomLayoutGuideTop { 32 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 33 | } 34 | - (MASViewAttribute *)mas_bottomLayoutGuideBottom { 35 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 36 | } 37 | 38 | #pragma clang diagnostic pop 39 | 40 | @end 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /SDDemo/Resource/baidu.json: -------------------------------------------------------------------------------- 1 | { 2 | "feed": [ 3 | { 4 | "title": "Hello world", 5 | "content": "When i was young, my dream is not to be a coder,my dream is to be a poet", 6 | "username": "xuelin", 7 | "time": "2017.04.29", 8 | "imageName": "a", 9 | "height":432 10 | }, 11 | { 12 | "title": "Team member - xuelin", 13 | "content": "For our ever-lasting friendship, send sincere blessings and warm greetings to my friends whom I miss so much.", 14 | "username": "xuelin", 15 | "time": "2017.04.11", 16 | "imageName": "b", 17 | "height":195 18 | }, 19 | { 20 | "title": "Team member - zhixinHe", 21 | "content": "Only when our eyes have been washed by tears, can we have a broader field of vision.", 22 | "username": "zhixinHe", 23 | "time": "2017.04.15", 24 | "imageName": "c", 25 | "height":250 26 | }, 27 | { 28 | "title": "Team member - Phil", 29 | "content": "This photo was taken in Nyingchi City of China.鲁朗.It's amazing.I like here.", 30 | "username": "phil", 31 | "time": "2017.04.15", 32 | "imageName": "phil", 33 | "height":213 34 | }, 35 | { 36 | "title": "William Shakespeare", 37 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 38 | "username": "xuelin", 39 | "time": "2017.04.12", 40 | "imageName": "d", 41 | "height":340 42 | }, 43 | { 44 | "title": "You know my loneliness", 45 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 46 | "username": "xuelin", 47 | "time": "2017.04.16", 48 | "imageName": "e", 49 | "height":517.5 50 | }, 51 | { 52 | "title": "Life isn't about waiting for the storm to pass,", 53 | "content": "If living on the earth is a mission from the lord… living with you is the award of the lord…", 54 | "username": "xuelin", 55 | "time": "2017.04.16", 56 | "imageName": "f", 57 | "height":200 58 | }, 59 | { 60 | "title": "North", 61 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 62 | "username": "xuelin", 63 | "time": "2017.04.17", 64 | "imageName": "g", 65 | "height":200 66 | }, 67 | { 68 | "title": "Worth", 69 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 70 | "username": "xuelin", 71 | "time": "2017.04.12", 72 | "imageName": "h", 73 | "height":395 74 | }, 75 | { 76 | "title": "William Shakespeare", 77 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 78 | "username": "xuelin", 79 | "time": "2017.04.16", 80 | "imageName": "", 81 | "height":0 82 | }, 83 | { 84 | "title": "Life is ten percent what you make it and ninety percent how you take it.", 85 | "content": "Love makes you stupid : whispers of love is a bunch of stupid, a love letter is silly talk, valentine is a little fool.", 86 | "username": "xuelin", 87 | "time": "2017.04.16", 88 | "imageName": "j", 89 | "height":100 90 | }, 91 | { 92 | "title": "I love three things in this world. Sun, moon and you. Sun for morning, moon for night , and you forever.", 93 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 94 | "username": "xuelin", 95 | "time": "2017.04.17", 96 | "imageName": "", 97 | "height":0 98 | }, 99 | { 100 | "title": "I love three things in this world. Sun, moon and you. Sun for morning, moon for night , and you forever.", 101 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 102 | "username": "xuelin", 103 | "time": "2017.04.16", 104 | "imageName": "", 105 | "height":0 106 | }, 107 | { 108 | "title": "Hello world", 109 | "content": "When i was young, my dream is not to be a coder,my dream is to be a poet", 110 | "username": "xuelin", 111 | "time": "2017.04.29", 112 | "imageName": "a", 113 | "height":432 114 | }, 115 | { 116 | "title": "Team member - xuelin", 117 | "content": "For our ever-lasting friendship, send sincere blessings and warm greetings to my friends whom I miss so much.", 118 | "username": "xuelin", 119 | "time": "2017.04.11", 120 | "imageName": "b", 121 | "height":195 122 | }, 123 | { 124 | "title": "Team member - zhixinHe", 125 | "content": "Only when our eyes have been washed by tears, can we have a broader field of vision.", 126 | "username": "zhixinHe", 127 | "time": "2017.04.15", 128 | "imageName": "c", 129 | "height":250 130 | }, 131 | { 132 | "title": "Team member - Phil", 133 | "content": "Do you understand the feeling of missing someone? It is just like that you will spend a long hard time to turn the ice-cold water you have drunk into tears.", 134 | "username": "phil", 135 | "time": "2017.04.15", 136 | "imageName": "phil", 137 | "height":213 138 | }, 139 | { 140 | "title": "William Shakespeare", 141 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 142 | "username": "xuelin", 143 | "time": "2017.04.12", 144 | "imageName": "d", 145 | "height":340 146 | }, 147 | { 148 | "title": "You know my loneliness", 149 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 150 | "username": "xuelin", 151 | "time": "2017.04.16", 152 | "imageName": "e", 153 | "height":517.5 154 | }, 155 | { 156 | "title": "Life isn't about waiting for the storm to pass,", 157 | "content": "If living on the earth is a mission from the lord… living with you is the award of the lord…", 158 | "username": "xuelin", 159 | "time": "2017.04.16", 160 | "imageName": "f", 161 | "height":200 162 | }, 163 | { 164 | "title": "North", 165 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 166 | "username": "xuelin", 167 | "time": "2017.04.17", 168 | "imageName": "g", 169 | "height":200 170 | }, 171 | { 172 | "title": "Worth", 173 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 174 | "username": "xuelin", 175 | "time": "2017.04.12", 176 | "imageName": "h", 177 | "height":395 178 | }, 179 | { 180 | "title": "William Shakespeare", 181 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 182 | "username": "xuelin", 183 | "time": "2017.04.16", 184 | "imageName": "", 185 | "height":0 186 | }, 187 | { 188 | "title": "Life is ten percent what you make it and ninety percent how you take it.", 189 | "content": "Love makes you stupid : whispers of love is a bunch of stupid, a love letter is silly talk, valentine is a little fool.", 190 | "username": "xuelin", 191 | "time": "2017.04.16", 192 | "imageName": "j", 193 | "height":100 194 | }, 195 | { 196 | "title": "I love three things in this world. Sun, moon and you. Sun for morning, moon for night , and you forever.", 197 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 198 | "username": "xuelin", 199 | "time": "2017.04.17", 200 | "imageName": "", 201 | "height":0 202 | }, 203 | { 204 | "title": "I love three things in this world. Sun, moon and you. Sun for morning, moon for night , and you forever.", 205 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 206 | "username": "xuelin", 207 | "time": "2017.04.16", 208 | "imageName": "", 209 | "height":0 210 | }, 211 | { 212 | "title": "Hello world", 213 | "content": "When i was young, my dream is not to be a coder,my dream is to be a poet", 214 | "username": "xuelin", 215 | "time": "2017.04.29", 216 | "imageName": "a", 217 | "height":432 218 | }, 219 | { 220 | "title": "Team member - xuelin", 221 | "content": "For our ever-lasting friendship, send sincere blessings and warm greetings to my friends whom I miss so much.", 222 | "username": "xuelin", 223 | "time": "2017.04.11", 224 | "imageName": "b", 225 | "height":195 226 | }, 227 | { 228 | "title": "Team member - zhixinHe", 229 | "content": "Only when our eyes have been washed by tears, can we have a broader field of vision.", 230 | "username": "zhixinHe", 231 | "time": "2017.04.15", 232 | "imageName": "c", 233 | "height":250 234 | }, 235 | { 236 | "title": "Team member - Phil", 237 | "content": "Do you understand the feeling of missing someone? It is just like that you will spend a long hard time to turn the ice-cold water you have drunk into tears.", 238 | "username": "phil", 239 | "time": "2017.04.15", 240 | "imageName": "phil", 241 | "height":213 242 | }, 243 | { 244 | "title": "William Shakespeare", 245 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 246 | "username": "xuelin", 247 | "time": "2017.04.12", 248 | "imageName": "d", 249 | "height":340 250 | }, 251 | { 252 | "title": "You know my loneliness", 253 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 254 | "username": "xuelin", 255 | "time": "2017.04.16", 256 | "imageName": "e", 257 | "height":517.5 258 | }, 259 | { 260 | "title": "Life isn't about waiting for the storm to pass,", 261 | "content": "If living on the earth is a mission from the lord… living with you is the award of the lord…", 262 | "username": "xuelin", 263 | "time": "2017.04.16", 264 | "imageName": "f", 265 | "height":200 266 | }, 267 | { 268 | "title": "North", 269 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 270 | "username": "xuelin", 271 | "time": "2017.04.17", 272 | "imageName": "g", 273 | "height":200 274 | }, 275 | { 276 | "title": "Worth", 277 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 278 | "username": "xuelin", 279 | "time": "2017.04.12", 280 | "imageName": "h", 281 | "height":395 282 | }, 283 | { 284 | "title": "William Shakespeare", 285 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 286 | "username": "xuelin", 287 | "time": "2017.04.16", 288 | "imageName": "", 289 | "height":0 290 | }, 291 | { 292 | "title": "Life is ten percent what you make it and ninety percent how you take it.", 293 | "content": "Love makes you stupid : whispers of love is a bunch of stupid, a love letter is silly talk, valentine is a little fool.", 294 | "username": "xuelin", 295 | "time": "2017.04.16", 296 | "imageName": "j", 297 | "height":100 298 | }, 299 | { 300 | "title": "I love three things in this world. Sun, moon and you. Sun for morning, moon for night , and you forever.", 301 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 302 | "username": "xuelin", 303 | "time": "2017.04.17", 304 | "imageName": "", 305 | "height":0 306 | }, 307 | { 308 | "title": "I love three things in this world. Sun, moon and you. Sun for morning, moon for night , and you forever.", 309 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 310 | "username": "xuelin", 311 | "time": "2017.04.16", 312 | "imageName": "", 313 | "height":0 314 | }, 315 | ] 316 | } 317 | -------------------------------------------------------------------------------- /SDDemo/Resource/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Resource/demo.gif -------------------------------------------------------------------------------- /SDDemo/Resource/item.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Resource/item.png -------------------------------------------------------------------------------- /SDDemo/Resource/qq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/Resource/qq.png -------------------------------------------------------------------------------- /SDDemo/SDMacro.h: -------------------------------------------------------------------------------- 1 | // 2 | // SDMacro.h 3 | // SDDemo 4 | // 5 | // Created by xuelin on 2019/2/27. 6 | // Copyright © 2019 xuelin. All rights reserved. 7 | // 8 | 9 | #ifndef SDMacro_h 10 | #define SDMacro_h 11 | 12 | #define kScreenWidth [UIScreen mainScreen].bounds.size.width 13 | #define kScreenHeight [UIScreen mainScreen].bounds.size.height 14 | 15 | #endif /* SDMacro_h */ 16 | -------------------------------------------------------------------------------- /SDDemo/SDPrefixHeader.pch: -------------------------------------------------------------------------------- 1 | // 2 | // SDPrefixHeader.pch 3 | // SDDemo 4 | // 5 | // Created by xuelin on 2019/2/27. 6 | // Copyright © 2019 xuelin. All rights reserved. 7 | // 8 | 9 | #ifndef SDPrefixHeader_pch 10 | #define SDPrefixHeader_pch 11 | 12 | #import "SDMacro.h" 13 | #import "Masonry.h" 14 | 15 | #endif /* SDPrefixHeader_pch */ 16 | -------------------------------------------------------------------------------- /SDDemo/TableViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // TableViewController.h 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TableViewController : UITableViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /SDDemo/TableViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // TableViewController.m 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import "TableViewController.h" 10 | #import "BaiduTableViewController.h" 11 | #import "SDAnimationControllerViewController.h" 12 | #import "SDItemLayoutViewController.h" 13 | 14 | @interface TableViewController () 15 | 16 | @end 17 | 18 | @implementation TableViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | self.navigationItem.title = @"花田"; 23 | } 24 | 25 | 26 | #pragma mark - Table view data source 27 | 28 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 29 | 30 | return 3; 31 | } 32 | 33 | 34 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 35 | UITableViewCell *cell = [UITableViewCell new]; 36 | if (indexPath.row == 0) { 37 | cell.textLabel.text = @"花田小憩"; 38 | } else if(indexPath.row == 1){ 39 | cell.textLabel.text = @"递归算法的补位动画"; 40 | } else { 41 | cell.textLabel.text = @"一个Item自排的算法"; 42 | } 43 | cell.imageView.image = [UIImage imageNamed:@"qq"]; 44 | cell.imageView.layer.cornerRadius = 76; 45 | 46 | return cell; 47 | } 48 | 49 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 50 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; 51 | if (indexPath.row == 0) { 52 | [self.navigationController pushViewController:[SDTableViewController new] animated:YES]; 53 | } else if(indexPath.row == 1){ 54 | [self.navigationController pushViewController:[SDAnimationControllerViewController new] animated:YES]; 55 | } else { 56 | [self.navigationController pushViewController:[SDItemLayoutViewController new] animated:YES]; 57 | } 58 | 59 | } 60 | 61 | 62 | @end 63 | -------------------------------------------------------------------------------- /SDDemo/Tools/HTTPManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // HTTPManager.h 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface HTTPManager : NSURLSession 12 | 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /SDDemo/Tools/HTTPManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // HTTPManager.m 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. All rights reserved. 7 | // 8 | 9 | #import "HTTPManager.h" 10 | 11 | @implementation HTTPManager 12 | 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /SDDemo/listView.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xlsd/SDDemo/e050287925179ac643a98d464fc3a0e08801606c/SDDemo/listView.gif -------------------------------------------------------------------------------- /SDDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // SDDemo 4 | // 5 | // Created by 薛林 on 2017/4/27. 6 | // Copyright © 2017年 xuelin. 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 | --------------------------------------------------------------------------------