Преглед изворни кода

添加 UICollectionViewLeftAlignedLayout

南鑫林 пре 6 година
родитељ
комит
dff50e43ab

+ 6 - 4
RainbowPlanet/RainbowPlanet.xcodeproj/project.pbxproj

@@ -46,7 +46,6 @@
 		A70DD1202294CA2700C802FD /* SwiftRXMoyaReamlObjectSwiftJsonNetWorkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70DD11F2294CA2700C802FD /* SwiftRXMoyaReamlObjectSwiftJsonNetWorkManager.swift */; };
 		A70EBBC022561179000AD74F /* Extension+UITableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70EBBBF22561179000AD74F /* Extension+UITableView.swift */; };
 		A7146349228EFCE20066099B /* ORSKUDataFilter.m in Sources */ = {isa = PBXBuildFile; fileRef = A7146347228EFCE20066099B /* ORSKUDataFilter.m */; };
-		A714634E228F020A0066099B /* ORSKUDataFilterSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = A714634D228F020A0066099B /* ORSKUDataFilterSwift.swift */; };
 		A715333E22980CC60017C861 /* ConfigModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A715333D22980CC60017C861 /* ConfigModel.swift */; };
 		A71738A622897892000AEA6A /* SwiftMoyaServiceShopApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = A71738A522897892000AEA6A /* SwiftMoyaServiceShopApi.swift */; };
 		A71738A822897E96000AEA6A /* SwiftMoyaNetWorkServiceShop.swift in Sources */ = {isa = PBXBuildFile; fileRef = A71738A722897E96000AEA6A /* SwiftMoyaNetWorkServiceShop.swift */; };
@@ -159,6 +158,7 @@
 		A738D66F225D9BD900EEE860 /* UMLoginModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A738D66E225D9BD900EEE860 /* UMLoginModel.swift */; };
 		A73911AD229CD62F0033177E /* AlamofireReachabilityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73911AC229CD62F0033177E /* AlamofireReachabilityManager.swift */; };
 		A73911B0229E4D970033177E /* PGYManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73911AF229E4D970033177E /* PGYManager.swift */; };
+		A73911B3229ECE340033177E /* UICollectionViewLeftAlignedLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = A73911B1229ECE340033177E /* UICollectionViewLeftAlignedLayout.m */; };
 		A73D7C682268A032002A4CE3 /* SwiftyStarRatingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73D7C672268A032002A4CE3 /* SwiftyStarRatingView.swift */; };
 		A75414FD224B5F28002480B5 /* MobileLoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A75414FB224B5F28002480B5 /* MobileLoginViewController.swift */; };
 		A7541502224C5ECB002480B5 /* BaiduMapManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7541501224C5ECA002480B5 /* BaiduMapManager.swift */; };
@@ -433,7 +433,6 @@
 		A70EBBBF22561179000AD74F /* Extension+UITableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Extension+UITableView.swift"; sourceTree = "<group>"; };
 		A7146347228EFCE20066099B /* ORSKUDataFilter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ORSKUDataFilter.m; sourceTree = "<group>"; };
 		A7146348228EFCE20066099B /* ORSKUDataFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ORSKUDataFilter.h; sourceTree = "<group>"; };
-		A714634D228F020A0066099B /* ORSKUDataFilterSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ORSKUDataFilterSwift.swift; sourceTree = "<group>"; };
 		A715333D22980CC60017C861 /* ConfigModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigModel.swift; sourceTree = "<group>"; };
 		A71738A522897892000AEA6A /* SwiftMoyaServiceShopApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftMoyaServiceShopApi.swift; sourceTree = "<group>"; };
 		A71738A722897E96000AEA6A /* SwiftMoyaNetWorkServiceShop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftMoyaNetWorkServiceShop.swift; sourceTree = "<group>"; };
@@ -548,6 +547,8 @@
 		A738D66E225D9BD900EEE860 /* UMLoginModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UMLoginModel.swift; sourceTree = "<group>"; };
 		A73911AC229CD62F0033177E /* AlamofireReachabilityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlamofireReachabilityManager.swift; sourceTree = "<group>"; };
 		A73911AF229E4D970033177E /* PGYManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PGYManager.swift; sourceTree = "<group>"; };
+		A73911B1229ECE340033177E /* UICollectionViewLeftAlignedLayout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UICollectionViewLeftAlignedLayout.m; sourceTree = "<group>"; };
+		A73911B2229ECE340033177E /* UICollectionViewLeftAlignedLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UICollectionViewLeftAlignedLayout.h; sourceTree = "<group>"; };
 		A73D7C672268A032002A4CE3 /* SwiftyStarRatingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyStarRatingView.swift; sourceTree = "<group>"; };
 		A75414FB224B5F28002480B5 /* MobileLoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MobileLoginViewController.swift; sourceTree = "<group>"; };
 		A7541501224C5ECA002480B5 /* BaiduMapManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaiduMapManager.swift; sourceTree = "<group>"; };
@@ -1109,7 +1110,8 @@
 			children = (
 				A7146348228EFCE20066099B /* ORSKUDataFilter.h */,
 				A7146347228EFCE20066099B /* ORSKUDataFilter.m */,
