From 730a3d4c51ace35960f052c1f411f73602236ebd Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Sun, 4 Dec 2016 14:11:36 +0200 Subject: [PATCH 01/14] swift3+view controller life cycle --- .../TinderLike.xcodeproj/project.pbxproj | 22 ++ Demos/TinderLike/TinderLike/AppDelegate.swift | 57 ++--- .../AppIcon.appiconset/Contents.json | 10 + Demos/TinderLike/TinderLike/VC0.swift | 34 +++ Demos/TinderLike/TinderLike/VC1.swift | 34 +++ Demos/TinderLike/TinderLike/VC2.swift | 34 +++ Demos/TinderLike/TinderLike/VC3.swift | 34 +++ .../TinderLikeTests/TinderLikeTests.swift | 2 +- SLPagingViewSwift/SLPagingViewSwift.swift | 218 +++++++++++------- 9 files changed, 336 insertions(+), 109 deletions(-) create mode 100644 Demos/TinderLike/TinderLike/VC0.swift create mode 100644 Demos/TinderLike/TinderLike/VC1.swift create mode 100644 Demos/TinderLike/TinderLike/VC2.swift create mode 100644 Demos/TinderLike/TinderLike/VC3.swift diff --git a/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj b/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj index 49f9d7f..d32f9c4 100644 --- a/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj +++ b/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj @@ -12,6 +12,10 @@ F85CA9C11A61963B0013CF3F /* TinderLikeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F85CA9C01A61963B0013CF3F /* TinderLikeTests.swift */; }; F85CA9CC1A61968B0013CF3F /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F85CA9CB1A61968B0013CF3F /* Default-568h@2x.png */; }; F85CA9D31A61990A0013CF3F /* SLPagingViewSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = F85CA9D21A61990A0013CF3F /* SLPagingViewSwift.swift */; }; + FC7D60C61DF43CE900700038 /* VC0.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC7D60C21DF43CE900700038 /* VC0.swift */; }; + FC7D60C71DF43CE900700038 /* VC1.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC7D60C31DF43CE900700038 /* VC1.swift */; }; + FC7D60C81DF43CE900700038 /* VC2.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC7D60C41DF43CE900700038 /* VC2.swift */; }; + FC7D60C91DF43CE900700038 /* VC3.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC7D60C51DF43CE900700038 /* VC3.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -35,6 +39,10 @@ F85CA9CA1A6196820013CF3F /* TinderLike-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "TinderLike-Prefix.pch"; sourceTree = ""; }; F85CA9CB1A61968B0013CF3F /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default-568h@2x.png"; path = "../Default-568h@2x.png"; sourceTree = ""; }; F85CA9D21A61990A0013CF3F /* SLPagingViewSwift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SLPagingViewSwift.swift; path = ../../SLPagingViewSwift/SLPagingViewSwift.swift; sourceTree = ""; }; + FC7D60C21DF43CE900700038 /* VC0.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VC0.swift; path = TinderLike/VC0.swift; sourceTree = ""; }; + FC7D60C31DF43CE900700038 /* VC1.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VC1.swift; path = TinderLike/VC1.swift; sourceTree = ""; }; + FC7D60C41DF43CE900700038 /* VC2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VC2.swift; path = TinderLike/VC2.swift; sourceTree = ""; }; + FC7D60C51DF43CE900700038 /* VC3.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VC3.swift; path = TinderLike/VC3.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -115,6 +123,10 @@ isa = PBXGroup; children = ( F85CA9D21A61990A0013CF3F /* SLPagingViewSwift.swift */, + FC7D60C21DF43CE900700038 /* VC0.swift */, + FC7D60C31DF43CE900700038 /* VC1.swift */, + FC7D60C41DF43CE900700038 /* VC2.swift */, + FC7D60C51DF43CE900700038 /* VC3.swift */, ); name = SLPagingViewSwift; sourceTree = ""; @@ -168,9 +180,11 @@ TargetAttributes = { F85CA9A41A61963B0013CF3F = { CreatedOnToolsVersion = 6.1.1; + LastSwiftMigration = 0810; }; F85CA9B91A61963B0013CF3F = { CreatedOnToolsVersion = 6.1.1; + LastSwiftMigration = 0810; TestTargetID = F85CA9A41A61963B0013CF3F; }; }; @@ -218,7 +232,11 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + FC7D60C91DF43CE900700038 /* VC3.swift in Sources */, + FC7D60C61DF43CE900700038 /* VC0.swift in Sources */, F85CA9AB1A61963B0013CF3F /* AppDelegate.swift in Sources */, + FC7D60C81DF43CE900700038 /* VC2.swift in Sources */, + FC7D60C71DF43CE900700038 /* VC1.swift in Sources */, F85CA9D31A61990A0013CF3F /* SLPagingViewSwift.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -326,6 +344,7 @@ INFOPLIST_FILE = TinderLike/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -336,6 +355,7 @@ INFOPLIST_FILE = TinderLike/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -354,6 +374,7 @@ INFOPLIST_FILE = TinderLikeTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/TinderLike.app/TinderLike"; }; name = Debug; @@ -369,6 +390,7 @@ INFOPLIST_FILE = TinderLikeTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/TinderLike.app/TinderLike"; }; name = Release; diff --git a/Demos/TinderLike/TinderLike/AppDelegate.swift b/Demos/TinderLike/TinderLike/AppDelegate.swift index a9e405f..a7a0a20 100644 --- a/Demos/TinderLike/TinderLike/AppDelegate.swift +++ b/Demos/TinderLike/TinderLike/AppDelegate.swift @@ -15,31 +15,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var nav: UINavigationController? var controller: SLPagingViewSwift! - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - self.window = UIWindow(frame: UIScreen.mainScreen().bounds) + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + self.window = UIWindow(frame: UIScreen.main.bounds) let orange = UIColor(red: 255/255, green: 69.0/255, blue: 0.0/255, alpha: 1.0) let gray = UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.0) - var ctr1 = UIViewController() + let ctr0 = VC0() + ctr0.title = "Ctr0" + ctr0.view.backgroundColor = UIColor.brown + let ctr1 = VC1() ctr1.title = "Ctr1" ctr1.view.backgroundColor = orange - var ctr2 = UIViewController() + let ctr2 = VC2() ctr2.title = "Ctr2" - ctr2.view.backgroundColor = UIColor.yellowColor() - var ctr3 = UIViewController() + ctr2.view.backgroundColor = UIColor.yellow + let ctr3 = VC3() ctr3.title = "Ctr3" ctr3.view.backgroundColor = gray + var img0 = UIImage(named: "chat") + img0 = img0?.withRenderingMode(.alwaysTemplate) var img1 = UIImage(named: "gear") - img1 = img1?.imageWithRenderingMode(.AlwaysTemplate) + img1 = img1?.withRenderingMode(.alwaysTemplate) var img2 = UIImage(named: "profile") - img2 = img2?.imageWithRenderingMode(.AlwaysTemplate) + img2 = img2?.withRenderingMode(.alwaysTemplate) var img3 = UIImage(named: "chat") - img3 = img3?.imageWithRenderingMode(.AlwaysTemplate) + img3 = img3?.withRenderingMode(.alwaysTemplate) - var items = [UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)] - var controllers = [ctr1, ctr2, ctr3] + let items = [UIImageView(image: img0), UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)] + let controllers = [ctr0, ctr1, ctr2, ctr3] controller = SLPagingViewSwift(items: items, controllers: controllers, showPageControl: false) controller.pagingViewMoving = ({ subviews in @@ -64,49 +69,49 @@ class AppDelegate: UIResponder, UIApplicationDelegate { self.nav = UINavigationController(rootViewController: self.controller) self.window?.rootViewController = self.nav - self.window?.backgroundColor = UIColor.whiteColor() + self.window?.backgroundColor = UIColor.white self.window?.makeKeyAndVisible() return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } - func viewWithBackground(color: UIColor) -> UIView{ - var v = UIView() + func viewWithBackground(_ color: UIColor) -> UIView{ + let v = UIView() v.backgroundColor = color return v } - func gradient(percent: Double, topX: Double, bottomX: Double, initC: UIColor, goal: UIColor) -> UIColor{ - var t = (percent - bottomX) / (topX - bottomX) + func gradient(_ percent: Double, topX: Double, bottomX: Double, initC: UIColor, goal: UIColor) -> UIColor{ + let t = (percent - bottomX) / (topX - bottomX) - let cgInit = CGColorGetComponents(initC.CGColor) - let cgGoal = CGColorGetComponents(goal.CGColor) + let cgInit = initC.cgColor.components! + let cgGoal = goal.cgColor.components! - var r = cgInit[0] + CGFloat(t) * (cgGoal[0] - cgInit[0]) - var g = cgInit[1] + CGFloat(t) * (cgGoal[1] - cgInit[1]) - var b = cgInit[2] + CGFloat(t) * (cgGoal[2] - cgInit[2]) + let r = cgInit[0] + CGFloat(t) * (cgGoal[0] - cgInit[0]) + let g = cgInit[1] + CGFloat(t) * (cgGoal[1] - cgInit[1]) + let b = cgInit[2] + CGFloat(t) * (cgGoal[2] - cgInit[2]) return UIColor(red: r, green: g, blue: b, alpha: 1.0) } diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/AppIcon.appiconset/Contents.json b/Demos/TinderLike/TinderLike/Images.xcassets/AppIcon.appiconset/Contents.json index 118c98f..b8236c6 100644 --- a/Demos/TinderLike/TinderLike/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/Demos/TinderLike/TinderLike/Images.xcassets/AppIcon.appiconset/Contents.json @@ -1,5 +1,15 @@ { "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "29x29", diff --git a/Demos/TinderLike/TinderLike/VC0.swift b/Demos/TinderLike/TinderLike/VC0.swift new file mode 100644 index 0000000..8cdbf98 --- /dev/null +++ b/Demos/TinderLike/TinderLike/VC0.swift @@ -0,0 +1,34 @@ +// +// VC0.swift +// TinderLike +// +// Created by Bari Levi on 28/11/2016. +// Copyright © 2016 Stefan Lage. All rights reserved. +// + +import Foundation +import UIKit + +class VC0: UIViewController { + + override func viewDidLoad() { + NSLog("viewDidLoad - 0") + } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + NSLog("viewWillAppear - 0") + } + override public func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + NSLog("viewDidAppear - 0") + } + override public func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + NSLog("viewWillDisappear - 0") + } + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + NSLog("viewDidDisappear - 0") + } + +} diff --git a/Demos/TinderLike/TinderLike/VC1.swift b/Demos/TinderLike/TinderLike/VC1.swift new file mode 100644 index 0000000..070bf57 --- /dev/null +++ b/Demos/TinderLike/TinderLike/VC1.swift @@ -0,0 +1,34 @@ +// +// VC1.swift +// TinderLike +// +// Created by Bari Levi on 28/11/2016. +// Copyright © 2016 Stefan Lage. All rights reserved. +// + +import Foundation +import UIKit + +class VC1: UIViewController { + + override func viewDidLoad() { + NSLog("viewDidLoad - 1") + } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + NSLog("viewWillAppear - 1") + } + override public func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + NSLog("viewDidAppear - 1") + } + override public func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + NSLog("viewWillDisappear - 1") + } + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + NSLog("viewDidDisappear - 1") + } + +} diff --git a/Demos/TinderLike/TinderLike/VC2.swift b/Demos/TinderLike/TinderLike/VC2.swift new file mode 100644 index 0000000..73e3b0e --- /dev/null +++ b/Demos/TinderLike/TinderLike/VC2.swift @@ -0,0 +1,34 @@ +// +// VC2.swift +// TinderLike +// +// Created by Bari Levi on 28/11/2016. +// Copyright © 2016 Stefan Lage. All rights reserved. +// + +import Foundation +import UIKit + +class VC2: UIViewController { + + override func viewDidLoad() { + NSLog("viewDidLoad - 2") + } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + NSLog("viewWillAppear - 2") + } + override public func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + NSLog("viewDidAppear - 2") + } + override public func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + NSLog("viewWillDisappear - 2") + } + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + NSLog("viewDidDisappear - 2") + } + +} diff --git a/Demos/TinderLike/TinderLike/VC3.swift b/Demos/TinderLike/TinderLike/VC3.swift new file mode 100644 index 0000000..c7956c1 --- /dev/null +++ b/Demos/TinderLike/TinderLike/VC3.swift @@ -0,0 +1,34 @@ +// +// VC3.swift +// TinderLike +// +// Created by Bari Levi on 28/11/2016. +// Copyright © 2016 Stefan Lage. All rights reserved. +// + +import Foundation +import UIKit + +class VC3: UIViewController { + + override func viewDidLoad() { + NSLog("viewDidLoad - 3") + } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + NSLog("viewWillAppear - 3") + } + override public func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + NSLog("viewDidAppear - 3") + } + override public func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + NSLog("viewWillDisappear - 3") + } + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + NSLog("viewDidDisappear - 3") + } + +} diff --git a/Demos/TinderLike/TinderLikeTests/TinderLikeTests.swift b/Demos/TinderLike/TinderLikeTests/TinderLikeTests.swift index 438e449..729ce70 100644 --- a/Demos/TinderLike/TinderLikeTests/TinderLikeTests.swift +++ b/Demos/TinderLike/TinderLikeTests/TinderLikeTests.swift @@ -28,7 +28,7 @@ class TinderLikeTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock() { + self.measure() { // Put the code you want to measure the time of here. } } diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index 7164355..8f27f59 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -9,58 +9,58 @@ import UIKit public enum SLNavigationSideItemsStyle: Int { - case SLNavigationSideItemsStyleOnBounds = 40 - case SLNavigationSideItemsStyleClose = 30 - case SLNavigationSideItemsStyleNormal = 20 - case SLNavigationSideItemsStyleFar = 10 - case SLNavigationSideItemsStyleDefault = 0 - case SLNavigationSideItemsStyleCloseToEachOne = -40 + case slNavigationSideItemsStyleOnBounds = 40 + case slNavigationSideItemsStyleClose = 30 + case slNavigationSideItemsStyleNormal = 20 + case slNavigationSideItemsStyleFar = 10 + case slNavigationSideItemsStyleDefault = 0 + case slNavigationSideItemsStyleCloseToEachOne = -40 } -public typealias SLPagingViewMoving = ((subviews: [UIView])-> ()) -public typealias SLPagingViewMovingRedefine = ((scrollView: UIScrollView, subviews: NSArray)-> ()) -public typealias SLPagingViewDidChanged = ((currentPage: Int)-> ()) +public typealias SLPagingViewMoving = ((_ subviews: [UIView])-> ()) +public typealias SLPagingViewMovingRedefine = ((_ scrollView: UIScrollView, _ subviews: NSArray)-> ()) +public typealias SLPagingViewDidChanged = ((_ currentPage: Int)-> ()) -public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { +open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { // MARK: - Public properties - var views = [Int : UIView]() - public var currentPageControlColor: UIColor? - public var tintPageControlColor: UIColor? - public var pagingViewMoving: SLPagingViewMoving? - public var pagingViewMovingRedefine: SLPagingViewMovingRedefine? - public var didChangedPage: SLPagingViewDidChanged? - public var navigationSideItemsStyle: SLNavigationSideItemsStyle = .SLNavigationSideItemsStyleDefault + var viewControllers = [Int : UIViewController]() + open var currentPageControlColor: UIColor? + open var tintPageControlColor: UIColor? + open var pagingViewMoving: SLPagingViewMoving? + open var pagingViewMovingRedefine: SLPagingViewMovingRedefine? + open var didChangedPage: SLPagingViewDidChanged? + open var navigationSideItemsStyle: SLNavigationSideItemsStyle = .slNavigationSideItemsStyleDefault // MARK: - Private properties - private var SCREENSIZE: CGSize { - return UIScreen.mainScreen().bounds.size + fileprivate var SCREENSIZE: CGSize { + return UIScreen.main.bounds.size } - private var scrollView: UIScrollView! - private var pageControl: UIPageControl! - private var navigationBarView: UIView = UIView() - private var navItems: [UIView] = [] - private var needToShowPageControl: Bool = false - private var isUserInteraction: Bool = false - private var indexSelected: Int = 0 + fileprivate var scrollView: UIScrollView! + fileprivate var pageControl: UIPageControl! + fileprivate var navigationBarView: UIView = UIView() + fileprivate var navItems: [UIView] = [] + fileprivate var needToShowPageControl: Bool = false + fileprivate var isUserInteraction: Bool = false + fileprivate var indexSelected: Int = 0 // MARK: - Constructors public required init(coder decoder: NSCoder) { - super.init(coder: decoder) + super.init(coder: decoder)! } - public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { + public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) // Here you can init your properties } // MARK: - Constructors with items & views - public convenience init(items: [UIView], views: [UIView]) { - self.init(items: items, views: views, showPageControl:false, navBarBackground:UIColor.whiteColor()) + public convenience init(items: [UIView], views: [UIViewController]) { + self.init(items: items, views: views, showPageControl:false, navBarBackground:UIColor.white) } - public convenience init(items: [UIView], views: [UIView], showPageControl: Bool){ - self.init(items: items, views: views, showPageControl:showPageControl, navBarBackground:UIColor.whiteColor()) + public convenience init(items: [UIView], views: [UIViewController], showPageControl: Bool){ + self.init(items: items, views: views, showPageControl:showPageControl, navBarBackground:UIColor.white) } /* @@ -73,36 +73,36 @@ public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { * * @return Instance of SLPagingViewController */ - public init(items: [UIView], views: [UIView], showPageControl: Bool, navBarBackground: UIColor) { + public init(items: [UIView], views: [UIViewController], showPageControl: Bool, navBarBackground: UIColor) { super.init(nibName: nil, bundle: nil) needToShowPageControl = showPageControl navigationBarView.backgroundColor = navBarBackground isUserInteraction = true - for (i, v) in enumerate(items) { + for (i, v) in items.enumerated() { let vSize: CGSize = (v as? UILabel)?._slpGetSize() ?? v.frame.size let originX = (self.SCREENSIZE.width/2.0 - vSize.width/2.0) + CGFloat(i * 100) - v.frame = CGRectMake(originX, 8, vSize.width, vSize.height) + v.frame = CGRect(x: originX, y: 8, width: vSize.width, height: vSize.height) v.tag = i - let tap = UITapGestureRecognizer(target: self, action: "tapOnHeader:") + let tap = UITapGestureRecognizer(target: self, action: #selector(SLPagingViewSwift.tapOnHeader(_:))) v.addGestureRecognizer(tap) - v.userInteractionEnabled = true + v.isUserInteractionEnabled = true self.navigationBarView.addSubview(v) self.navItems.append(v) } - for (i, view) in enumerate(views) { - view.tag = i - self.views[i] = view + for (i, view) in views.enumerated() { + view.view.tag = i + self.viewControllers[i] = view } } // MARK: - Constructors with controllers public convenience init(controllers: [UIViewController]){ - self.init(controllers: controllers, showPageControl: true, navBarBackground: UIColor.whiteColor()) + self.init(controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) } public convenience init(controllers: [UIViewController], showPageControl: Bool){ - self.init(controllers: controllers, showPageControl: true, navBarBackground: UIColor.whiteColor()) + self.init(controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) } /* @@ -125,15 +125,15 @@ public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { views.append(ctr.view) items.append(item) } - self.init(items: items, views: views, showPageControl:showPageControl, navBarBackground:navBarBackground) + self.init(items: items, views: controllers, showPageControl:showPageControl, navBarBackground:navBarBackground) } // MARK: - Constructors with items & controllers public convenience init(items: [UIView], controllers: [UIViewController]){ - self.init(items: items, controllers: controllers, showPageControl: true, navBarBackground: UIColor.whiteColor()) + self.init(items: items, controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) } public convenience init(items: [UIView], controllers: [UIViewController], showPageControl: Bool){ - self.init(items: items, controllers: controllers, showPageControl: showPageControl, navBarBackground: UIColor.whiteColor()) + self.init(items: items, controllers: controllers, showPageControl: showPageControl, navBarBackground: UIColor.white) } /* @@ -151,20 +151,20 @@ public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { for ctr in controllers { views.append(ctr.view) } - self.init(items: items, views: views, showPageControl:showPageControl, navBarBackground:navBarBackground) + self.init(items: items, views: controllers, showPageControl:showPageControl, navBarBackground:navBarBackground) } // MARK: - Life cycle - public override func viewDidLoad() { + open override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.setupPagingProcess() self.setCurrentIndex(self.indexSelected, animated: false) } - public override func viewWillLayoutSubviews() { + open override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() - self.navigationBarView.frame = CGRectMake(0, 0, self.SCREENSIZE.width, 44) + self.navigationBarView.frame = CGRect(x: 0, y: 0, width: self.SCREENSIZE.width, height: 44) } // MARK: - Public methods @@ -174,7 +174,7 @@ public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { * * @param activate state you want to set to UserInteraction */ - public func updateUserInteractionOnNavigation(active: Bool){ + open func updateUserInteractionOnNavigation(_ active: Bool){ self.isUserInteraction = active } @@ -184,25 +184,27 @@ public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { * @param index of the wanted page * @param animated animate the moving */ - public func setCurrentIndex(index: Int, animated: Bool){ + open func setCurrentIndex(_ index: Int, animated: Bool){ // Be sure we got an existing index if(index < 0 || index > self.navigationBarView.subviews.count-1){ - var exc = NSException(name: "Index out of range", reason: "The index is out of range of subviews's countsd!", userInfo: nil) + let exc = NSException(name: NSExceptionName(rawValue: "Index out of range"), reason: "The index is out of range of subviews's countsd!", userInfo: nil) exc.raise() } self.indexSelected = index // Get the right position and update it let xOffset = CGFloat(index) * self.SCREENSIZE.width - self.scrollView.setContentOffset(CGPointMake(xOffset, self.scrollView.contentOffset.y), animated: animated) + self.scrollView.setContentOffset(CGPoint(x: xOffset, y: self.scrollView.contentOffset.y), animated: animated) + + showPage(at: self.indexSelected) } // MARK: - Internal methods - private func setupPagingProcess() { - var frame: CGRect = CGRectMake(0, 0, SCREENSIZE.width, self.view.frame.height) + fileprivate func setupPagingProcess() { + let frame: CGRect = CGRect(x: 0, y: 0, width: SCREENSIZE.width, height: self.view.frame.height) self.scrollView = UIScrollView(frame: frame) - self.scrollView.backgroundColor = UIColor.clearColor() - self.scrollView.pagingEnabled = true + self.scrollView.backgroundColor = UIColor.clear + self.scrollView.isPagingEnabled = true self.scrollView.showsHorizontalScrollIndicator = false self.scrollView.showsVerticalScrollIndicator = false self.scrollView.delegate = self @@ -216,7 +218,7 @@ public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { if(self.needToShowPageControl){ // Make the page control - self.pageControl = UIPageControl(frame: CGRectMake(0, 35, 0, 0)) + self.pageControl = UIPageControl(frame: CGRect(x: 0, y: 35, width: 0, height: 0)) self.pageControl.numberOfPages = self.navigationBarView.subviews.count self.pageControl.currentPage = 0 if self.currentPageControlColor != nil { @@ -233,30 +235,65 @@ public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } // Loads all views - private func addViews() { - if self.views.count > 0 { - let width = SCREENSIZE.width * CGFloat(self.views.count) + fileprivate func addViews() { + if self.viewControllers.count > 0 { + let width = SCREENSIZE.width * CGFloat(self.viewControllers.count) let height = self.view.frame.height self.scrollView.contentSize = CGSize(width: width, height: height) var i: Int = 0 - while let v = views[i] { - v.frame = CGRectMake(self.SCREENSIZE.width * CGFloat(i), 0, self.SCREENSIZE.width, self.SCREENSIZE.height) - self.scrollView.addSubview(v) - i++ + while let v = viewControllers[i] { + v.view.frame = CGRect(x: self.SCREENSIZE.width * CGFloat(i), y: 0, width: self.SCREENSIZE.width, height: self.SCREENSIZE.height) +// self.scrollView.addSubview(v) + i += 1 } } } - - private func sendNewIndex(scrollView: UIScrollView){ + func showPage(at index:Int) -> Void { + + if let prevVC = self.viewControllers[self.indexSelected] , prevVC.parent == self { + + prevVC.willMove(toParentViewController: nil) + } + if let nextVC = self.viewControllers[index] , nextVC.parent == nil { + NSLog("show page = \(index)") + + nextVC.willMove(toParentViewController: self) + self.addChildViewController(nextVC) + self.scrollView.addSubview(nextVC.view) + nextVC.didMove(toParentViewController: self) + + } + + } + func hidePage(at index:Int) -> Void { + + if let prevVC = self.viewControllers[index] , prevVC.parent == self { + + prevVC.willMove(toParentViewController: nil) + prevVC.view.removeFromSuperview() + prevVC.removeFromParentViewController() + prevVC.didMove(toParentViewController: nil) + + } + + + + } + + fileprivate func sendNewIndex(_ scrollView: UIScrollView){ let xOffset = Float(scrollView.contentOffset.x) - var currentIndex = (Int(roundf(xOffset)) % (self.navigationBarView.subviews.count * Int(self.SCREENSIZE.width))) / Int(self.SCREENSIZE.width) + let currentIndex = (Int(roundf(xOffset)) % (self.navigationBarView.subviews.count * Int(self.SCREENSIZE.width))) / Int(self.SCREENSIZE.width) if self.needToShowPageControl && self.pageControl.currentPage != currentIndex { self.pageControl.currentPage = currentIndex - self.didChangedPage?(currentPage: currentIndex) + self.didChangedPage?(currentIndex) } + let prevIndex = self.indexSelected + self.indexSelected = currentIndex + hidePage(at: prevIndex) + } - func getOriginX(vSize: CGSize, idx: CGFloat, distance: CGFloat, xOffset: CGFloat) -> CGFloat{ + func getOriginX(_ vSize: CGSize, idx: CGFloat, distance: CGFloat, xOffset: CGFloat) -> CGFloat{ var result = self.SCREENSIZE.width / 2.0 - vSize.width/2.0 result += (idx * distance) result -= xOffset / (self.SCREENSIZE.width / distance) @@ -264,38 +301,55 @@ public class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } // Scroll to the view tapped - func tapOnHeader(recognizer: UITapGestureRecognizer){ - if let key = recognizer.view?.tag, view = self.views[key] where self.isUserInteraction { - self.scrollView.scrollRectToVisible(view.frame, animated: true) + func tapOnHeader(_ recognizer: UITapGestureRecognizer){ + if let key = recognizer.view?.tag, let view = self.viewControllers[key], self.isUserInteraction { + self.scrollView.scrollRectToVisible(view.view.frame, animated: true) + showPage(at: key) } } // MARK: - UIScrollViewDelegate - public func scrollViewDidScroll(scrollView: UIScrollView) { + open func scrollViewDidScroll(_ scrollView: UIScrollView) { let xOffset = scrollView.contentOffset.x let distance = CGFloat(100 + self.navigationSideItemsStyle.rawValue) - for (i, v) in enumerate(self.navItems) { + for (i, v) in self.navItems.enumerated() { let vSize = v.frame.size let originX = self.getOriginX(vSize, idx: CGFloat(i), distance: CGFloat(distance), xOffset: xOffset) - v.frame = CGRectMake(originX, 8, vSize.width, vSize.height) + v.frame = CGRect(x: originX, y: 8, width: vSize.width, height: vSize.height) } - self.pagingViewMovingRedefine?(scrollView: scrollView, subviews: self.navItems) - self.pagingViewMoving?(subviews: self.navItems) + self.pagingViewMovingRedefine?(scrollView, self.navItems as NSArray) + self.pagingViewMoving?(self.navItems) } - public func scrollViewDidEndDecelerating(scrollView: UIScrollView) { + open func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { self.sendNewIndex(scrollView) } - public func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) { + open func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { self.sendNewIndex(scrollView) } + public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) + { + + var direction = 0 + let yVelocity: CGFloat = scrollView.panGestureRecognizer.velocity(in: scrollView).x + if yVelocity < 0 { direction = 1 } else + if yVelocity > 0 { direction = -1 } + + let xOffset = Int(roundf(Float(scrollView.contentOffset.x))) + let W:Int = Int(self.SCREENSIZE.width) + + let nextIndex = xOffset / W + direction + + showPage(at: nextIndex) + + } } extension UILabel { func _slpGetSize() -> CGSize? { - return (text as NSString?)?.sizeWithAttributes([NSFontAttributeName: font]) + return (text as NSString?)?.size(attributes: [NSFontAttributeName: font]) } -} \ No newline at end of file +} From fa0cbfeeeb1b0abb440ed838a3e6e335ecfb8462 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Sun, 4 Dec 2016 14:35:52 +0200 Subject: [PATCH 02/14] renames --- .../TinderLike.xcodeproj/project.pbxproj | 14 ++++- .../xcschemes/TinderLike.xcscheme | 13 ++-- Demos/TinderLike/TinderLike/AppDelegate.swift | 4 +- Demos/TinderLike/TinderLike/Info.plist | 2 +- Demos/TinderLike/TinderLikeTests/Info.plist | 2 +- SLPagingViewSwift/SLPagingViewSwift.swift | 59 ++++++++----------- 6 files changed, 51 insertions(+), 43 deletions(-) diff --git a/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj b/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj index d32f9c4..8fdc105 100644 --- a/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj +++ b/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj @@ -175,7 +175,7 @@ F85CA99D1A61963B0013CF3F /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0610; + LastUpgradeCheck = 0810; ORGANIZATIONNAME = "Stefan Lage"; TargetAttributes = { F85CA9A41A61963B0013CF3F = { @@ -273,15 +273,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", @@ -315,8 +319,10 @@ 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"; @@ -324,6 +330,7 @@ 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; @@ -333,6 +340,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.1; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; VALIDATE_PRODUCT = YES; }; name = Release; @@ -343,6 +351,7 @@ GCC_PREFIX_HEADER = "$SRCROOT/$PROJECT_NAME/TinderLike-Prefix.pch"; INFOPLIST_FILE = TinderLike/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "SL.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 3.0; }; @@ -354,6 +363,7 @@ GCC_PREFIX_HEADER = "$SRCROOT/$PROJECT_NAME/TinderLike-Prefix.pch"; INFOPLIST_FILE = TinderLike/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "SL.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 3.0; }; @@ -373,6 +383,7 @@ ); INFOPLIST_FILE = TinderLikeTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "SL.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/TinderLike.app/TinderLike"; @@ -389,6 +400,7 @@ ); INFOPLIST_FILE = TinderLikeTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "SL.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/TinderLike.app/TinderLike"; diff --git a/Demos/TinderLike/TinderLike.xcodeproj/xcshareddata/xcschemes/TinderLike.xcscheme b/Demos/TinderLike/TinderLike.xcodeproj/xcshareddata/xcschemes/TinderLike.xcscheme index 819bb99..e4d1b7f 100644 --- a/Demos/TinderLike/TinderLike.xcodeproj/xcshareddata/xcschemes/TinderLike.xcscheme +++ b/Demos/TinderLike/TinderLike.xcodeproj/xcshareddata/xcschemes/TinderLike.xcscheme @@ -1,6 +1,6 @@ + shouldUseLaunchSchemeArgsEnv = "YES"> @@ -62,15 +62,18 @@ ReferencedContainer = "container:TinderLike.xcodeproj"> + + @@ -86,10 +89,10 @@ diff --git a/Demos/TinderLike/TinderLike/AppDelegate.swift b/Demos/TinderLike/TinderLike/AppDelegate.swift index a7a0a20..1694b20 100644 --- a/Demos/TinderLike/TinderLike/AppDelegate.swift +++ b/Demos/TinderLike/TinderLike/AppDelegate.swift @@ -43,9 +43,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { img3 = img3?.withRenderingMode(.alwaysTemplate) - let items = [UIImageView(image: img0), UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)] + let barItems = [UIImageView(image: img0), UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)] let controllers = [ctr0, ctr1, ctr2, ctr3] - controller = SLPagingViewSwift(items: items, controllers: controllers, showPageControl: false) + controller = SLPagingViewSwift(barItems: barItems, controllers: controllers, showPageControl: false) controller.pagingViewMoving = ({ subviews in if let imageViews = subviews as? [UIImageView] { diff --git a/Demos/TinderLike/TinderLike/Info.plist b/Demos/TinderLike/TinderLike/Info.plist index 8b0b716..fb4115c 100644 --- a/Demos/TinderLike/TinderLike/Info.plist +++ b/Demos/TinderLike/TinderLike/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - SL.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/Demos/TinderLike/TinderLikeTests/Info.plist b/Demos/TinderLike/TinderLikeTests/Info.plist index e598704..ba72822 100644 --- a/Demos/TinderLike/TinderLikeTests/Info.plist +++ b/Demos/TinderLike/TinderLikeTests/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - SL.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index 8f27f59..ce8bb07 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -55,12 +55,12 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } // MARK: - Constructors with items & views - public convenience init(items: [UIView], views: [UIViewController]) { - self.init(items: items, views: views, showPageControl:false, navBarBackground:UIColor.white) + public convenience init(barItems: [UIView], viewControllers: [UIViewController]) { + self.init(barItems: barItems, views: viewControllers, showPageControl:false, navBarBackground:UIColor.white) } - public convenience init(items: [UIView], views: [UIViewController], showPageControl: Bool){ - self.init(items: items, views: views, showPageControl:showPageControl, navBarBackground:UIColor.white) + public convenience init(barItems: [UIView], viewControllers: [UIViewController], showPageControl: Bool){ + self.init(barItems: barItems, views: viewControllers, showPageControl:showPageControl, navBarBackground:UIColor.white) } /* @@ -73,12 +73,12 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { * * @return Instance of SLPagingViewController */ - public init(items: [UIView], views: [UIViewController], showPageControl: Bool, navBarBackground: UIColor) { + public init(barItems: [UIView], views: [UIViewController], showPageControl: Bool, navBarBackground: UIColor) { super.init(nibName: nil, bundle: nil) needToShowPageControl = showPageControl navigationBarView.backgroundColor = navBarBackground isUserInteraction = true - for (i, v) in items.enumerated() { + for (i, v) in barItems.enumerated() { let vSize: CGSize = (v as? UILabel)?._slpGetSize() ?? v.frame.size let originX = (self.SCREENSIZE.width/2.0 - vSize.width/2.0) + CGFloat(i * 100) v.frame = CGRect(x: originX, y: 8, width: vSize.width, height: vSize.height) @@ -98,11 +98,11 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { // MARK: - Constructors with controllers public convenience init(controllers: [UIViewController]){ - self.init(controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) + self.init(viewControllers: controllers, showPageControl: true, navBarBackground: UIColor.white) } public convenience init(controllers: [UIViewController], showPageControl: Bool){ - self.init(controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) + self.init(viewControllers: controllers, showPageControl: true, navBarBackground: UIColor.white) } /* @@ -116,24 +116,24 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { * * @return Instance of SLPagingViewController */ - public convenience init(controllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ + public convenience init(viewControllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ var views = [UIView]() var items = [UILabel]() - for ctr in controllers { + for ctr in viewControllers { let item = UILabel() item.text = ctr.title views.append(ctr.view) items.append(item) } - self.init(items: items, views: controllers, showPageControl:showPageControl, navBarBackground:navBarBackground) + self.init(barItems: items, views: viewControllers, showPageControl:showPageControl, navBarBackground:navBarBackground) } // MARK: - Constructors with items & controllers - public convenience init(items: [UIView], controllers: [UIViewController]){ - self.init(items: items, controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) + public convenience init(barItems: [UIView], controllers: [UIViewController]){ + self.init(barItems: barItems, controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) } - public convenience init(items: [UIView], controllers: [UIViewController], showPageControl: Bool){ - self.init(items: items, controllers: controllers, showPageControl: showPageControl, navBarBackground: UIColor.white) + public convenience init(barItems: [UIView], controllers: [UIViewController], showPageControl: Bool){ + self.init(barItems: barItems, controllers: controllers, showPageControl: showPageControl, navBarBackground: UIColor.white) } /* @@ -146,12 +146,12 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { * * @return Instance of SLPagingViewController */ - public convenience init(items: [UIView], controllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ + public convenience init(barItems: [UIView], controllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ var views = [UIView]() for ctr in controllers { views.append(ctr.view) } - self.init(items: items, views: controllers, showPageControl:showPageControl, navBarBackground:navBarBackground) + self.init(barItems: barItems, views: controllers, showPageControl:showPageControl, navBarBackground:navBarBackground) } // MARK: - Life cycle @@ -195,7 +195,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { let xOffset = CGFloat(index) * self.SCREENSIZE.width self.scrollView.setContentOffset(CGPoint(x: xOffset, y: self.scrollView.contentOffset.y), animated: animated) - showPage(at: self.indexSelected) + showViewController(at: self.indexSelected) } // MARK: - Internal methods @@ -214,7 +214,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { self.view.addSubview(self.scrollView) // Adds all views - self.addViews() + self.setViewSizeAndLocation() if(self.needToShowPageControl){ // Make the page control @@ -234,8 +234,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } - // Loads all views - fileprivate func addViews() { + fileprivate func setViewSizeAndLocation() { if self.viewControllers.count > 0 { let width = SCREENSIZE.width * CGFloat(self.viewControllers.count) let height = self.view.frame.height @@ -243,29 +242,25 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { var i: Int = 0 while let v = viewControllers[i] { v.view.frame = CGRect(x: self.SCREENSIZE.width * CGFloat(i), y: 0, width: self.SCREENSIZE.width, height: self.SCREENSIZE.height) -// self.scrollView.addSubview(v) i += 1 } } } - func showPage(at index:Int) -> Void { + func showViewController(at index:Int) -> Void { if let prevVC = self.viewControllers[self.indexSelected] , prevVC.parent == self { prevVC.willMove(toParentViewController: nil) } if let nextVC = self.viewControllers[index] , nextVC.parent == nil { - NSLog("show page = \(index)") - nextVC.willMove(toParentViewController: self) self.addChildViewController(nextVC) self.scrollView.addSubview(nextVC.view) - nextVC.didMove(toParentViewController: self) - + nextVC.didMove(toParentViewController: self) } } - func hidePage(at index:Int) -> Void { + func hideViewController(at index:Int) -> Void { if let prevVC = self.viewControllers[index] , prevVC.parent == self { @@ -276,8 +271,6 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } - - } fileprivate func sendNewIndex(_ scrollView: UIScrollView){ @@ -289,7 +282,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } let prevIndex = self.indexSelected self.indexSelected = currentIndex - hidePage(at: prevIndex) + hideViewController(at: prevIndex) } @@ -304,7 +297,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { func tapOnHeader(_ recognizer: UITapGestureRecognizer){ if let key = recognizer.view?.tag, let view = self.viewControllers[key], self.isUserInteraction { self.scrollView.scrollRectToVisible(view.view.frame, animated: true) - showPage(at: key) + showViewController(at: key) } } @@ -342,7 +335,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { let nextIndex = xOffset / W + direction - showPage(at: nextIndex) + showViewController(at: nextIndex) } From ec00bf25be0e7bc9b7357a2d6c51e24052df7809 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Sun, 4 Dec 2016 14:50:29 +0200 Subject: [PATCH 03/14] support cancel scroll --- SLPagingViewSwift/SLPagingViewSwift.swift | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index ce8bb07..6439e13 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -256,7 +256,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { nextVC.willMove(toParentViewController: self) self.addChildViewController(nextVC) self.scrollView.addSubview(nextVC.view) - nextVC.didMove(toParentViewController: self) + nextVC.didMove(toParentViewController: self) } } @@ -280,9 +280,22 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { self.pageControl.currentPage = currentIndex self.didChangedPage?(currentIndex) } - let prevIndex = self.indexSelected - self.indexSelected = currentIndex - hideViewController(at: prevIndex) + if currentIndex != self.indexSelected { + let prevIndex = self.indexSelected + self.indexSelected = currentIndex + hideViewController(at: prevIndex) + } + else{//scroll was canceled + + for (i,vc) in self.viewControllers { + if let parent = vc.view.superview , parent == self, i != self.indexSelected + { + hideViewController(at: i) + break + } + } + } + } From 0aded3188a044c1a267ce5fad35933698f274b64 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Sun, 4 Dec 2016 15:59:54 +0200 Subject: [PATCH 04/14] Twitter Like : Swift 3 +ViewController LifeCycle --- .../TwitterLike.xcodeproj/project.pbxproj | 24 ++++- .../xcschemes/TwitterLike.xcscheme | 13 ++- .../TwitterLike/TwitterLike/AppDelegate.swift | 102 +++++++----------- Demos/TwitterLike/TwitterLike/Info.plist | 2 +- .../TwitterLike/TableViewController.swift | 65 +++++++++++ Demos/TwitterLike/TwitterLikeTests/Info.plist | 2 +- .../TwitterLikeTests/TwitterLikeTests.swift | 2 +- 7 files changed, 137 insertions(+), 73 deletions(-) create mode 100644 Demos/TwitterLike/TwitterLike/TableViewController.swift diff --git a/Demos/TwitterLike/TwitterLike.xcodeproj/project.pbxproj b/Demos/TwitterLike/TwitterLike.xcodeproj/project.pbxproj index 9305426..5261aed 100644 --- a/Demos/TwitterLike/TwitterLike.xcodeproj/project.pbxproj +++ b/Demos/TwitterLike/TwitterLike.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ F85CA98D1A618E180013CF3F /* TwitterLikeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F85CA98C1A618E180013CF3F /* TwitterLikeTests.swift */; }; F85CA9981A618E790013CF3F /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F85CA9971A618E790013CF3F /* Default-568h@2x.png */; }; F85CA9D11A6198F10013CF3F /* SLPagingViewSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = F85CA9D01A6198F10013CF3F /* SLPagingViewSwift.swift */; }; + FC7D60CB1DF451FC00700038 /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC7D60CA1DF451FC00700038 /* TableViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -35,6 +36,7 @@ F85CA9961A618E610013CF3F /* TwitterLike-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "TwitterLike-Prefix.pch"; sourceTree = ""; }; F85CA9971A618E790013CF3F /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default-568h@2x.png"; path = "../Default-568h@2x.png"; sourceTree = ""; }; F85CA9D01A6198F10013CF3F /* SLPagingViewSwift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SLPagingViewSwift.swift; path = ../../SLPagingViewSwift/SLPagingViewSwift.swift; sourceTree = ""; }; + FC7D60CA1DF451FC00700038 /* TableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -78,6 +80,7 @@ isa = PBXGroup; children = ( F85CA9761A618E180013CF3F /* AppDelegate.swift */, + FC7D60CA1DF451FC00700038 /* TableViewController.swift */, F85CA97D1A618E180013CF3F /* Images.xcassets */, F85CA9741A618E180013CF3F /* Supporting Files */, ); @@ -163,14 +166,16 @@ F85CA9691A618E170013CF3F /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0610; + LastUpgradeCheck = 0810; ORGANIZATIONNAME = "Stefan Lage"; TargetAttributes = { F85CA9701A618E180013CF3F = { CreatedOnToolsVersion = 6.1.1; + LastSwiftMigration = 0810; }; F85CA9851A618E180013CF3F = { CreatedOnToolsVersion = 6.1.1; + LastSwiftMigration = 0810; TestTargetID = F85CA9701A618E180013CF3F; }; }; @@ -219,6 +224,7 @@ buildActionMask = 2147483647; files = ( F85CA9771A618E180013CF3F /* AppDelegate.swift in Sources */, + FC7D60CB1DF451FC00700038 /* TableViewController.swift in Sources */, F85CA9D11A6198F10013CF3F /* SLPagingViewSwift.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -255,15 +261,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", @@ -297,8 +307,10 @@ 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"; @@ -306,6 +318,7 @@ 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; @@ -315,6 +328,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.1; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; VALIDATE_PRODUCT = YES; }; name = Release; @@ -325,7 +339,9 @@ GCC_PREFIX_HEADER = "$SRCROOT/$PROJECT_NAME/TwitterLike-Prefix.pch"; INFOPLIST_FILE = TwitterLike/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "SL.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -335,7 +351,9 @@ GCC_PREFIX_HEADER = "$SRCROOT/$PROJECT_NAME/TwitterLike-Prefix.pch"; INFOPLIST_FILE = TwitterLike/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "SL.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -353,7 +371,9 @@ ); INFOPLIST_FILE = TwitterLikeTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "SL.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/TwitterLike.app/TwitterLike"; }; name = Debug; @@ -368,7 +388,9 @@ ); INFOPLIST_FILE = TwitterLikeTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "SL.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/TwitterLike.app/TwitterLike"; }; name = Release; diff --git a/Demos/TwitterLike/TwitterLike.xcodeproj/xcshareddata/xcschemes/TwitterLike.xcscheme b/Demos/TwitterLike/TwitterLike.xcodeproj/xcshareddata/xcschemes/TwitterLike.xcscheme index 511bbda..a464eb6 100644 --- a/Demos/TwitterLike/TwitterLike.xcodeproj/xcshareddata/xcschemes/TwitterLike.xcscheme +++ b/Demos/TwitterLike/TwitterLike.xcodeproj/xcshareddata/xcschemes/TwitterLike.xcscheme @@ -1,6 +1,6 @@ + shouldUseLaunchSchemeArgsEnv = "YES"> @@ -62,15 +62,18 @@ ReferencedContainer = "container:TwitterLike.xcodeproj"> + + @@ -86,10 +89,10 @@ diff --git a/Demos/TwitterLike/TwitterLike/AppDelegate.swift b/Demos/TwitterLike/TwitterLike/AppDelegate.swift index d9eea6c..9220a6c 100644 --- a/Demos/TwitterLike/TwitterLike/AppDelegate.swift +++ b/Demos/TwitterLike/TwitterLike/AppDelegate.swift @@ -9,38 +9,35 @@ import UIKit @UIApplicationMain -class AppDelegate: UIResponder, UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource { - +class AppDelegate: UIResponder, UIApplicationDelegate{ + var window: UIWindow? var nav: UINavigationController! var controller: SLPagingViewSwift! - var dataSource: [String]! - - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - self.window = UIWindow(frame: UIScreen.mainScreen().bounds) + + func applicationDidFinishLaunching(_ application: UIApplication) { - self.dataSource = ["Hello world!", "Shaqtin' a fool!", "YEAHHH!", - "Hello world!", "Shaqtin' a fool!", "YEAHHH!", - "Hello world!", "Shaqtin' a fool!", "YEAHHH!", - "Hello world!", "Shaqtin' a fool!", "YEAHHH!", - "Hello world!", "Shaqtin' a fool!", "YEAHHH!"] + self.window = UIWindow(frame: UIScreen.main.bounds) - var navTitleLabel1 = UILabel() + let navTitleLabel1 = UILabel() navTitleLabel1.text = "Home" navTitleLabel1.font = UIFont(name: "Helvetica", size: 20) - navTitleLabel1.textColor = UIColor.whiteColor() - var navTitleLabel2 = UILabel() + navTitleLabel1.textColor = UIColor.white + let navTitleLabel2 = UILabel() navTitleLabel2.text = "Discover" navTitleLabel2.font = UIFont(name: "Helvetica", size: 20) - navTitleLabel2.textColor = UIColor.whiteColor() - var navTitleLabel3 = UILabel() + navTitleLabel2.textColor = UIColor.white + let navTitleLabel3 = UILabel() navTitleLabel3.text = "Activity" navTitleLabel3.font = UIFont(name: "Helvetica", size: 20) - navTitleLabel3.textColor = UIColor.whiteColor() + navTitleLabel3.textColor = UIColor.white - controller = SLPagingViewSwift(items: [navTitleLabel1, navTitleLabel2, navTitleLabel3], views: [self.tableView(), self.tableView(), self.tableView()], showPageControl: true, navBarBackground: UIColor(red: 0.33, green: 0.68, blue: 0.91, alpha: 1.0)) + let barItems = [navTitleLabel1, navTitleLabel2, navTitleLabel3] + let vcs = [TableViewController(style: .plain), TableViewController(style: .plain), TableViewController(style: .plain)] - controller.currentPageControlColor = UIColor.whiteColor() + controller = SLPagingViewSwift(barItems: barItems, views: vcs, showPageControl: true, navBarBackground:UIColor(red: 0.33, green: 0.68, blue: 0.91, alpha: 1.0)) + + controller.currentPageControlColor = UIColor.white controller.tintPageControlColor = UIColor(white: 0.799, alpha: 1.0) controller.pagingViewMovingRedefine = ({ scrollView, subviews in var i = 0 @@ -59,82 +56,59 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UITableViewDelegate, UITa alpha = 1.0 } lbl.alpha = alpha - i++ + i += 1 } } }) controller.didChangedPage = ({ currentIndex in - println(currentIndex) + NSLog("\(currentIndex)") }) self.nav = UINavigationController(rootViewController: self.controller) self.window?.rootViewController = self.nav - self.window?.backgroundColor = UIColor.whiteColor() + self.window?.backgroundColor = UIColor.white self.window?.makeKeyAndVisible() - return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } - func tableView() -> UITableView { - var frame = CGRectMake(0, 0, 320, 568) - frame.size.height -= 44 - var tableView = UITableView(frame: frame, style: .Plain) - tableView.delegate = self - tableView.dataSource = self - tableView.scrollsToTop = false - return tableView - } - - // MARK: - UITableView DataSource - - func numberOfSectionsInTableView(tableView: UITableView) -> Int { - return 1 - } - - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return self.dataSource.count - } - - func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - return 120.0 - } - - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - var cellIdentifier = "cellIdentifier" - var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell - if cell == nil { - cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier) - cell?.textLabel?.numberOfLines = 0 - } - cell!.imageView?.image = UIImage(named: "avatar_\(indexPath.row % 3).png") - cell!.textLabel!.text = self.dataSource[indexPath.row] - return cell! - } - } + + + + + + + + + + + + + + diff --git a/Demos/TwitterLike/TwitterLike/Info.plist b/Demos/TwitterLike/TwitterLike/Info.plist index 8b0b716..fb4115c 100644 --- a/Demos/TwitterLike/TwitterLike/Info.plist +++ b/Demos/TwitterLike/TwitterLike/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - SL.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/Demos/TwitterLike/TwitterLike/TableViewController.swift b/Demos/TwitterLike/TwitterLike/TableViewController.swift new file mode 100644 index 0000000..55330ab --- /dev/null +++ b/Demos/TwitterLike/TwitterLike/TableViewController.swift @@ -0,0 +1,65 @@ +// +// TableViewController.swift +// TwitterLike +// +// Created by Bari Levi on 04/12/2016. +// Copyright © 2016 Stefan Lage. All rights reserved. +// + +import Foundation +import UIKit + +public class TableViewController: UITableViewController +{ + var dataSource: [String]! + + + + override init(style: UITableViewStyle) { + super.init(style: .plain) + self.view.frame = CGRect(x:0, y:0, width:320, height:568) + self.tableView.delegate = self + self.tableView.dataSource = self + self.tableView.scrollsToTop = false + + self.dataSource = ["Hello world!", "Shaqtin' a fool!", "YEAHHH!", + "Hello world!", "Shaqtin' a fool!", "YEAHHH!", + "Hello world!", "Shaqtin' a fool!", "YEAHHH!", + "Hello world!", "Shaqtin' a fool!", "YEAHHH!", + "Hello world!", "Shaqtin' a fool!", "YEAHHH!"] + } + + required public init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + + // MARK: - UITableView DataSource + + override public func numberOfSections(in tableView: UITableView) -> Int { + return 1 + } + + override public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ + return self.dataSource.count + } + + override public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { + return 120.0 + } + + override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cellIdentifier = "cellIdentifier" + + var cell = tableView.dequeueReusableCell(withIdentifier:cellIdentifier) + if cell == nil { + cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier) + cell?.textLabel?.numberOfLines = 0 + } + cell!.imageView?.image = UIImage(named: "avatar_\(indexPath.row % 3).png") + cell!.textLabel!.text = self.dataSource[indexPath.row] + + return cell! + } + +} diff --git a/Demos/TwitterLike/TwitterLikeTests/Info.plist b/Demos/TwitterLike/TwitterLikeTests/Info.plist index e598704..ba72822 100644 --- a/Demos/TwitterLike/TwitterLikeTests/Info.plist +++ b/Demos/TwitterLike/TwitterLikeTests/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - SL.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/Demos/TwitterLike/TwitterLikeTests/TwitterLikeTests.swift b/Demos/TwitterLike/TwitterLikeTests/TwitterLikeTests.swift index 37c516e..722e1a7 100644 --- a/Demos/TwitterLike/TwitterLikeTests/TwitterLikeTests.swift +++ b/Demos/TwitterLike/TwitterLikeTests/TwitterLikeTests.swift @@ -28,7 +28,7 @@ class TwitterLikeTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock() { + self.measure() { // Put the code you want to measure the time of here. } } From 9a8b64023ea22cf5c4ff54b551afbfa4471167ee Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Wed, 14 Dec 2016 17:29:56 +0200 Subject: [PATCH 05/14] bouncing = true + bg white --- SLPagingViewSwift/SLPagingViewSwift.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index 6439e13..06ae20f 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -160,6 +160,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { // Do any additional setup after loading the view, typically from a nib. self.setupPagingProcess() self.setCurrentIndex(self.indexSelected, animated: false) + self.view.backgroundColor = UIColor.white } open override func viewWillLayoutSubviews() { @@ -208,7 +209,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { self.scrollView.showsHorizontalScrollIndicator = false self.scrollView.showsVerticalScrollIndicator = false self.scrollView.delegate = self - self.scrollView.bounces = false + self.scrollView.bounces = true self.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -80, right: 0) self.view.addSubview(self.scrollView) From 36338161b53c232fb902463d64d7c247777baf8b Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Mon, 19 Dec 2016 08:30:52 +0200 Subject: [PATCH 06/14] add nav bar header --- .../TinderLike.xcodeproj/project.pbxproj | 6 ++ Demos/TinderLike/TinderLike/AppDelegate.swift | 69 +++++++++---- .../1481719609_left.png | Bin 0 -> 745 bytes .../nav_back_left.imageset/Contents.json | 21 ++++ .../1481719801_right.png | Bin 0 -> 738 bytes .../nav_back_right.imageset/Contents.json | 21 ++++ Demos/TinderLike/TinderLike/VC3.swift | 12 +++ SLPagingViewSwift/SLPagingViewSwift.swift | 91 +++++++++++++----- 8 files changed, 178 insertions(+), 42 deletions(-) create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/1481719609_left.png create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/Contents.json create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back_right.imageset/1481719801_right.png create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back_right.imageset/Contents.json diff --git a/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj b/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj index 8fdc105..a8b60b0 100644 --- a/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj +++ b/Demos/TinderLike/TinderLike.xcodeproj/project.pbxproj @@ -180,10 +180,12 @@ TargetAttributes = { F85CA9A41A61963B0013CF3F = { CreatedOnToolsVersion = 6.1.1; + DevelopmentTeam = M4BCYUQAZQ; LastSwiftMigration = 0810; }; F85CA9B91A61963B0013CF3F = { CreatedOnToolsVersion = 6.1.1; + DevelopmentTeam = M4BCYUQAZQ; LastSwiftMigration = 0810; TestTargetID = F85CA9A41A61963B0013CF3F; }; @@ -348,6 +350,7 @@ F85CA9C51A61963B0013CF3F /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + DEVELOPMENT_TEAM = M4BCYUQAZQ; GCC_PREFIX_HEADER = "$SRCROOT/$PROJECT_NAME/TinderLike-Prefix.pch"; INFOPLIST_FILE = TinderLike/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -360,6 +363,7 @@ F85CA9C61A61963B0013CF3F /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + DEVELOPMENT_TEAM = M4BCYUQAZQ; GCC_PREFIX_HEADER = "$SRCROOT/$PROJECT_NAME/TinderLike-Prefix.pch"; INFOPLIST_FILE = TinderLike/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -373,6 +377,7 @@ isa = XCBuildConfiguration; buildSettings = { BUNDLE_LOADER = "$(TEST_HOST)"; + DEVELOPMENT_TEAM = M4BCYUQAZQ; FRAMEWORK_SEARCH_PATHS = ( "$(SDKROOT)/Developer/Library/Frameworks", "$(inherited)", @@ -394,6 +399,7 @@ isa = XCBuildConfiguration; buildSettings = { BUNDLE_LOADER = "$(TEST_HOST)"; + DEVELOPMENT_TEAM = M4BCYUQAZQ; FRAMEWORK_SEARCH_PATHS = ( "$(SDKROOT)/Developer/Library/Frameworks", "$(inherited)", diff --git a/Demos/TinderLike/TinderLike/AppDelegate.swift b/Demos/TinderLike/TinderLike/AppDelegate.swift index 1694b20..aee8fa0 100644 --- a/Demos/TinderLike/TinderLike/AppDelegate.swift +++ b/Demos/TinderLike/TinderLike/AppDelegate.swift @@ -17,7 +17,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) - let orange = UIColor(red: 255/255, green: 69.0/255, blue: 0.0/255, alpha: 1.0) +// let orange = UIColor(red: 255/255, green: 69.0/255, blue: 0.0/255, alpha: 1.0) + let orange = UIColor(red: 0/255, green: 0.0/255, blue: 255.0/255, alpha: 1.0) let gray = UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.0) let ctr0 = VC0() @@ -33,36 +34,64 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ctr3.title = "Ctr3" ctr3.view.backgroundColor = gray - var img0 = UIImage(named: "chat") + var img0 = UIImage(named: "nav_back_left") img0 = img0?.withRenderingMode(.alwaysTemplate) - var img1 = UIImage(named: "gear") + var img1 = UIImage(named: "nav_back_left") img1 = img1?.withRenderingMode(.alwaysTemplate) - var img2 = UIImage(named: "profile") + var img2 = UIImage(named: "nav_back_left") img2 = img2?.withRenderingMode(.alwaysTemplate) - var img3 = UIImage(named: "chat") + var img3 = UIImage(named: "nav_back_left") img3 = img3?.withRenderingMode(.alwaysTemplate) + var imgSelected0 = UIImage(named: "chat") + imgSelected0 = imgSelected0?.withRenderingMode(.alwaysTemplate) + var imgSelected1 = UIImage(named: "gear") + imgSelected1 = imgSelected1?.withRenderingMode(.alwaysTemplate) + var imgSelected2 = UIImage(named: "profile") + imgSelected2 = imgSelected2?.withRenderingMode(.alwaysTemplate) + var imgSelected3 = UIImage(named: "chat") + imgSelected3 = imgSelected3?.withRenderingMode(.alwaysTemplate) + + let barItemsSelected = [UIImageView(image: imgSelected0), UIImageView(image: imgSelected1), UIImageView(image: imgSelected2), UIImageView(image: imgSelected3)] let barItems = [UIImageView(image: img0), UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)] - let controllers = [ctr0, ctr1, ctr2, ctr3] - controller = SLPagingViewSwift(barItems: barItems, controllers: controllers, showPageControl: false) +// for v in barItems +// { +// AppDelegate.addLayer(view: v) +// } + let controllers = [ctr0, ctr1, ctr2, ctr3] + controller = SLPagingViewSwift(barItems: barItems, barItemsSelected: barItemsSelected, controllers: controllers, showPageControl: false) + controller.navigationSideItemsStyle = .slNavigationSideItemsStyleOnBounds + controller.pagingViewMovingRedefine = ( { scrollView, subviews in + + }) controller.pagingViewMoving = ({ subviews in - if let imageViews = subviews as? [UIImageView] { - for imgView in imageViews { - var c = gray - let originX = Double(imgView.frame.origin.x) + + if let navBarItems = subviews as? [NavBarHeader] { + for navBarItem in navBarItems { - if (originX > 45 && originX < 145) { - c = self.gradient(originX, topX: 46, bottomX: 144, initC: orange, goal: gray) - } - else if (originX > 145 && originX < 245) { - c = self.gradient(originX, topX: 146, bottomX: 244, initC: gray, goal: orange) - } - else if(originX == 145){ - c = orange + let originX = CGFloat(navBarItem.frame.origin.x) + + let distance = CGFloat( self.controller.navigationSideItemsStyle.rawValue) + + let vSize = navBarItem.frame.size + let margin = self.controller.getOriginX(vSize, idx: CGFloat(navBarItem.tag), distance: CGFloat(distance), xOffset: 0)-100 + + + let screenCenter = CGFloat(UIScreen.main.bounds.width/2) + let scale = CGFloat(screenCenter-margin) + let xOnScale = CGFloat(abs(originX - screenCenter)) + let alpha = xOnScale/scale + + if navBarItem.tag == 0 + { + print("originX: \(originX) alpha: \(alpha) xOnScale: \(xOnScale) scale:\(scale) margin: \(margin)" ) } - imgView.tintColor = c + +// let alpha = CGFloat(abs(originX - 145))/(145-45) + navBarItem.regularView.alpha = alpha + navBarItem.selectedView.alpha = 1.0 - alpha } } }) diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/1481719609_left.png b/Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/1481719609_left.png new file mode 100644 index 0000000000000000000000000000000000000000..a3e8e51269102d6e08d9d4bac2a2f5118a75e591 GIT binary patch literal 745 zcmVWr6=*4F=|Tl97og$*$q8%$?OGO!XcG~nU0YPp63F`CV0Cw12r5Aav{ix`f&%8ctGh&}?P>wy19V+3Hqd$sYE*iGu=3z=2Mn~HCJieOjsTik9vlFyD{tXsg?%KkJUG4wT$|NO z-NK6f`MvfBJIu1N!A{HGSDnt5>*skck-uMX1Z|@3Lz)}R6!spLT~_(g%AY*Xv5CH)35X1mL90XAS5FSAs08~e~(@*z*dX-f0BaD={x1LQ#_oWNoFklGL2&q}O*{a=vdBMQA{dUPK4CwHZjH2qlt{U;EyZ{EWHN z-95k9jub_i@K8e1Y{mJtdAbehex8{tSqZf_bFWMmnkFhG(d z*PT{toWUU?cQ(zjjG*!mOp@d}3KT)@XE0O*H2??`K@|YPLr@2R+z7*FtCL>MXP?VB zL*A4E0J#uE1n&r?0YDfCr2>F`gwg@PHbNx;U>Bh>0Qig0jiL`}mK|9^s1yKTKA-18 zXtg^xRUlLj0QnFQMpYnq0f2P`IYjV=;0*v)2<@>?1g`+#AHh2Sn(XC`QJJH-* zG#4wHIF5UZ%$?$Ruzgl`a(QuaptYydT1X?4$>di<(h(`&x6hJi`*D1%wWlcn$cZ5C z;L($(A2s$fbqlN~j^p0%RKCcL^tR2(QjWiUJ%3(gA9Gj0DruUENLKp!OaiRlHsM@% z+Z>H=9}M0C+g`P;N2@#-1E{(@7`g>=l+PUURB zp#*@C%7ZdMXyri>AjI;Z1Q2R@Pyh(IJV2x90*jr-dk1@4hE10Th5rN_H;b2~>5mOV z=F5ZPGawarigWju9|ch!l>QZ-PNzQ`!f^RMFPQS`iQ~ApKN!67eU3VH>eR`bKWxD5 UM3xYTYybcN07*qoM6N<$f ()) public typealias SLPagingViewMovingRedefine = ((_ scrollView: UIScrollView, _ subviews: NSArray)-> ()) public typealias SLPagingViewDidChanged = ((_ currentPage: Int)-> ()) +class NavBarHeader: UIView { + + var regularView:UIView + var selectedView:UIView + + init( regularView:UIView, selectedView:UIView) { + + self.regularView = regularView + self.selectedView = selectedView + super.init(frame: CGRect.zero)//(x: 0, y: 0, width: 0, height: 0)) + + self.addSubview(self.regularView) + self.addSubview(self.selectedView) + +// setSelected(perc: 0) + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + func setSelected(perc:CGFloat) -> Void { + + self.regularView.alpha = 1 - perc + self.selectedView.alpha = perc + } +} open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { // MARK: - Public properties @@ -36,10 +62,10 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { fileprivate var SCREENSIZE: CGSize { return UIScreen.main.bounds.size } - fileprivate var scrollView: UIScrollView! + open var scrollView: UIScrollView! fileprivate var pageControl: UIPageControl! fileprivate var navigationBarView: UIView = UIView() - fileprivate var navItems: [UIView] = [] + fileprivate var navItems: [NavBarHeader] = [] fileprivate var needToShowPageControl: Bool = false fileprivate var isUserInteraction: Bool = false fileprivate var indexSelected: Int = 0 @@ -55,12 +81,12 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } // MARK: - Constructors with items & views - public convenience init(barItems: [UIView], viewControllers: [UIViewController]) { - self.init(barItems: barItems, views: viewControllers, showPageControl:false, navBarBackground:UIColor.white) + public convenience init(barItems: [UIView], barItemsSelected: [UIView], viewControllers: [UIViewController]) { + self.init(barItems: barItems, barItemsSelected: barItemsSelected, views: viewControllers, showPageControl:false, navBarBackground:UIColor.white) } - public convenience init(barItems: [UIView], viewControllers: [UIViewController], showPageControl: Bool){ - self.init(barItems: barItems, views: viewControllers, showPageControl:showPageControl, navBarBackground:UIColor.white) + public convenience init(barItems: [UIView], barItemsSelected: [UIView], viewControllers: [UIViewController], showPageControl: Bool){ + self.init(barItems: barItems, barItemsSelected: barItemsSelected, views: viewControllers, showPageControl:showPageControl, navBarBackground:UIColor.white) } /* @@ -73,21 +99,33 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { * * @return Instance of SLPagingViewController */ - public init(barItems: [UIView], views: [UIViewController], showPageControl: Bool, navBarBackground: UIColor) { + + public init(barItems: [UIView], barItemsSelected: [UIView], views: [UIViewController], showPageControl: Bool, navBarBackground: UIColor) { super.init(nibName: nil, bundle: nil) + needToShowPageControl = showPageControl navigationBarView.backgroundColor = navBarBackground isUserInteraction = true - for (i, v) in barItems.enumerated() { - let vSize: CGSize = (v as? UILabel)?._slpGetSize() ?? v.frame.size + + for (i, vRegular) in barItems.enumerated() { + let vSelected = barItemsSelected[i] + let navBarHeader = NavBarHeader(regularView: vRegular, selectedView: vSelected) + + let vSize: CGSize = (navBarHeader as? UILabel)?._slpGetSize() ?? navBarHeader.frame.size let originX = (self.SCREENSIZE.width/2.0 - vSize.width/2.0) + CGFloat(i * 100) - v.frame = CGRect(x: originX, y: 8, width: vSize.width, height: vSize.height) - v.tag = i + navBarHeader.frame = CGRect(x: originX, y: 8, width: vSize.width, height: vSize.height) +// for v in navBarHeader.subviews { +// +// for layer in v.layer.sublayers! { +// layer.bounds = CGRect(x: CGFloat(v.frame.origin.x), y: CGFloat(v.frame.origin.y), width: navBarHeader.frame.size.width, height: navBarHeader.frame.size.height) +// } +// } + navBarHeader.tag = i let tap = UITapGestureRecognizer(target: self, action: #selector(SLPagingViewSwift.tapOnHeader(_:))) - v.addGestureRecognizer(tap) - v.isUserInteractionEnabled = true - self.navigationBarView.addSubview(v) - self.navItems.append(v) + navBarHeader.addGestureRecognizer(tap) + navBarHeader.isUserInteractionEnabled = true + self.navigationBarView.addSubview(navBarHeader) + self.navItems.append(navBarHeader) } for (i, view) in views.enumerated() { @@ -119,21 +157,25 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { public convenience init(viewControllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ var views = [UIView]() var items = [UILabel]() + var itemsSel = [UILabel]() for ctr in viewControllers { let item = UILabel() + let itemSel = UILabel() item.text = ctr.title + itemSel.text = ctr.title views.append(ctr.view) items.append(item) + itemsSel.append(itemSel) } - self.init(barItems: items, views: viewControllers, showPageControl:showPageControl, navBarBackground:navBarBackground) + self.init(barItems: items, barItemsSelected: itemsSel, views: viewControllers, showPageControl:showPageControl, navBarBackground:navBarBackground) } // MARK: - Constructors with items & controllers - public convenience init(barItems: [UIView], controllers: [UIViewController]){ - self.init(barItems: barItems, controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) + public convenience init(barItems: [UIView], barItemsSelected: [UIView], controllers: [UIViewController]){ + self.init(barItems: barItems, barItemsSelected:barItemsSelected, controllers: controllers, showPageControl: true, navBarBackground: UIColor.white) } - public convenience init(barItems: [UIView], controllers: [UIViewController], showPageControl: Bool){ - self.init(barItems: barItems, controllers: controllers, showPageControl: showPageControl, navBarBackground: UIColor.white) + public convenience init(barItems: [UIView], barItemsSelected: [UIView], controllers: [UIViewController], showPageControl: Bool){ + self.init(barItems: barItems, barItemsSelected: barItemsSelected, controllers: controllers, showPageControl: showPageControl, navBarBackground: UIColor.white) } /* @@ -146,12 +188,12 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { * * @return Instance of SLPagingViewController */ - public convenience init(barItems: [UIView], controllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ + public convenience init(barItems: [UIView], barItemsSelected: [UIView], controllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ var views = [UIView]() for ctr in controllers { views.append(ctr.view) } - self.init(barItems: barItems, views: controllers, showPageControl:showPageControl, navBarBackground:navBarBackground) + self.init(barItems: barItems, barItemsSelected: barItemsSelected, views: controllers, showPageControl:showPageControl, navBarBackground:navBarBackground) } // MARK: - Life cycle @@ -196,6 +238,11 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { let xOffset = CGFloat(index) * self.SCREENSIZE.width self.scrollView.setContentOffset(CGPoint(x: xOffset, y: self.scrollView.contentOffset.y), animated: animated) + for (i , item) in navItems.enumerated() + { + let perc:CGFloat = (i == index) ? 1 : 0 + item.setSelected(perc: perc) + } showViewController(at: self.indexSelected) } From 8f3dedb5f868c317f1a3df90d5d4f7fb79636ee9 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Mon, 19 Dec 2016 15:22:43 +0200 Subject: [PATCH 07/14] alignment --- Demos/TinderLike/TinderLike/AppDelegate.swift | 33 +++++---- .../Contents.json | 4 +- .../nav_back.imageset/nav_back.png | Bin 0 -> 349 bytes .../nav_back.imageset/nav_back@2x.png | Bin 0 -> 589 bytes .../nav_back.imageset/nav_back@3x.png | Bin 0 -> 868 bytes .../1481719609_left.png | Bin 745 -> 0 bytes .../1481719801_right.png | Bin 738 -> 0 bytes .../Contents.json | 4 +- .../nav_forward.imageset/nav_forward.png | Bin 0 -> 341 bytes .../nav_forward.imageset/nav_forward@2x.png | Bin 0 -> 602 bytes .../nav_forward.imageset/nav_forward@3x.png | Bin 0 -> 884 bytes SLPagingViewSwift/SLPagingViewSwift.swift | 69 +++++++++++++----- 12 files changed, 76 insertions(+), 34 deletions(-) rename Demos/TinderLike/TinderLike/Images.xcassets/{nav_back_left.imageset => nav_back.imageset}/Contents.json (70%) create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back.png create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back@2x.png create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back@3x.png delete mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/1481719609_left.png delete mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_back_right.imageset/1481719801_right.png rename Demos/TinderLike/TinderLike/Images.xcassets/{nav_back_right.imageset => nav_forward.imageset}/Contents.json (69%) create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_forward.imageset/nav_forward.png create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_forward.imageset/nav_forward@2x.png create mode 100644 Demos/TinderLike/TinderLike/Images.xcassets/nav_forward.imageset/nav_forward@3x.png diff --git a/Demos/TinderLike/TinderLike/AppDelegate.swift b/Demos/TinderLike/TinderLike/AppDelegate.swift index aee8fa0..9ba3fc1 100644 --- a/Demos/TinderLike/TinderLike/AppDelegate.swift +++ b/Demos/TinderLike/TinderLike/AppDelegate.swift @@ -30,18 +30,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate { let ctr2 = VC2() ctr2.title = "Ctr2" ctr2.view.backgroundColor = UIColor.yellow - let ctr3 = VC3() - ctr3.title = "Ctr3" - ctr3.view.backgroundColor = gray + //let ctr3 = VC3() + //ctr3.title = "Ctr3" + //ctr3.view.backgroundColor = gray - var img0 = UIImage(named: "nav_back_left") + var img0 = UIImage(named: "chat") img0 = img0?.withRenderingMode(.alwaysTemplate) - var img1 = UIImage(named: "nav_back_left") + var img1 = UIImage(named: "nav_forward") img1 = img1?.withRenderingMode(.alwaysTemplate) - var img2 = UIImage(named: "nav_back_left") + var img2 = UIImage(named: "profile") img2 = img2?.withRenderingMode(.alwaysTemplate) - var img3 = UIImage(named: "nav_back_left") - img3 = img3?.withRenderingMode(.alwaysTemplate) +// var img3 = UIImage(named: "nav_back_left") +// img3 = img3?.withRenderingMode(.alwaysTemplate) var imgSelected0 = UIImage(named: "chat") imgSelected0 = imgSelected0?.withRenderingMode(.alwaysTemplate) @@ -49,18 +49,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate { imgSelected1 = imgSelected1?.withRenderingMode(.alwaysTemplate) var imgSelected2 = UIImage(named: "profile") imgSelected2 = imgSelected2?.withRenderingMode(.alwaysTemplate) - var imgSelected3 = UIImage(named: "chat") - imgSelected3 = imgSelected3?.withRenderingMode(.alwaysTemplate) +// var imgSelected3 = UIImage(named: "chat") +// imgSelected3 = imgSelected3?.withRenderingMode(.alwaysTemplate) + let title = UILabel() + title.text = "BBBBBBBB" + title.frame = CGRect(x: 0, y: 0, width: 110, height: 22) + title.frame.size = title._slpGetSize()! - let barItemsSelected = [UIImageView(image: imgSelected0), UIImageView(image: imgSelected1), UIImageView(image: imgSelected2), UIImageView(image: imgSelected3)] - let barItems = [UIImageView(image: img0), UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)] + let barItemsSelected = [UIImageView(image: imgSelected0), title, UIImageView(image: imgSelected2)]//, UIImageView(image: imgSelected3)] + let barItems = [UIImageView(image: img0), + UIImageView(image: img1), + UIImageView(image: img2)] + //UIImageView(image: img3)] // for v in barItems // { // AppDelegate.addLayer(view: v) // } - let controllers = [ctr0, ctr1, ctr2, ctr3] + let controllers = [ctr0, ctr1, ctr2] controller = SLPagingViewSwift(barItems: barItems, barItemsSelected: barItemsSelected, controllers: controllers, showPageControl: false) controller.navigationSideItemsStyle = .slNavigationSideItemsStyleOnBounds controller.pagingViewMovingRedefine = ( { scrollView, subviews in diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/Contents.json b/Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/Contents.json similarity index 70% rename from Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/Contents.json rename to Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/Contents.json index 91d1860..65a64df 100644 --- a/Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/Contents.json +++ b/Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/Contents.json @@ -2,15 +2,17 @@ "images" : [ { "idiom" : "universal", + "filename" : "nav_back.png", "scale" : "1x" }, { "idiom" : "universal", - "filename" : "1481719609_left.png", + "filename" : "nav_back@2x.png", "scale" : "2x" }, { "idiom" : "universal", + "filename" : "nav_back@3x.png", "scale" : "3x" } ], diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back.png b/Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back.png new file mode 100644 index 0000000000000000000000000000000000000000..5305e7347ac622e4f6f94f5d8e4b5861a3a3e084 GIT binary patch literal 349 zcmV-j0iyniP)-t3%E3lprgAVn zXvN^7I<^K@=nse~RaLVyfpQb2FmdA8_;R_E?Ry@aj)?huZiO!1;Vs_x`~4686SVgf zQygI(2O<*fY+{NRN(FXBB$|7T6FgHYFv3p}iDnM*5nZJMBg`z3sF&gs_EZXNW2Q!8 zxea{9K&8MA&aIG$dyY%=R0@poLjytFDS9e@FvfQc1#wUOtAn70Aa00jl{MtZG!(@B z!T{e?Qsl@q5G=RA8yu^o$dOqgsCSP;?Bh-;MUG63pqbCu!f&M%IWkKG&0XLnu9Q;b v*b))6bAwknQ%dngM9|&>LridsiAUit>qIf|SLj1|00000NkvXXu0mjf*C3GQ literal 0 HcmV?d00001 diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back@2x.png b/Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..82645d9197ca53b2c7cc38133e3410eea67fa454 GIT binary patch literal 589 zcmeAS@N?(olHy`uVBq!ia0vp^Iv~u!1|;QLq8Nb`lc$SgNCo2?sk%!*4jYK~_Mmlk za3TZ4h0Fct)puCjaNWzdh*atD4q_t^NH=k_M6eXoN;)_7O63tw8hpznQ^fM_yD{|6q& zU7=Stu5}Om-V&4ANweHc+=X2eY&h2RmIH47D~6J zvTg)TSW)eN;lfR+S2uzdEU+%Wc;Tkis~aI36!v#lEZtmsWy4Aq$GUt2mD}b(rix>o zd#RYq%`ovZX%pZ@4QU7-k4vrAw~IzPWbYwTOqjE7t1 zG<`aoEM$Glx;xU6wfQ4!a>TWJE9NvEym`RF?&kZQa~2)^)vLXuxte3fv1)d{KkEM& Z96u*nXkR$_3Yb0^JYD@<);T3K0RU}=0?_~f literal 0 HcmV?d00001 diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back@3x.png b/Demos/TinderLike/TinderLike/Images.xcassets/nav_back.imageset/nav_back@3x.png new file mode 100644 index 0000000000000000000000000000000000000000..935594be30df08b36dcf205fff3b4b524d64bcf9 GIT binary patch literal 868 zcmV-q1DpJbP)}u|FW-UiWug4pFlm#rH@&*k5vpnknEkKEZ_1oRve=JUeh62aWDLzLG=K zEYIL7o;SKhyo;e6qULxL7xAdkEn*%6IYrHI3|}G|-6G~OP=Kgj+wcwElg>rVW1t{W zeICa$UX#ux%weEFQ9WM96+9`OOXy&rU{UoB;|EMiX9*n)lpw0k1U|>7n2^pAW-(Bb zs2Y#p9Nv-M5;_r3pjw=(rcrKM1_L#T*n^V zl3p8qBq|b=YZcSzO0R`J5)}x_w}u&mrLFo$*Nw9rSQL_xJK-~et* zr-eQeB?+o`9eZ$7IxX~(C_zwiD_*M?l u*6|Mx;V_Ql1^j}OxMpl@Y;0_-GXDZ`hM<6A-13S50000?SjMr literal 0 HcmV?d00001 diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/1481719609_left.png b/Demos/TinderLike/TinderLike/Images.xcassets/nav_back_left.imageset/1481719609_left.png deleted file mode 100644 index a3e8e51269102d6e08d9d4bac2a2f5118a75e591..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 745 zcmVWr6=*4F=|Tl97og$*$q8%$?OGO!XcG~nU0YPp63F`CV0Cw12r5Aav{ix`f&%8ctGh&}?P>wy19V+3Hqd$sYE*iGu=3z=2Mn~HCJieOjsTik9vlFyD{tXsg?%KkJUG4wT$|NO z-NK6f`MvfBJIu1N!A{HGSDnt5>*skck-uMX1Z|@3Lz)}R6!spLT~_(g%AY*Xv5CH)35X1mL90XAS5FSAs08~e~(@*z*dX-f0BaD={x1LQ#_oWNoFklGL2&q}O*{a=vdBMQA{dUPK4CwHZjH2qlt{U;EyZ{EWHN z-95k9jub_i@K8e1Y{mJtdAbehex8{tSqZf_bFWMmnkFhG(d z*PT{toWUU?cQ(zjjG*!mOp@d}3KT)@XE0O*H2??`K@|YPLr@2R+z7*FtCL>MXP?VB zL*A4E0J#uE1n&r?0YDfCr2>F`gwg@PHbNx;U>Bh>0Qig0jiL`}mK|9^s1yKTKA-18 zXtg^xRUlLj0QnFQMpYnq0f2P`IYjV=;0*v)2<@>?1g`+#AHh2Sn(XC`QJJH-* zG#4wHIF5UZ%$?$Ruzgl`a(QuaptYydT1X?4$>di<(h(`&x6hJi`*D1%wWlcn$cZ5C z;L($(A2s$fbqlN~j^p0%RKCcL^tR2(QjWiUJ%3(gA9Gj0DruUENLKp!OaiRlHsM@% z+Z>H=9}M0C+g`P;N2@#-1E{(@7`g>=l+PUURB zp#*@C%7ZdMXyri>AjI;Z1Q2R@Pyh(IJV2x90*jr-dk1@4hE10Th5rN_H;b2~>5mOV z=F5ZPGawarigWju9|ch!l>QZ-PNzQ`!f^RMFPQS`iQ~ApKN!67eU3VH>eR`bKWxD5 UM3xYTYybcN07*qoM6N<$f!h<#2Ajn!b$@_Sol@khC|PV~A7xN7Ta;D)fZ^m~ zk&&|9$)=1Zi&9Ry8P0H`_uhN%$n*Gf21K-4t$pm`6o+W*qS0vhEr{F0C=QTgSf7Hp z4HQaqxW|M(1aVhbL7_B-M@*|J=&Xqq6iQ>b!=@U7?wVLep)`au9Ai)yL2p-BLZPya zON{6ssOK7sC{$+gfN>E)eK%M@p)!f4h@d}hwEgrUBB(Dzj#T9Zt0EHhWXO@KJYX5G zA`-o2$dRg?V+)@;NOYGWN2>IN9h~SP(OHHZsnQ$PaI1zy+zfK0N>7-_vziid7f6+w nSjW3Q1aT+$z!4Vk?%(?cj>B!B9000000NkvXXu0mjfDY1}o literal 0 HcmV?d00001 diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/nav_forward.imageset/nav_forward@2x.png b/Demos/TinderLike/TinderLike/Images.xcassets/nav_forward.imageset/nav_forward@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..80b344298a7947d57955a36a00b8cc1ca686213b GIT binary patch literal 602 zcmV-g0;T{%F@DD4WX#p z-D0JD+^&VKV)ObaX|fnAGs8yBoSCdx2pO?p=e+OvopavzlseC2WaKy3l3-?L#!rWE z86R*7SJh-}Y)pzwy0e(_!#Z5SP8`8ULlUIPaMG{#;}JF*k|50rUg&Harm@u!1nDO6 zR%e^=1p72BNcS4Mu&lE`@ITI?r6ECYUSb@}I%{DHcd$kSg5JGEj%DTc;xRU>S#w!ZZ&S9yu0O8^bhq=tEGid5mLGwe=`)K#zj@%_GO6YO8PqXY?Rw#w+Al z(A{|)lqhJst=iZL==dsJ;wwV^eHpAhyqcy|8N+e3?MU@ zhyqc$uQ-j%YLMw)M1iQ>drabMDQ6MTekKLG4qfGB23Pk1Z<8QoEvrKOy3Pk1p z!3liTfK0m0c!(`J`-CI7sX>`^x3NWMZA@TV!!l_y?9|ya>_OWQ1Zh@q&981DN86BO o(jCJaoWciOz*T%RGBT3vJ0F>DDiI^i#sB~S07*qoM6N<$f{b_#I{*Lx literal 0 HcmV?d00001 diff --git a/Demos/TinderLike/TinderLike/Images.xcassets/nav_forward.imageset/nav_forward@3x.png b/Demos/TinderLike/TinderLike/Images.xcassets/nav_forward.imageset/nav_forward@3x.png new file mode 100644 index 0000000000000000000000000000000000000000..07211f3cf59066157fef85c4bcb1540cc70b9e7d GIT binary patch literal 884 zcmV-)1B?8LP)7dZig;`VDJBX8}ofeMn*

l3zmT(pB>nNmMd+= z5!`4Q2(r5dFS@!1Fo)F|E-2qB%-|6X7G!r3Z%O8Q^znj*3i6r4tYpTp7w@30VS;?l zV-jCW?g@N`n>0v}-|u(`bCSCsbGS=G1jQI)0&|kP9n;vL0fOQTF@aghU55|w5?X2$ z6l;iwaYAwdui;&E)Fde05HU_jXEVOQ&1w*oV~7}EO6NX&g}YTOD9;dG%t&V?2H2=V zLAi#Q#Ef*V#UV_gr2;|uhUj8OIxXzRdl*-;pc?b&Vp@8S<0x)XqM$nSn8dX7?!g?^ zDoIeSd314HdbeVLO-c|{Zvm4yF1>La#7?vnD5%E*x;QSq7GA;oSfL<6eHPKhKsJxz z7*;AkP_IQyVj!Ef7+{s0g8D6@i-BzJz{j{j4nZ@VLl?(nvl_eQ5H!a*OyZbqw#Xr9 zmcMXbHVbkHnxlgrV%fYdhoBic=pmNPr+8BiLH#=DA(qVBE@Q>k61cy;CcM1CXwQ_ z(MK$~3wRd$)htqsHu{Jq_X{>+S_4G#8^?!;CHDdpZ*5 ()) public typealias SLPagingViewMovingRedefine = ((_ scrollView: UIScrollView, _ subviews: NSArray)-> ()) public typealias SLPagingViewDidChanged = ((_ currentPage: Int)-> ()) -class NavBarHeader: UIView { +open class NavBarHeader: UIView { - var regularView:UIView - var selectedView:UIView + open var regularView:UIView + open var selectedView:UIView - init( regularView:UIView, selectedView:UIView) { - + init( regularView:UIView, selectedView:UIView) { + self.regularView = regularView self.selectedView = selectedView super.init(frame: CGRect.zero)//(x: 0, y: 0, width: 0, height: 0)) - self.addSubview(self.regularView) self.addSubview(self.selectedView) + self.addSubview(self.regularView) + + self.selectedView.backgroundColor = UIColor.red + self.regularView.backgroundColor = UIColor.brown + + self.backgroundColor = UIColor.black + + self.frame.size = self.recomandedSize() -// setSelected(perc: 0) + // setSelected(perc: 0) } - required init?(coder aDecoder: NSCoder) { + required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setSelected(perc:CGFloat) -> Void { @@ -46,6 +53,29 @@ class NavBarHeader: UIView { self.regularView.alpha = 1 - perc self.selectedView.alpha = perc } + func recomandedSize() -> CGSize { + let size = CGSize(width: max(self.regularView.frame.size.width,self.selectedView.frame.size.width), + height:max(self.regularView.frame.size.height,self.selectedView.frame.size.height)) + + return size; + } +// open override func layoutIfNeeded() { +// self.frame.size = self.recomandedSize() +// self.frame.origin = CGPoint(x:0, y:0) +// } + override open func layoutSubviews() + { + super.layoutSubviews() + + + self.regularView.frame.origin = CGPoint(x: (self.frame.size.width - self.regularView.frame.size.width)/2, + y: (self.frame.size.height - self.regularView.frame.size.height)/2) + + let selSize = (self.selectedView as? UILabel)?._slpGetSize() ?? self.selectedView.frame.size + + self.selectedView.frame.origin = CGPoint(x: (self.frame.size.width - selSize.width)/2, + y: (self.frame.size.height - selSize.height)/2) + } } open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { @@ -62,7 +92,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { fileprivate var SCREENSIZE: CGSize { return UIScreen.main.bounds.size } - open var scrollView: UIScrollView! + fileprivate var scrollView: UIScrollView! fileprivate var pageControl: UIPageControl! fileprivate var navigationBarView: UIView = UIView() fileprivate var navItems: [NavBarHeader] = [] @@ -90,21 +120,22 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } /* - * SLPagingViewController's constructor - * - * @param items should contain all subviews of the navigation bar - * @param navBarBackground navigation bar's background color - * @param views all subviews corresponding to each page - * @param showPageControl inform if we need to display the page control in the navigation bar - * - * @return Instance of SLPagingViewController - */ + * SLPagingViewController's constructor + * + * @param items should contain all subviews of the navigation bar + * @param navBarBackground navigation bar's background color + * @param views all subviews corresponding to each page + * @param showPageControl inform if we need to display the page control in the navigation bar + * + * @return Instance of SLPagingViewController + */ public init(barItems: [UIView], barItemsSelected: [UIView], views: [UIViewController], showPageControl: Bool, navBarBackground: UIColor) { super.init(nibName: nil, bundle: nil) needToShowPageControl = showPageControl navigationBarView.backgroundColor = navBarBackground + UINavigationBar.appearance().barTintColor = navBarBackground isUserInteraction = true for (i, vRegular) in barItems.enumerated() { @@ -320,7 +351,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } } - + fileprivate func sendNewIndex(_ scrollView: UIScrollView){ let xOffset = Float(scrollView.contentOffset.x) let currentIndex = (Int(roundf(xOffset)) % (self.navigationBarView.subviews.count * Int(self.SCREENSIZE.width))) / Int(self.SCREENSIZE.width) From 429b45083ddd9ea3233d63e8ea5a45584cc9aa9a Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Tue, 20 Dec 2016 09:18:12 +0200 Subject: [PATCH 08/14] no message --- SLPagingViewSwift/SLPagingViewSwift.swift | 108 ++++++++++++---------- 1 file changed, 57 insertions(+), 51 deletions(-) diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index fcd3636..5a7cc1f 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -9,6 +9,7 @@ import UIKit public enum SLNavigationSideItemsStyle: Int { + case slNavigationSideItemsStyleOnEdge = 60 case slNavigationSideItemsStyleOnBounds = 40 case slNavigationSideItemsStyleClose = 30 case slNavigationSideItemsStyleNormal = 20 @@ -35,10 +36,10 @@ open class NavBarHeader: UIView { self.addSubview(self.selectedView) self.addSubview(self.regularView) - self.selectedView.backgroundColor = UIColor.red - self.regularView.backgroundColor = UIColor.brown - - self.backgroundColor = UIColor.black + //self.selectedView.backgroundColor = UIColor.red + //self.regularView.backgroundColor = UIColor.brown + // + //self.backgroundColor = UIColor.cyan self.frame.size = self.recomandedSize() @@ -59,22 +60,19 @@ open class NavBarHeader: UIView { return size; } -// open override func layoutIfNeeded() { -// self.frame.size = self.recomandedSize() -// self.frame.origin = CGPoint(x:0, y:0) -// } + // open override func layoutIfNeeded() { + // self.frame.size = self.recomandedSize() + // self.frame.origin = CGPoint(x:0, y:0) + // } override open func layoutSubviews() { super.layoutSubviews() - self.regularView.frame.origin = CGPoint(x: (self.frame.size.width - self.regularView.frame.size.width)/2, y: (self.frame.size.height - self.regularView.frame.size.height)/2) - let selSize = (self.selectedView as? UILabel)?._slpGetSize() ?? self.selectedView.frame.size - - self.selectedView.frame.origin = CGPoint(x: (self.frame.size.width - selSize.width)/2, - y: (self.frame.size.height - selSize.height)/2) + self.selectedView.frame.origin = CGPoint(x: (self.frame.size.width - selectedView.frame.size.width)/2, + y: (self.frame.size.height - selectedView.frame.size.height)/2) } } open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { @@ -145,12 +143,12 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { let vSize: CGSize = (navBarHeader as? UILabel)?._slpGetSize() ?? navBarHeader.frame.size let originX = (self.SCREENSIZE.width/2.0 - vSize.width/2.0) + CGFloat(i * 100) navBarHeader.frame = CGRect(x: originX, y: 8, width: vSize.width, height: vSize.height) -// for v in navBarHeader.subviews { -// -// for layer in v.layer.sublayers! { -// layer.bounds = CGRect(x: CGFloat(v.frame.origin.x), y: CGFloat(v.frame.origin.y), width: navBarHeader.frame.size.width, height: navBarHeader.frame.size.height) -// } -// } + // for v in navBarHeader.subviews { + // + // for layer in v.layer.sublayers! { + // layer.bounds = CGRect(x: CGFloat(v.frame.origin.x), y: CGFloat(v.frame.origin.y), width: navBarHeader.frame.size.width, height: navBarHeader.frame.size.height) + // } + // } navBarHeader.tag = i let tap = UITapGestureRecognizer(target: self, action: #selector(SLPagingViewSwift.tapOnHeader(_:))) navBarHeader.addGestureRecognizer(tap) @@ -175,16 +173,16 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } /* - * SLPagingViewController's constructor - * - * Use controller's title as a navigation item - * - * @param controllers view controllers containing sall subviews corresponding to each page - * @param navBarBackground navigation bar's background color - * @param showPageControl inform if we need to display the page control in the navigation bar - * - * @return Instance of SLPagingViewController - */ + * SLPagingViewController's constructor + * + * Use controller's title as a navigation item + * + * @param controllers view controllers containing sall subviews corresponding to each page + * @param navBarBackground navigation bar's background color + * @param showPageControl inform if we need to display the page control in the navigation bar + * + * @return Instance of SLPagingViewController + */ public convenience init(viewControllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ var views = [UIView]() var items = [UILabel]() @@ -210,15 +208,15 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } /* - * SLPagingViewController's constructor - * - * @param items should contain all subviews of the navigation bar - * @param navBarBackground navigation bar's background color - * @param controllers view controllers containing sall subviews corresponding to each page - * @param showPageControl inform if we need to display the page control in the navigation bar - * - * @return Instance of SLPagingViewController - */ + * SLPagingViewController's constructor + * + * @param items should contain all subviews of the navigation bar + * @param navBarBackground navigation bar's background color + * @param controllers view controllers containing sall subviews corresponding to each page + * @param showPageControl inform if we need to display the page control in the navigation bar + * + * @return Instance of SLPagingViewController + */ public convenience init(barItems: [UIView], barItemsSelected: [UIView], controllers: [UIViewController], showPageControl: Bool, navBarBackground: UIColor){ var views = [UIView]() for ctr in controllers { @@ -232,6 +230,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.setupPagingProcess() + self.navigationBarView.frame = CGRect(x: 0, y: 0, width: self.SCREENSIZE.width, height: 44)////////////////// self.setCurrentIndex(self.indexSelected, animated: false) self.view.backgroundColor = UIColor.white } @@ -244,20 +243,20 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { // MARK: - Public methods /* - * Update the state of the UserInteraction on the navigation bar - * - * @param activate state you want to set to UserInteraction - */ + * Update the state of the UserInteraction on the navigation bar + * + * @param activate state you want to set to UserInteraction + */ open func updateUserInteractionOnNavigation(_ active: Bool){ self.isUserInteraction = active } /* - * Set the current index page and scroll to its position - * - * @param index of the wanted page - * @param animated animate the moving - */ + * Set the current index page and scroll to its position + * + * @param index of the wanted page + * @param animated animate the moving + */ open func setCurrentIndex(_ index: Int, animated: Bool){ // Be sure we got an existing index if(index < 0 || index > self.navigationBarView.subviews.count-1){ @@ -280,7 +279,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { // MARK: - Internal methods fileprivate func setupPagingProcess() { let frame: CGRect = CGRect(x: 0, y: 0, width: SCREENSIZE.width, height: self.view.frame.height) - + self.scrollView = UIScrollView(frame: frame) self.scrollView.backgroundColor = UIColor.clear self.scrollView.isPagingEnabled = true @@ -375,15 +374,21 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } } - + } - func getOriginX(_ vSize: CGSize, idx: CGFloat, distance: CGFloat, xOffset: CGFloat) -> CGFloat{ + open func getOriginX(_ vSize: CGSize, idx: CGFloat, distance: CGFloat, xOffset: CGFloat) -> CGFloat{ var result = self.SCREENSIZE.width / 2.0 - vSize.width/2.0 result += (idx * distance) result -= xOffset / (self.SCREENSIZE.width / distance) return result } + open func getOriginY(_ vSize: CGSize, idx: CGFloat, distance: CGFloat, xOffset: CGFloat) -> CGFloat{ + var result = self.navigationBarView.frame.size.height / 2.0 - vSize.height/2.0 + // result += (idx * distance) + // result -= xOffset / (self.SCREENSIZE.width / distance) + return result + } // Scroll to the view tapped func tapOnHeader(_ recognizer: UITapGestureRecognizer){ @@ -401,7 +406,8 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { for (i, v) in self.navItems.enumerated() { let vSize = v.frame.size let originX = self.getOriginX(vSize, idx: CGFloat(i), distance: CGFloat(distance), xOffset: xOffset) - v.frame = CGRect(x: originX, y: 8, width: vSize.width, height: vSize.height) + let originY = self.getOriginY(vSize, idx: CGFloat(i), distance: CGFloat(distance), xOffset: xOffset) + v.frame = CGRect(x: originX, y: originY, width: vSize.width, height: vSize.height) } self.pagingViewMovingRedefine?(scrollView, self.navItems as NSArray) self.pagingViewMoving?(self.navItems) From daf1b12562551d513aa95fda0570f6be32634420 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Wed, 21 Dec 2016 16:04:22 +0200 Subject: [PATCH 09/14] no message --- Demos/TinderLike/TinderLike/AppDelegate.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Demos/TinderLike/TinderLike/AppDelegate.swift b/Demos/TinderLike/TinderLike/AppDelegate.swift index 9ba3fc1..774b334 100644 --- a/Demos/TinderLike/TinderLike/AppDelegate.swift +++ b/Demos/TinderLike/TinderLike/AppDelegate.swift @@ -78,13 +78,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate { if let navBarItems = subviews as? [NavBarHeader] { for navBarItem in navBarItems { - let originX = CGFloat(navBarItem.frame.origin.x) + let vSize = navBarItem.frame.size + let originX = CGFloat(navBarItem.frame.origin.x + vSize.width/2) - let distance = CGFloat( self.controller.navigationSideItemsStyle.rawValue) + let distance = CGFloat( self.controller.navigationSideItemsStyle.rawValue+100) - let vSize = navBarItem.frame.size - let margin = self.controller.getOriginX(vSize, idx: CGFloat(navBarItem.tag), distance: CGFloat(distance), xOffset: 0)-100 + let margin = self.controller.getOriginX(vSize, idx: 0, distance: CGFloat(distance), xOffset: 0) let screenCenter = CGFloat(UIScreen.main.bounds.width/2) let scale = CGFloat(screenCenter-margin) @@ -93,10 +93,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { if navBarItem.tag == 0 { - print("originX: \(originX) alpha: \(alpha) xOnScale: \(xOnScale) scale:\(scale) margin: \(margin)" ) + print("originX: \(originX) alpha: \(alpha) xOnScale: \(xOnScale) scale:\(scale) margin: \(margin) screenCenter:\(screenCenter)" ) } -// let alpha = CGFloat(abs(originX - 145))/(145-45) navBarItem.regularView.alpha = alpha navBarItem.selectedView.alpha = 1.0 - alpha } @@ -154,3 +153,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } + From b18590af9ab29ead795f75ce02818085df378753 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Tue, 27 Dec 2016 16:28:59 +0200 Subject: [PATCH 10/14] fix paging right and left --- Demos/TinderLike/TinderLike/AppDelegate.swift | 8 ++++---- SLPagingViewSwift/SLPagingViewSwift.swift | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Demos/TinderLike/TinderLike/AppDelegate.swift b/Demos/TinderLike/TinderLike/AppDelegate.swift index 774b334..b664c1b 100644 --- a/Demos/TinderLike/TinderLike/AppDelegate.swift +++ b/Demos/TinderLike/TinderLike/AppDelegate.swift @@ -91,10 +91,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { let xOnScale = CGFloat(abs(originX - screenCenter)) let alpha = xOnScale/scale - if navBarItem.tag == 0 - { - print("originX: \(originX) alpha: \(alpha) xOnScale: \(xOnScale) scale:\(scale) margin: \(margin) screenCenter:\(screenCenter)" ) - } +// if navBarItem.tag == 0 +// { +// print("originX: \(originX) alpha: \(alpha) xOnScale: \(xOnScale) scale:\(scale) margin: \(margin) screenCenter:\(screenCenter)" ) +// } navBarItem.regularView.alpha = alpha navBarItem.selectedView.alpha = 1.0 - alpha diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index 5a7cc1f..be75fcd 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -96,7 +96,8 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { fileprivate var navItems: [NavBarHeader] = [] fileprivate var needToShowPageControl: Bool = false fileprivate var isUserInteraction: Bool = false - fileprivate var indexSelected: Int = 0 + fileprivate var indexSelected: Int = 1 + fileprivate var nextIndex:Int = -1 // MARK: - Constructors public required init(coder decoder: NSCoder) { @@ -411,6 +412,12 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } self.pagingViewMovingRedefine?(scrollView, self.navItems as NSArray) self.pagingViewMoving?(self.navItems) + + let candidateNextIndex = self.calcNextIndex(scrollView: scrollView) + if nextIndex != candidateNextIndex{ + nextIndex = candidateNextIndex + showViewController(at: nextIndex) + } } open func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { @@ -422,7 +429,11 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { } public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { + nextIndex = self.calcNextIndex(scrollView: scrollView) + showViewController(at: nextIndex) + } + func calcNextIndex(scrollView:UIScrollView) -> Int { var direction = 0 let yVelocity: CGFloat = scrollView.panGestureRecognizer.velocity(in: scrollView).x if yVelocity < 0 { direction = 1 } else @@ -432,9 +443,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { let W:Int = Int(self.SCREENSIZE.width) let nextIndex = xOffset / W + direction - - showViewController(at: nextIndex) - + return nextIndex } } From f567c6a9bb03e947b8d90de3a70c6dc97258e428 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Tue, 27 Dec 2016 17:01:30 +0200 Subject: [PATCH 11/14] fix set current selected --- SLPagingViewSwift/SLPagingViewSwift.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index be75fcd..bd9bf95 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -96,7 +96,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { fileprivate var navItems: [NavBarHeader] = [] fileprivate var needToShowPageControl: Bool = false fileprivate var isUserInteraction: Bool = false - fileprivate var indexSelected: Int = 1 + fileprivate var indexSelected: Int = 0 fileprivate var nextIndex:Int = -1 // MARK: - Constructors @@ -267,7 +267,9 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { self.indexSelected = index // Get the right position and update it let xOffset = CGFloat(index) * self.SCREENSIZE.width - self.scrollView.setContentOffset(CGPoint(x: xOffset, y: self.scrollView.contentOffset.y), animated: animated) + if self.scrollView != nil{ + self.scrollView.setContentOffset(CGPoint(x: xOffset, y: self.scrollView.contentOffset.y), animated: animated) + } for (i , item) in navItems.enumerated() { @@ -331,7 +333,7 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { prevVC.willMove(toParentViewController: nil) } - if let nextVC = self.viewControllers[index] , nextVC.parent == nil { + if let nextVC = self.viewControllers[index] , nextVC.parent == nil , self.scrollView != nil{ nextVC.willMove(toParentViewController: self) self.addChildViewController(nextVC) self.scrollView.addSubview(nextVC.view) From 9f94cbe964f33826a3ab170a1724b3bb1ed37cd6 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Thu, 29 Dec 2016 09:43:55 +0200 Subject: [PATCH 12/14] fix animation --- Demos/TinderLike/TinderLike/AppDelegate.swift | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Demos/TinderLike/TinderLike/AppDelegate.swift b/Demos/TinderLike/TinderLike/AppDelegate.swift index b664c1b..f9cfd3e 100644 --- a/Demos/TinderLike/TinderLike/AppDelegate.swift +++ b/Demos/TinderLike/TinderLike/AppDelegate.swift @@ -7,6 +7,7 @@ // import UIKit +import MediaPlayer @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { @@ -16,6 +17,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var controller: SLPagingViewSwift! func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + + (MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(1, animated: false) + + self.window = UIWindow(frame: UIScreen.main.bounds) // let orange = UIColor(red: 255/255, green: 69.0/255, blue: 0.0/255, alpha: 1.0) let orange = UIColor(red: 0/255, green: 0.0/255, blue: 255.0/255, alpha: 1.0) @@ -62,10 +67,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { UIImageView(image: img1), UIImageView(image: img2)] //UIImageView(image: img3)] -// for v in barItems -// { -// AppDelegate.addLayer(view: v) -// } + let controllers = [ctr0, ctr1, ctr2] controller = SLPagingViewSwift(barItems: barItems, barItemsSelected: barItemsSelected, controllers: controllers, showPageControl: false) @@ -74,30 +76,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { }) controller.pagingViewMoving = ({ subviews in - + if let navBarItems = subviews as? [NavBarHeader] { + + for navBarItem in navBarItems { let vSize = navBarItem.frame.size - let originX = CGFloat(navBarItem.frame.origin.x + vSize.width/2) + let currentX = CGFloat(navBarItem.frame.origin.x) - let distance = CGFloat( self.controller.navigationSideItemsStyle.rawValue+100) + let distance = CGFloat( self.controller.navigationSideItemsStyle.rawValue + 100) + let destX = self.controller.getOriginX(vSize, idx: 0, distance: distance, xOffset: 0).truncatingRemainder(dividingBy: distance) - let margin = self.controller.getOriginX(vSize, idx: 0, distance: CGFloat(distance), xOffset: 0) - - let screenCenter = CGFloat(UIScreen.main.bounds.width/2) - let scale = CGFloat(screenCenter-margin) - let xOnScale = CGFloat(abs(originX - screenCenter)) + let xOnScale = CGFloat(abs( currentX-destX)) + let scale = distance let alpha = xOnScale/scale -// if navBarItem.tag == 0 -// { -// print("originX: \(originX) alpha: \(alpha) xOnScale: \(xOnScale) scale:\(scale) margin: \(margin) screenCenter:\(screenCenter)" ) +// if navBarItem.tag == 0 { +// print("originX: \(Int(currentX)) destX: \(Int(destX)) xOnScale: \(Int(xOnScale)) scale:\(Int(scale)) alpha: \(alpha) " ) // } navBarItem.regularView.alpha = alpha navBarItem.selectedView.alpha = 1.0 - alpha + } } }) From 53479a384c3f0384ca0cdd7c20930d7690c2c903 Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Thu, 29 Dec 2016 12:00:43 +0200 Subject: [PATCH 13/14] fix layouting --- SLPagingViewSwift/SLPagingViewSwift.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index bd9bf95..578ff00 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -322,11 +322,15 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { self.scrollView.contentSize = CGSize(width: width, height: height) var i: Int = 0 while let v = viewControllers[i] { - v.view.frame = CGRect(x: self.SCREENSIZE.width * CGFloat(i), y: 0, width: self.SCREENSIZE.width, height: self.SCREENSIZE.height) + v.view.frame = CGRect(x: self.SCREENSIZE.width * CGFloat(i), y: 0, width: self.SCREENSIZE.width, height: height) i += 1 } } } + override open func viewDidLayoutSubviews() { + setViewSizeAndLocation() + } + func showViewController(at index:Int) -> Void { if let prevVC = self.viewControllers[self.indexSelected] , prevVC.parent == self { From 83fe07667e2f601c0899c2d8ae4c74b38bbc02af Mon Sep 17 00:00:00 2001 From: Bary Levy Date: Sun, 1 Jan 2017 23:40:51 +0200 Subject: [PATCH 14/14] hide/show nav bar on vc lifecycle --- SLPagingViewSwift/SLPagingViewSwift.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/SLPagingViewSwift/SLPagingViewSwift.swift b/SLPagingViewSwift/SLPagingViewSwift.swift index 578ff00..d2b3d93 100644 --- a/SLPagingViewSwift/SLPagingViewSwift.swift +++ b/SLPagingViewSwift/SLPagingViewSwift.swift @@ -235,7 +235,12 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { self.setCurrentIndex(self.indexSelected, animated: false) self.view.backgroundColor = UIColor.white } - + open override func viewWillAppear(_ animated: Bool) { + self.navigationBarView.isHidden = false + } + open override func viewWillDisappear(_ animated: Bool) { + self.navigationBarView.isHidden = true + } open override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() self.navigationBarView.frame = CGRect(x: 0, y: 0, width: self.SCREENSIZE.width, height: 44) @@ -330,7 +335,6 @@ open class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { override open func viewDidLayoutSubviews() { setViewSizeAndLocation() } - func showViewController(at index:Int) -> Void { if let prevVC = self.viewControllers[self.indexSelected] , prevVC.parent == self {