Prechádzať zdrojové kódy

Add `flipsHorizontallyInOppositeLayoutDirection` mirroring support in ColletionViewLayout.

This scenario is used in RTL environment.
Frank 1 rok pred
rodič
commit
75bafbe025

+ 3 - 0
Examples/MJRefreshExample/MJRefreshExample.xcodeproj/project.pbxproj

@@ -73,6 +73,7 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
+		012CECBB2CF80CF8002C71E3 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/MJRefresh_i18n.strings; sourceTree = "<group>"; };
 		0133FBB82693F7E200E98F66 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MJRefresh_i18n.strings"; sourceTree = "<group>"; };
 		0133FBB92693F7EA00E98F66 /* zh-Hant */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hant"; path = "zh-Hant.lproj/MJRefresh_i18n.strings"; sourceTree = "<group>"; };
 		0133FBBB2693F80400E98F66 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/MJRefresh_i18n.strings; sourceTree = "<group>"; };
@@ -406,6 +407,7 @@
 				ru,
 				uk,
 				ko,
+				ar,
 			);
 			mainGroup = 2DA7F91C1AA6B4C4005627AB;
 			productRefGroup = 2DA7F9261AA6B4C4005627AB /* Products */;
@@ -516,6 +518,7 @@
 				0133FBBB2693F80400E98F66 /* ru */,
 				0133FBBC2693F81700E98F66 /* uk */,
 				0133FBBD2693F81F00E98F66 /* ko */,
+				012CECBB2CF80CF8002C71E3 /* ar */,
 			);
 			name = MJRefresh_i18n.strings;
 			sourceTree = "<group>";

+ 1 - 0
Examples/MJRefreshExample/MJRefreshExample/Classes/Second/MJCollectionViewController.m

@@ -9,6 +9,7 @@
 #import "MJCollectionViewController.h"
 #import "MJTestViewController.h"
 #import "UIViewController+Example.h"
+#import "MJChiBaoziHeader.h"
 
 @import MJRefresh;
 

+ 4 - 0
Examples/MJRefreshExample/MJRefreshExample/Classes/Second/MJPinHeaderCollectionViewController.swift

@@ -29,6 +29,10 @@ class MJPinHeaderCollectionViewController: MJCollectionViewController {
 
 @objcMembers
 class PinHeaderFlowLayout: UICollectionViewFlowLayout {
+    override var flipsHorizontallyInOppositeLayoutDirection: Bool {
+        true
+    }
+    
     override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
         let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, with: indexPath)
         // TODO: Implementation of PinHeader

BIN
Examples/MJRefreshExample/MJRefreshExample/Classes/i18n/ar.lproj/MJRefresh_i18n.strings


+ 7 - 0
MJRefresh/Base/MJRefreshComponent.h

@@ -95,6 +95,13 @@ typedef void (^MJRefreshComponentAction)(void);
 /** 父控件 */
 @property (weak, nonatomic, readonly) UIScrollView *scrollView;
 
+/// 当在 CollectionView 中使用 CollectionViewLayout 中的 flipsHorizontallyInOppositeLayoutDirection 时, 会反向 View 内容. 这里列出的 view 会被自动反向回来.
+@property (nonatomic, readonly) NSArray<UIView *> *flipsHorizontallyInOppositeLayoutDirectionViews;
+
+/// 当在 CollectionView 中使用 CollectionViewLayout 中的 flipsHorizontallyInOppositeLayoutDirection 时, 会反向 View 内容. 这里列出的 view 会被自动反向回来. 默认为空
+/// - Attention: 增量内容, 不会覆盖 MJRefresh 中的 flipsHorizontallyInOppositeLayoutDirectionViews
+@property (nonatomic, readonly) NSArray<UIView *> *additionalFlipsViews;
+
 #pragma mark - 交给子类们去实现
 /** 初始化 */
 - (void)prepare NS_REQUIRES_SUPER;

+ 13 - 0
MJRefresh/Base/MJRefreshComponent.m

@@ -46,6 +46,19 @@ - (void)layoutSubviews
     [self placeSubviews];
     
     [super layoutSubviews];
+    
+    mj_cguard([self.scrollView isKindOfClass:UICollectionView.class]) else { return; }
+        
+    UICollectionView *collectionView = (UICollectionView *)self.scrollView;
+    mj_cguard(collectionView.collectionViewLayout.flipsHorizontallyInOppositeLayoutDirection) else { return; }
+    
+    CGAffineTransform flipTransform = CGAffineTransformMakeScale(-1, 1);
+    for (UIView *view in self.flipsHorizontallyInOppositeLayoutDirectionViews) {
+        view.transform = flipTransform;
+    }
+    for (UIView *view in self.additionalFlipsViews) {
+        view.transform = flipTransform;
+    }
 }
 
 - (void)placeSubviews{}

+ 7 - 0
MJRefresh/Custom/Footer/Auto/MJRefreshAutoGifFooter.m

@@ -117,5 +117,12 @@ - (void)setState:(MJRefreshState)state
         self.gifView.hidden = YES;
     }
 }
+
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return [super.flipsHorizontallyInOppositeLayoutDirectionViews arrayByAddingObjectsFromArray:@[
+        self.gifView
+    ]];
+}
+
 @end
 

