From 1466db5a8b1e70341e8e1d48dafea9d83e41c516 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sun, 18 Sep 2016 08:22:43 +0900 Subject: [PATCH] Use page data model to generate each pages. --- .../project.pbxproj | 82 +++++- .../RDImageViewerController.xcscheme | 2 +- .../RDViewController.m | 172 ++++------- .../RDScrollViewPageContentData.h | 17 ++ .../RDScrollViewPageContentData.m | 62 ++++ .../SampleDataClass/RDViewPageContentData.h | 17 ++ .../SampleDataClass/RDViewPageContentData.m | 60 ++++ .../RDImageViewerController.h | 20 +- .../RDImageViewerController.m | 274 +++--------------- .../RDImageContentData/RDImageContentData.h | 21 ++ .../RDImageContentData/RDImageContentData.m | 90 ++++++ .../RDPageContentData/RDPageContentData.h | 23 ++ .../RDPageContentData/RDPageContentData.m | 41 +++ .../RDRemoteImageContentData.h | 19 ++ .../RDRemoteImageContentData.m | 98 +++++++ Pod/Classes/RDPagingView/RDPagingView.h | 2 +- Pod/Classes/RDPagingView/RDPagingView.m | 16 +- RDImageViewerController.podspec | 2 +- 18 files changed, 635 insertions(+), 383 deletions(-) create mode 100644 Example/RDImageViewerController/SampleDataClass/RDScrollViewPageContentData.h create mode 100644 Example/RDImageViewerController/SampleDataClass/RDScrollViewPageContentData.m create mode 100644 Example/RDImageViewerController/SampleDataClass/RDViewPageContentData.h create mode 100644 Example/RDImageViewerController/SampleDataClass/RDViewPageContentData.m create mode 100644 Pod/Classes/RDPageContentData/RDImageContentData/RDImageContentData.h create mode 100644 Pod/Classes/RDPageContentData/RDImageContentData/RDImageContentData.m create mode 100644 Pod/Classes/RDPageContentData/RDPageContentData.h create mode 100644 Pod/Classes/RDPageContentData/RDPageContentData.m create mode 100644 Pod/Classes/RDPageContentData/RDRemoteImageContentData/RDRemoteImageContentData.h create mode 100644 Pod/Classes/RDPageContentData/RDRemoteImageContentData/RDRemoteImageContentData.m diff --git a/Example/RDImageViewerController.xcodeproj/project.pbxproj b/Example/RDImageViewerController.xcodeproj/project.pbxproj index 930a0e8..947d32d 100644 --- a/Example/RDImageViewerController.xcodeproj/project.pbxproj +++ b/Example/RDImageViewerController.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ 6003F5BA195388D20070C39A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6003F5B8195388D20070C39A /* InfoPlist.strings */; }; 6003F5BC195388D20070C39A /* Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6003F5BB195388D20070C39A /* Tests.m */; }; E3A3A445DE0F421C9D66D081 /* libPods-Tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4ADF8DB7BCBB4D47A97E8F2C /* libPods-Tests.a */; }; + E90C11501D8E022000630F12 /* RDViewPageContentData.m in Sources */ = {isa = PBXBuildFile; fileRef = E90C114F1D8E022000630F12 /* RDViewPageContentData.m */; }; + E90C11531D8E022800630F12 /* RDScrollViewPageContentData.m in Sources */ = {isa = PBXBuildFile; fileRef = E90C11521D8E022800630F12 /* RDScrollViewPageContentData.m */; }; E90E268A1A34981000F1F54A /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = E90E26881A34981000F1F54A /* LaunchScreen.xib */; }; E919D29119D8005C003F4BD0 /* RDImageScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = E919D29019D8005C003F4BD0 /* RDImageScrollView.m */; }; E919D29619D80683003F4BD0 /* RDPagingView.m in Sources */ = {isa = PBXBuildFile; fileRef = E919D29519D80683003F4BD0 /* RDPagingView.m */; }; @@ -37,6 +39,9 @@ E919D2AA19D82F8E003F4BD0 /* 8.JPG in Resources */ = {isa = PBXBuildFile; fileRef = E919D2A019D82F8E003F4BD0 /* 8.JPG */; }; E919D2AB19D82F8E003F4BD0 /* 9.JPG in Resources */ = {isa = PBXBuildFile; fileRef = E919D2A119D82F8E003F4BD0 /* 9.JPG */; }; E928DBE01AB2011C00594AAD /* rd_M2DURLConnectionOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = E928DBDF1AB2011C00594AAD /* rd_M2DURLConnectionOperation.m */; }; + E9437D751D8DF4BC003DC413 /* RDPageContentData.m in Sources */ = {isa = PBXBuildFile; fileRef = E9437D741D8DF4BC003DC413 /* RDPageContentData.m */; }; + E9437D781D8DF59A003DC413 /* RDRemoteImageContentData.m in Sources */ = {isa = PBXBuildFile; fileRef = E9437D771D8DF59A003DC413 /* RDRemoteImageContentData.m */; }; + E9437D7B1D8DF5A4003DC413 /* RDImageContentData.m in Sources */ = {isa = PBXBuildFile; fileRef = E9437D7A1D8DF5A4003DC413 /* RDImageContentData.m */; }; E96FFD8419CE7970008ADEDF /* RDImageViewerController.m in Sources */ = {isa = PBXBuildFile; fileRef = E96FFD8119CE7970008ADEDF /* RDImageViewerController.m */; }; E9B106601AB760DA000DEEE4 /* 11.JPG in Resources */ = {isa = PBXBuildFile; fileRef = E9B1065F1AB760DA000DEEE4 /* 11.JPG */; }; E9B106621AB76127000DEEE4 /* 12.JPG in Resources */ = {isa = PBXBuildFile; fileRef = E9B106611AB76127000DEEE4 /* 12.JPG */; }; @@ -80,6 +85,10 @@ 89025E39FA3270FE8EDDEEB7 /* RDImageViewerController.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = RDImageViewerController.podspec; path = ../RDImageViewerController.podspec; sourceTree = ""; }; 8908584DC4E04959ABA607A8 /* libPods-RDImageViewerController.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RDImageViewerController.a"; sourceTree = BUILT_PRODUCTS_DIR; }; E6BEC608C2E338A1EFB29850 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = ""; }; + E90C114E1D8E022000630F12 /* RDViewPageContentData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RDViewPageContentData.h; sourceTree = ""; }; + E90C114F1D8E022000630F12 /* RDViewPageContentData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RDViewPageContentData.m; sourceTree = ""; }; + E90C11511D8E022800630F12 /* RDScrollViewPageContentData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RDScrollViewPageContentData.h; sourceTree = ""; }; + E90C11521D8E022800630F12 /* RDScrollViewPageContentData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RDScrollViewPageContentData.m; sourceTree = ""; }; E90E26891A34981000F1F54A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; E919D28F19D8005C003F4BD0 /* RDImageScrollView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RDImageScrollView.h; sourceTree = ""; }; E919D29019D8005C003F4BD0 /* RDImageScrollView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RDImageScrollView.m; sourceTree = ""; }; @@ -97,6 +106,12 @@ E919D2A119D82F8E003F4BD0 /* 9.JPG */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 9.JPG; sourceTree = ""; }; E928DBDE1AB2011C00594AAD /* rd_M2DURLConnectionOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rd_M2DURLConnectionOperation.h; sourceTree = ""; }; E928DBDF1AB2011C00594AAD /* rd_M2DURLConnectionOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = rd_M2DURLConnectionOperation.m; sourceTree = ""; }; + E9437D731D8DF4BC003DC413 /* RDPageContentData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RDPageContentData.h; sourceTree = ""; }; + E9437D741D8DF4BC003DC413 /* RDPageContentData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RDPageContentData.m; sourceTree = ""; }; + E9437D761D8DF59A003DC413 /* RDRemoteImageContentData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RDRemoteImageContentData.h; sourceTree = ""; }; + E9437D771D8DF59A003DC413 /* RDRemoteImageContentData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RDRemoteImageContentData.m; sourceTree = ""; }; + E9437D791D8DF5A4003DC413 /* RDImageContentData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RDImageContentData.h; sourceTree = ""; }; + E9437D7A1D8DF5A4003DC413 /* RDImageContentData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RDImageContentData.m; sourceTree = ""; }; E96FFD8019CE7970008ADEDF /* RDImageViewerController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RDImageViewerController.h; sourceTree = ""; }; E96FFD8119CE7970008ADEDF /* RDImageViewerController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RDImageViewerController.m; sourceTree = ""; }; E9B1065F1AB760DA000DEEE4 /* 11.JPG */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 11.JPG; sourceTree = ""; }; @@ -174,6 +189,7 @@ 6003F59D195388D20070C39A /* RDAppDelegate.m */, 6003F5A5195388D20070C39A /* RDViewController.h */, 6003F5A6195388D20070C39A /* RDViewController.m */, + E90C114D1D8E020D00630F12 /* SampleDataClass */, 6003F5A8195388D20070C39A /* Images.xcassets */, 6003F594195388D20070C39A /* Supporting Files */, ); @@ -222,6 +238,17 @@ name = "Podspec Metadata"; sourceTree = ""; }; + E90C114D1D8E020D00630F12 /* SampleDataClass */ = { + isa = PBXGroup; + children = ( + E90C114E1D8E022000630F12 /* RDViewPageContentData.h */, + E90C114F1D8E022000630F12 /* RDViewPageContentData.m */, + E90C11511D8E022800630F12 /* RDScrollViewPageContentData.h */, + E90C11521D8E022800630F12 /* RDScrollViewPageContentData.m */, + ); + path = SampleDataClass; + sourceTree = ""; + }; E919D28E19D80053003F4BD0 /* RDImageScrollView */ = { isa = PBXGroup; children = ( @@ -268,9 +295,39 @@ path = M2DURLConnectionOperation; sourceTree = ""; }; + E9437D701D8DF414003DC413 /* RDPageContentData */ = { + isa = PBXGroup; + children = ( + E9437D711D8DF414003DC413 /* RDImageContentData */, + E9437D721D8DF414003DC413 /* RDRemoteImageContentData */, + E9437D731D8DF4BC003DC413 /* RDPageContentData.h */, + E9437D741D8DF4BC003DC413 /* RDPageContentData.m */, + ); + path = RDPageContentData; + sourceTree = ""; + }; + E9437D711D8DF414003DC413 /* RDImageContentData */ = { + isa = PBXGroup; + children = ( + E9437D791D8DF5A4003DC413 /* RDImageContentData.h */, + E9437D7A1D8DF5A4003DC413 /* RDImageContentData.m */, + ); + path = RDImageContentData; + sourceTree = ""; + }; + E9437D721D8DF414003DC413 /* RDRemoteImageContentData */ = { + isa = PBXGroup; + children = ( + E9437D761D8DF59A003DC413 /* RDRemoteImageContentData.h */, + E9437D771D8DF59A003DC413 /* RDRemoteImageContentData.m */, + ); + path = RDRemoteImageContentData; + sourceTree = ""; + }; E96FFD7A19CE7970008ADEDF /* Classes */ = { isa = PBXGroup; children = ( + E9437D701D8DF414003DC413 /* RDPageContentData */, E928DBDD1AB2011C00594AAD /* M2DURLConnectionOperation */, E919D29319D80679003F4BD0 /* RDPagingView */, E919D28E19D80053003F4BD0 /* RDImageScrollView */, @@ -346,7 +403,7 @@ isa = PBXProject; attributes = { CLASSPREFIX = RD; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Akira Matsuda"; TargetAttributes = { 6003F589195388D20070C39A = { @@ -480,10 +537,15 @@ files = ( E96FFD8419CE7970008ADEDF /* RDImageViewerController.m in Sources */, 6003F59E195388D20070C39A /* RDAppDelegate.m in Sources */, + E9437D781D8DF59A003DC413 /* RDRemoteImageContentData.m in Sources */, + E9437D7B1D8DF5A4003DC413 /* RDImageContentData.m in Sources */, E919D29119D8005C003F4BD0 /* RDImageScrollView.m in Sources */, 6003F5A7195388D20070C39A /* RDViewController.m in Sources */, + E90C11531D8E022800630F12 /* RDScrollViewPageContentData.m in Sources */, + E90C11501D8E022000630F12 /* RDViewPageContentData.m in Sources */, 6003F59A195388D20070C39A /* main.m in Sources */, E919D29619D80683003F4BD0 /* RDPagingView.m in Sources */, + E9437D751D8DF4BC003DC413 /* RDPageContentData.m in Sources */, E928DBE01AB2011C00594AAD /* rd_M2DURLConnectionOperation.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -555,14 +617,19 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", @@ -575,7 +642,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 7.1; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; @@ -595,20 +662,25 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = YES; ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 7.1; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; @@ -626,7 +698,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "RDImageViewerController/RDImageViewerController-Prefix.pch"; INFOPLIST_FILE = "RDImageViewerController/RDImageViewerController-Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; @@ -646,7 +718,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "RDImageViewerController/RDImageViewerController-Prefix.pch"; INFOPLIST_FILE = "RDImageViewerController/RDImageViewerController-Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; diff --git a/Example/RDImageViewerController.xcodeproj/xcshareddata/xcschemes/RDImageViewerController.xcscheme b/Example/RDImageViewerController.xcodeproj/xcshareddata/xcschemes/RDImageViewerController.xcscheme index 6f3be98..ee02869 100644 --- a/Example/RDImageViewerController.xcodeproj/xcshareddata/xcschemes/RDImageViewerController.xcscheme +++ b/Example/RDImageViewerController.xcodeproj/xcshareddata/xcschemes/RDImageViewerController.xcscheme @@ -1,6 +1,6 @@ *contentData = [NSMutableArray new]; + for (NSInteger i = 0; i < kNumberOfImages; i++) { + NSString *imageName = [NSString stringWithFormat:@"%ld.JPG", (long)i + 1]; + RDImageContentData *data = [[RDImageContentData alloc] initWithImageName:imageName]; + [contentData addObject:data]; + } + RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithContentData:contentData direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; viewController.showSlider = sliderSwitch.on; viewController.showPageNumberHud = hudSwitch.on; - viewController.landscapeMode = RDImageScrollViewResizeModeAspectFit; viewController.preloadCount = kPreloadCount; viewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:viewController animated:YES]; @@ -67,16 +74,16 @@ - (IBAction)showImageAsAspectFit:(id)sender - (IBAction)showImageAsDisplayFit:(id)sender { - RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithImageHandler:^UIImage *(NSInteger pageIndex) { - NSString *imageName = [NSString stringWithFormat:@"%ld.JPG", (long)pageIndex + 1]; - return [UIImage imageNamed:imageName]; - } numberOfImages:kNumberOfImages direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; - [viewController setImageViewConfigurationHandler:^(NSInteger pageIndex, RDImageScrollView *imageView) { - imageView.borderColor = [UIColor redColor]; - }]; + NSMutableArray *contentData = [NSMutableArray new]; + for (NSInteger i = 0; i < kNumberOfImages; i++) { + NSString *imageName = [NSString stringWithFormat:@"%ld.JPG", (long)i + 1]; + RDImageContentData *data = [[RDImageContentData alloc] initWithImageName:imageName]; + data.landscapeMode = RDImageScrollViewResizeModeDisplayFit; + [contentData addObject:data]; + } + RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithContentData:contentData direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; viewController.showSlider = sliderSwitch.on; viewController.showPageNumberHud = hudSwitch.on; - viewController.landscapeMode = RDImageScrollViewResizeModeDisplayFit; viewController.preloadCount = kPreloadCount; viewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:viewController animated:YES]; @@ -84,30 +91,14 @@ - (IBAction)showImageAsDisplayFit:(id)sender - (IBAction)showView:(id)sender { - CGRect frame = self.view.bounds; - RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithViewHandler:^UIView *(NSString *reuseIdentifier, NSInteger pageIndex, UIView *reusedView) { - if (reusedView == nil) { - UIView *view = [[UIView alloc] initWithFrame:frame]; - UILabel *label = [[UILabel alloc] initWithFrame:frame]; - label.textAlignment = NSTextAlignmentCenter; - label.font = [UIFont systemFontOfSize:50]; - label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - label.tag = 100; - [view addSubview:label]; - view.backgroundColor = [UIColor whiteColor]; - reusedView = view; - } - - UILabel *label = (UILabel *)[reusedView viewWithTag:100]; - label.text = [NSString stringWithFormat:@"%ld", (long)pageIndex]; - - return reusedView; - } reuseIdentifier:^NSString *(NSInteger pageIndex) { - return @"view"; - } numberOfImages:kNumberOfImages direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; + NSMutableArray *contentData = [NSMutableArray new]; + for (NSInteger i = 0; i < kNumberOfImages; i++) { + RDViewPageContentData *data = [[RDViewPageContentData alloc] initWithString:[NSString stringWithFormat:@"%ld", (long)i + 1]]; + [contentData addObject:data]; + } + RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithContentData:contentData direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; viewController.showSlider = sliderSwitch.on; viewController.showPageNumberHud = hudSwitch.on; - viewController.landscapeMode = RDImageScrollViewResizeModeDisplayFit; viewController.preloadCount = kPreloadCount; viewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:viewController animated:YES]; @@ -115,85 +106,40 @@ - (IBAction)showView:(id)sender - (IBAction)showImageAsync:(id)sender { - RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithRemoteImageHandler:^NSURLRequest *(NSInteger pageIndex) { - NSLog(@"downloading...:%@", [array[pageIndex] absoluteString]); - return [NSURLRequest requestWithURL:array[pageIndex]]; - } numberOfImages:kNumberOfImages direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; - -// RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithImageHandler:^UIImage *(NSInteger pageIndex) { -// NSLog(@"downloading...:%@", [array[pageIndex] absoluteString]); -// NSURLResponse *response = nil; -// NSError *error = nil; -// NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:array[pageIndex]] returningResponse:&response error:&error]; -// UIImage *image = [UIImage imageWithData:data]; -// if (image == nil) { -// NSLog(@"error:%@", [error localizedDescription]); -// } -// else { -// NSLog(@"done:%@", response.URL.absoluteString); -// } -// -// return image; -// } numberOfImages:10 direction:self.directionSwitch.on ? RDPagingViewDirectionLeft : RDPagingViewDirectionRight]; - - viewController.autoBarsHiddenDuration = 1; + NSMutableArray *contentData = [NSMutableArray new]; + for (NSInteger i = 0; i < kNumberOfImages; i++) { + RDRemoteImageContentData *data = [[RDRemoteImageContentData alloc] initWithRequest:[NSURLRequest requestWithURL:array[i]] pageIndex:i]; + [contentData addObject:data]; + } + RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithContentData:contentData direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; viewController.showSlider = sliderSwitch.on; viewController.showPageNumberHud = hudSwitch.on; - viewController.landscapeMode = RDImageScrollViewResizeModeAspectFit; - viewController.loadAsync = YES; viewController.preloadCount = kPreloadCount; + viewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:viewController animated:YES]; } - (IBAction)showImageAndView:(id)sender { - RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithImageHandler:^UIImage *(NSInteger pageIndex) { - NSLog(@"downloading...:%@", [array[pageIndex] absoluteString]); - NSURLResponse *response = nil; - NSError *error = nil; - NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:array[pageIndex]] returningResponse:&response error:&error]; - UIImage *image = [UIImage imageWithData:data]; - if (image == nil) { - NSLog(@"error:%@", [error localizedDescription]); + NSMutableArray *contentData = [NSMutableArray new]; + for (NSInteger i = 0; i < kNumberOfImages; i++) { + if (i % 2) { + RDViewPageContentData *data = [[RDViewPageContentData alloc] initWithString:[NSString stringWithFormat:@"%ld", (long)i + 1]]; + [contentData addObject:data]; } else { - NSLog(@"done:%@", response.URL.absoluteString); - } - - return image; - } numberOfImages:kNumberOfImages direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; - CGRect frame = self.view.bounds; - [viewController setViewHandler:^UIView *(NSString *reuseIdentifier, NSInteger pageIndex, UIView *reusedView) { - if (reusedView == nil) { - UIView *view = [[UIView alloc] initWithFrame:frame]; - UILabel *label = [[UILabel alloc] initWithFrame:frame]; - label.textAlignment = NSTextAlignmentCenter; - label.font = [UIFont systemFontOfSize:50]; - label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - label.tag = 100; - [view addSubview:label]; - view.backgroundColor = [UIColor whiteColor]; - reusedView = view; - } - - UILabel *label = (UILabel *)[reusedView viewWithTag:100]; - label.text = [NSString stringWithFormat:@"%ld", (long)pageIndex]; - - return reusedView; - }]; - [viewController setReuseIdentifierHandler:^NSString *(NSInteger index) { - if (index % 2 == 0) { - return @"view"; + NSString *imageName = [NSString stringWithFormat:@"%ld.JPG", (long)i + 1]; + RDImageContentData *data = [[RDImageContentData alloc] initWithImageName:imageName]; + data.landscapeMode = RDImageScrollViewResizeModeDisplayFit; + [contentData addObject:data]; } - - return RDImageViewerControllerReuseIdentifierImage; - }]; + } + RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithContentData:contentData direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; [viewController setContentViewWillAppearHandler:^(NSInteger index, UIView *view) { NSLog(@"%ld %@", (long)index, [view description]); }]; viewController.showSlider = sliderSwitch.on; viewController.showPageNumberHud = hudSwitch.on; - viewController.landscapeMode = RDImageScrollViewResizeModeAspectFit; viewController.loadAsync = YES; viewController.preloadCount = kPreloadCount; viewController.hidesBottomBarWhenPushed = YES; @@ -202,31 +148,15 @@ - (IBAction)showImageAndView:(id)sender - (IBAction)showScrollView:(id)sender { - CGRect frame = self.view.bounds; - RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithViewHandler:^UIView *(NSString *reuseIdentifier, NSInteger pageIndex, UIView *reusedView) { - if (reusedView == nil) { - UIScrollView *view = [[UIScrollView alloc] initWithFrame:frame]; - UILabel *label = [[UILabel alloc] initWithFrame:frame]; - label.textAlignment = NSTextAlignmentCenter; - label.font = [UIFont systemFontOfSize:50]; - label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - label.tag = 100; - [view addSubview:label]; - view.backgroundColor = [UIColor whiteColor]; - view.contentSize = CGSizeMake(1000, 1000); - reusedView = view; - } - - UILabel *label = (UILabel *)[reusedView viewWithTag:100]; - label.text = [NSString stringWithFormat:@"%ld", (long)pageIndex]; - - return reusedView; - } reuseIdentifier:^NSString *(NSInteger pageIndex) { - return @"view"; - } numberOfImages:kNumberOfImages direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; + NSMutableArray *contentData = [NSMutableArray new]; + for (NSInteger i = 0; i < kNumberOfImages; i++) { + RDScrollViewPageContentData *data = [[RDScrollViewPageContentData alloc] initWithString:[NSString stringWithFormat:@"%ld", (long)i + 1]]; + [contentData addObject:data]; + } + RDImageViewerController *viewController = [[RDImageViewerController alloc] initWithContentData:contentData direction:self.directionSwitch.on ? RDPagingViewForwardDirectionLeft : RDPagingViewForwardDirectionRight]; viewController.showSlider = sliderSwitch.on; viewController.showPageNumberHud = hudSwitch.on; - viewController.landscapeMode = RDImageScrollViewResizeModeDisplayFit; + viewController.loadAsync = YES; viewController.preloadCount = kPreloadCount; viewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:viewController animated:YES]; diff --git a/Example/RDImageViewerController/SampleDataClass/RDScrollViewPageContentData.h b/Example/RDImageViewerController/SampleDataClass/RDScrollViewPageContentData.h new file mode 100644 index 0000000..e1c5ff7 --- /dev/null +++ b/Example/RDImageViewerController/SampleDataClass/RDScrollViewPageContentData.h @@ -0,0 +1,17 @@ +// +// RDScrollViewPageContentData.h +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDPageContentData.h" + +@interface RDScrollViewPageContentData : RDPageContentData + +@property (nonatomic, strong) NSString *labelString; + +- (instancetype)initWithString:(NSString *)string; + +@end diff --git a/Example/RDImageViewerController/SampleDataClass/RDScrollViewPageContentData.m b/Example/RDImageViewerController/SampleDataClass/RDScrollViewPageContentData.m new file mode 100644 index 0000000..3db3f36 --- /dev/null +++ b/Example/RDImageViewerController/SampleDataClass/RDScrollViewPageContentData.m @@ -0,0 +1,62 @@ +// +// RDScrollViewPageContentData.m +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDScrollViewPageContentData.h" + +@implementation RDScrollViewPageContentData + +- (instancetype)initWithString:(NSString *)string +{ + self = [super init]; + if (self) { + self.labelString = string; + } + + return self; +} + +#pragma mark - + ++ (UIView * _Nonnull)contentViewWithFrame:(CGRect)frame +{ + UIScrollView *view = [[UIScrollView alloc] initWithFrame:frame]; + UILabel *label = [[UILabel alloc] initWithFrame:frame]; + label.textAlignment = NSTextAlignmentCenter; + label.font = [UIFont systemFontOfSize:50]; + label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + label.tag = 100; + [view addSubview:label]; + view.backgroundColor = [UIColor whiteColor]; + view.contentSize = CGSizeMake(1000, 1000); + + return view; +} + +- (void)preload +{ + +} + +- (void)reload +{ + +} + +- (void)stopPreloading +{ + +} + +- (void)configureForView:(UIView * _Nonnull)view +{ + UILabel *label = (UILabel *)[view viewWithTag:100]; + label.text = self.labelString; +} + + +@end diff --git a/Example/RDImageViewerController/SampleDataClass/RDViewPageContentData.h b/Example/RDImageViewerController/SampleDataClass/RDViewPageContentData.h new file mode 100644 index 0000000..c45fb13 --- /dev/null +++ b/Example/RDImageViewerController/SampleDataClass/RDViewPageContentData.h @@ -0,0 +1,17 @@ +// +// RDViewPageContentData.h +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDPageContentData.h" + +@interface RDViewPageContentData : RDPageContentData + +@property (nonatomic, strong) NSString *labelString; + +- (instancetype)initWithString:(NSString *)string; + +@end diff --git a/Example/RDImageViewerController/SampleDataClass/RDViewPageContentData.m b/Example/RDImageViewerController/SampleDataClass/RDViewPageContentData.m new file mode 100644 index 0000000..1df01d7 --- /dev/null +++ b/Example/RDImageViewerController/SampleDataClass/RDViewPageContentData.m @@ -0,0 +1,60 @@ +// +// RDViewPageContentData.m +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDViewPageContentData.h" + +@implementation RDViewPageContentData + +- (instancetype)initWithString:(NSString *)string +{ + self = [super init]; + if (self) { + self.labelString = string; + } + + return self; +} + +#pragma mark - + ++ (UIView * _Nonnull)contentViewWithFrame:(CGRect)frame +{ + UIView *view = [[UIView alloc] initWithFrame:frame]; + UILabel *label = [[UILabel alloc] initWithFrame:frame]; + label.textAlignment = NSTextAlignmentCenter; + label.font = [UIFont systemFontOfSize:50]; + label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + label.tag = 100; + [view addSubview:label]; + view.backgroundColor = [UIColor whiteColor]; + + return view; +} + +- (void)preload +{ + +} + +- (void)reload +{ + +} + +- (void)stopPreloading +{ + +} + +- (void)configureForView:(UIView * _Nonnull)view +{ + UILabel *label = (UILabel *)[view viewWithTag:100]; + label.text = self.labelString; +} + +@end diff --git a/Pod/Classes/RDImageViewerController/RDImageViewerController.h b/Pod/Classes/RDImageViewerController/RDImageViewerController.h index 118b2a1..38e3be3 100755 --- a/Pod/Classes/RDImageViewerController/RDImageViewerController.h +++ b/Pod/Classes/RDImageViewerController/RDImageViewerController.h @@ -8,6 +8,7 @@ #import #import "RDPagingView.h" #import "RDImageScrollView.h" +#import "RDPageContentData.h" @class RDImageViewerController; @protocol RDImageViewerControllerDelegate @@ -30,30 +31,15 @@ extern NSString *const RDImageViewerControllerReuseIdentifierRemoteImage; @property (nonatomic, assign) BOOL restoreBarsState; @property (nonatomic, assign) BOOL pagingEnabled; @property (nonatomic, assign) BOOL loadAsync; -@property (nonatomic, assign) BOOL precedenceLatestImageLoadRequest; @property (nonatomic, assign) BOOL showSlider; @property (nonatomic, assign) BOOL showPageNumberHud; -@property (nonatomic, assign) RDImageScrollViewResizeMode landscapeMode; -@property (nonatomic, assign) CGFloat maximumZoomScale; @property (nonatomic, copy) UIColor *pageSliderMaximumTrackTintColor; @property (nonatomic, copy) UIColor *pageSliderMinimumTrackTintColor; @property (nonatomic, readonly) UILabel *currentPageHudLabel; -@property (nonatomic, copy) UIImage *(^imageHandler)(NSInteger pageIndex); -@property (nonatomic, copy) NSURLRequest *(^remoteImageHandler)(NSInteger pageIndex); -@property (nonatomic, copy) void (^requestCompletionHandler)(NSInteger pageIndex, NSURLResponse *response, NSData *data, NSError *connectionError); -@property (nonatomic, copy) UIImage *(^imageDecodeHandler)(NSData *data, NSInteger pageIndex); -@property (nonatomic, copy) void(^imageViewConfigurationHandler)(NSInteger pageIndex, RDImageScrollView *imageView); -@property (nonatomic, copy) UIView *(^viewHandler)(NSString *identifier, NSInteger pageIndex, UIView *reusedView); -@property (nonatomic, copy) NSString *(^reuseIdentifierHandler)(NSInteger pageIndex); -@property (nonatomic, copy) void(^reloadViewHandler)(NSString *identifier, NSInteger pageIndex, UIView *view); +@property (nonatomic, strong) NSArray *contentData; @property (nonatomic, copy) void(^contentViewWillAppearHandler)(NSInteger pageIndex, UIView *view); -@property (nonatomic, strong) NSURLSessionConfiguration *configuration; -- (instancetype)initWithNumberOfPages:(NSInteger)num direction:(RDPagingViewForwardDirection)direction; -- (instancetype)initWithImageHandler:(UIImage *(^)(NSInteger pageIndex))imageHandler numberOfImages:(NSInteger)pageCount direction:(RDPagingViewForwardDirection)direction; -- (instancetype)initWithViewHandler:(UIView *(^)(NSString *reuseIdentifier, NSInteger pageIndex, UIView *reusedView))viewHandler reuseIdentifier:(NSString *(^)(NSInteger pageIndex))reuseIdentifierHandler numberOfImages:(NSInteger)pageCount direction:(RDPagingViewForwardDirection)direction; -- (instancetype)initWithRemoteImageHandler:(NSURLRequest *(^)(NSInteger pageIndex))remoteImageHandler numberOfImages:(NSInteger)pageCount direction:(RDPagingViewForwardDirection)direction; -- (void)setRemoteImageHandler:(NSURLRequest *(^)(NSInteger pageIndex))remoteImageHandler completionHandler:(void (^)(NSInteger pageIndex, NSURLResponse *response, NSData *data, NSError *connectionError))completionHandler decodeHandler:(UIImage *(^)(NSData *data, NSInteger pageIndex))decodeHandler; +- (instancetype)initWithContentData:(NSArray *)contentData direction:(RDPagingViewForwardDirection)direction; - (void)setBarsHidden:(BOOL)hidden animated:(BOOL)animated; - (void)setHudHidden:(BOOL)hidden animated:(BOOL)animated; - (void)setPageHudNumberWithPageIndex:(NSInteger)pageIndex; diff --git a/Pod/Classes/RDImageViewerController/RDImageViewerController.m b/Pod/Classes/RDImageViewerController/RDImageViewerController.m index 4c9da4a..4158a01 100644 --- a/Pod/Classes/RDImageViewerController/RDImageViewerController.m +++ b/Pod/Classes/RDImageViewerController/RDImageViewerController.m @@ -7,6 +7,7 @@ #import "RDImageViewerController.h" #import +#import "RDImageScrollView.h" #import "rd_M2DURLConnectionOperation.h" typedef NS_ENUM(NSInteger, ViewTag) { @@ -28,11 +29,11 @@ typedef NS_ENUM(NSInteger, Direction) { @interface RDImageViewerController () { - RDImageViewerControllerDelegateFlag delegateFlag; BOOL statusBarHidden_; NSInteger previousPageIndex_; } +@property (nonatomic, assign) RDImageViewerControllerDelegateFlag delegateFlag; @property (nonatomic, strong) RDPagingView *pagingView; @property (nonatomic, strong) UIView *currentPageHud; @property (nonatomic, strong) NSOperationQueue *asynchronousImageHandlerQueue; @@ -40,6 +41,8 @@ @interface RDImageViewerController () @property (nonatomic, strong) NSMutableArray *remoteImageRequestArray; @property (nonatomic, strong) NSMutableArray *remoteImageRequestRunnintArray; +- (instancetype)initWithNumberOfPages:(NSInteger)num direction:(RDPagingViewForwardDirection)direction; + @end @implementation RDImageViewerController @@ -48,7 +51,6 @@ @implementation RDImageViewerController NSString *const RDImageViewerControllerReuseIdentifierRemoteImage = @"RDImageViewerControllerReuseIdentifierRemoteImage"; static NSInteger kPreloadDefaultCount = 1; -static CGFloat kDefaultMaximumZoomScale = 2.5; - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { @@ -128,13 +130,20 @@ - (instancetype)init return self; } +- (instancetype)initWithContentData:(NSArray *)contentData direction:(RDPagingViewForwardDirection)direction +{ + self = [self initWithNumberOfPages:contentData.count direction:direction]; + if (self) { + self.contentData = contentData; + } + + return self; +} + - (instancetype)initWithNumberOfPages:(NSInteger)num direction:(RDPagingViewForwardDirection)direction { self = [self init]; if (self) { - self.configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; - self.maximumZoomScale = kDefaultMaximumZoomScale; - self.landscapeMode = RDImageScrollViewResizeModeAspectFit; self.pagingView = [[RDPagingView alloc] initWithFrame:self.view.bounds]; self.pagingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.pagingView.backgroundColor = [UIColor blackColor]; @@ -156,37 +165,6 @@ - (instancetype)initWithNumberOfPages:(NSInteger)num direction:(RDPagingViewForw return self; } -- (instancetype)initWithImageHandler:(UIImage *(^)(NSInteger pageIndex))imageHandler numberOfImages:(NSInteger)pageCount direction:(RDPagingViewForwardDirection)direction -{ - self = [self initWithNumberOfPages:pageCount direction:direction]; - if (self) { - self.imageHandler = imageHandler; - } - - return self; -} - -- (instancetype)initWithRemoteImageHandler:(NSURLRequest *(^)(NSInteger pageIndex))remoteImageHandler numberOfImages:(NSInteger)pageCount direction:(RDPagingViewForwardDirection)direction -{ - self = [self initWithNumberOfPages:pageCount direction:direction]; - if (self) { - self.remoteImageHandler = remoteImageHandler; - } - - return self; -} - -- (instancetype)initWithViewHandler:(UIView *(^)(NSString *reuseIdentifier, NSInteger pageIndex, UIView *reusedView))viewHandler reuseIdentifier:(NSString *(^)(NSInteger pageIndex))reuseIdentifierHandler numberOfImages:(NSInteger)pageCount direction:(RDPagingViewForwardDirection)direction -{ - self = [self initWithNumberOfPages:pageCount direction:direction]; - if (self) { - self.viewHandler = viewHandler; - self.reuseIdentifierHandler = reuseIdentifierHandler; - } - - return self; -} - - (void)viewDidLoad { [super viewDidLoad]; @@ -289,7 +267,7 @@ - (void)viewDidDisappear:(BOOL)animated - (void)setDelegate:(id)delegate { _delegate = delegate; - delegateFlag.willChangeIndexTo = [_delegate respondsToSelector:@selector(imageViewerController:willChangeIndexTo:)]; + _delegateFlag.willChangeIndexTo = [_delegate respondsToSelector:@selector(imageViewerController:willChangeIndexTo:)]; } - (NSInteger)currentPageIndex @@ -364,13 +342,6 @@ - (BOOL)pagingEnabled #pragma mark - -- (void)setRemoteImageHandler:(NSURLRequest *(^)(NSInteger pageIndex))remoteImageHandler completionHandler:(void (^)(NSInteger pageIndex, NSURLResponse *response, NSData *data, NSError *connectionError))completionHandler decodeHandler:(UIImage *(^)(NSData *data, NSInteger pageIndex))decodeHandler -{ - self.remoteImageHandler = remoteImageHandler; - self.requestCompletionHandler = completionHandler; - self.imageDecodeHandler = decodeHandler; -} - - (void)hideBars { [self setBarsHidden:YES animated:YES]; @@ -418,9 +389,7 @@ - (void)setHudHidden:(BOOL)hidden animated:(BOOL)animated else { self.currentPageHud.alpha = !hidden * 0.8; } - - } completion:^(BOOL finished) { - }]; + } completion:nil]; } - (void)cancelAutoBarHidden @@ -445,225 +414,66 @@ - (void)sliderDidTouchUpInside:(id)sender [slider setValue:trueValue animated:YES]; } -- (RDImageScrollView *)imageScrollViewForIndex:(NSInteger)index reuseIdentifier:(NSString *)identifier -{ - RDImageScrollView *imageScrollView = (RDImageScrollView *)[self.pagingView dequeueViewWithReuseIdentifier:identifier]; - if (imageScrollView == nil) { - imageScrollView = [[RDImageScrollView alloc] initWithFrame:self.view.bounds]; - imageScrollView.maximumZoomScale = self.maximumZoomScale; - [self.pagingView.gestureRecognizers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - UIGestureRecognizer *gesture = obj; - if ([gesture isMemberOfClass:[UITapGestureRecognizer class]]) { - [imageScrollView addGestureRecognizerPriorityHigherThanZoomGestureRecogniser:gesture]; - *stop = YES; - } - }]; - } - imageScrollView.mode = self.landscapeMode; - [imageScrollView setZoomScale:1.0]; - imageScrollView.image = nil; - - [self loadImageAtIndex:index imageScrollView:imageScrollView reuseIdentifier:identifier]; - - return imageScrollView; -} - -- (void)loadImageAtIndex:(NSInteger)index imageScrollView:(RDImageScrollView *)imageScrollView reuseIdentifier:(NSString *)identifier -{ - imageScrollView.image = nil; - __weak typeof(self) bself = self; - __weak RDImageScrollView *bimagescrollView = imageScrollView; - if ([identifier isEqualToString:RDImageViewerControllerReuseIdentifierImage] && self.imageHandler) { - if (self.loadAsync) { - NSBlockOperation *op = [[NSBlockOperation alloc] init]; - [op addExecutionBlock:^{ - UIImage *image = self.imageHandler(index); - if (bimagescrollView.indexOfPage == index) { - dispatch_async(dispatch_get_main_queue(), ^{ - bimagescrollView.image = image; - if (bself.imageViewConfigurationHandler) { - bself.imageViewConfigurationHandler(index, bimagescrollView); - } - }); - } - }]; - [self.asynchronousImageHandlerQueue addOperation:op]; - } - else { - bimagescrollView.image = _imageHandler(index); - if (self.imageViewConfigurationHandler) { - self.imageViewConfigurationHandler(index, imageScrollView); - } - } - } - else if ([identifier isEqualToString:RDImageViewerControllerReuseIdentifierRemoteImage] && self.remoteImageHandler) { - __weak typeof(self) bself = self; - __weak NSMutableArray *bremoteImageRequestRunnintArray = self.remoteImageRequestRunnintArray; - NSURLRequest *request = self.remoteImageHandler(index); - rd_M2DURLConnectionOperation *op = [[rd_M2DURLConnectionOperation alloc] initWithRequest:request completeBlock:^(rd_M2DURLConnectionOperation *operation, NSURLResponse *response, NSData *data, NSError *error) { - if (bself.requestCompletionHandler) { - bself.requestCompletionHandler(index, response, data, error); - } - - UIImage *image = nil; - if (bself.imageDecodeHandler) { - image = bself.imageDecodeHandler(data, index); - } - else { - image = [[UIImage alloc] initWithData:data]; - } - if (bimagescrollView.indexOfPage == index) { - dispatch_async(dispatch_get_main_queue(), ^{ - bimagescrollView.image = image; - if (bself.imageViewConfigurationHandler) { - bself.imageViewConfigurationHandler(index, bimagescrollView); - } - }); - } - @synchronized (bself) { - [bremoteImageRequestRunnintArray removeObject:operation]; - [bself popImageOperation]; - } - }]; - op.configuration = self.configuration; - [self pushImageOperation:op]; - } - [bimagescrollView adjustContentAspect]; -} - -- (void)pushImageOperation:(rd_M2DURLConnectionOperation *)op -{ - @synchronized (self) { - NSInteger waitingOperationCount = self.remoteImageRequestArray.count - self.preloadCount; - if (waitingOperationCount > 0) { - for (NSInteger i = 0; i < waitingOperationCount; i++) { - rd_M2DURLConnectionOperation *op = self.remoteImageRequestArray[i]; - [op stop]; - [self.remoteImageRequestArray removeObject:op]; - } - } - [self.remoteImageRequestArray addObject:op]; - if (self.remoteImageRequestRunnintArray.count <= self.preloadCount * 2) { - [self popImageOperation]; - } - } -} - -- (void)popImageOperation -{ - @synchronized (self) { - rd_M2DURLConnectionOperation *op = self.remoteImageRequestArray.firstObject; - [self.remoteImageRequestArray removeObject:op]; - if (op) { - [self.remoteImageRequestRunnintArray addObject:op]; - [op sendRequest]; - } - } -} - -- (UIView *)contentViewForIndex:(NSInteger)index -{ - NSString *identifier = nil; - if (self.reuseIdentifierHandler) { - identifier = self.reuseIdentifierHandler(index); - } - - UIView *view = [self.pagingView dequeueViewWithReuseIdentifier:identifier]; - - return self.viewHandler(identifier, index, view); -} - - (void)reloadViewAtIndex:(NSInteger)index { + RDPageContentData *data = self.contentData[index]; + [data reload]; UIView *view = [self.pagingView viewForIndex:index]; - if (self.reuseIdentifierHandler) { - NSString *reuseIdentifier = self.reuseIdentifierHandler(index); - if ([reuseIdentifier isEqualToString:RDImageViewerControllerReuseIdentifierImage] || [reuseIdentifier isEqualToString:RDImageViewerControllerReuseIdentifierRemoteImage]) { - [self loadImageAtIndex:index imageScrollView:(RDImageScrollView *)view reuseIdentifier:reuseIdentifier]; - } - } - else { - NSString *reuseIdentifier = nil; - if (self.imageHandler) { - reuseIdentifier = RDImageViewerControllerReuseIdentifierImage; - } - else if (self.remoteImageHandler) { - reuseIdentifier = RDImageViewerControllerReuseIdentifierRemoteImage; - } - - if (reuseIdentifier != nil) { - [self loadImageAtIndex:index imageScrollView:(RDImageScrollView *)view reuseIdentifier:reuseIdentifier]; - } - else { - self.reloadViewHandler(reuseIdentifier, index, view); - } - } + [data configureForView:view]; } #pragma mark - RDPagingViewDelegate - (void)pagingView:(RDPagingView *)pagingView willChangeIndexTo:(NSInteger)index { - if (delegateFlag.willChangeIndexTo) { + if (_delegateFlag.willChangeIndexTo) { [self.delegate imageViewerController:self willChangeIndexTo:index]; } } - (UIView *)pagingView:(RDPagingView *)pageView viewForIndex:(NSInteger)index { - UIView *view = nil; - if (self.reuseIdentifierHandler) { - NSString *reuseIdentifier = self.reuseIdentifierHandler(index); - if ([reuseIdentifier isEqualToString:RDImageViewerControllerReuseIdentifierImage] || [reuseIdentifier isEqualToString:RDImageViewerControllerReuseIdentifierRemoteImage]) { - view = [self imageScrollViewForIndex:index reuseIdentifier:reuseIdentifier]; - } - else { - view = [self contentViewForIndex:index]; - } - } - else { - if (self.imageHandler) { - view = [self imageScrollViewForIndex:index reuseIdentifier:RDImageViewerControllerReuseIdentifierImage]; - } - else if (self.remoteImageHandler) { - view = [self imageScrollViewForIndex:index reuseIdentifier:RDImageViewerControllerReuseIdentifierRemoteImage]; - } - else { - view = [self contentViewForIndex:index]; - } + RDPageContentData *data = self.contentData[index]; + UIView *view = [[data class] contentViewWithFrame:CGRectMake(0, 0, CGRectGetWidth(pageView.bounds), CGRectGetHeight(pageView.bounds))]; + [data preload]; + [data configureForView:view]; + if ([view isKindOfClass:[RDImageScrollView class]]) { + RDImageScrollView *imageScrollView = (RDImageScrollView *)view; + [self.pagingView.gestureRecognizers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + UIGestureRecognizer *gesture = obj; + if ([gesture isMemberOfClass:[UITapGestureRecognizer class]]) { + [imageScrollView addGestureRecognizerPriorityHigherThanZoomGestureRecogniser:gesture]; + *stop = YES; + } + }]; } - if (self.contentViewWillAppearHandler) { self.contentViewWillAppearHandler(index, view); } - + return view; } - (NSString *)paginView:(RDPagingView *)paginView reuseIdentifierForIndex:(NSInteger)index { - NSString *identifier = RDImageViewerControllerReuseIdentifierImage; - if (self.reuseIdentifierHandler) { - identifier = self.reuseIdentifierHandler(index); - } - - return identifier; + RDPageContentData *data = self.contentData[index]; + return NSStringFromClass([data class]); } - (void)pagingView:(RDPagingView *)pagingView willChangeViewSize:(CGSize)size duration:(NSTimeInterval)duration visibleViews:(NSArray *)views { [views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) { - if (view.indexOfPage != pagingView.currentPageIndex) { + if (view.pageIndex != pagingView.currentPageIndex) { view.hidden = YES; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ view.hidden = NO; }); } if (RDPagingViewForwardDirectionVertical(pagingView.direction)) { - view.frame = CGRectMake((pagingView.direction == RDPagingViewForwardDirectionRight ? view.indexOfPage : (pagingView.numberOfPages - view.indexOfPage - 1)) * size.width, 0, size.width, size.height); + view.frame = CGRectMake((pagingView.direction == RDPagingViewForwardDirectionRight ? view.pageIndex : (pagingView.numberOfPages - view.pageIndex - 1)) * size.width, 0, size.width, size.height); } else { - view.frame = CGRectMake(0, (pagingView.direction == RDPagingViewForwardDirectionDown ? view.indexOfPage : (pagingView.numberOfPages - view.indexOfPage - 1)) * size.height, size.width, size.height); + view.frame = CGRectMake(0, (pagingView.direction == RDPagingViewForwardDirectionDown ? view.pageIndex : (pagingView.numberOfPages - view.pageIndex - 1)) * size.height, size.width, size.height); } if ([view isKindOfClass:[UIScrollView class]]) { @@ -675,6 +485,12 @@ - (void)pagingView:(RDPagingView *)pagingView willChangeViewSize:(CGSize)size du }]; } +- (void)pagingView:(RDPagingView *)pagingView willViewEnqueue:(UIView *)view +{ + RDPageContentData *data = self.contentData[view.pageIndex]; + [data stopPreloading]; +} + - (void)pagingView:(RDPagingView *)pagingView didScrollToPosition:(CGFloat)position { UISlider *slider = _pageSlider; diff --git a/Pod/Classes/RDPageContentData/RDImageContentData/RDImageContentData.h b/Pod/Classes/RDPageContentData/RDImageContentData/RDImageContentData.h new file mode 100644 index 0000000..b19b408 --- /dev/null +++ b/Pod/Classes/RDPageContentData/RDImageContentData/RDImageContentData.h @@ -0,0 +1,21 @@ +// +// RDImageContentData.h +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDPageContentData.h" +#import "RDImageScrollView.h" + +@interface RDImageContentData : RDPageContentData + +@property (nonatomic, assign) CGFloat maximumZoomScale; +@property (nonatomic, assign) RDImageScrollViewResizeMode landscapeMode; +@property (nonatomic, strong) UIImage *image; + +- (instancetype)initWithImage:(UIImage *)image; +- (instancetype)initWithImageName:(NSString *)imageName; + +@end diff --git a/Pod/Classes/RDPageContentData/RDImageContentData/RDImageContentData.m b/Pod/Classes/RDPageContentData/RDImageContentData/RDImageContentData.m new file mode 100644 index 0000000..18d90fa --- /dev/null +++ b/Pod/Classes/RDPageContentData/RDImageContentData/RDImageContentData.m @@ -0,0 +1,90 @@ +// +// RDImageContentData.m +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDImageContentData.h" + +static CGFloat kDefaultMaximumZoomScale = 2.5; + +@interface RDImageContentData () + +@property (nonatomic, strong) NSString *imageName; + +@end + +@implementation RDImageContentData + +- (instancetype)init +{ + self = [super init]; + if (self) { + self.maximumZoomScale = kDefaultMaximumZoomScale; + self.landscapeMode = RDImageScrollViewResizeModeAspectFit; + } + + return self; +} + +- (instancetype)initWithImage:(UIImage *)image +{ + self = [self init]; + if (self) { + self.image = image; + } + + return self; +} + +- (instancetype)initWithImageName:(NSString *)imageName +{ + self = [self init]; + if (self) { + self.imageName = imageName; + } + + return self; +} + +#pragma mark - RDPageContentDataDelegate + ++ (UIView * _Nonnull)contentViewWithFrame:(CGRect)frame +{ + RDImageScrollView *view = [[RDImageScrollView alloc] initWithFrame:frame]; + + return view; +} + +- (void)preload +{ + if (self.image == nil) { + self.image = [UIImage imageNamed:self.imageName]; + } +} + +- (void)reload +{ + if (self.imageName) { + self.image = nil; + [self preload]; + } +} + +- (void)stopPreloading +{ + +} + +- (void)configureForView:(UIView * _Nonnull)view +{ + RDImageScrollView *imageView = (RDImageScrollView *)view; + imageView.maximumZoomScale = self.maximumZoomScale; + imageView.mode = self.landscapeMode; + [imageView setZoomScale:1.0]; + imageView.image = self.image; +} + +@end diff --git a/Pod/Classes/RDPageContentData/RDPageContentData.h b/Pod/Classes/RDPageContentData/RDPageContentData.h new file mode 100644 index 0000000..fe1e407 --- /dev/null +++ b/Pod/Classes/RDPageContentData/RDPageContentData.h @@ -0,0 +1,23 @@ +// +// RDPageContentData.h +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import + +@protocol RDPageContentDataDelegate + ++ (UIView * _Nonnull)contentViewWithFrame:(CGRect)frame; +- (void)preload; +- (void)stopPreloading; +- (void)reload; +- (void)configureForView:(UIView * _Nonnull)view; + +@end + +@interface RDPageContentData : NSObject + +@end diff --git a/Pod/Classes/RDPageContentData/RDPageContentData.m b/Pod/Classes/RDPageContentData/RDPageContentData.m new file mode 100644 index 0000000..d82ad15 --- /dev/null +++ b/Pod/Classes/RDPageContentData/RDPageContentData.m @@ -0,0 +1,41 @@ +// +// RDPageContentData.m +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDPageContentData.h" + +@implementation RDPageContentData + ++ (UIView * _Nonnull)contentViewWithFrame:(CGRect)frame +{ + UIView *view = [[UIView alloc] initWithFrame:frame]; + @throw [NSException exceptionWithName:NSGenericException reason:@"You have to override this method." userInfo:nil]; + + return view; +} + +- (void)preload +{ + @throw [NSException exceptionWithName:NSGenericException reason:@"You have to override this method." userInfo:nil]; +} + +- (void)reload +{ + @throw [NSException exceptionWithName:NSGenericException reason:@"You have to override this method." userInfo:nil]; +} + +- (void)stopPreloading +{ + @throw [NSException exceptionWithName:NSGenericException reason:@"You have to override this method." userInfo:nil]; +} + +- (void)configureForView:(UIView * _Nonnull)view +{ + @throw [NSException exceptionWithName:NSGenericException reason:@"You have to override this method." userInfo:nil]; +} + +@end diff --git a/Pod/Classes/RDPageContentData/RDRemoteImageContentData/RDRemoteImageContentData.h b/Pod/Classes/RDPageContentData/RDRemoteImageContentData/RDRemoteImageContentData.h new file mode 100644 index 0000000..05a7e43 --- /dev/null +++ b/Pod/Classes/RDPageContentData/RDRemoteImageContentData/RDRemoteImageContentData.h @@ -0,0 +1,19 @@ +// +// RDRemoteImageContentData.h +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDImageContentData.h" + +@interface RDRemoteImageContentData : RDImageContentData + +@property (nonatomic, strong) NSURLSessionConfiguration *configuration; +@property (nonatomic, copy) void (^requestCompletionHandler)(NSURLResponse *response, NSData *data, NSError *connectionError); +@property (nonatomic, copy) UIImage *(^imageDecodeHandler)(NSData *data); + +- (instancetype)initWithRequest:(NSURLRequest *)request pageIndex:(NSInteger)index; + +@end diff --git a/Pod/Classes/RDPageContentData/RDRemoteImageContentData/RDRemoteImageContentData.m b/Pod/Classes/RDPageContentData/RDRemoteImageContentData/RDRemoteImageContentData.m new file mode 100644 index 0000000..6fc9327 --- /dev/null +++ b/Pod/Classes/RDPageContentData/RDRemoteImageContentData/RDRemoteImageContentData.m @@ -0,0 +1,98 @@ +// +// RDRemoteImageContentData.m +// RDImageViewerController +// +// Created by Akira Matsuda on 9/18/16. +// Copyright © 2016 Akira Matsuda. All rights reserved. +// + +#import "RDRemoteImageContentData.h" +#import "RDImageScrollView.h" +#import "rd_M2DURLConnectionOperation.h" + +@interface RDRemoteImageContentData () + +@property (nonatomic, strong) rd_M2DURLConnectionOperation *operation; +@property (nonatomic, strong) NSURLRequest *request; +@property (nonatomic, assign) NSInteger pageIndex; +@property (nonatomic, strong) void(^lazyConfigurationHandler)(UIImage *image); + +@end + +@implementation RDRemoteImageContentData + +- (instancetype)initWithRequest:(NSURLRequest *)request pageIndex:(NSInteger)index +{ + self = [super init]; + if (self) { + self.request = request; + self.pageIndex = index; + } + + return self; +} + +- (void)stopPreloading +{ + [self.operation stop]; +} + +#pragma mark - RDPageContentDataDelegate + ++ (UIView * _Nonnull)contentViewWithFrame:(CGRect)frame +{ + RDImageScrollView *view = [[RDImageScrollView alloc] initWithFrame:frame]; + + return view; +} + +- (void)preload +{ + if (self.image == nil) { + __weak typeof(self) bself = self; + self.operation = [[rd_M2DURLConnectionOperation alloc] initWithRequest:self.request completeBlock:^(rd_M2DURLConnectionOperation *operation, NSURLResponse *response, NSData *data, NSError *error) { + if (bself.requestCompletionHandler) { + bself.requestCompletionHandler(response, data, error); + } + + UIImage *image = nil; + if (bself.imageDecodeHandler) { + image = bself.imageDecodeHandler(data); + } + else { + image = [[UIImage alloc] initWithData:data]; + } + + bself.image = image; + if (bself.lazyConfigurationHandler) { + bself.lazyConfigurationHandler(image); + } + }]; + self.operation.configuration = self.configuration; + [self.operation sendRequest]; + } +} + +- (void)reload +{ + [self preload]; +} + +- (void)configureForView:(UIView * _Nonnull)view +{ + [super configureForView:view]; + if (self.image == nil) { + RDImageScrollView *imageView = (RDImageScrollView *)view; + __weak typeof(self) bself = self; + __weak typeof(imageView) bimageView = imageView; + self.lazyConfigurationHandler = ^(UIImage *image) { + if (bimageView.pageIndex == bself.pageIndex) { + dispatch_async(dispatch_get_main_queue(), ^{ + bimageView.image = image; + }); + } + }; + } +} + +@end diff --git a/Pod/Classes/RDPagingView/RDPagingView.h b/Pod/Classes/RDPagingView/RDPagingView.h index 4a58485..3ba92cf 100644 --- a/Pod/Classes/RDPagingView/RDPagingView.h +++ b/Pod/Classes/RDPagingView/RDPagingView.h @@ -24,7 +24,7 @@ typedef NS_ENUM(NSInteger, RDPagingViewMovingDirection) { @interface UIView (RDPagingView) -@property (nonatomic, readonly) NSInteger indexOfPage; +@property (nonatomic, readonly) NSInteger pageIndex; @end diff --git a/Pod/Classes/RDPagingView/RDPagingView.m b/Pod/Classes/RDPagingView/RDPagingView.m index d756b58..601ca72 100644 --- a/Pod/Classes/RDPagingView/RDPagingView.m +++ b/Pod/Classes/RDPagingView/RDPagingView.m @@ -24,16 +24,16 @@ @implementation UIView (RDPagingView) static NSString *const kRDPagingViewIndexOfPage = @"RDPagingViewIndexOfPage"; -@dynamic indexOfPage; +@dynamic pageIndex; -- (NSInteger)indexOfPage +- (NSInteger)pageIndex { return [(NSNumber *)objc_getAssociatedObject(self, (__bridge const void *)(kRDPagingViewIndexOfPage)) integerValue]; } -- (void)setIndexOfPage:(NSInteger)indexOfPage +- (void)setPageIndex:(NSInteger)pageIndex { - objc_setAssociatedObject(self, (__bridge const void *)(kRDPagingViewIndexOfPage), @(indexOfPage), OBJC_ASSOCIATION_RETAIN); + objc_setAssociatedObject(self, (__bridge const void *)(kRDPagingViewIndexOfPage), @(pageIndex), OBJC_ASSOCIATION_RETAIN); } @end @@ -209,8 +209,8 @@ - (void)pageIndexWillChangeToIndex:(NSInteger)index NSInteger minimumIndex = index - _preloadCount; [usingViews_ enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { UIView *view = obj; - if (view.indexOfPage < minimumIndex || view.indexOfPage > maximumIndex) { - [self setViewAsPrepared:view reuseIdentifier:[self.pagingDelegate paginView:self reuseIdentifierForIndex:view.indexOfPage]]; + if (view.pageIndex < minimumIndex || view.pageIndex > maximumIndex) { + [self setViewAsPrepared:view reuseIdentifier:[self.pagingDelegate paginView:self reuseIdentifierForIndex:view.pageIndex]]; } }]; @@ -241,7 +241,7 @@ - (void)loadViewAtIndex:(NSInteger)index else { view.frame = CGRectMake(0, [self indexInScrollView:index] * CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)); } - [view setIndexOfPage:index]; + [view setPageIndex:index]; [self setViewAsUsingView:view reuseIdentifier:[self.pagingDelegate paginView:self reuseIdentifierForIndex:index]]; [self addSubview:view]; } @@ -250,7 +250,7 @@ - (UIView *)viewForIndex:(NSInteger)index { UIView *result = nil; for (UIView *view in usingViews_) { - if (view.indexOfPage == index) { + if (view.pageIndex == index) { result = view; break; } diff --git a/RDImageViewerController.podspec b/RDImageViewerController.podspec index 76d18de..fc046d9 100644 --- a/RDImageViewerController.podspec +++ b/RDImageViewerController.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.name = "RDImageViewerController" - s.version = "1.0.2" + s.version = "2.0.0" s.summary = "Simple but powerful image viewer." s.homepage = "https://github.com/0x0c/RDImageViewerController" # s.screenshots = "https://raw.githubusercontent.com/0x0c/RDImageViewerController/master/Example/Screenshot/1.png", "https://raw.githubusercontent.com/0x0c/RDImageViewerController/master/Example/Screenshot/2.png", "https://raw.githubusercontent.com/0x0c/RDImageViewerController/master/Example/Screenshot/view_and_image.png"