From 8dd330fd4a06f3a92824f384414e29a3810df14b Mon Sep 17 00:00:00 2001 From: zhoujianping Date: Wed, 11 Sep 2024 17:14:30 +0800 Subject: [PATCH] Dynamically adapting RTL layout --- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++ .../project.pbxproj | 6 +++ .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++ .../TYCyclePagerView/TYCyclePagerView.h | 6 +++ .../TYCyclePagerView/TYCyclePagerView.m | 38 ++++++++++++++++++- .../TYRTLCyclePagerTransformLayout.h | 17 +++++++++ .../TYRTLCyclePagerTransformLayout.m | 23 +++++++++++ 7 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 TYCyclePagerView.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 TYCyclePagerViewDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 TYCyclePagerViewDemo/TYCyclePagerView/TYRTLCyclePagerTransformLayout.h create mode 100644 TYCyclePagerViewDemo/TYCyclePagerView/TYRTLCyclePagerTransformLayout.m diff --git a/TYCyclePagerView.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/TYCyclePagerView.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/TYCyclePagerView.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/TYCyclePagerViewDemo.xcodeproj/project.pbxproj b/TYCyclePagerViewDemo.xcodeproj/project.pbxproj index d763e83..3270ff6 100644 --- a/TYCyclePagerViewDemo.xcodeproj/project.pbxproj +++ b/TYCyclePagerViewDemo.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 00EF9DC72C9195A80048F754 /* TYRTLCyclePagerTransformLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 00EF9DC62C9195A80048F754 /* TYRTLCyclePagerTransformLayout.m */; }; 2B9C0EF61F0B2F32009BC0BD /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B9C0EF51F0B2F32009BC0BD /* main.m */; }; 2B9C0EF91F0B2F32009BC0BD /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B9C0EF81F0B2F32009BC0BD /* AppDelegate.m */; }; 2B9C0EFC1F0B2F32009BC0BD /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B9C0EFB1F0B2F32009BC0BD /* ViewController.m */; }; @@ -31,6 +32,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 00EF9DC52C9195A80048F754 /* TYRTLCyclePagerTransformLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TYRTLCyclePagerTransformLayout.h; sourceTree = ""; }; + 00EF9DC62C9195A80048F754 /* TYRTLCyclePagerTransformLayout.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TYRTLCyclePagerTransformLayout.m; sourceTree = ""; }; 2B9C0EF11F0B2F32009BC0BD /* TYCyclePagerViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TYCyclePagerViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 2B9C0EF51F0B2F32009BC0BD /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 2B9C0EF71F0B2F32009BC0BD /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; @@ -133,6 +136,8 @@ 2B9C0F1D1F0B2F62009BC0BD /* TYCyclePagerView.m */, 2B9C0F1A1F0B2F62009BC0BD /* TYCyclePagerTransformLayout.h */, 2B9C0F1B1F0B2F62009BC0BD /* TYCyclePagerTransformLayout.m */, + 00EF9DC52C9195A80048F754 /* TYRTLCyclePagerTransformLayout.h */, + 00EF9DC62C9195A80048F754 /* TYRTLCyclePagerTransformLayout.m */, 2B9C0F1E1F0B2F62009BC0BD /* TYPageControl.h */, 2B9C0F1F1F0B2F62009BC0BD /* TYPageControl.m */, ); @@ -249,6 +254,7 @@ 2B9C0EF91F0B2F32009BC0BD /* AppDelegate.m in Sources */, 2B9C0F251F0B2F96009BC0BD /* TYCyclePagerViewCell.m in Sources */, 2B9C0F201F0B2F62009BC0BD /* TYCyclePagerTransformLayout.m in Sources */, + 00EF9DC72C9195A80048F754 /* TYRTLCyclePagerTransformLayout.m in Sources */, 2B9C0EF61F0B2F32009BC0BD /* main.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/TYCyclePagerViewDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/TYCyclePagerViewDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/TYCyclePagerViewDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/TYCyclePagerViewDemo/TYCyclePagerView/TYCyclePagerView.h b/TYCyclePagerViewDemo/TYCyclePagerView/TYCyclePagerView.h index 1f2899b..c249813 100755 --- a/TYCyclePagerViewDemo/TYCyclePagerView/TYCyclePagerView.h +++ b/TYCyclePagerViewDemo/TYCyclePagerView/TYCyclePagerView.h @@ -85,6 +85,12 @@ typedef NS_ENUM(NSUInteger, TYPagerScrollDirection) { @interface TYCyclePagerView : UIView +/// initialize and determine RTL Adaptation +- (instancetype)initWithFrame:(CGRect)frame isAutoRTL:(BOOL)isAutoRTL; + +/// is use RTL Adaptation +@property (nonatomic, assign) BOOL isAutoRTL; + // will be automatically resized to track the size of the pagerView @property (nonatomic, strong, nullable) UIView *backgroundView; diff --git a/TYCyclePagerViewDemo/TYCyclePagerView/TYCyclePagerView.m b/TYCyclePagerViewDemo/TYCyclePagerView/TYCyclePagerView.m index a2f42d5..c006b94 100755 --- a/TYCyclePagerViewDemo/TYCyclePagerView/TYCyclePagerView.m +++ b/TYCyclePagerViewDemo/TYCyclePagerView/TYCyclePagerView.m @@ -7,6 +7,7 @@ // #import "TYCyclePagerView.h" +#import "TYRTLCyclePagerTransformLayout.h" NS_INLINE BOOL TYEqualIndexSection(TYIndexSection indexSection1,TYIndexSection indexSection2) { return indexSection1.index == indexSection2.index && indexSection1.section == indexSection2.section; @@ -58,6 +59,15 @@ @implementation TYCyclePagerView #pragma mark - life Cycle +- (instancetype)initWithFrame:(CGRect)frame isAutoRTL:(BOOL)isAutoRTL { + if (self = [super initWithFrame:frame]) { + [self configureProperty]; + _isAutoRTL = isAutoRTL; + [self addCollectionView]; + } + return self; +} + - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self configureProperty]; @@ -77,6 +87,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder { } - (void)configureProperty { + _isAutoRTL = NO; _needResetIndex = NO; _didReloadData = NO; _didLayout = NO; @@ -90,7 +101,12 @@ - (void)configureProperty { } - (void)addCollectionView { - TYCyclePagerTransformLayout *layout = [[TYCyclePagerTransformLayout alloc]init]; + TYCyclePagerTransformLayout *layout; + if (self.isAutoRTL) { + layout = [[TYRTLCyclePagerTransformLayout alloc] init]; + } else { + layout = [[TYCyclePagerTransformLayout alloc] init]; + } UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout]; layout.delegate = _delegateFlags.applyTransformToAttributes ? self : nil;; collectionView.backgroundColor = [UIColor clearColor]; @@ -202,6 +218,26 @@ - (NSArray *)visibleIndexs { #pragma mark - setter +- (void)setIsAutoRTL:(BOOL)isAutoRTL { + if (_isAutoRTL == isAutoRTL) return; + _isAutoRTL = isAutoRTL; + + TYCyclePagerTransformLayout *newLayout; + if (isAutoRTL) { + newLayout = [[TYRTLCyclePagerTransformLayout alloc] init]; + } else { + newLayout = [[TYCyclePagerTransformLayout alloc] init]; + } + + TYCyclePagerTransformLayout *oldLayout = (TYCyclePagerTransformLayout *)self.collectionView.collectionViewLayout; + oldLayout.delegate = nil; + + newLayout.layout = oldLayout.layout; + + self.collectionView.collectionViewLayout = newLayout; + newLayout.delegate = _delegateFlags.applyTransformToAttributes ? self : nil; +} + - (void)setBackgroundView:(UIView *)backgroundView { [_collectionView setBackgroundView:backgroundView]; } diff --git a/TYCyclePagerViewDemo/TYCyclePagerView/TYRTLCyclePagerTransformLayout.h b/TYCyclePagerViewDemo/TYCyclePagerView/TYRTLCyclePagerTransformLayout.h new file mode 100644 index 0000000..c62a108 --- /dev/null +++ b/TYCyclePagerViewDemo/TYCyclePagerView/TYRTLCyclePagerTransformLayout.h @@ -0,0 +1,17 @@ +// +// TYRTLCyclePagerTransformLayout.h +// TYCyclePagerViewDemo +// +// Created by aa on 2024/9/11. +// Copyright © 2024 tany. All rights reserved. +// + +#import "TYCyclePagerTransformLayout.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface TYRTLCyclePagerTransformLayout : TYCyclePagerTransformLayout + +@end + +NS_ASSUME_NONNULL_END diff --git a/TYCyclePagerViewDemo/TYCyclePagerView/TYRTLCyclePagerTransformLayout.m b/TYCyclePagerViewDemo/TYCyclePagerView/TYRTLCyclePagerTransformLayout.m new file mode 100644 index 0000000..562b338 --- /dev/null +++ b/TYCyclePagerViewDemo/TYCyclePagerView/TYRTLCyclePagerTransformLayout.m @@ -0,0 +1,23 @@ +// +// TYRTLCyclePagerTransformLayout.m +// TYCyclePagerViewDemo +// +// Created by aa on 2024/9/11. +// Copyright © 2024 tany. All rights reserved. +// + +#import "TYRTLCyclePagerTransformLayout.h" + +@implementation TYRTLCyclePagerTransformLayout + +- (BOOL)flipsHorizontallyInOppositeLayoutDirection { + UIWindow *window = [UIApplication sharedApplication].delegate.window; + if (!window) { + return UIUserInterfaceLayoutDirectionLeftToRight; + } + + UIUserInterfaceLayoutDirection layoutDirection = [UIView userInterfaceLayoutDirectionForSemanticContentAttribute:window.semanticContentAttribute]; + return layoutDirection == UIUserInterfaceLayoutDirectionRightToLeft; +} + +@end