-				A714634D228F020A0066099B /* ORSKUDataFilterSwift.swift */,
+				A73911B2229ECE340033177E /* UICollectionViewLeftAlignedLayout.h */,
+				A73911B1229ECE340033177E /* UICollectionViewLeftAlignedLayout.m */,
 			);
 			path = SKUDataFilter;
 			sourceTree = "<group>";
@@ -3610,6 +3612,7 @@
 				A71AA52622732068008FF1A5 /* SwiftMoyaServiceConfigApi.swift in Sources */,
 				A77F2C982231FD25001BD3F6 /* BaseNavigationViewController.swift in Sources */,
 				A7274C5D228EE636000E3A07 /* LBXScanViewStyle.swift in Sources */,
+				A73911B3229ECE340033177E /* UICollectionViewLeftAlignedLayout.m in Sources */,
 				A7A98E1B227EEE49005306E9 /* SpecialView.swift in Sources */,
 				BDF47D7A228271F600941AB9 /* ShoppingCartView.swift in Sources */,
 				A7824AFB2271EA2600ABA381 /* SelfMentionContactsListView.swift in Sources */,
@@ -3658,7 +3661,6 @@
 				A7284440224DFACD00F82F30 /* InfoModel.swift in Sources */,
 				A72A72B522321DE000B21995 /* Extension+UITextView.swift in Sources */,
 				BDF47D822282B3D100941AB9 /* ShoppingCartHotSaleTableViewCell.swift in Sources */,
-				A714634E228F020A0066099B /* ORSKUDataFilterSwift.swift in Sources */,
 				BD20F1CB2283C12000677D8E /* ShoppingCartListNoneItemCell.swift in Sources */,
 				A7B4E756228281620012914A /* ShoppingMallSlidingLeftRightBgCollectionViewCell.swift in Sources */,
 				A757C92E22744ACE00226355 /* AddressManagerListModel.swift in Sources */,

+ 1 - 0
RainbowPlanet/RainbowPlanet/Macro/RainbowPlanet-Bridging-Header.h

@@ -22,6 +22,7 @@
 #import <LYEmptyView/LYEmptyViewHeader.h>
 
 #import "ORSKUDataFilter.h"
+#import "UICollectionViewLeftAlignedLayout.h"
 
 #import "FMDB.h"
 

+ 2 - 2
RainbowPlanet/RainbowPlanet/Modules/CommonModules/SearchModule/Search/View/SearchView.swift

@@ -47,8 +47,8 @@ class SearchView: BaseView {
         return collectionView
     }()
     
-    private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
-        let collectionViewLayout = UICollectionViewFlowLayout.init()
+    private lazy var collectionViewLayout: UICollectionViewLeftAlignedLayout = {
+        let collectionViewLayout = UICollectionViewLeftAlignedLayout.init()
         collectionViewLayout.minimumLineSpacing = 10
         collectionViewLayout.minimumInteritemSpacing =  10
         collectionViewLayout.sectionInset = UIEdgeInsets(top: 10, left: 14, bottom: 10, right: 14)

+ 2 - 0
RainbowPlanet/RainbowPlanet/Modules/MineModule/AddressManager/View/AddressManagerSelfMentionExpressHeaderView.swift

@@ -72,6 +72,8 @@ class AddressManagerSelfMentionExpressHeaderView: BaseView {
         didSet {
             if self.section == 1 {
                 managerButton.setTitle("新建地址", for: UIControl.State.normal)
+                managerButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.right, imageTitleSpace: 4)
+
             }
         }
     }

