@@ -5,7 +5,7 @@ public protocol ImageDataSource:class {
55 func imageItem( at index: Int ) -> ImageItem
66}
77
8- class ImageCarouselViewController : UIPageViewController {
8+ public class ImageCarouselViewController : UIPageViewController {
99
1010 weak var imageDatasource : ImageDataSource ?
1111 var initialIndex = 0
@@ -19,6 +19,8 @@ class ImageCarouselViewController:UIPageViewController {
1919
2020 var options : [ ImageViewerOption ] = [ ]
2121
22+ weak var rightNavItemDelegate : RightNavItemDelegate ?
23+
2224 private( set) lazy var navBar : UINavigationBar = {
2325 let _navBar = UINavigationBar ( frame: . zero)
2426 _navBar. isTranslucent = true
@@ -36,6 +38,30 @@ class ImageCarouselViewController:UIPageViewController {
3638
3739 private( set) lazy var navItem = UINavigationItem ( )
3840
41+ public static func create(
42+ sourceView: UIImageView ,
43+ imageDataSource: ImageDataSource ? ,
44+ options: [ ImageViewerOption ] = [ ] ,
45+ initialIndex: Int = 0 ) -> ImageCarouselViewController {
46+
47+ let pageOptions = [ UIPageViewController . OptionsKey. interPageSpacing: 20 ]
48+
49+ let imageCarousel = ImageCarouselViewController (
50+ transitionStyle: . scroll,
51+ navigationOrientation: . horizontal,
52+ options: pageOptions)
53+
54+ imageCarousel. modalPresentationStyle = . overFullScreen
55+ imageCarousel. modalPresentationCapturesStatusBarAppearance = true
56+
57+ imageCarousel. sourceView = sourceView
58+ imageCarousel. imageDatasource = imageDataSource
59+ imageCarousel. options = options
60+ imageCarousel. initialIndex = initialIndex
61+
62+ return imageCarousel
63+ }
64+
3965 private func addNavBar( ) {
4066 // Add Navigation Bar
4167 let closeBarButton = UIBarButtonItem (
@@ -61,15 +87,29 @@ class ImageCarouselViewController:UIPageViewController {
6187
6288 options. forEach {
6389 switch $0 {
64- case . theme( let theme) :
65- self . theme = theme
66- case . closeIcon( let icon) :
67- navItem. leftBarButtonItem? . image = icon
90+ case . theme( let theme) :
91+ self . theme = theme
92+ case . closeIcon( let icon) :
93+ navItem. leftBarButtonItem? . image = icon
94+ case . rightNavItemTitle( let title, let delegate) :
95+ navItem. rightBarButtonItem = UIBarButtonItem (
96+ title: title,
97+ style: . plain,
98+ target: self ,
99+ action: #selector( diTapRightNavBarItem ( _: ) ) )
100+ rightNavItemDelegate = delegate
101+ case . rightNavItemIcon( let icon, let delegate) :
102+ navItem. rightBarButtonItem = UIBarButtonItem (
103+ image: icon,
104+ style: . plain,
105+ target: self ,
106+ action: #selector( diTapRightNavBarItem ( _: ) ) )
107+ rightNavItemDelegate = delegate
68108 }
69109 }
70110 }
71111
72- override func viewDidLoad( ) {
112+ override public func viewDidLoad( ) {
73113 super. viewDidLoad ( )
74114
75115 addBackgroundView ( )
@@ -92,24 +132,36 @@ class ImageCarouselViewController:UIPageViewController {
92132 setViewControllers ( [ initialVC] , direction: . forward, animated: true , completion: nil )
93133 }
94134
95- override func viewDidAppear( _ animated: Bool ) {
135+ override public func viewDidAppear( _ animated: Bool ) {
96136 super. viewDidAppear ( animated)
97137 UIView . animate ( withDuration: 0.235 ) {
98138 self . navBar. alpha = 1.0
99139 }
100140 }
101141
102142 @objc
103- func dismiss( _ sender: UIBarButtonItem ? ) {
143+ private func dismiss( _ sender: UIBarButtonItem ) {
144+ dismissMe ( completion: nil )
145+ }
146+
147+ public func dismissMe( completion: ( ( ) -> Void ) ? = nil ) {
104148 sourceView. alpha = 1.0
105149 UIView . animate ( withDuration: 0.235 , animations: {
106150 self . view. alpha = 0.0
107151 } ) { _ in
108- self . dismiss ( animated: false , completion: nil )
152+ self . dismiss ( animated: false , completion: completion )
109153 }
110154 }
111155
112- override var preferredStatusBarStyle : UIStatusBarStyle {
156+ @objc
157+ func diTapRightNavBarItem( _ sender: UIBarButtonItem ) {
158+ guard let _delegate = rightNavItemDelegate,
159+ let _firstVC = viewControllers? . first as? ImageViewerController
160+ else { return }
161+ _delegate. imageViewer ( self , didTapRightNavItem: _firstVC. index)
162+ }
163+
164+ override public var preferredStatusBarStyle : UIStatusBarStyle {
113165 if theme == . dark {
114166 return . lightContent
115167 }
@@ -118,7 +170,7 @@ class ImageCarouselViewController:UIPageViewController {
118170}
119171
120172extension ImageCarouselViewController : UIPageViewControllerDataSource {
121- func pageViewController(
173+ public func pageViewController(
122174 _ pageViewController: UIPageViewController ,
123175 viewControllerBefore viewController: UIViewController ) -> UIViewController ? {
124176
@@ -135,7 +187,7 @@ extension ImageCarouselViewController:UIPageViewControllerDataSource {
135187 delegate: self )
136188 }
137189
138- func pageViewController(
190+ public func pageViewController(
139191 _ pageViewController: UIPageViewController ,
140192 viewControllerAfter viewController: UIViewController ) -> UIViewController ? {
141193
0 commit comments