+ 6 - 0
MJRefresh/Custom/Footer/Auto/MJRefreshAutoStateFooter.m

@@ -116,4 +116,10 @@ - (void)setState:(MJRefreshState)state
         self.stateLabel.text = self.stateTitles[@(state)];
     }
 }
+
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return @[
+        self.stateLabel
+    ];
+}
 @end

+ 6 - 0
MJRefresh/Custom/Footer/Back/MJRefreshBackGifFooter.m

@@ -129,4 +129,10 @@ - (void)setState:(MJRefreshState)state
         self.gifView.hidden = YES;
     }
 }
+
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return [super.flipsHorizontallyInOppositeLayoutDirectionViews arrayByAddingObjectsFromArray:@[
+        self.gifView
+    ]];
+}
 @end

+ 6 - 0
MJRefresh/Custom/Footer/Back/MJRefreshBackNormalFooter.m

@@ -129,4 +129,10 @@ - (void)setState:(MJRefreshState)state
     }
 }
 
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return [super.flipsHorizontallyInOppositeLayoutDirectionViews arrayByAddingObjectsFromArray:@[
+        self.arrowView
+    ]];
+}
+
 @end

+ 6 - 0
MJRefresh/Custom/Footer/Back/MJRefreshBackStateFooter.m

@@ -90,4 +90,10 @@ - (void)setState:(MJRefreshState)state
     // 设置状态文字
     self.stateLabel.text = self.stateTitles[@(state)];
 }
+
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return @[
+        self.stateLabel
+    ];
+}
 @end

+ 6 - 0
MJRefresh/Custom/Header/MJRefreshGifHeader.m

@@ -132,4 +132,10 @@ - (void)setState:(MJRefreshState)state
         [self.gifView stopAnimating];
     }
 }
+
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return [super.flipsHorizontallyInOppositeLayoutDirectionViews arrayByAddingObjectsFromArray:@[
+        self.gifView
+    ]];
+}
 @end

+ 6 - 0
MJRefresh/Custom/Header/MJRefreshNormalHeader.m

@@ -134,4 +134,10 @@ - (void)setState:(MJRefreshState)state
         self.arrowView.hidden = YES;
     }
 }
+
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return [super.flipsHorizontallyInOppositeLayoutDirectionViews arrayByAddingObjectsFromArray:@[
+        self.arrowView
+    ]];
+}
 @end

+ 6 - 0
MJRefresh/Custom/Header/MJRefreshStateHeader.m

@@ -178,6 +178,12 @@ - (void)setState:(MJRefreshState)state
     // 重新设置key(重新显示时间)
     self.lastUpdatedTimeKey = self.lastUpdatedTimeKey;
 }
+
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return @[
+        self.stateLabel, self.lastUpdatedTimeLabel
+    ];
+}
 @end
 
 #pragma mark - <<< 为 Swift 扩展链式语法 >>> -

+ 5 - 1
MJRefresh/Custom/Trailer/MJRefreshNormalTrailer.m

@@ -75,6 +75,10 @@ - (void)setState:(MJRefreshState)state {
     }
 }
 
-
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return [super.flipsHorizontallyInOppositeLayoutDirectionViews arrayByAddingObjectsFromArray:@[
+        self.arrowView
+    ]];
+}
 
 @end

+ 6 - 0
MJRefresh/Custom/Trailer/MJRefreshStateTrailer.m

@@ -84,4 +84,10 @@ - (void)placeSubviews {
     }
 }
 
+- (NSArray<UIView *> *)flipsHorizontallyInOppositeLayoutDirectionViews {
+    return @[
+        self.stateLabel
+    ];
+}
+
 @end

+ 4 - 0
MJRefresh/MJRefreshConst.h

@@ -13,6 +13,10 @@
 #define MJRefreshLog(...)
 #endif
 
+#ifndef mj_cguard
+#define mj_cguard(X) if (X);
+#endif
+
 // 过期提醒
 #define MJRefreshDeprecated(DESCRIPTION) __attribute__((deprecated(DESCRIPTION)))
 

+ 8 - 0
README.md

@@ -10,6 +10,7 @@
 ## Contents
 
 - New Features
+    - [Support Flips in CollectionViewLayout](#Support_Flips_In_CollectionViewLayout)
     - [Dynamic i18n Switching](#dynamic_i18n_switching)
     - [SPM Supported](#spm_supported)
     - [Swift Chaining Grammar Supported](#swift_chaining_grammar_supported)
@@ -49,6 +50,13 @@
 * [Hope](#Hope)
 
 ## New Features
+
+### <a id="SupportFlipsInCollectionViewLayout"></a>Support Flips In CollectionViewLayout
+
+> Refer to [issue #1616](https://github.com/CoderMJLee/MJRefresh/issues/1616).
+- Use `additionalFlipsViews` to append views that need to flip when collectionViewLayout flipsHorizontallyInOppositeLayoutDirection is `true`.
+- Use `flipsHorizontallyInOppositeLayoutDirectionViews` to modify views as the same way above. **⚠️ BUT parent class may be affect. Use `additionalFlipsViews` instead.**
+
 ### <a id="dynamic_i18n_switching"></a>Dynamic i18n Switching
 
 Now `MJRefresh components` will be rerendered automatically with `MJRefreshConfig.default.language` setting.