+ 1 - 1
RainbowPlanet/RainbowPlanet/Modules/MineModule/AddressManager/View/AddressManagerSelfMentionTableViewCell.swift

@@ -62,7 +62,6 @@ class AddressManagerSelfMentionTableViewCell: UITableViewCell {
     
     private lazy var selectedButton: UIButton = {
         let selectedButton = UIButton(type: UIButton.ButtonType.custom)
-//        selectedButton.setImage(kImage(name: "common_uncheck_icon"), for: UIControl.State.normal)
         selectedButton.setImage(kImage(name: "common_check_icon"), for: UIControl.State.normal)
         selectedButton.setImage(kImage(name: "common_check_icon"), for: UIControl.State.selected)
         return selectedButton
@@ -73,6 +72,7 @@ class AddressManagerSelfMentionTableViewCell: UITableViewCell {
         addressLabel.text = "曲江华府智能柜机"
         addressLabel.textColor = k333333Color
         addressLabel.font = kMediumFont16
+        addressLabel.numberOfLines = 0
         return addressLabel
     }()
     

+ 2 - 2
RainbowPlanet/RainbowPlanet/Modules/ShoppingMallModule/ProductDetail/View/ProductDetailSkuView/ProductDetailSkuView.swift

@@ -210,8 +210,8 @@ class ProductDetailSkuView: FWPopupView {
         return collectionView
     }()
     
-    private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
-        let collectionViewLayout = UICollectionViewFlowLayout.init()
+    private lazy var collectionViewLayout: UICollectionViewLeftAlignedLayout = {
+        let collectionViewLayout = UICollectionViewLeftAlignedLayout.init()
         collectionViewLayout.minimumLineSpacing = 10
         collectionViewLayout.minimumInteritemSpacing = 10
         collectionViewLayout.estimatedItemSize = CGSize(width: 72, height: 25)

+ 0 - 13
RainbowPlanet/RainbowPlanet/Tools/SKUDataFilter/ORSKUDataFilterSwift.swift

@@ -1,13 +0,0 @@
-//
-//  ORSKUDataFilterSwift.swift
-//  RainbowPlanet
-//
-//  Created by 南鑫林 on 2019/5/17.
-//  Copyright © 2019 RainbowPlanet. All rights reserved.
-//
-
-import UIKit
-
-class ORSKUDataFilterSwift: NSObject {
-
-}

+ 40 - 0
RainbowPlanet/RainbowPlanet/Tools/SKUDataFilter/UICollectionViewLeftAlignedLayout.h

@@ -0,0 +1,40 @@
+
+// Copyright (c) 2014 Giovanni Lodi
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy of
+// this software and associated documentation files (the "Software"), to deal in
+// the Software without restriction, including without limitation the rights to
+// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+// the Software, and to permit persons to whom the Software is furnished to do so,
+// subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+/**
+ *  Simple UICollectionViewFlowLayout that aligns the cells to the left rather than justify them
+ *
+ *  Based on http://stackoverflow.com/questions/13017257/how-do-you-determine-spacing-between-cells-in-uicollectionview-flowlayout
+ */
+
+#import <UIKit/UIKit.h>
+
+@interface UICollectionViewLeftAlignedLayout : UICollectionViewFlowLayout
+
+@end
+
+/**
+ *  Just a convenience protocol to keep things consistent.
+ *  Someone could find it confusing for a delegate object to conform to UICollectionViewDelegateFlowLayout
+ *  while using UICollectionViewLeftAlignedLayout.
+ */
+@protocol UICollectionViewDelegateLeftAlignedLayout <UICollectionViewDelegateFlowLayout>
+
+@end

+ 117 - 0
RainbowPlanet/RainbowPlanet/Tools/SKUDataFilter/UICollectionViewLeftAlignedLayout.m

@@ -0,0 +1,117 @@
+
+// Copyright (c) 2014 Giovanni Lodi
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy of
+// this software and associated documentation files (the "Software"), to deal in
+// the Software without restriction, including without limitation the rights to
+// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+// the Software, and to permit persons to whom the Software is furnished to do so,
+// subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+#import "UICollectionViewLeftAlignedLayout.h"
+
+@interface UICollectionViewLayoutAttributes (LeftAligned)
+
+- (void)leftAlignFrameWithSectionInset:(UIEdgeInsets)sectionInset;
+
+@end
+
+@implementation UICollectionViewLayoutAttributes (LeftAligned)
+
+- (void)leftAlignFrameWithSectionInset:(UIEdgeInsets)sectionInset
+{
+    CGRect frame = self.frame;
+    frame.origin.x = sectionInset.left;
+    self.frame = frame;
+}
+
+@end
+
+#pragma mark -
+
+@implementation UICollectionViewLeftAlignedLayout
+
+#pragma mark - UICollectionViewLayout
+
+- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
+    NSArray *originalAttributes = [super layoutAttributesForElementsInRect:rect];
+    NSMutableArray *updatedAttributes = [NSMutableArray arrayWithArray:originalAttributes];
+    for (UICollectionViewLayoutAttributes *attributes in originalAttributes) {
+        if (!attributes.representedElementKind) {
+            NSUInteger index = [updatedAttributes indexOfObject:attributes];
+            updatedAttributes[index] = [self layoutAttributesForItemAtIndexPath:attributes.indexPath];
+        }
+    }
+
+    return updatedAttributes;
+}
+
+- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
+    UICollectionViewLayoutAttributes* currentItemAttributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];
+    UIEdgeInsets sectionInset = [self evaluatedSectionInsetForItemAtIndex:indexPath.section];
+
+    BOOL isFirstItemInSection = indexPath.item == 0;
+    CGFloat layoutWidth = CGRectGetWidth(self.collectionView.frame) - sectionInset.left - sectionInset.right;
+
+    if (isFirstItemInSection) {
+        [currentItemAttributes leftAlignFrameWithSectionInset:sectionInset];
+        return currentItemAttributes;
+    }
+
+    NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
+    CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
+    CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width;
+    CGRect currentFrame = currentItemAttributes.frame;
+    CGRect strecthedCurrentFrame = CGRectMake(sectionInset.left,
+                                              currentFrame.origin.y,
+                                              layoutWidth,
+                                              currentFrame.size.height);
+    // if the current frame, once left aligned to the left and stretched to the full collection view
+    // widht intersects the previous frame then they are on the same line
+    BOOL isFirstItemInRow = !CGRectIntersectsRect(previousFrame, strecthedCurrentFrame);
+
+    if (isFirstItemInRow) {
+        // make sure the first item on a line is left aligned
+        [currentItemAttributes leftAlignFrameWithSectionInset:sectionInset];
+        return currentItemAttributes;
+    }
+
+    CGRect frame = currentItemAttributes.frame;
+    frame.origin.x = previousFrameRightPoint + [self evaluatedMinimumInteritemSpacingForSectionAtIndex:indexPath.section];
+    currentItemAttributes.frame = frame;
+    return currentItemAttributes;
+}
+
+- (CGFloat)evaluatedMinimumInteritemSpacingForSectionAtIndex:(NSInteger)sectionIndex
+{
+    if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
+        id<UICollectionViewDelegateLeftAlignedLayout> delegate = (id<UICollectionViewDelegateLeftAlignedLayout>)self.collectionView.delegate;
+
+        return [delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:sectionIndex];
+    } else {
+        return self.minimumInteritemSpacing;
+    }
+}
+
+- (UIEdgeInsets)evaluatedSectionInsetForItemAtIndex:(NSInteger)index
+{
+    if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
+        id<UICollectionViewDelegateLeftAlignedLayout> delegate = (id<UICollectionViewDelegateLeftAlignedLayout>)self.collectionView.delegate;
+
+        return [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:index];
+    } else {
+        return self.sectionInset;
+    }
+}
+
+@end