diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..b8f46221 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: SCENEE diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 00000000..ddd80a82 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,40 @@ +> Please fill out this template appropriately when filing a bug report. +> +> Please remove this line and everything above it before submitting. + +### Description + +### Expected behavior + +### Actual behavior + +### Steps to reproduce + +**Code example that reproduces the issue** + + +**How do you display panel(s)?** + +* Add as child view controllers +* Present modally + +**How many panels do you displays?** + +* 1 +* 2+ + +### Environment + +**Library version** + +**Installation method** + +* CocoaPods +* Carthage +* Swift Package Manager + +**iOS version(s)** + + +**Xcode version** + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..d29ea13b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,121 @@ +name: ci + +on: + push: + branches: + - master + pull_request: + branches: + - '*' + +jobs: + build: + runs-on: ${{ matrix.runsOn }} + env: + DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer + strategy: + fail-fast: false + matrix: + include: + - swift: "5.6" + xcode: "13.3.1" + runsOn: macos-12 + - swift: "5.5" + xcode: "13.2.1" + runsOn: macos-11 + - swift: "5.4" + xcode: "12.5.1" + runsOn: macos-11 + - swift: "5.3" + xcode: "12.4" + runsOn: macos-10.15 + - swift: "5.2" + xcode: "11.7" + runsOn: macos-10.15 + - swift: "5.1" + xcode: "11.3.1" + runsOn: macos-10.15 + steps: + - uses: actions/checkout@v2 + - name: Building in Swift ${{ matrix.swift }} + run: xcodebuild -scheme FloatingPanel SWIFT_VERSION=${{ matrix.swift }} clean build + + test: + runs-on: ${{ matrix.runsOn }} + env: + DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer + strategy: + fail-fast: false + matrix: + include: + - os: "15.4" + xcode: "13.3.1" + sim: "iPhone 13 Pro" + runsOn: macos-12 + - os: "15.2" + xcode: "13.2.1" + sim: "iPhone 13 Pro" + runsOn: macos-11 + - os: "14.5" + xcode: "12.5.1" + sim: "iPhone 12 Pro" + runsOn: macos-11 + - os: "14.4" + xcode: "12.4" + sim: "iPhone 12 Pro" + runsOn: macos-10.15 + - os: "13.7" + xcode: "11.7" + sim: "iPhone 11 Pro" + runsOn: macos-10.15 + steps: + - uses: actions/checkout@v2 + - name: Testing in iOS ${{ matrix.os }} + run: xcodebuild clean test -scheme FloatingPanel -workspace FloatingPanel.xcworkspace -destination 'platform=iOS Simulator,OS=${{ matrix.os }},name=${{ matrix.sim }}' + + example: + runs-on: macos-12 + env: + DEVELOPER_DIR: /Applications/Xcode_13.3.1.app/Contents/Developer + strategy: + fail-fast: false + matrix: + include: + - example: "Maps" + - example: "Stocks" + - example: "Samples" + steps: + - uses: actions/checkout@v2 + - name: Building ${{ matrix.example }} + run: xcodebuild -workspace FloatingPanel.xcworkspace -scheme ${{ matrix.example }} -sdk iphonesimulator clean build + + swiftpm: + runs-on: macos-12 + env: + DEVELOPER_DIR: /Applications/Xcode_13.3.1.app/Contents/Developer + strategy: + fail-fast: false + matrix: + include: + - target: "arm64-apple-ios15.4-simulator" + - target: "x86_64-apple-ios15.4-simulator" + steps: + - uses: actions/checkout@v2 + - name: "Swift Package Manager build" + run: swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "${{ matrix.target }}" + + carthage: + runs-on: macos-11 + steps: + - uses: actions/checkout@v2 + - name: "Carthage build" + run: carthage build --use-xcframeworks --no-skip-current + + cocoapods: + runs-on: macos-12 + steps: + - uses: actions/checkout@v2 + - name: "CocoaPods: pod lib lint" + run: pod lib lint --allow-warnings + - name: "CocoaPods: pod spec lint" + run: pod spec lint --allow-warnings diff --git a/.gitignore b/.gitignore index abc5fcca..e6990820 100644 --- a/.gitignore +++ b/.gitignore @@ -17,11 +17,13 @@ DerivedData/ *.perspectivev3 !default.perspectivev3 xcuserdata/ +IDEWorkspaceChecks.plis ## Other *.moved-aside *.xccheckout *.xcscmblueprint +*.xcsettings ## Obj-C/Swift specific *.hmap @@ -29,6 +31,9 @@ xcuserdata/ *.dSYM.zip *.dSYM +## Swift Package Manager Specific +.swiftpm/ + ## Playgrounds timeline.xctimeline playground.xcworkspace diff --git a/Documentation/FloatingPanel 2.0 Migration Guide.md b/Documentation/FloatingPanel 2.0 Migration Guide.md new file mode 100644 index 00000000..7084efa3 --- /dev/null +++ b/Documentation/FloatingPanel 2.0 Migration Guide.md @@ -0,0 +1,294 @@ +# FloatingPanel 2.0 Migration Guide + +FloatingPanel 2.0 is the latest major release of FloatingPanel. As a major release, following Semantic Versioning conventions, 2.0 introduces API-breaking changes. + +This guide is provided in order to ease the transition of existing applications using FloatingPanel 1.x to the latest APIs, as well as explain the design and structure of new and updated functionality. + +## Updated Minimum Requirements + +* Swift 5.0 +* iOS 11 (iOS 10 is still the deployment target, but not tested well) +* Xcode 11.0 + +## Benefits of Upgrading + +* __Top, left and right positioned panel__ + * FloatingPanel is not just a library for a bottom positioned panel, but also top, left and right positioned ones. +* __Objective-C compatibility__ + * The entire APIs are exposed in Objective-C. So you can use them in Objective-C directly. +* __Flexible and explicit layout customization__ + * `FloatingPanelLayout` is redesigned. There is no implicit rules to lay out a panel anymore. +* __New spring animation without UIViewPropertyAnimator__ + * The new spring animation uses [Numeric springing](http://allenchou.net/2015/04/game-math-precise-control-over-numeric-springing/) which is a very powerful tool for procedural animation. Therefore a library consumer is easy to modify a panel behavior by 2 paramters of the deceleration rate and response time. +* __Handle the panel position anytime__ + * `floatingPanelDidMove(_:)` delegate method is also called while a panel is moving. The method behavior becomes same as `scrollViewDidScroll(_:)` in `UIScrollViewDelegate`. And in the method a library consumer is able to change a panel location. +* __Update the removal interaction's invocation__ + * Now you can invoke the removal interaction at any time where you want. There is no restrictions in the library. +* __Fix many issues depending on API design__ + * See the following sections for details. + +## API Name Changes + +* `FloatingPanelPosition` is now `FloatingPanelState`. + * `FloatingPanelPosition` in v2 is used to specify a panel position(top, left, bottom and right) in a screen. +* `FloatingPanelSurfaceView` is `SurfaceView` only in Swift. +* `FloatingPanelBackdropView` is `BackdropView` only in Swift. +* `FloatingPanelGrabberHandleView` is `GrabberView` only in Swift. +* "decelerate" term is replaced with "attract" because the panel's behavior is not unidirectional, but going back and forth so that it is settled to a location. + +## `FloatingPanelController` + +* `layout` and `behavior` properties can be changed directly without using the delegate methods. + +```swift +fpc.behavior = SearchPaneliPadBehavior() +fpc.layout = SearchPaneliPadLayout() +fpc.invalidateLayout() // If needed +``` + +* The second argument of `addPanel(toParent:)` changes to specify an index of subviews of a view in which a panel is added. + +```diff +- public func addPanel(toParent parent: UIViewController, belowView: UIView? = nil, animated: Bool = false) { ++ public func addPanel(toParent parent: UIViewController, at viewIndex: Int = -1, animated: Bool = false) { +``` + +* `surfaceOriginY` is now `surfaceLocation`. +* `updateLayout` is now `invalidateLayout`. +* The scroll tracking API is changed a bit to support multiple scroll view tracking in the future. + * Now `untrack(scrollView:)` is used to disable the scroll tracking. + +## `FloatingPanelControllerDelegate` + +* `floatingPanelDidEndDragging(_ vc:willAttract:)` is added to check whether a panel will continue to move after dragging. +* `floatingPanelDidMove(_:)` behavior changes. The method is also called in the spring animation. +* The removal interaction delegate is updated. + * `floatingPanel(_:shouldRemoveAt:with:)` is added to determine whether it invokes the removal interaction in any state. + * `floatingPanelWillRemove(_:)` is added. +* `floatingPanel(_: FloatingPanelController, layoutFor size: CGSize)` is added to respond to a layout change in regular size classes on iPad. + +```swift +func floatingPanel(_ fpc: FloatingPanelController, layoutFor size: CGSize) -> FloatingPanelLayout { + if aCondition(for: size) { + return SearchPanelLayout() + } + return SearchPanel2Layout() +} +``` + +* The `targetState` argument type of `floatingPanelWillEndDragging(_:withVelocity:targetState:)` is changed from `FloatingPanelState` to `UnsafeMutablePointer` to modify a target state on demand. + +```swift +func floatingPanelWillEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer) { + switch targetState.pointee { + case .full: + // do something... + case .half: + if aCondition { + targetState.pointee = .tip + } + default: + break + } +} +``` + +### Deprecated APIs + +* `floatingPanel(_:behaviorFor:)` + * Please update `FloatingPanelController.behavior` directly. +* `floatingPanel(_:shouldRecognizeSimultaneouslyWith:)` + * Please use `FloatingPanelController.panGestureRecognizer.delegateProxy`. + +## `FloatingPanelLayout` + +* `position` property is added to determine a panel position. +* `initialPosition` is now `initialState`. +* `supportedPositions` and `insetFor(position:)` are replaced with `anchors` property. +* `backdropAlphaFor(position:)` is now `backdropAlpha(for:)`. + +```swift +class SearchPanelPadLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .top + let initialState: FloatingPanelState = .tip + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + ... + ] + } + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.3 + } + ... +} +``` + +### New `FloatingPanelLayoutAnchoring` classes + +The following objects adopting `FloatingPanelLayoutAnchoring` protocol are added to configure the flexible and explicit layout. + +#### `FloatingPanelLayoutAnchor` + +This class is used to specify a panel layout using insets from a rectangle area of the superview or safe area. + +* `FloatingPanelFullScreenLayout` is replaced with anchors using `.superview` reference guide. +* `FloatingPanelLayoutAnchor(fractionalInset:edge:referenceGuide:)` lets you lay out a panel at a relative position in a reference rectangle area. + +```swift +// Before: +class MyPanelLayout: FloatingPanelLayout { + var initialPosition: FloatingPanelPosition { + return .half + } + + func insetFor(position: FloatingPanelPosition) -> CGFloat? { + switch position { + case .full: return 16.0 + case .half: return 262.0 + case .tip: return 44.0 + case .hidden: return nil + } + } +} + +// After: +class MyPanelLayout: FloatingPanelLayout { + var position: FloatingPanelPosition = .bottom + var initialState: FloatingPanelState { .half } + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .superview), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 44.0, edge: .bottom, referenceGuide: .safeArea) + ] + } +} +``` + +#### `FloatingPanelIntrinsicLayoutAnchor` + +This class is used to specify a panel layout using offsets from the intrinsic size layout. + +* This replaces `FloatingPanelIntrinsicLayout`. +* This is also able to configure a fractional layout in the intrinsic size. + +```swift +// Before: +class MyPanelIntrinsicLayout: FloatingPanelIntrinsicLayout { + var initialPosition: FloatingPanelPosition { + return .half + } + func insetFor(position: FloatingPanelPosition) -> CGFloat? { + switch position { + case .full: return 16.0 + case .half: return 262.0 + case .tip: return 44.0 + case .hidden: return nil + } + } +} + +// After: +class MyPanelIntrinsicLayout: FloatingPanelLayout { + var position: FloatingPanelPosition = .bottom + var initialState: FloatingPanelState { .full } + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 16.0, referenceGuide: .safeArea), + .half: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.5, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 44.0, edge: .bottom, referenceGuide: .safeArea) + ] + } +} +``` + +### Deprecated APIs + +* `.topInteractionBuffer` and `.bottomInteractionBuffer`. + * Please control the max/min range of the motion in `floatingPanelDidMove(_:)` delegate method as below. + +```swift +func floatingPanelDidMove(_ fpc: FloatingPanelController) { + if fpc.isAttracting == false { + let loc = fpc.surfaceLocation + let minY = fpc.surfaceLocation(for: .full).y - 6.0 + let maxY = fpc.surfaceLocation(for: .tip).y + 6.0 + fpc.surfaceLocation = CGPoint(x: loc.x, y: min(max(loc.y, minY), maxY)) + } +} +``` + +## `FloatingPanelBehavior` + +* `.springDecelerationRate` and `.springResponseTime` properties are added to control the new spring effect of Numeric springing. + +### Deprecated APIs + +* `addAnimator(_:to:)`, `removeAnimator(_:from:)` + * They are moved into `floatingPanel(_:animatorForPresentingTo:)` and `floatingPanel(_:animatorForDismissingWith:)` of `FloatingPanelControllerDelegate` because they are used for view transitions. +* `interactionAnimator(_:to:with:)`, `moveAnimator(_:from:to:)` + * They are removed because the animators are replaced with the new spring effect. +* `removalVelocity`, `removalProgress` + * They are replaced with `floatingPanel(_:shouldRemoveAt:with:)` of `FloatingPanelControllerDelegate` +* `removalInteractionAnimator(_:with:)` + * It is integrated with `floatingPanel(_:animatorForDismissingWith:)` of `FloatingPanelControllerDelegate`. + +## `SurfaceView` + +* `SurfaceAppearance` class and `SurfaceView.appearance` property are added to specify the rounding corners, shadows and background color. + * `SurfaceView.appearance` property avoids `Ambiguous use of 'cornerRadius'` error, for instance. + * `SurfaceAppearance` enables to apply layered box shadows into a surface to materialize it. + +```swift +// Before: +fpc.surfaceView.cornerRadius = 6.0 +fpc.surfaceView.backgroundColor = .clear +fpc.surfaceView.shadowHidden = false +fpc.surfaceView.shadowColor = .black +fpc.surfaceView.shadowOffset = CGSize(width: 0, height: 16) +fpc.surfaceView.shadowRadius = 16.0 + +// After: +let appearance = SurfaceAppearance() +appearance.cornerRadius = 8.0 +appearance.backgroundColor = .clear + +let shadow = SurfaceAppearance.Shadow() +shadow.color = .black +shadow.offset = CGSize(width: 0, height: 16) +shadow.radius = 16 +shadow.spread = 8 +appearance.shadows = [shadow] + +fpc.surfaceView.appearance = appearance +``` + +* These properties are changed for the top, left and right positioned panel. + * `grabberTopPadding` is now `grabberHandlePadding`. + * `topGrabberBarHeight` is now `grabberAreaOffset`. + * `grabberHandleWidth` and `grabberHandleHeight` are replaced with `grabberHandleSize`. + +## `BackdropView` + +* The dismissal action of the backdrop is disabled by default. + * You can enable it to set `BackdropView.dismissalTapGestureRecognizer.isEnabled` to `true`. + +## `FloatingPanelPanGestureRecognizer` + +* `delegateProxy` property is added to intercept the gesture recognizer delegate. + +```swift +func layoutPanelForPad() { + fpc.behavior = SearchPaneliPadBehavior() + fpc.panGestureRecognizer.delegateProxy = self +} + +func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return false +} +``` + +## Miscellaneous + +* `UISpringTimingParameters(decelerationRate:frequencyResponse:initialVelocity:)` initializer is added. +* The directory structure and file names in the Xcode project changes. diff --git a/Examples/Maps-SwiftUI/Maps-SwiftUI.xcodeproj/project.pbxproj b/Examples/Maps-SwiftUI/Maps-SwiftUI.xcodeproj/project.pbxproj new file mode 100644 index 00000000..71e73f6a --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps-SwiftUI.xcodeproj/project.pbxproj @@ -0,0 +1,426 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 55; + objects = { + +/* Begin PBXBuildFile section */ + 6467E8642699AC5F00565F4F /* SurfaceAppearance+phone.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6467E8632699AC5F00565F4F /* SurfaceAppearance+phone.swift */; }; + 6467E86A2699B19D00565F4F /* SearchPanelPhoneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6467E8692699B19D00565F4F /* SearchPanelPhoneDelegate.swift */; }; + 649A122926C14D0900DAB961 /* UIHostingController+ignoreKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649A122826C14D0900DAB961 /* UIHostingController+ignoreKeyboard.swift */; }; + 649A122D26C168CF00DAB961 /* View+floatingPanelGrabberHandlePadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649A122C26C168CF00DAB961 /* View+floatingPanelGrabberHandlePadding.swift */; }; + 64A5B7232691323900BCAA05 /* MapsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64A5B7222691323900BCAA05 /* MapsApp.swift */; }; + 64A5B7252691323900BCAA05 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64A5B7242691323900BCAA05 /* ContentView.swift */; }; + 64A5B734269133DC00BCAA05 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 64A5B733269133DC00BCAA05 /* FloatingPanel.framework */; }; + 64A5B735269133DC00BCAA05 /* FloatingPanel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 64A5B733269133DC00BCAA05 /* FloatingPanel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 64A5B738269134CA00BCAA05 /* VisualEffectBlur.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64A5B737269134CA00BCAA05 /* VisualEffectBlur.swift */; }; + 64A5B73C2691469900BCAA05 /* FloatingPanelContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64A5B73B2691469900BCAA05 /* FloatingPanelContentView.swift */; }; + 64A5B73E269147DC00BCAA05 /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64A5B73D269147DC00BCAA05 /* SearchBar.swift */; }; + 64A5B7402691532400BCAA05 /* ResultsList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64A5B73F2691532400BCAA05 /* ResultsList.swift */; }; + 64A5B7422691541A00BCAA05 /* HostingCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64A5B7412691541A00BCAA05 /* HostingCell.swift */; }; + 64F7E83126AD70EB00A0E0F7 /* View+floatingPanelContentInsetAdjustmentBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64F7E82C26AD70EB00A0E0F7 /* View+floatingPanelContentInsetAdjustmentBehavior.swift */; }; + 64F7E83226AD70EB00A0E0F7 /* View+floatingPanelContentMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64F7E82D26AD70EB00A0E0F7 /* View+floatingPanelContentMode.swift */; }; + 64F7E83326AD70EB00A0E0F7 /* FloatingPanelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64F7E82E26AD70EB00A0E0F7 /* FloatingPanelView.swift */; }; + 64F7E83426AD70EB00A0E0F7 /* View+floatingPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64F7E82F26AD70EB00A0E0F7 /* View+floatingPanel.swift */; }; + 64F7E83526AD70EB00A0E0F7 /* View+floatingPanelSurfaceAppearance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64F7E83026AD70EB00A0E0F7 /* View+floatingPanelSurfaceAppearance.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 64A5B736269133DC00BCAA05 /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + 64A5B735269133DC00BCAA05 /* FloatingPanel.framework in Embed Frameworks */, + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 6467E8632699AC5F00565F4F /* SurfaceAppearance+phone.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SurfaceAppearance+phone.swift"; sourceTree = ""; }; + 6467E8692699B19D00565F4F /* SearchPanelPhoneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchPanelPhoneDelegate.swift; sourceTree = ""; }; + 649A122826C14D0900DAB961 /* UIHostingController+ignoreKeyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIHostingController+ignoreKeyboard.swift"; sourceTree = ""; }; + 649A122C26C168CF00DAB961 /* View+floatingPanelGrabberHandlePadding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+floatingPanelGrabberHandlePadding.swift"; sourceTree = ""; }; + 64A5B71F2691323900BCAA05 /* Maps-SwiftUI.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Maps-SwiftUI.app"; sourceTree = BUILT_PRODUCTS_DIR; }; + 64A5B7222691323900BCAA05 /* MapsApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapsApp.swift; sourceTree = ""; }; + 64A5B7242691323900BCAA05 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 64A5B733269133DC00BCAA05 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 64A5B737269134CA00BCAA05 /* VisualEffectBlur.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VisualEffectBlur.swift; sourceTree = ""; }; + 64A5B73B2691469900BCAA05 /* FloatingPanelContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingPanelContentView.swift; sourceTree = ""; }; + 64A5B73D269147DC00BCAA05 /* SearchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = ""; }; + 64A5B73F2691532400BCAA05 /* ResultsList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultsList.swift; sourceTree = ""; }; + 64A5B7412691541A00BCAA05 /* HostingCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HostingCell.swift; sourceTree = ""; }; + 64F7E82C26AD70EB00A0E0F7 /* View+floatingPanelContentInsetAdjustmentBehavior.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+floatingPanelContentInsetAdjustmentBehavior.swift"; sourceTree = ""; }; + 64F7E82D26AD70EB00A0E0F7 /* View+floatingPanelContentMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+floatingPanelContentMode.swift"; sourceTree = ""; }; + 64F7E82E26AD70EB00A0E0F7 /* FloatingPanelView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FloatingPanelView.swift; sourceTree = ""; }; + 64F7E82F26AD70EB00A0E0F7 /* View+floatingPanel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+floatingPanel.swift"; sourceTree = ""; }; + 64F7E83026AD70EB00A0E0F7 /* View+floatingPanelSurfaceAppearance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+floatingPanelSurfaceAppearance.swift"; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 64A5B71C2691323900BCAA05 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 64A5B734269133DC00BCAA05 /* FloatingPanel.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 64A5B7162691323900BCAA05 = { + isa = PBXGroup; + children = ( + 64A5B7212691323900BCAA05 /* Maps */, + 64A5B7202691323900BCAA05 /* Products */, + 64A5B732269133DC00BCAA05 /* Frameworks */, + ); + sourceTree = ""; + }; + 64A5B7202691323900BCAA05 /* Products */ = { + isa = PBXGroup; + children = ( + 64A5B71F2691323900BCAA05 /* Maps-SwiftUI.app */, + ); + name = Products; + sourceTree = ""; + }; + 64A5B7212691323900BCAA05 /* Maps */ = { + isa = PBXGroup; + children = ( + 64F7E82B26AD70EB00A0E0F7 /* FloatingPanel */, + 64BE55702691BBB0006D98BD /* Representable */, + 64A5B7222691323900BCAA05 /* MapsApp.swift */, + 64A5B7242691323900BCAA05 /* ContentView.swift */, + 64A5B73B2691469900BCAA05 /* FloatingPanelContentView.swift */, + 6467E8692699B19D00565F4F /* SearchPanelPhoneDelegate.swift */, + 6467E8632699AC5F00565F4F /* SurfaceAppearance+phone.swift */, + 64A5B7412691541A00BCAA05 /* HostingCell.swift */, + 64A5B73F2691532400BCAA05 /* ResultsList.swift */, + ); + path = Maps; + sourceTree = ""; + }; + 64A5B732269133DC00BCAA05 /* Frameworks */ = { + isa = PBXGroup; + children = ( + 64A5B733269133DC00BCAA05 /* FloatingPanel.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + 64BE55702691BBB0006D98BD /* Representable */ = { + isa = PBXGroup; + children = ( + 64A5B73D269147DC00BCAA05 /* SearchBar.swift */, + 64A5B737269134CA00BCAA05 /* VisualEffectBlur.swift */, + ); + path = Representable; + sourceTree = ""; + }; + 64F7E82B26AD70EB00A0E0F7 /* FloatingPanel */ = { + isa = PBXGroup; + children = ( + 64F7E82E26AD70EB00A0E0F7 /* FloatingPanelView.swift */, + 64F7E82F26AD70EB00A0E0F7 /* View+floatingPanel.swift */, + 64F7E82C26AD70EB00A0E0F7 /* View+floatingPanelContentInsetAdjustmentBehavior.swift */, + 64F7E82D26AD70EB00A0E0F7 /* View+floatingPanelContentMode.swift */, + 64F7E83026AD70EB00A0E0F7 /* View+floatingPanelSurfaceAppearance.swift */, + 649A122C26C168CF00DAB961 /* View+floatingPanelGrabberHandlePadding.swift */, + 649A122826C14D0900DAB961 /* UIHostingController+ignoreKeyboard.swift */, + ); + path = FloatingPanel; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 64A5B71E2691323900BCAA05 /* Maps-SwiftUI */ = { + isa = PBXNativeTarget; + buildConfigurationList = 64A5B72D2691323A00BCAA05 /* Build configuration list for PBXNativeTarget "Maps-SwiftUI" */; + buildPhases = ( + 64A5B71B2691323900BCAA05 /* Sources */, + 64A5B71C2691323900BCAA05 /* Frameworks */, + 64A5B71D2691323900BCAA05 /* Resources */, + 64A5B736269133DC00BCAA05 /* Embed Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "Maps-SwiftUI"; + productName = "Maps-SwiftUI"; + productReference = 64A5B71F2691323900BCAA05 /* Maps-SwiftUI.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 64A5B7172691323900BCAA05 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1300; + LastUpgradeCheck = 1300; + TargetAttributes = { + 64A5B71E2691323900BCAA05 = { + CreatedOnToolsVersion = 13.0; + }; + }; + }; + buildConfigurationList = 64A5B71A2691323900BCAA05 /* Build configuration list for PBXProject "Maps-SwiftUI" */; + compatibilityVersion = "Xcode 13.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 64A5B7162691323900BCAA05; + productRefGroup = 64A5B7202691323900BCAA05 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 64A5B71E2691323900BCAA05 /* Maps-SwiftUI */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 64A5B71D2691323900BCAA05 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 64A5B71B2691323900BCAA05 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 6467E86A2699B19D00565F4F /* SearchPanelPhoneDelegate.swift in Sources */, + 64A5B7422691541A00BCAA05 /* HostingCell.swift in Sources */, + 64A5B738269134CA00BCAA05 /* VisualEffectBlur.swift in Sources */, + 64A5B7402691532400BCAA05 /* ResultsList.swift in Sources */, + 6467E8642699AC5F00565F4F /* SurfaceAppearance+phone.swift in Sources */, + 64F7E83226AD70EB00A0E0F7 /* View+floatingPanelContentMode.swift in Sources */, + 64A5B7252691323900BCAA05 /* ContentView.swift in Sources */, + 64A5B7232691323900BCAA05 /* MapsApp.swift in Sources */, + 64A5B73C2691469900BCAA05 /* FloatingPanelContentView.swift in Sources */, + 649A122D26C168CF00DAB961 /* View+floatingPanelGrabberHandlePadding.swift in Sources */, + 64F7E83326AD70EB00A0E0F7 /* FloatingPanelView.swift in Sources */, + 64F7E83426AD70EB00A0E0F7 /* View+floatingPanel.swift in Sources */, + 64F7E83526AD70EB00A0E0F7 /* View+floatingPanelSurfaceAppearance.swift in Sources */, + 64F7E83126AD70EB00A0E0F7 /* View+floatingPanelContentInsetAdjustmentBehavior.swift in Sources */, + 64A5B73E269147DC00BCAA05 /* SearchBar.swift in Sources */, + 649A122926C14D0900DAB961 /* UIHostingController+ignoreKeyboard.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 64A5B72B2691323A00BCAA05 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 64A5B72C2691323A00BCAA05 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 64A5B72E2691323A00BCAA05 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "example.Maps-SwiftUI"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 64A5B72F2691323A00BCAA05 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "exmaple.Maps-SwiftUI"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 64A5B71A2691323900BCAA05 /* Build configuration list for PBXProject "Maps-SwiftUI" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 64A5B72B2691323A00BCAA05 /* Debug */, + 64A5B72C2691323A00BCAA05 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 64A5B72D2691323A00BCAA05 /* Build configuration list for PBXNativeTarget "Maps-SwiftUI" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 64A5B72E2691323A00BCAA05 /* Debug */, + 64A5B72F2691323A00BCAA05 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 64A5B7172691323900BCAA05 /* Project object */; +} diff --git a/Examples/Maps-SwiftUI/Maps/ContentView.swift b/Examples/Maps-SwiftUI/Maps/ContentView.swift new file mode 100644 index 00000000..00d82722 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/ContentView.swift @@ -0,0 +1,33 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import SwiftUI +import MapKit + +struct ContentView: View { + @State private var region = MKCoordinateRegion( + center: CLLocationCoordinate2D(latitude: 37.623198015869235, longitude: -122.43066818432008), + span: MKCoordinateSpan(latitudeDelta: 0.4425100023575723, longitudeDelta: 0.28543697435880233) + ) + + var body: some View { + ZStack { + Map(coordinateRegion: $region) + .ignoresSafeArea() + statusBarBlur + } + } + + private var statusBarBlur: some View { + GeometryReader { geometry in + VisualEffectBlur() + .frame(height: geometry.safeAreaInsets.top) + .ignoresSafeArea() + } + } +} + +struct ContentView_Previews: PreviewProvider { + static var previews: some View { + ContentView() + } +} diff --git a/Examples/Maps-SwiftUI/Maps/FloatingPanel/FloatingPanelView.swift b/Examples/Maps-SwiftUI/Maps/FloatingPanel/FloatingPanelView.swift new file mode 100644 index 00000000..e0376323 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/FloatingPanel/FloatingPanelView.swift @@ -0,0 +1,133 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import FloatingPanel +import SwiftUI + +/// A proxy for exposing the methods of the floating panel controller. +public struct FloatingPanelProxy { + /// The associated floating panel controller. + public weak var fpc: FloatingPanelController? + + /// Tracks the specified scroll view to correspond with the scroll. + /// + /// - Parameter scrollView: Specify a scroll view to continuously and + /// seamlessly work in concert with interactions of the surface view. + public func track(scrollView: UIScrollView) { + fpc?.track(scrollView: scrollView) + } + + /// Moves the floating panel to the specified position. + /// + /// - Parameters: + /// - floatingPanelState: The state to move to. + /// - animated: `true` to animate the transition to the new state; `false` + /// otherwise. + public func move( + to floatingPanelState: FloatingPanelState, + animated: Bool, + completion: (() -> Void)? = nil + ) { + fpc?.move(to: floatingPanelState, animated: animated, completion: completion) + } +} + +/// A view with an associated floating panel. +struct FloatingPanelView: UIViewControllerRepresentable { + /// A type that conforms to the `FloatingPanelControllerDelegate` protocol. + var delegate: FloatingPanelControllerDelegate? + + /// The behavior for determining the adjusted content offsets. + @Environment(\.contentInsetAdjustmentBehavior) var contentInsetAdjustmentBehavior + + /// Constants that define how a panel content fills in the surface. + @Environment(\.contentMode) var contentMode + + /// The floating panel grabber handle offset. + @Environment(\.grabberHandlePadding) var grabberHandlePadding + + /// The floating panel `surfaceView` appearance. + @Environment(\.surfaceAppearance) var surfaceAppearance + + /// The view builder that creates the floating panel parent view content. + @ViewBuilder var content: Content + + /// The view builder that creates the floating panel content. + @ViewBuilder var floatingPanelContent: (FloatingPanelProxy) -> FloatingPanelContent + + public func makeUIViewController(context: Context) -> UIHostingController { + let hostingController = UIHostingController(rootView: content) + hostingController.view.backgroundColor = nil + // We need to wait for the current runloop cycle to complete before our + // view is actually added (into the view hierarchy), otherwise the + // environment is not ready yet. + DispatchQueue.main.async { + context.coordinator.setupFloatingPanel(hostingController) + } + return hostingController + } + + public func updateUIViewController( + _ uiViewController: UIHostingController, + context: Context + ) { + context.coordinator.updateIfNeeded() + } + + public func makeCoordinator() -> Coordinator { + Coordinator(parent: self) + } + + /// `FloatingPanelView` coordinator. + /// + /// Responsible to setup the view hierarchy and floating panel. + final class Coordinator { + private let parent: FloatingPanelView + private lazy var fpc = FloatingPanelController() + + init(parent: FloatingPanelView) { + self.parent = parent + } + + func setupFloatingPanel(_ parentViewController: UIViewController) { + updateIfNeeded() + let panelContent = parent.floatingPanelContent(FloatingPanelProxy(fpc: fpc)) + let hostingViewController = UIHostingController( + rootView: panelContent, + ignoresKeyboard: true + ) + hostingViewController.view.backgroundColor = nil + let contentViewController = UIViewController() + contentViewController.view.addSubview(hostingViewController.view) + fpc.set(contentViewController: contentViewController) + fpc.addPanel(toParent: parentViewController, animated: false) + + hostingViewController.view.translatesAutoresizingMaskIntoConstraints = false + let bottomConstraint = hostingViewController.view.bottomAnchor.constraint(equalTo: contentViewController.view.bottomAnchor) + bottomConstraint.priority = .defaultHigh + NSLayoutConstraint.activate([ + hostingViewController.view.topAnchor.constraint(equalTo: contentViewController.view.topAnchor), + hostingViewController.view.leadingAnchor.constraint(equalTo: contentViewController.view.leadingAnchor), + hostingViewController.view.trailingAnchor.constraint(equalTo: contentViewController.view.trailingAnchor), + bottomConstraint + ]) + } + + func updateIfNeeded() { + if fpc.contentInsetAdjustmentBehavior != parent.contentInsetAdjustmentBehavior { + fpc.contentInsetAdjustmentBehavior = parent.contentInsetAdjustmentBehavior + } + if fpc.contentMode != parent.contentMode { + fpc.contentMode = parent.contentMode + } + if fpc.delegate !== parent.delegate { + fpc.delegate = parent.delegate + } + if fpc.surfaceView.grabberHandlePadding != parent.grabberHandlePadding { + fpc.surfaceView.grabberHandlePadding = parent.grabberHandlePadding + } + if fpc.surfaceView.appearance != parent.surfaceAppearance { + fpc.surfaceView.appearance = parent.surfaceAppearance + } + } + } +} diff --git a/Examples/Maps-SwiftUI/Maps/FloatingPanel/UIHostingController+ignoreKeyboard.swift b/Examples/Maps-SwiftUI/Maps/FloatingPanel/UIHostingController+ignoreKeyboard.swift new file mode 100644 index 00000000..7d9e5308 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/FloatingPanel/UIHostingController+ignoreKeyboard.swift @@ -0,0 +1,44 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import SwiftUI + +/// This extension makes sure SwiftUI views are not affected by iOS keyboard. +/// +/// Credits to https://steipete.me/posts/disabling-keyboard-avoidance-in-swiftui-uihostingcontroller/ +extension UIHostingController { + public convenience init(rootView: Content, ignoresKeyboard: Bool) { + self.init(rootView: rootView) + + if ignoresKeyboard { + guard let viewClass = object_getClass(view) else { return } + + let viewSubclassName = String( + cString: class_getName(viewClass) + ).appending("_IgnoresKeyboard") + + if let viewSubclass = NSClassFromString(viewSubclassName) { + object_setClass(view, viewSubclass) + } else { + guard + let viewClassNameUtf8 = (viewSubclassName as NSString).utf8String, + let viewSubclass = objc_allocateClassPair(viewClass, viewClassNameUtf8, 0) + else { return } + + if let method = class_getInstanceMethod( + viewClass, + NSSelectorFromString("keyboardWillShowWithNotification:") + ) { + let keyboardWillShow: @convention(block) (AnyObject, AnyObject) -> Void = { _, _ in } + class_addMethod( + viewSubclass, + NSSelectorFromString("keyboardWillShowWithNotification:"), + imp_implementationWithBlock(keyboardWillShow), + method_getTypeEncoding(method) + ) + } + objc_registerClassPair(viewSubclass) + object_setClass(view, viewSubclass) + } + } + } +} diff --git a/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanel.swift b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanel.swift new file mode 100644 index 00000000..43ed1c3d --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanel.swift @@ -0,0 +1,31 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import FloatingPanel +import SwiftUI + +extension View { + /// Presents a floating panel using the given closure as its content. + /// + /// The modifier's content view builder receives a `FloatingPanelProxy` + /// instance; you use the proxy's methods to interact with the associated + /// `FloatingPanelController`. + /// + /// - Parameters: + /// - delegate: A type that conforms to the + /// `FloatingPanelControllerDelegate` protocol. You have comprehensive + /// control over the floating panel behavior when you use a delegate. + /// - floatingPanelContent: The floating panel content. This view builder + /// receives a `FloatingPanelProxy` instance that you use to interact + /// with the `FloatingPanelController`. + public func floatingPanel( + delegate: FloatingPanelControllerDelegate? = nil, + @ViewBuilder _ floatingPanelContent: @escaping (_: FloatingPanelProxy) -> FloatingPanelContent + ) -> some View { + FloatingPanelView( + delegate: delegate, + content: { self }, + floatingPanelContent: floatingPanelContent + ) + .ignoresSafeArea() + } +} diff --git a/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelContentInsetAdjustmentBehavior.swift b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelContentInsetAdjustmentBehavior.swift new file mode 100644 index 00000000..99094dc3 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelContentInsetAdjustmentBehavior.swift @@ -0,0 +1,38 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import FloatingPanel +import SwiftUI + +struct ContentInsetKey: EnvironmentKey { + static var defaultValue: FloatingPanelController.ContentInsetAdjustmentBehavior = .always +} + +extension EnvironmentValues { + /// The behavior for determining the adjusted content offsets. + var contentInsetAdjustmentBehavior: FloatingPanelController.ContentInsetAdjustmentBehavior { + get { self[ContentInsetKey.self] } + set { self[ContentInsetKey.self] = newValue } + } +} + +extension View { + /// Sets the content inset adjustment behavior for floating panels within + /// this view. + /// + /// Use this modifier to set a specific content inset adjustment behavior + /// for floating panel instances within a view: + /// + /// MainView() + /// .floatingPanel { _ in + /// FloatingPanelContent() + /// } + /// .floatingPanelContentInsetAdjustmentBehavior(.never) + /// + /// - Parameter contentInsetAdjustmentBehavior: The content inset adjustment + /// behavior to set. + public func floatingPanelContentInsetAdjustmentBehavior( + _ contentInsetAdjustmentBehavior: FloatingPanelController.ContentInsetAdjustmentBehavior + ) -> some View { + environment(\.contentInsetAdjustmentBehavior, contentInsetAdjustmentBehavior) + } +} diff --git a/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelContentMode.swift b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelContentMode.swift new file mode 100644 index 00000000..05594c2f --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelContentMode.swift @@ -0,0 +1,37 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import FloatingPanel +import SwiftUI + +struct ContentModeKey: EnvironmentKey { + static var defaultValue: FloatingPanelController.ContentMode = .static +} + +extension EnvironmentValues { + /// Used to determine how the floating panel controller lays out the content + /// view when the surface position changes. + var contentMode: FloatingPanelController.ContentMode { + get { self[ContentModeKey.self] } + set { self[ContentModeKey.self] = newValue } + } +} + +extension View { + /// Sets the content mode for floating panels within this view. + /// + /// Use this modifier to set a specific content mode for floating panel + /// instances within a view: + /// + /// MainView() + /// .floatingPanel { _ in + /// FloatingPanelContent() + /// } + /// .floatingPanelContentMode(.static) + /// + /// - Parameter contentMode: The content mode to set. + public func floatingPanelContentMode( + _ contentMode: FloatingPanelController.ContentMode + ) -> some View { + environment(\.contentMode, contentMode) + } +} diff --git a/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelGrabberHandlePadding.swift b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelGrabberHandlePadding.swift new file mode 100644 index 00000000..64ff6e10 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelGrabberHandlePadding.swift @@ -0,0 +1,36 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import FloatingPanel +import SwiftUI + +struct GrabberHandlePaddingKey: EnvironmentKey { + static var defaultValue: CGFloat = 6.0 +} + +extension EnvironmentValues { + /// The offset of the grabber handle from the interactive edge. + var grabberHandlePadding: CGFloat { + get { self[GrabberHandlePaddingKey.self] } + set { self[GrabberHandlePaddingKey.self] = newValue } + } +} + +extension View { + /// Sets the grabber handle padding for floating panels within this view. + /// + /// Use this modifier to set a specific padding to floating panel instances + /// within a view: + /// + /// MainView() + /// .floatingPanel { _ in + /// FloatingPanelContent() + /// } + /// .floatingPanelGrabberHandlePadding(16) + /// + /// - Parameter padding: The grabber handle padding to set. + public func floatingPanelGrabberHandlePadding( + _ padding: CGFloat + ) -> some View { + environment(\.grabberHandlePadding, padding) + } +} diff --git a/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelSurfaceAppearance.swift b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelSurfaceAppearance.swift new file mode 100644 index 00000000..0f004541 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/FloatingPanel/View+floatingPanelSurfaceAppearance.swift @@ -0,0 +1,44 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import FloatingPanel +import SwiftUI + +struct SurfaceAppearanceKey: EnvironmentKey { + static var defaultValue = SurfaceAppearance() +} + +extension EnvironmentValues { + /// The appearance of a surface view. + var surfaceAppearance: SurfaceAppearance { + get { self[SurfaceAppearanceKey.self] } + set { self[SurfaceAppearanceKey.self] = newValue } + } +} + +extension View { + /// Sets the surface appearance for floating panels within this view. + /// + /// Use this modifier to set a specific surface appearance for floating + /// panel instances within a view: + /// + /// MainView() + /// .floatingPanel { _ in + /// FloatingPanelContent() + /// } + /// .floatingPanelSurfaceAppearance(.transparent) + /// + /// extension SurfaceAppearance { + /// static var transparent: SurfaceAppearance { + /// let appearance = SurfaceAppearance() + /// appearance.backgroundColor = .clear + /// return appearance + /// } + /// } + /// + /// - Parameter surfaceAppearance: The surface appearance to set. + public func floatingPanelSurfaceAppearance( + _ surfaceAppearance: SurfaceAppearance + ) -> some View { + environment(\.surfaceAppearance, surfaceAppearance) + } +} diff --git a/Examples/Maps-SwiftUI/Maps/FloatingPanelContentView.swift b/Examples/Maps-SwiftUI/Maps/FloatingPanelContentView.swift new file mode 100644 index 00000000..1be68d93 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/FloatingPanelContentView.swift @@ -0,0 +1,43 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import SwiftUI + +struct FloatingPanelContentView: View { + @State private var searchText = "" + @State private var isShowingCancelButton = false + var proxy: FloatingPanelProxy + + var body: some View { + VStack(spacing: 0) { + searchBar + resultsList + } + // 👇🏻 for the floating panel grabber handle. + .padding(.top, 6) + .background( + VisualEffectBlur(blurStyle: .systemMaterial) + // ⚠️ If the `VisualEffectBlur` view receives taps, it's going + // to mess up with the whole panel and render it + // non-interactive, make sure it never receives any taps. + .allowsHitTesting(false) + ) + .ignoresSafeArea() + } + + var searchBar: some View { + SearchBar( + "Search for a place or address", + text: $searchText, + isShowingCancelButton: $isShowingCancelButton + ) { isFocused in + proxy.move(to: isFocused ? .full : .half, animated: true) + isShowingCancelButton = isFocused + } onCancel: { + UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) + } + } + + var resultsList: some View { + ResultsList(onScrollViewCreated: proxy.track(scrollView:)) + } +} diff --git a/Examples/Maps-SwiftUI/Maps/HostingCell.swift b/Examples/Maps-SwiftUI/Maps/HostingCell.swift new file mode 100644 index 00000000..cfea30c2 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/HostingCell.swift @@ -0,0 +1,49 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import SwiftUI + +/// A `UITableViewCell` that accepts a SwiftUI view as its content. +/// +/// Credits to https://noahgilmore.com/blog/swiftui-self-sizing-cells/ . +public final class HostingCell: UITableViewCell { + private let hostingController = UIHostingController( + rootView: nil, + ignoresKeyboard: true + ) + + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { + super.init(style: style, reuseIdentifier: reuseIdentifier) + hostingController.view.backgroundColor = nil + backgroundColor = .clear + } + + @available(*, unavailable) + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + public func set(rootView: Content, parentController: UIViewController) { + hostingController.rootView = rootView + hostingController.view.invalidateIntrinsicContentSize() + + let requiresControllerMove = hostingController.parent != parentController + if requiresControllerMove { + parentController.addChild(hostingController) + } + + if !contentView.subviews.contains(hostingController.view) { + contentView.addSubview(hostingController.view) + hostingController.view.translatesAutoresizingMaskIntoConstraints = false + contentView.addConstraints([ + hostingController.view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), + hostingController.view.topAnchor.constraint(equalTo: contentView.topAnchor), + hostingController.view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), + hostingController.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) + ]) + } + + if requiresControllerMove { + hostingController.didMove(toParent: parentController) + } + } +} diff --git a/Examples/Maps-SwiftUI/Maps/MapsApp.swift b/Examples/Maps-SwiftUI/Maps/MapsApp.swift new file mode 100644 index 00000000..78a40f38 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/MapsApp.swift @@ -0,0 +1,18 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import SwiftUI + +@main +struct MapsApp: App { + var body: some Scene { + WindowGroup { + ContentView() + .floatingPanel(delegate: SearchPanelPhoneDelegate()) { proxy in + FloatingPanelContentView(proxy: proxy) + } + .floatingPanelSurfaceAppearance(.phone) + .floatingPanelContentMode(.fitToBounds) + .floatingPanelContentInsetAdjustmentBehavior(.never) + } + } +} diff --git a/Examples/Maps-SwiftUI/Maps/Representable/SearchBar.swift b/Examples/Maps-SwiftUI/Maps/Representable/SearchBar.swift new file mode 100644 index 00000000..111e9959 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/Representable/SearchBar.swift @@ -0,0 +1,74 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import SwiftUI + +/// UIKit's `UISearchBar`brought to SwiftUI. +public struct SearchBar: UIViewRepresentable { + var title: String + @Binding var text: String + @Binding var isShowingCancelButton: Bool + var onEditingChanged: (Bool) -> Void + var onCancel: () -> Void + + public init( + _ title: String = "", + text: Binding, + isShowingCancelButton: Binding, + onEditingChanged: @escaping (Bool) -> Void = { _ in }, + onCancel: @escaping () -> Void + ) { + self.title = title + self._text = text + self._isShowingCancelButton = isShowingCancelButton + self.onEditingChanged = onEditingChanged + self.onCancel = onCancel + } + + public func makeUIView(context: UIViewRepresentableContext) -> UISearchBar { + let searchBar = UISearchBar(frame: .zero) + searchBar.searchBarStyle = .minimal + searchBar.isTranslucent = true + searchBar.placeholder = title + searchBar.delegate = context.coordinator + searchBar.autocapitalizationType = .none + return searchBar + } + + public func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext) { + uiView.text = text + uiView.placeholder = title + uiView.setShowsCancelButton(isShowingCancelButton, animated: true) + } + + public func makeCoordinator() -> SearchBar.Coordinator { + Coordinator(parent: self) + } + + public class Coordinator: NSObject, UISearchBarDelegate { + var parent: SearchBar + + init(parent: SearchBar) { + self.parent = parent + } + + public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { + parent.text = searchText + } + + public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { + parent.onEditingChanged(true) + } + + public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { + searchBar.resignFirstResponder() + } + + public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { + parent.onEditingChanged(false) + } + + public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { + parent.onCancel() + } + } +} diff --git a/Examples/Maps-SwiftUI/Maps/Representable/VisualEffectBlur.swift b/Examples/Maps-SwiftUI/Maps/Representable/VisualEffectBlur.swift new file mode 100644 index 00000000..4adda4fb --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/Representable/VisualEffectBlur.swift @@ -0,0 +1,69 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import SwiftUI + +@available(iOS, introduced: 13, deprecated: 15, message: "Use iOS 15 material API.") +public struct VisualEffectBlur: UIViewRepresentable { + var blurStyle: UIBlurEffect.Style = .systemMaterial + var vibrancyStyle: UIVibrancyEffectStyle? = nil + @ViewBuilder var content: Content + + public func makeUIView(context: Context) -> UIVisualEffectView { + context.coordinator.blurView + } + + public func updateUIView(_ view: UIVisualEffectView, context: Context) { + context.coordinator.update( + content: content, + blurStyle: blurStyle, + vibrancyStyle: vibrancyStyle + ) + } + + public func makeCoordinator() -> Coordinator { + Coordinator(content: content) + } + + public class Coordinator { + let blurView = UIVisualEffectView() + let vibrancyView = UIVisualEffectView() + let hostingController: UIHostingController + + init(content: Content) { + hostingController = UIHostingController(rootView: content) + hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] + hostingController.view.backgroundColor = nil + blurView.contentView.addSubview(vibrancyView) + + blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + vibrancyView.contentView.addSubview(hostingController.view) + vibrancyView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + } + + func update(content: Content, blurStyle: UIBlurEffect.Style, vibrancyStyle: UIVibrancyEffectStyle?) { + hostingController.rootView = content + + let blurEffect = UIBlurEffect(style: blurStyle) + blurView.effect = blurEffect + + if let vibrancyStyle = vibrancyStyle { + vibrancyView.effect = UIVibrancyEffect(blurEffect: blurEffect, style: vibrancyStyle) + } else { + vibrancyView.effect = nil + } + + hostingController.view.setNeedsDisplay() + } + } +} + +public extension VisualEffectBlur where Content == EmptyView { + init( + blurStyle: UIBlurEffect.Style = .systemMaterial, + vibrancyStyle: UIVibrancyEffectStyle? = nil + ) { + self.init(blurStyle: blurStyle, vibrancyStyle: vibrancyStyle) { + EmptyView() + } + } +} diff --git a/Examples/Maps-SwiftUI/Maps/ResultsList.swift b/Examples/Maps-SwiftUI/Maps/ResultsList.swift new file mode 100644 index 00000000..df7c5dee --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/ResultsList.swift @@ -0,0 +1,138 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import SwiftUI + +struct ResultsList: UIViewControllerRepresentable { + var onScrollViewCreated: (_ scrollView: UIScrollView) -> Void + + func makeUIViewController( + context: Context + ) -> ResultsTableViewController { + let rtvc = ResultsTableViewController() + onScrollViewCreated(rtvc.tableView) + return rtvc + } + + func updateUIViewController( + _ uiViewController: ResultsTableViewController, + context: Context + ) { + } +} + +final class ResultsTableViewController: UITableViewController { + private let reuseIdentifier = "HostingCell" + + private enum Section: CaseIterable { + case main + } + + private struct TableViewItem: Hashable { + let color: Color + let symbolName: String + let title: String + let description: String + } + + private var dataSource: UITableViewDiffableDataSource? + + // MARK: Lifecycle + + override func viewDidLoad() { + super.viewDidLoad() + tableView.backgroundColor = nil + tableView.register(HostingCell.self, forCellReuseIdentifier: reuseIdentifier) + configureDataSource() + } + + // MARK: UITableViewDataSource + + private func configureDataSource() { + dataSource = UITableViewDiffableDataSource + (tableView: tableView) { [weak self] tableView, _, tableItem -> UITableViewCell? in + self?.tableView(tableView, cellForTableViewItem: tableItem) + } + tableView.dataSource = dataSource + + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([.main]) + let results: [TableViewItem] = (1...100).map { + TableViewItem( + color: Color(red: 255 / 255.0, green: 94 / 255.0 , blue: 94 / 255.0), + symbolName: "heart.fill", + title: "Favorites", + description: "\($0) Places" + ) + } + snapshot.appendItems(results, toSection: .main) + dataSource?.apply(snapshot, animatingDifferences: false) + } + + private func tableView( + _ tableView: UITableView, + cellForTableViewItem tableViewItem: TableViewItem + ) -> UITableViewCell { + let cell: HostingCell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) as! HostingCell + setupResultTableViewCell( + cell, + color: tableViewItem.color, + symbolName: tableViewItem.symbolName, + title: tableViewItem.title, + description: tableViewItem.description + ) + return cell + } + + private func setupResultTableViewCell( + _ cell: HostingCell, + color: Color, + symbolName: String, + title: String, + description: String + ) { + cell.set( + rootView: ResultListCell( + color: color, + symbolName: symbolName, + title: title, + description: description + ), + parentController: self + ) + } + + // MARK: UITableViewDelegate + + override func tableView( + _ tableView: UITableView, + didSelectRowAt indexPath: IndexPath + ) { + tableView.deselectRow(at: indexPath, animated: true) + } +} + +struct ResultListCell: View { + let color: Color + let symbolName: String + let title: String + let description: String + + var body: some View { + HStack { + Image(systemName: symbolName) + .foregroundColor(.white) + .font(.headline) + .padding(8) + .background(Circle().fill(color)) + VStack(alignment: .leading, spacing: 8) { + Text(title) + .font(.system(size: 20, weight: .bold)) + .frame(maxWidth: .infinity, alignment: .leading) + Text(description) + .font(.system(size: 13)) + .foregroundColor(Color(.secondaryLabel)) + } + } + .padding() + } +} diff --git a/Examples/Maps-SwiftUI/Maps/SearchPanelPhoneDelegate.swift b/Examples/Maps-SwiftUI/Maps/SearchPanelPhoneDelegate.swift new file mode 100644 index 00000000..6b95df37 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/SearchPanelPhoneDelegate.swift @@ -0,0 +1,12 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import FloatingPanel +import UIKit + +final class SearchPanelPhoneDelegate: FloatingPanelControllerDelegate { + func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) { + if vc.state == .full { + UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) + } + } +} diff --git a/Examples/Maps-SwiftUI/Maps/SurfaceAppearance+phone.swift b/Examples/Maps-SwiftUI/Maps/SurfaceAppearance+phone.swift new file mode 100644 index 00000000..ee791d31 --- /dev/null +++ b/Examples/Maps-SwiftUI/Maps/SurfaceAppearance+phone.swift @@ -0,0 +1,13 @@ +// Copyright 2021 the FloatingPanel authors. All rights reserved. MIT license. + +import FloatingPanel + +extension FloatingPanel.SurfaceAppearance { + static var phone: SurfaceAppearance { + let appearance = SurfaceAppearance() + appearance.cornerCurve = .continuous + appearance.cornerRadius = 8.0 + appearance.backgroundColor = .clear + return appearance + } +} diff --git a/Examples/Maps/Maps.xcodeproj/project.pbxproj b/Examples/Maps/Maps.xcodeproj/project.pbxproj index c22ac05a..64489c71 100644 --- a/Examples/Maps/Maps.xcodeproj/project.pbxproj +++ b/Examples/Maps/Maps.xcodeproj/project.pbxproj @@ -7,13 +7,17 @@ objects = { /* Begin PBXBuildFile section */ + 543844BD23D2BE2000D5EDE4 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 543844BC23D2BE2000D5EDE4 /* MapKit.framework */; }; + 549A5F59244673FE0025F312 /* SearchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 549A5F58244673FE0025F312 /* SearchViewController.swift */; }; + 549D23D2233C77D5008EF4D7 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 549D23D1233C77D5008EF4D7 /* FloatingPanel.framework */; }; + 549D23D3233C77D5008EF4D7 /* FloatingPanel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 549D23D1233C77D5008EF4D7 /* FloatingPanel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 54B5112A216C3D840033A6F3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B51129216C3D840033A6F3 /* AppDelegate.swift */; }; - 54B5112C216C3D840033A6F3 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B5112B216C3D840033A6F3 /* ViewController.swift */; }; + 54B5112C216C3D840033A6F3 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B5112B216C3D840033A6F3 /* MainViewController.swift */; }; 54B5112F216C3D840033A6F3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 54B5112D216C3D840033A6F3 /* Main.storyboard */; }; 54B51131216C3D860033A6F3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 54B51130216C3D860033A6F3 /* Assets.xcassets */; }; 54B51134216C3D860033A6F3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 54B51132216C3D860033A6F3 /* LaunchScreen.storyboard */; }; - 54B5113F216C407F0033A6F3 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54B5113E216C407F0033A6F3 /* FloatingPanel.framework */; }; - 54B51140216C407F0033A6F3 /* FloatingPanel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 54B5113E216C407F0033A6F3 /* FloatingPanel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 54E26CB624A989090066C720 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54E26CB524A989090066C720 /* Utils.swift */; }; + 54E26CB824A98E310066C720 /* DetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54E26CB724A98E310066C720 /* DetailViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -23,7 +27,7 @@ dstPath = ""; dstSubfolderSpec = 10; files = ( - 54B51140216C407F0033A6F3 /* FloatingPanel.framework in Embed Frameworks */, + 549D23D3233C77D5008EF4D7 /* FloatingPanel.framework in Embed Frameworks */, ); name = "Embed Frameworks"; runOnlyForDeploymentPostprocessing = 0; @@ -31,14 +35,18 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 543844BC23D2BE2000D5EDE4 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; }; + 549A5F58244673FE0025F312 /* SearchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchViewController.swift; sourceTree = ""; }; + 549D23D1233C77D5008EF4D7 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 54B51126216C3D840033A6F3 /* Maps.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Maps.app; sourceTree = BUILT_PRODUCTS_DIR; }; 54B51129216C3D840033A6F3 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 54B5112B216C3D840033A6F3 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + 54B5112B216C3D840033A6F3 /* MainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; 54B5112E216C3D840033A6F3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 54B51130216C3D860033A6F3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 54B51133216C3D860033A6F3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 54B51135216C3D860033A6F3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 54B5113E216C407F0033A6F3 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 54E26CB524A989090066C720 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = ""; }; + 54E26CB724A98E310066C720 /* DetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -46,19 +54,29 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 54B5113F216C407F0033A6F3 /* FloatingPanel.framework in Frameworks */, + 543844BD23D2BE2000D5EDE4 /* MapKit.framework in Frameworks */, + 549D23D2233C77D5008EF4D7 /* FloatingPanel.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 543844BB23D2BE1F00D5EDE4 /* Frameworks */ = { + isa = PBXGroup; + children = ( + 543844BC23D2BE2000D5EDE4 /* MapKit.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; 54B5111D216C3D840033A6F3 = { isa = PBXGroup; children = ( - 54B5113E216C407F0033A6F3 /* FloatingPanel.framework */, + 549D23D1233C77D5008EF4D7 /* FloatingPanel.framework */, 54B51128216C3D840033A6F3 /* Maps */, 54B51127216C3D840033A6F3 /* Products */, + 543844BB23D2BE1F00D5EDE4 /* Frameworks */, ); sourceTree = ""; }; @@ -73,11 +91,14 @@ 54B51128216C3D840033A6F3 /* Maps */ = { isa = PBXGroup; children = ( - 54B51129216C3D840033A6F3 /* AppDelegate.swift */, - 54B5112B216C3D840033A6F3 /* ViewController.swift */, - 54B5112D216C3D840033A6F3 /* Main.storyboard */, 54B51130216C3D860033A6F3 /* Assets.xcassets */, 54B51132216C3D860033A6F3 /* LaunchScreen.storyboard */, + 54B5112D216C3D840033A6F3 /* Main.storyboard */, + 54B51129216C3D840033A6F3 /* AppDelegate.swift */, + 54B5112B216C3D840033A6F3 /* MainViewController.swift */, + 549A5F58244673FE0025F312 /* SearchViewController.swift */, + 54E26CB724A98E310066C720 /* DetailViewController.swift */, + 54E26CB524A989090066C720 /* Utils.swift */, 54B51135216C3D860033A6F3 /* Info.plist */, ); path = Maps; @@ -111,7 +132,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1000; - LastUpgradeCheck = 1000; + LastUpgradeCheck = 1300; ORGANIZATIONNAME = scenee; TargetAttributes = { 54B51125216C3D840033A6F3 = { @@ -155,8 +176,11 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 54B5112C216C3D840033A6F3 /* ViewController.swift in Sources */, + 549A5F59244673FE0025F312 /* SearchViewController.swift in Sources */, + 54B5112C216C3D840033A6F3 /* MainViewController.swift in Sources */, + 54E26CB824A98E310066C720 /* DetailViewController.swift in Sources */, 54B5112A216C3D840033A6F3 /* AppDelegate.swift in Sources */, + 54E26CB624A989090066C720 /* Utils.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -208,6 +232,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -269,6 +294,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -305,14 +331,14 @@ CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = Maps/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.Maps; + PRODUCT_BUNDLE_IDENTIFIER = example.Maps; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -324,14 +350,14 @@ CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = Maps/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.Maps; + PRODUCT_BUNDLE_IDENTIFIER = example.Maps; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/Examples/Maps/Maps.xcodeproj/xcshareddata/xcschemes/Maps.xcscheme b/Examples/Maps/Maps.xcodeproj/xcshareddata/xcschemes/Maps.xcscheme index e45ed143..fdc44775 100644 --- a/Examples/Maps/Maps.xcodeproj/xcshareddata/xcschemes/Maps.xcscheme +++ b/Examples/Maps/Maps.xcodeproj/xcshareddata/xcschemes/Maps.xcscheme @@ -1,6 +1,6 @@ - - - - + + - - Bool { - return true - } } diff --git a/Examples/Maps/Maps/Base.lproj/Main.storyboard b/Examples/Maps/Maps/Base.lproj/Main.storyboard index b3913fac..35cfa749 100644 --- a/Examples/Maps/Maps/Base.lproj/Main.storyboard +++ b/Examples/Maps/Maps/Base.lproj/Main.storyboard @@ -1,35 +1,32 @@ - - - - + - - + - + - + - + - + - + - + - + + @@ -41,7 +38,6 @@ - @@ -51,36 +47,36 @@ - + - + - + - + - + - + - + - + - + - + @@ -90,7 +86,7 @@ - + @@ -108,7 +104,7 @@ - + @@ -128,7 +124,7 @@ - + @@ -148,7 +144,7 @@ - + - + @@ -185,16 +181,16 @@ - + - + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/Maps/Maps/DetailViewController.swift b/Examples/Maps/Maps/DetailViewController.swift new file mode 100644 index 00000000..a0f628ad --- /dev/null +++ b/Examples/Maps/Maps/DetailViewController.swift @@ -0,0 +1,7 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit + +class DetailViewController: UIViewController { + var item: LocationItem? +} diff --git a/Examples/Maps/Maps/MainViewController.swift b/Examples/Maps/Maps/MainViewController.swift new file mode 100644 index 00000000..3252404c --- /dev/null +++ b/Examples/Maps/Maps/MainViewController.swift @@ -0,0 +1,394 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit +import MapKit +import FloatingPanel + +class MainViewController: UIViewController { + typealias PanelDelegate = FloatingPanelControllerDelegate & UIGestureRecognizerDelegate + + // Search Panel + lazy var fpc = FloatingPanelController() + lazy var fpcDelegate: PanelDelegate = + (traitCollection.userInterfaceIdiom == .pad) ? SearchPanelPadDelegate(owner: self) : SearchPanelPhoneDelegate(owner: self) + lazy var searchVC = + storyboard?.instantiateViewController(withIdentifier: "SearchViewController") as! SearchViewController + + // Detail Panel + lazy var detailFpc = FloatingPanelController() + lazy var detailFpcDelegate: PanelDelegate = + (traitCollection.userInterfaceIdiom == .pad) ? DetailPanelPadDelegate(owner: self) : DetailPanelPhoneDelegate(owner: self) + lazy var detailVC = + storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController + + @IBOutlet weak var mapView: MKMapView! +} + +extension MainViewController { + override func viewDidLoad() { + super.viewDidLoad() + fpc.contentMode = .fitToBounds + fpc.delegate = fpcDelegate + fpc.set(contentViewController: searchVC) + fpc.track(scrollView: searchVC.tableView) + + detailFpc.isRemovalInteractionEnabled = true + detailFpc.set(contentViewController: detailVC) + + switch traitCollection.userInterfaceIdiom { + case .pad: + layoutPanelForPad() + default: + layoutPanelForPhone() + } + + setUpMapView() + setUpSearchView() + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + // Must be here + searchVC.searchBar.delegate = self + } + + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + tearDownMapView() + } +} + +extension MainViewController { + func layoutPanelForPad() { + fpc.behavior = SearchPaneliPadBehavior() + fpc.panGestureRecognizer.delegateProxy = fpcDelegate + + // Not use addPanel(toParent:) because of the Auto Layout configuration of fpc.view. + view.addSubview(fpc.view) + addChild(fpc) + fpc.view.frame = view.bounds // Needed for a correct safe area configuration + fpc.view.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + fpc.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0), + fpc.view.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0 ), + fpc.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0), + fpc.view.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0.0), + ]) + fpc.show(animated: false) { [weak self] in + guard let self = self else { return } + self.didMove(toParent: self) + } + + [fpc, detailFpc].forEach { $0.setAppearanceForPad() } + } + + func layoutPanelForPhone() { + fpc.track(scrollView: searchVC.tableView) // Only track the table view on iPhone + fpc.addPanel(toParent: self, animated: true) + + [fpc, detailFpc].forEach { $0.setAppearanceForPhone()} + } +} + +extension FloatingPanelController { + func setAppearanceForPhone() { + let appearance = SurfaceAppearance() + if #available(iOS 13.0, *) { + appearance.cornerCurve = .continuous + } + appearance.cornerRadius = 8.0 + appearance.backgroundColor = .clear + surfaceView.appearance = appearance + } + + func setAppearanceForPad() { + view.clipsToBounds = false + let appearance = SurfaceAppearance() + appearance.cornerRadius = 8.0 + let shadow = SurfaceAppearance.Shadow() + shadow.color = UIColor.black + shadow.offset = CGSize(width: 0, height: 16) + shadow.radius = 16 + shadow.spread = 8 + appearance.shadows = [shadow] + appearance.backgroundColor = .clear + surfaceView.appearance = appearance + } +} + +// MARK: - UISearchBarDelegate + +extension MainViewController: UISearchBarDelegate { + func activate(searchBar: UISearchBar) { + searchBar.showsCancelButton = true + searchVC.showHeader(animated: true) + searchVC.tableView.alpha = 1.0 + detailVC.dismiss(animated: true, completion: nil) + } + func deactivate(searchBar: UISearchBar) { + searchBar.resignFirstResponder() + searchBar.showsCancelButton = false + searchVC.hideHeader(animated: true) + } + + func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { + deactivate(searchBar: searchBar) + UIView.animate(withDuration: 0.25) { + self.fpc.move(to: .half, animated: false) + } + } + + func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { + activate(searchBar: searchBar) + UIView.animate(withDuration: 0.25) { [weak self] in + self?.fpc.move(to: .full, animated: false) + } + } +} + +// MARK: - iPhone + +class SearchPanelPhoneDelegate: NSObject, FloatingPanelControllerDelegate, UIGestureRecognizerDelegate { + unowned let owner: MainViewController + + init(owner: MainViewController) { + self.owner = owner + } + + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + switch newCollection.verticalSizeClass { + case .compact: + let appearance = vc.surfaceView.appearance + appearance.borderWidth = 1.0 / owner.traitCollection.displayScale + appearance.borderColor = UIColor.black.withAlphaComponent(0.2) + vc.surfaceView.appearance = appearance + return SearchPanelLandscapeLayout() + default: + let appearance = vc.surfaceView.appearance + appearance.borderWidth = 0.0 + appearance.borderColor = nil + vc.surfaceView.appearance = appearance + return FloatingPanelBottomLayout() + } + } + + func floatingPanelDidMove(_ vc: FloatingPanelController) { + debugPrint("surfaceLocation: ", vc.surfaceLocation) + let loc = vc.surfaceLocation + + if vc.isAttracting == false { + let minY = vc.surfaceLocation(for: .full).y - 6.0 + let maxY = vc.surfaceLocation(for: .tip).y + 6.0 + vc.surfaceLocation = CGPoint(x: loc.x, y: min(max(loc.y, minY), maxY)) + } + + let tipY = vc.surfaceLocation(for: .tip).y + if loc.y > tipY - 44.0 { + let progress = max(0.0, min((tipY - loc.y) / 44.0, 1.0)) + owner.searchVC.tableView.alpha = progress + } else { + owner.searchVC.tableView.alpha = 1.0 + } + debugPrint("NearbyState : ",vc.nearbyState) + } + + func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) { + if vc.state == .full { + owner.searchVC.searchBar.showsCancelButton = false + owner.searchVC.searchBar.resignFirstResponder() + } + } + + func floatingPanelWillEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer) { + if targetState.pointee != .full { + owner.searchVC.hideHeader(animated: true) + } + } +} + +class SearchPanelLandscapeLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .tip + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 69.0, edge: .bottom, referenceGuide: .safeArea), + ] + func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { + if #available(iOS 11.0, *) { + return [ + surfaceView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 8.0), + surfaceView.widthAnchor.constraint(equalToConstant: 291), + ] + } else { + return [ + surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8.0), + surfaceView.widthAnchor.constraint(equalToConstant: 291), + ] + } + } + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.0 + } +} + +class DetailPanelPhoneDelegate: NSObject, FloatingPanelControllerDelegate, UIGestureRecognizerDelegate { + unowned let owner: MainViewController + + init(owner: MainViewController) { + self.owner = owner + } +} + +class DetailPanelPhoneLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + ] + let initialState: FloatingPanelState = .full + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.0 + } +} + +// MARK: - iPad + +class SearchPanelPadDelegate: NSObject, FloatingPanelControllerDelegate, UIGestureRecognizerDelegate { + unowned let owner: MainViewController + + init(owner: MainViewController) { + self.owner = owner + } + + func floatingPanel(_ fpc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + if newCollection.horizontalSizeClass == .compact { + fpc.surfaceView.containerMargins = .zero + return FloatingPanelBottomLayout() + } + fpc.surfaceView.containerMargins = UIEdgeInsets(top: .leastNonzeroMagnitude, // For top left/right rounding corners + left: 16, + bottom: 0.0, + right: 0.0) + return SearchPanelPadLayout() + } + + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return false + } + + func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) { + if vc.state == .full { + owner.searchVC.searchBar.showsCancelButton = false + owner.searchVC.searchBar.resignFirstResponder() + } + } + + func floatingPanelWillEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer) { + if targetState.pointee != .full { + owner.searchVC.hideHeader(animated: true) + } + } +} + +class SearchPanelPadLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .top + let initialState: FloatingPanelState = .tip + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .tip: FloatingPanelLayoutAnchor(absoluteInset: 80.0, edge: .top, referenceGuide: .superview), + .half: FloatingPanelLayoutAnchor(absoluteInset: 200.0, edge: .top, referenceGuide: .superview), + .full: FloatingPanelLayoutAnchor(absoluteInset: 60.0, edge: .bottom, referenceGuide: .superview), + ] + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.0 + } + func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { + return [ + surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor), + surfaceView.widthAnchor.constraint(equalToConstant: 375), + ] + } +} + +class SearchPaneliPadBehavior: FloatingPanelBehavior { + var springDecelerationRate: CGFloat { + return UIScrollView.DecelerationRate.fast.rawValue - 0.003 + } + var springResponseTime: CGFloat { + return 0.3 + } + var momentumProjectionRate: CGFloat { + return UIScrollView.DecelerationRate.fast.rawValue + } + func shouldProjectMomentum(_ fpc: FloatingPanelController, to proposedTargetPosition: FloatingPanelState) -> Bool { + return true + } +} + +class DetailPanelPadDelegate: NSObject, FloatingPanelControllerDelegate, UIGestureRecognizerDelegate { + unowned let owner: MainViewController + + init(owner: MainViewController) { + self.owner = owner + } + + func floatingPanel(_ fpc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + if newCollection.horizontalSizeClass == .compact { + fpc.surfaceView.containerMargins = .zero + return FloatingPanelBottomLayout() + } + if let item = owner.detailVC.item, item.title.contains("Right") { + fpc.surfaceView.containerMargins = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: .leastNonzeroMagnitude) + return DetailPanelPadRightLayout() + } + fpc.surfaceView.containerMargins = UIEdgeInsets(top: 0.0, left: .leastNonzeroMagnitude, bottom: 0.0, right: 0.0) + return DetailPanelPadLeftLayout() + } + + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return false + } +} + +class DetailPanelPadLeftLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .left + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 375, edge: .left, referenceGuide: .superview) + ] + let initialState: FloatingPanelState = .full + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.0 + } +} + +class DetailPanelPadRightLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .right + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 375, edge: .right, referenceGuide: .superview) + ] + let initialState: FloatingPanelState = .full + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.0 + } +} + +// MARK: - MKMapViewDelegate + +extension MainViewController: MKMapViewDelegate { + func setUpMapView() { + let center = CLLocationCoordinate2D(latitude: 37.623198015869235, + longitude: -122.43066818432008) + let span = MKCoordinateSpan(latitudeDelta: 0.4425100023575723, + longitudeDelta: 0.28543697435880233) + let region = MKCoordinateRegion(center: center, span: span) + mapView.region = region + mapView.showsCompass = true + mapView.showsUserLocation = true + mapView.delegate = self + } + + func tearDownMapView() { + // Prevent a crash + mapView.delegate = nil + mapView = nil + } +} diff --git a/Examples/Maps/Maps/SearchViewController.swift b/Examples/Maps/Maps/SearchViewController.swift new file mode 100644 index 00000000..c0e26641 --- /dev/null +++ b/Examples/Maps/Maps/SearchViewController.swift @@ -0,0 +1,184 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit + +// MARK: - UITableViewDelegate + +extension MainViewController: UITableViewDelegate { + func setUpSearchView() { + searchVC.loadViewIfNeeded() + searchVC.tableView.delegate = self + searchVC.searchBar.placeholder = "Search for a place or address" + let isPad = (traitCollection.userInterfaceIdiom == .pad) + searchVC.items = [ + .init(mark: "mark", title: "Marked Location" + (isPad ? " (Left panel)" : ""), subtitle: "Golden Gate Bridge, San Francisco"), + .init(mark: "mark", title: "Marked Location" + (isPad ? " (Right panel)" : ""), subtitle: "San Francisco Museum of Modern Art"), + ] + searchVC.items.append(contentsOf: (0...98).map { + .init(mark: "like", title: "Favorites", subtitle: "\($0) Places") + }) + } + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + tableView.deselectRow(at: indexPath, animated: true) + + deactivate(searchBar: searchVC.searchBar) + + // Show a detail panel + switch indexPath.row { + case 0: + detailVC.item = searchVC.items[safe: 0] + + // Show detail vc in the left positioned panel + switch traitCollection.userInterfaceIdiom { + case .pad: + detailFpc.layout = DetailPanelPadLeftLayout() + detailFpc.surfaceView.containerMargins = UIEdgeInsets(top: 0.0, left: 16.0, bottom: 0.0, right: 0.0) + default: + detailFpc.layout = DetailPanelPhoneLayout() + detailFpc.surfaceView.containerMargins = .zero + } + detailFpc.addPanel(toParent: self, animated: true) + case 1: + detailVC.item = searchVC.items[safe: 1] + + // Show detail vc in the right positioned panel + switch traitCollection.userInterfaceIdiom { + case .pad: + detailFpc.layout = DetailPanelPadRightLayout() + detailFpc.surfaceView.containerMargins = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 16.0) + default: + detailFpc.layout = DetailPanelPhoneLayout() + detailFpc.surfaceView.containerMargins = .zero + } + detailFpc.addPanel(toParent: self, animated: true) + default: + break + } + } +} + +// MARK: - Models + +struct LocationItem { + let mark: String + let title: String + let subtitle: String + + init(mark: String, title: String, subtitle: String) { + self.mark = mark + self.title = title + self.subtitle = subtitle + } +} + +// MARK: - + +class SearchViewController: UIViewController, UITableViewDataSource { + @IBOutlet weak var tableView: UITableView! + @IBOutlet weak var searchBar: UISearchBar! + @IBOutlet weak var visualEffectView: UIVisualEffectView! + + var items: [LocationItem] = [] + + // For iOS 10 only + private lazy var shadowLayer: CAShapeLayer = CAShapeLayer() + + override func viewDidLoad() { + super.viewDidLoad() + tableView.dataSource = self + searchBar.setSearchText(fontSize: 15.0) + + hideHeader(animated: false) + } + + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + if #available(iOS 11, *) { + } else { + // Exmaple: Add rounding corners on iOS 10 + visualEffectView.layer.cornerRadius = 9.0 + visualEffectView.clipsToBounds = true + + // Exmaple: Add shadow manually on iOS 10 + view.layer.insertSublayer(shadowLayer, at: 0) + let rect = visualEffectView.frame + let path = UIBezierPath(roundedRect: rect, + byRoundingCorners: [.topLeft, .topRight], + cornerRadii: CGSize(width: 9.0, height: 9.0)) + shadowLayer.frame = visualEffectView.frame + shadowLayer.shadowPath = path.cgPath + shadowLayer.shadowColor = UIColor.black.cgColor + shadowLayer.shadowOffset = CGSize(width: 0.0, height: 1.0) + shadowLayer.shadowOpacity = 0.2 + shadowLayer.shadowRadius = 3.0 + } + } + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return items.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) + if let cell = cell as? SearchCell, let item = items[safe: indexPath.row] { + cell.iconImageView.image = UIImage(named: item.mark) + cell.titleLabel.text = item.title + cell.subTitleLabel.text = item.subtitle + } + return cell + } + + func showHeader(animated: Bool) { + changeHeader(height: 116.0, aniamted: animated) + } + + func hideHeader(animated: Bool) { + changeHeader(height: 0.0, aniamted: animated) + } + + private func changeHeader(height: CGFloat, aniamted: Bool) { + guard let headerView = tableView.tableHeaderView, headerView.bounds.height != height else { return } + if aniamted == false { + updateHeader(height: height) + return + } + tableView.beginUpdates() + UIView.animate(withDuration: 0.25) { + self.updateHeader(height: height) + } + tableView.endUpdates() + } + + private func updateHeader(height: CGFloat) { + guard let headerView = tableView.tableHeaderView else { return } + var frame = headerView.frame + frame.size.height = height + self.tableView.tableHeaderView?.frame = frame + } +} + +class SearchCell: UITableViewCell { + @IBOutlet weak var iconImageView: UIImageView! + @IBOutlet weak var titleLabel: UILabel! + @IBOutlet weak var subTitleLabel: UILabel! +} + +class SearchHeaderView: UIView { + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + self.clipsToBounds = true + } +} + +extension UISearchBar { + func setSearchText(fontSize: CGFloat) { + if #available(iOS 13, *) { + let font = searchTextField.font + searchTextField.font = font?.withSize(fontSize) + } else { + let textField = value(forKey: "_searchField") as! UITextField + textField.font = textField.font?.withSize(fontSize) + } + } +} diff --git a/Examples/Maps/Maps/Utils.swift b/Examples/Maps/Maps/Utils.swift new file mode 100644 index 00000000..ab8dbe6b --- /dev/null +++ b/Examples/Maps/Maps/Utils.swift @@ -0,0 +1,19 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit + +extension Collection { + subscript (safe index: Index) -> Element? { + return indices.contains(index) ? self[index] : nil + } +} + +extension UIViewController { + var isLandscape: Bool { + if #available(iOS 13.0, *) { + return view.window?.windowScene?.interfaceOrientation.isLandscape ?? false + } else { + return UIApplication.shared.statusBarOrientation.isLandscape + } + } +} diff --git a/Examples/Maps/Maps/ViewController.swift b/Examples/Maps/Maps/ViewController.swift deleted file mode 100644 index f6e1d67a..00000000 --- a/Examples/Maps/Maps/ViewController.swift +++ /dev/null @@ -1,207 +0,0 @@ -// -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit -import MapKit -import FloatingPanel - -class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate, FloatingPanelControllerDelegate { - var fpc: FloatingPanelController! - var searchVC: SearchPanelViewController! - - @IBOutlet weak var mapView: MKMapView! - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view, typically from a nib. - // Initialize FloatingPanelController - fpc = FloatingPanelController() - fpc.delegate = self - - // Initialize FloatingPanelController and add the view - fpc.surfaceView.backgroundColor = .clear - fpc.surfaceView.cornerRadius = 9.0 - fpc.surfaceView.shadowHidden = false - - searchVC = storyboard?.instantiateViewController(withIdentifier: "SearchPanel") as? SearchPanelViewController - - // Add a content view controller - fpc.show(searchVC, sender: self) - fpc.track(scrollView: searchVC.tableView) - - setupMapView() - } - - override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - // Add FloatingPanel to a view with animation. - fpc.addPanel(toParent: self, animated: true) - - // Must be here - searchVC.searchBar.delegate = self - } - - override func viewDidDisappear(_ animated: Bool) { - super.viewDidDisappear(animated) - teardownMapView() - } - - func setupMapView() { - let center = CLLocationCoordinate2D(latitude: 37.623198015869235, - longitude: -122.43066818432008) - let span = MKCoordinateSpan(latitudeDelta: 0.4425100023575723, - longitudeDelta: 0.28543697435880233) - let region = MKCoordinateRegion(center: center, span: span) - mapView.region = region - mapView.showsCompass = true - mapView.showsUserLocation = true - mapView.delegate = self - } - - func teardownMapView() { - // Prevent a crash - mapView.delegate = nil - mapView = nil - } - - // MARK: UISearchBarDelegate - - func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { - searchBar.resignFirstResponder() - searchBar.showsCancelButton = false - searchVC.hideHeader() - fpc.move(to: .half, animated: true) - } - - func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { - searchBar.showsCancelButton = true - searchVC.showHeader() - searchVC.tableView.alpha = 1.0 - fpc.move(to: .full, animated: true) - } - - // MARK: FloatingPanelControllerDelegate - - func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? { - switch traitCollection.verticalSizeClass { - case .compact: - fpc.surfaceView.borderWidth = 1.0 / traitCollection.displayScale - fpc.surfaceView.borderColor = UIColor.black.withAlphaComponent(0.2) - default: - fpc.surfaceView.borderWidth = 0.0 - fpc.surfaceView.borderColor = nil - } - return nil - } - - func floatingPanelDidMove(_ vc: FloatingPanelController) { - let y = vc.surfaceView.frame.origin.y - let tipY = vc.originYOfSurface(for: .tip) - if y > tipY - 44.0 { - let progress = max(0.0, min((tipY - y) / 44.0, 1.0)) - self.searchVC.tableView.alpha = progress - } - } - - func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) { - if vc.position == .full { - searchVC.searchBar.showsCancelButton = false - searchVC.searchBar.resignFirstResponder() - } - } - - func floatingPanelDidEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetPosition: FloatingPanelPosition) { - if targetPosition != .full { - searchVC.hideHeader() - } - - UIView.animate(withDuration: 0.25, - delay: 0.0, - options: .allowUserInteraction, - animations: { - if targetPosition == .tip { - self.searchVC.tableView.alpha = 0.0 - } else { - self.searchVC.tableView.alpha = 1.0 - } - }, completion: nil) - } -} - -class SearchPanelViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { - - @IBOutlet weak var tableView: UITableView! - @IBOutlet weak var searchBar: UISearchBar! - - override func viewDidLoad() { - super.viewDidLoad() - tableView.dataSource = self - tableView.delegate = self - searchBar.placeholder = "Search for a place or address" - let textField = searchBar.value(forKey: "_searchField") as! UITextField - textField.font = UIFont(name: textField.font!.fontName, size: 15.0) - - hideHeader() - } - - func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return 2 - } - - func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) - if let cell = cell as? SearchCell { - switch indexPath.row { - case 0: - cell.iconImageView.image = UIImage(named: "mark") - cell.titleLabel.text = "Marked Location" - cell.subTitleLabel.text = "Golden Gate Bridge, San Francisco" - case 1: - cell.iconImageView.image = UIImage(named: "like") - cell.titleLabel.text = "Favorites" - cell.subTitleLabel.text = "0 Places" - default: - break - } - } - return cell - } - - func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - tableView.deselectRow(at: indexPath, animated: false) - } - - func showHeader() { - changeHeader(height: 116.0) - } - - func hideHeader() { - changeHeader(height: 0.0) - } - - func changeHeader(height: CGFloat) { - tableView.beginUpdates() - if let headerView = tableView.tableHeaderView { - UIView.animate(withDuration: 0.25) { - var frame = headerView.frame - frame.size.height = height - self.tableView.tableHeaderView?.frame = frame - } - } - tableView.endUpdates() - } -} - -class SearchCell: UITableViewCell { - @IBOutlet weak var iconImageView: UIImageView! - @IBOutlet weak var titleLabel: UILabel! - @IBOutlet weak var subTitleLabel: UILabel! -} - -class SearchHeaderView: UIView { - required init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - self.clipsToBounds = true - } -} diff --git a/Examples/Samples/Samples.xcodeproj/project.pbxproj b/Examples/Samples/Samples.xcodeproj/project.pbxproj index 85b9628a..c4074630 100644 --- a/Examples/Samples/Samples.xcodeproj/project.pbxproj +++ b/Examples/Samples/Samples.xcodeproj/project.pbxproj @@ -7,17 +7,32 @@ objects = { /* Begin PBXBuildFile section */ + 5442E22425FC51AF00A26F43 /* ImageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E22325FC51AF00A26F43 /* ImageViewController.swift */; }; + 5442E22825FC51E200A26F43 /* MultiPanelController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E22725FC51E200A26F43 /* MultiPanelController.swift */; }; + 5442E22C25FC521F00A26F43 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E22B25FC521F00A26F43 /* SettingsViewController.swift */; }; + 5442E23025FC525200A26F43 /* TabBarViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E22F25FC525200A26F43 /* TabBarViewController.swift */; }; + 5442E23425FC528400A26F43 /* DetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E23325FC528400A26F43 /* DetailViewController.swift */; }; + 5442E23A25FC52CD00A26F43 /* ModalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E23925FC52CD00A26F43 /* ModalViewController.swift */; }; + 5442E24025FC533800A26F43 /* DebugTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E23F25FC533800A26F43 /* DebugTableViewController.swift */; }; + 5442E24425FC538200A26F43 /* InspectorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E24325FC538200A26F43 /* InspectorViewController.swift */; }; + 5442E24A25FC53C100A26F43 /* DebugTextViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E24925FC53C100A26F43 /* DebugTextViewController.swift */; }; + 5442E25225FC541700A26F43 /* NestedScrollViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5442E25125FC541700A26F43 /* NestedScrollViewController.swift */; }; + 54496C59263A7E5A0031E0C8 /* UseCaseController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54496C58263A7E5A0031E0C8 /* UseCaseController.swift */; }; 545DB9EE21511E6300CA77B8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DB9ED21511E6300CA77B8 /* AppDelegate.swift */; }; - 545DB9F021511E6300CA77B8 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DB9EF21511E6300CA77B8 /* ViewController.swift */; }; + 545DB9F021511E6300CA77B8 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DB9EF21511E6300CA77B8 /* MainViewController.swift */; }; 545DB9F321511E6300CA77B8 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 545DB9F121511E6300CA77B8 /* Main.storyboard */; }; 545DB9F521511E6400CA77B8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 545DB9F421511E6400CA77B8 /* Assets.xcassets */; }; 545DB9F821511E6400CA77B8 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 545DB9F621511E6400CA77B8 /* LaunchScreen.storyboard */; }; 545DBA0321511E6400CA77B8 /* SampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DBA0221511E6400CA77B8 /* SampleTests.swift */; }; 545DBA0E21511E6400CA77B8 /* SampleUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DBA0D21511E6400CA77B8 /* SampleUITests.swift */; }; - 54B51116216AFE5F0033A6F3 /* UIExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B51115216AFE5F0033A6F3 /* UIExtensions.swift */; }; - 54B5113C216C40670033A6F3 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54B5113B216C40670033A6F3 /* FloatingPanel.framework */; }; - 54B5113D216C40670033A6F3 /* FloatingPanel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 54B5113B216C40670033A6F3 /* FloatingPanel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - 54CDC5D8215BBE23007D205C /* UIComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CDC5D7215BBE23007D205C /* UIComponents.swift */; }; + 546341A125C6415100CA0596 /* UseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 546341A025C6415100CA0596 /* UseCase.swift */; }; + 546341AC25C6426500CA0596 /* CustomState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 546341AB25C6426500CA0596 /* CustomState.swift */; }; + 549D23CB233C7779008EF4D7 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 549D23CA233C7779008EF4D7 /* FloatingPanel.framework */; }; + 549D23CC233C7779008EF4D7 /* FloatingPanel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 549D23CA233C7779008EF4D7 /* FloatingPanel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 54B51116216AFE5F0033A6F3 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B51115216AFE5F0033A6F3 /* Extensions.swift */; }; + 54CDC5D8215BBE23007D205C /* SupplementaryViews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CDC5D7215BBE23007D205C /* SupplementaryViews.swift */; }; + 54EAD35B263A75EB006A36EA /* PanelLayouts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54EAD35A263A75EB006A36EA /* PanelLayouts.swift */; }; + 54EAD365263A765F006A36EA /* PagePanelController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54EAD364263A765F006A36EA /* PagePanelController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -38,13 +53,13 @@ /* End PBXContainerItemProxy section */ /* Begin PBXCopyFilesBuildPhase section */ - 54B5111C216C3B300033A6F3 /* Embed Frameworks */ = { + 549D23CD233C7779008EF4D7 /* Embed Frameworks */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; dstPath = ""; dstSubfolderSpec = 10; files = ( - 54B5113D216C40670033A6F3 /* FloatingPanel.framework in Embed Frameworks */, + 549D23CC233C7779008EF4D7 /* FloatingPanel.framework in Embed Frameworks */, ); name = "Embed Frameworks"; runOnlyForDeploymentPostprocessing = 0; @@ -52,9 +67,20 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 5442E22325FC51AF00A26F43 /* ImageViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageViewController.swift; sourceTree = ""; }; + 5442E22725FC51E200A26F43 /* MultiPanelController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiPanelController.swift; sourceTree = ""; }; + 5442E22B25FC521F00A26F43 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = ""; }; + 5442E22F25FC525200A26F43 /* TabBarViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarViewController.swift; sourceTree = ""; }; + 5442E23325FC528400A26F43 /* DetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailViewController.swift; sourceTree = ""; }; + 5442E23925FC52CD00A26F43 /* ModalViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalViewController.swift; sourceTree = ""; }; + 5442E23F25FC533800A26F43 /* DebugTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugTableViewController.swift; sourceTree = ""; }; + 5442E24325FC538200A26F43 /* InspectorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorViewController.swift; sourceTree = ""; }; + 5442E24925FC53C100A26F43 /* DebugTextViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugTextViewController.swift; sourceTree = ""; }; + 5442E25125FC541700A26F43 /* NestedScrollViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NestedScrollViewController.swift; sourceTree = ""; }; + 54496C58263A7E5A0031E0C8 /* UseCaseController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UseCaseController.swift; sourceTree = ""; }; 545DB9EA21511E6300CA77B8 /* Samples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Samples.app; sourceTree = BUILT_PRODUCTS_DIR; }; 545DB9ED21511E6300CA77B8 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 545DB9EF21511E6300CA77B8 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + 545DB9EF21511E6300CA77B8 /* MainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; 545DB9F221511E6300CA77B8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 545DB9F421511E6400CA77B8 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 545DB9F721511E6400CA77B8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; @@ -65,9 +91,13 @@ 545DBA0921511E6400CA77B8 /* SamplesUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SamplesUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 545DBA0D21511E6400CA77B8 /* SampleUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleUITests.swift; sourceTree = ""; }; 545DBA0F21511E6400CA77B8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 54B51115216AFE5F0033A6F3 /* UIExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIExtensions.swift; sourceTree = ""; }; - 54B5113B216C40670033A6F3 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 54CDC5D7215BBE23007D205C /* UIComponents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIComponents.swift; sourceTree = ""; }; + 546341A025C6415100CA0596 /* UseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UseCase.swift; sourceTree = ""; }; + 546341AB25C6426500CA0596 /* CustomState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomState.swift; sourceTree = ""; }; + 549D23CA233C7779008EF4D7 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 54B51115216AFE5F0033A6F3 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; + 54CDC5D7215BBE23007D205C /* SupplementaryViews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupplementaryViews.swift; sourceTree = ""; }; + 54EAD35A263A75EB006A36EA /* PanelLayouts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PanelLayouts.swift; sourceTree = ""; }; + 54EAD364263A765F006A36EA /* PagePanelController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagePanelController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -75,7 +105,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 54B5113C216C40670033A6F3 /* FloatingPanel.framework in Frameworks */, + 549D23CB233C7779008EF4D7 /* FloatingPanel.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -96,15 +126,31 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 5442E22225FC519700A26F43 /* ContentViewControllers */ = { + isa = PBXGroup; + children = ( + 5442E23F25FC533800A26F43 /* DebugTableViewController.swift */, + 5442E24925FC53C100A26F43 /* DebugTextViewController.swift */, + 5442E23325FC528400A26F43 /* DetailViewController.swift */, + 5442E24325FC538200A26F43 /* InspectorViewController.swift */, + 5442E22325FC51AF00A26F43 /* ImageViewController.swift */, + 5442E25125FC541700A26F43 /* NestedScrollViewController.swift */, + 5442E23925FC52CD00A26F43 /* ModalViewController.swift */, + 5442E22725FC51E200A26F43 /* MultiPanelController.swift */, + 5442E22B25FC521F00A26F43 /* SettingsViewController.swift */, + 5442E22F25FC525200A26F43 /* TabBarViewController.swift */, + ); + path = ContentViewControllers; + sourceTree = ""; + }; 545DB9E121511E6300CA77B8 = { isa = PBXGroup; children = ( - 54B5113B216C40670033A6F3 /* FloatingPanel.framework */, + 549D23CA233C7779008EF4D7 /* FloatingPanel.framework */, 545DB9EC21511E6300CA77B8 /* Sources */, 545DBA0121511E6400CA77B8 /* Tests */, 545DBA0C21511E6400CA77B8 /* UITests */, 545DB9EB21511E6300CA77B8 /* Products */, - 545DBA1B2151CC1000CA77B8 /* Frameworks */, ); sourceTree = ""; }; @@ -125,9 +171,13 @@ 545DB9F621511E6400CA77B8 /* LaunchScreen.storyboard */, 545DB9F121511E6300CA77B8 /* Main.storyboard */, 545DB9ED21511E6300CA77B8 /* AppDelegate.swift */, - 545DB9EF21511E6300CA77B8 /* ViewController.swift */, - 54B51115216AFE5F0033A6F3 /* UIExtensions.swift */, - 54CDC5D7215BBE23007D205C /* UIComponents.swift */, + 545DB9EF21511E6300CA77B8 /* MainViewController.swift */, + 546341AA25C6421000CA0596 /* UseCases */, + 5442E22225FC519700A26F43 /* ContentViewControllers */, + 54EAD35A263A75EB006A36EA /* PanelLayouts.swift */, + 546341AB25C6426500CA0596 /* CustomState.swift */, + 54CDC5D7215BBE23007D205C /* SupplementaryViews.swift */, + 54B51115216AFE5F0033A6F3 /* Extensions.swift */, 545DB9F921511E6400CA77B8 /* Info.plist */, ); path = Sources; @@ -151,11 +201,14 @@ path = UITests; sourceTree = ""; }; - 545DBA1B2151CC1000CA77B8 /* Frameworks */ = { + 546341AA25C6421000CA0596 /* UseCases */ = { isa = PBXGroup; children = ( + 546341A025C6415100CA0596 /* UseCase.swift */, + 54496C58263A7E5A0031E0C8 /* UseCaseController.swift */, + 54EAD364263A765F006A36EA /* PagePanelController.swift */, ); - name = Frameworks; + path = UseCases; sourceTree = ""; }; /* End PBXGroup section */ @@ -168,7 +221,7 @@ 545DB9E621511E6300CA77B8 /* Sources */, 545DB9E721511E6300CA77B8 /* Frameworks */, 545DB9E821511E6300CA77B8 /* Resources */, - 54B5111C216C3B300033A6F3 /* Embed Frameworks */, + 549D23CD233C7779008EF4D7 /* Embed Frameworks */, ); buildRules = ( ); @@ -222,7 +275,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1000; - LastUpgradeCheck = 1000; + LastUpgradeCheck = 1300; ORGANIZATIONNAME = scenee; TargetAttributes = { 545DB9E921511E6300CA77B8 = { @@ -290,10 +343,25 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 54CDC5D8215BBE23007D205C /* UIComponents.swift in Sources */, - 54B51116216AFE5F0033A6F3 /* UIExtensions.swift in Sources */, - 545DB9F021511E6300CA77B8 /* ViewController.swift in Sources */, + 5442E23425FC528400A26F43 /* DetailViewController.swift in Sources */, + 54496C59263A7E5A0031E0C8 /* UseCaseController.swift in Sources */, + 54CDC5D8215BBE23007D205C /* SupplementaryViews.swift in Sources */, + 54B51116216AFE5F0033A6F3 /* Extensions.swift in Sources */, + 5442E24A25FC53C100A26F43 /* DebugTextViewController.swift in Sources */, + 546341AC25C6426500CA0596 /* CustomState.swift in Sources */, + 5442E23A25FC52CD00A26F43 /* ModalViewController.swift in Sources */, + 5442E22425FC51AF00A26F43 /* ImageViewController.swift in Sources */, + 5442E24025FC533800A26F43 /* DebugTableViewController.swift in Sources */, + 5442E25225FC541700A26F43 /* NestedScrollViewController.swift in Sources */, + 5442E22825FC51E200A26F43 /* MultiPanelController.swift in Sources */, + 546341A125C6415100CA0596 /* UseCase.swift in Sources */, + 545DB9F021511E6300CA77B8 /* MainViewController.swift in Sources */, 545DB9EE21511E6300CA77B8 /* AppDelegate.swift in Sources */, + 5442E23025FC525200A26F43 /* TabBarViewController.swift in Sources */, + 54EAD35B263A75EB006A36EA /* PanelLayouts.swift in Sources */, + 54EAD365263A765F006A36EA /* PagePanelController.swift in Sources */, + 5442E24425FC538200A26F43 /* InspectorViewController.swift in Sources */, + 5442E22C25FC521F00A26F43 /* SettingsViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -374,6 +442,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -435,6 +504,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -476,9 +546,9 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanelSample; + PRODUCT_BUNDLE_IDENTIFIER = example.Samples; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -495,9 +565,9 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanelSample; + PRODUCT_BUNDLE_IDENTIFIER = example.Samples; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/Examples/Samples/Samples.xcodeproj/xcshareddata/xcschemes/Samples.xcscheme b/Examples/Samples/Samples.xcodeproj/xcshareddata/xcschemes/Samples.xcscheme index 4b8bf3a5..bd2a24bb 100644 --- a/Examples/Samples/Samples.xcodeproj/xcshareddata/xcschemes/Samples.xcscheme +++ b/Examples/Samples/Samples.xcodeproj/xcshareddata/xcschemes/Samples.xcscheme @@ -1,6 +1,6 @@ - - - - + + - - - - - - + + - + + + - - - + + + @@ -29,24 +29,24 @@ - + - + - + - + - + + - - - + + + - - + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + + + + + + + + + + + - + + - - - - + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -124,8 +590,19 @@ - + + + + + + + + + + + + + + + + + + + + - + + + - - + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + - + @@ -205,23 +762,43 @@ Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC + - + - + - + + - + + + + + + + + + + + + + + + + + + + diff --git a/Examples/Samples/Sources/ContentViewControllers/DebugTableViewController.swift b/Examples/Samples/Sources/ContentViewControllers/DebugTableViewController.swift new file mode 100644 index 00000000..7d1230bb --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/DebugTableViewController.swift @@ -0,0 +1,279 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +class DebugTableViewController: InspectableViewController { + // MARK: - Views + + lazy var tableView: UITableView = { + let tableView = UITableView(frame: .zero, style: .plain) + tableView.dataSource = self + tableView.delegate = self + tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") + return tableView + }() + lazy var buttonStackView: UIStackView = { + let stackView = UIStackView() + stackView.axis = .vertical + stackView.distribution = .fillEqually + stackView.alignment = .trailing + stackView.spacing = 10.0 + return stackView + }() + private lazy var reorderButton: UIButton = { + let button = UIButton() + button.setTitle(Menu.reorder.rawValue, for: .normal) + button.setTitleColor(view.tintColor, for: .normal) + button.addTarget(self, action: #selector(reorderItems), for: .touchUpInside) + return button + }() + private lazy var trackingSwitchWrapper: UIStackView = { + let stackView = UIStackView() + stackView.axis = .horizontal + stackView.distribution = .fillProportionally + stackView.alignment = .center + stackView.spacing = 8.0 + stackView.addArrangedSubview(trackingLabel) + stackView.addArrangedSubview(trackingSwitch) + return stackView + }() + private lazy var trackingLabel: UILabel = { + let label = UILabel() + label.text = "Tracking" + label.font = UIFont.systemFont(ofSize: 17.0, weight: .regular) + return label + }() + private lazy var trackingSwitch: UISwitch = { + let trackingSwitch = UISwitch() + trackingSwitch.isOn = true + trackingSwitch.addTarget(self, action: #selector(turnTrackingOn), for: .touchUpInside) + return trackingSwitch + }() + + // MARK: - Properties + + var kvoObservers: [NSKeyValueObservation] = [] + private lazy var items: [String] = { + let items = (0..<100).map { "Items \($0)" } + return Command.replace(items: items) + }() + private var itemHeight: CGFloat = 66.0 + + enum Menu: String, CaseIterable { + case turnOffTracking = "Tracking" + case reorder = "Reorder" + } + + enum Command: Int, CaseIterable { + case animateScroll + case changeContentSize + case moveToFull + case moveToHalf + var text: String { + switch self { + case .animateScroll: return "Scroll in the middle" + case .changeContentSize: return "Change content size" + case .moveToFull: return "Move to Full" + case.moveToHalf: return "Move to Half" + } + } + + static func replace(items: [String]) -> [String] { + return items.enumerated().map { (index, text) -> String in + if let action = Command(rawValue: index) { + return "\(index). \(action.text)" + } + return text + } + } + + func execute(for vc: DebugTableViewController, sourceView: UIView) { + switch self { + case .animateScroll: + vc.animateScroll() + case .changeContentSize: + vc.changeContentSize(sourceView: sourceView) + case .moveToFull: + vc.moveToFull() + case .moveToHalf: + vc.moveToHalf() + } + } + } + + // MARK: - Lifecycle + + override func viewDidLoad() { + super.viewDidLoad() + layoutTableView() + layoutMenuStackView() + setUpMenu() + } + + private func layoutTableView() { + view.addSubview(tableView) + tableView.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + tableView.topAnchor.constraint(equalTo: view.topAnchor), + tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), + tableView.leftAnchor.constraint(equalTo: view.leftAnchor), + tableView.rightAnchor.constraint(equalTo: view.rightAnchor) + ]) + } + + private func layoutMenuStackView() { + view.addSubview(buttonStackView) + buttonStackView.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + buttonStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 22.0), + buttonStackView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -22.0), + ]) + } + + private func setUpMenu() { + for menu in Menu.allCases { + switch menu { + case .reorder: + buttonStackView.addArrangedSubview(reorderButton) + case .turnOffTracking: + buttonStackView.addArrangedSubview(trackingSwitchWrapper) + } + } + } + + // MARK: - Menu + @objc + private func reorderItems() { + if reorderButton.titleLabel?.text == Menu.reorder.rawValue { + tableView.isEditing = true + reorderButton.setTitle("Cancel", for: .normal) + } else { + tableView.isEditing = false + reorderButton.setTitle(Menu.reorder.rawValue, for: .normal) + } + } + + @objc + private func turnTrackingOn(_ sender: UISwitch) { + guard let fpc = self.parent as? FloatingPanelController else { return } + if sender.isOn { + fpc.track(scrollView: tableView) + } else { + fpc.untrack(scrollView: tableView) + } + } + + // MARK: - Actions + + private func execute(command: Command, sourceView: UIView) { + command.execute(for: self, sourceView: sourceView) + } + + @objc + private func animateScroll() { + tableView.scrollToRow(at: IndexPath(row: lround(Double(items.count) / 2.0), + section: 0), + at: .top, animated: true) + } + + @objc + private func changeContentSize(sourceView: UIView) { + let actionSheet = UIAlertController(title: "Change content size", message: "", preferredStyle: .actionSheet) + actionSheet.addAction(UIAlertAction(title: "Large", style: .default, handler: { (_) in + self.itemHeight = 66.0 + self.changeItems(100) + })) + actionSheet.addAction(UIAlertAction(title: "Match", style: .default, handler: { (_) in + switch self.tableView.bounds.height { + case 585: // iPhone 6,7,8 + self.itemHeight = self.tableView.bounds.height / 13.0 + self.changeItems(13) + case 656: // iPhone {6,7,8} Plus + self.itemHeight = self.tableView.bounds.height / 16.0 + self.changeItems(16) + default: // iPhone X family + self.itemHeight = self.tableView.bounds.height / 12.0 + self.changeItems(12) + } + })) + actionSheet.addAction(UIAlertAction(title: "Short", style: .default, handler: { (_) in + self.itemHeight = 66.0 + self.changeItems(3) + })) + + if let popoverController = actionSheet.popoverPresentationController { + popoverController.sourceView = sourceView + popoverController.sourceRect = sourceView.bounds + } + + self.present(actionSheet, animated: true, completion: nil) + } + + private func changeItems(_ count: Int) { + items = Command.replace(items: (0.. Int { + return items.count + } + + func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { + return itemHeight + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) + cell.textLabel?.text = items[indexPath.row] + return cell + } +} + +extension DebugTableViewController: UITableViewDelegate { + func scrollViewDidScroll(_ scrollView: UIScrollView) { + print("TableView --- ", scrollView.contentOffset, scrollView.contentInset) + } + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + print("DebugTableViewController -- select row \(indexPath.row)") + guard let action = Command(rawValue: indexPath.row) else { return } + let cell = tableView.cellForRow(at: indexPath) + execute(command: action, sourceView: cell ?? tableView) + } + + func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { + return [ + UITableViewRowAction(style: .destructive, title: "Delete", handler: { (action, path) in + self.items.remove(at: path.row) + tableView.deleteRows(at: [path], with: .automatic) + }), + ] + } + + func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { + return true + } + + func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { + items.insert(items.remove(at: sourceIndexPath.row), at: destinationIndexPath.row) + } +} diff --git a/Examples/Samples/Sources/ContentViewControllers/DebugTextViewController.swift b/Examples/Samples/Sources/ContentViewControllers/DebugTextViewController.swift new file mode 100644 index 00000000..d89418e2 --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/DebugTextViewController.swift @@ -0,0 +1,44 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit + +final class DebugTextViewController: UIViewController, UITextViewDelegate { + @IBOutlet weak var textView: UITextView! + @IBOutlet weak var textViewTopConstraint: NSLayoutConstraint! + + override func viewDidLoad() { + super.viewDidLoad() + textView.delegate = self + print("viewDidLoad: TextView --- ", textView.contentOffset, textView.contentInset) + + if #available(iOS 11.0, *) { + textView.contentInsetAdjustmentBehavior = .never + } + } + + override func viewWillLayoutSubviews() { + print("viewWillLayoutSubviews: TextView --- ", textView.contentOffset, textView.contentInset, textView.frame) + } + + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + print("viewDidLayoutSubviews: TextView --- ", textView.contentOffset, textView.contentInset, textView.frame) + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + print("TextView --- ", textView.contentOffset, textView.contentInset, textView.frame) + } + + func scrollViewDidScroll(_ scrollView: UIScrollView) { + print("TextView --- ", scrollView.contentOffset, scrollView.contentInset) + if #available(iOS 11.0, *) { + print("TextView --- ", scrollView.adjustedContentInset) + } + } + + @IBAction func close(sender: UIButton) { + // (self.parent as? FloatingPanelController)?.removePanelFromParent(animated: true, completion: nil) + dismiss(animated: true, completion: nil) + } +} diff --git a/Examples/Samples/Sources/ContentViewControllers/DetailViewController.swift b/Examples/Samples/Sources/ContentViewControllers/DetailViewController.swift new file mode 100644 index 00000000..dceb3245 --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/DetailViewController.swift @@ -0,0 +1,39 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +final class DetailViewController: InspectableViewController { + @IBOutlet weak var modeChangeView: UIStackView! + @IBOutlet weak var intrinsicHeightConstraint: NSLayoutConstraint! + @IBOutlet weak var closeButton: UIButton! + @IBAction func close(sender: UIButton) { + // (self.parent as? FloatingPanelController)?.removePanelFromParent(animated: true, completion: nil) + dismiss(animated: true, completion: nil) + } + + @IBAction func buttonPressed(_ sender: UIButton) { + switch sender.titleLabel?.text { + case "Show": + performSegue(withIdentifier: "ShowSegue", sender: self) + case "Present Modally": + performSegue(withIdentifier: "PresentModallySegue", sender: self) + default: + break + } + } + @IBAction func modeChanged(_ sender: Any) { + guard let fpc = parent as? FloatingPanelController else { return } + fpc.contentMode = (fpc.contentMode == .static) ? .fitToBounds : .static + } + + @IBAction func tapped(_ sender: Any) { + print("Detail panel is tapped!") + } + @IBAction func swipped(_ sender: Any) { + print("Detail panel is swipped!") + } + @IBAction func longPressed(_ sender: Any) { + print("Detail panel is longPressed!") + } +} diff --git a/Examples/Samples/Sources/ContentViewControllers/ImageViewController.swift b/Examples/Samples/Sources/ContentViewControllers/ImageViewController.swift new file mode 100644 index 00000000..3aa26352 --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/ImageViewController.swift @@ -0,0 +1,69 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +final class ImageViewController: UIViewController { + class PanelLayout: FloatingPanelLayout { + weak var targetGuide: UILayoutGuide? + init(targetGuide: UILayoutGuide?) { + self.targetGuide = targetGuide + } + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .full + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + if #available(iOS 11.0, *), let targetGuide = targetGuide { + return [ + .full: FloatingPanelAdaptiveLayoutAnchor(absoluteOffset: 0, + contentLayout: targetGuide, + referenceGuide: .superview), + .half: FloatingPanelAdaptiveLayoutAnchor(fractionalOffset: 0.5, + contentLayout: targetGuide, + referenceGuide: .superview) + ] + } else { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 500, + edge: .bottom, + referenceGuide: .superview) + ] + } + } + } + + @IBOutlet weak var headerView: UIView! + @IBOutlet weak var footerView: UIView! + @IBOutlet weak var scrollView: UIScrollView! + @IBOutlet weak var stackView: UIStackView! + + enum Mode { + case onlyImage + case withHeaderFooter + } + + @available(iOS 11.0, *) + func layoutGuideFor(mode: Mode) -> UILayoutGuide { + switch mode { + case .onlyImage: + self.headerView.isHidden = true + self.footerView.isHidden = true + return scrollView.contentLayoutGuide + case .withHeaderFooter: + self.headerView.isHidden = false + self.footerView.isHidden = false + let guide = UILayoutGuide() + view.addLayoutGuide(guide) + + NSLayoutConstraint.activate([ + scrollView.heightAnchor.constraint(equalTo: scrollView.contentLayoutGuide.heightAnchor), + + guide.topAnchor.constraint(equalTo: stackView.topAnchor), + guide.leftAnchor.constraint(equalTo: stackView.leftAnchor), + guide.bottomAnchor.constraint(equalTo: stackView.bottomAnchor), + guide.rightAnchor.constraint(equalTo: stackView.rightAnchor), + ]) + return guide + } + } +} + diff --git a/Examples/Samples/Sources/ContentViewControllers/InspectorViewController.swift b/Examples/Samples/Sources/ContentViewControllers/InspectorViewController.swift new file mode 100644 index 00000000..9ad45569 --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/InspectorViewController.swift @@ -0,0 +1,48 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit + +class InspectableViewController: UIViewController { + override func viewWillLayoutSubviews() { + super.viewWillLayoutSubviews() + print(">>> Content View: viewWillLayoutSubviews", layoutInsets) + } + + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + print(">>> Content View: viewDidLayoutSubviews", layoutInsets) + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + print(">>> Content View: viewWillAppear", layoutInsets) + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + print(">>> Content View: viewDidAppear", view.bounds, layoutInsets) + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + print(">>> Content View: viewWillDisappear") + } + + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + print(">>> Content View: viewDidDisappear") + } + + override func willMove(toParent parent: UIViewController?) { + super.willMove(toParent: parent) + print(">>> Content View: willMove(toParent: \(String(describing: parent))") + } + + override func didMove(toParent parent: UIViewController?) { + super.didMove(toParent: parent) + print(">>> Content View: didMove(toParent: \(String(describing: parent))") + } + public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { + print(">>> Content View: willTransition(to: \(newCollection), with: \(coordinator))", layoutInsets) + } +} diff --git a/Examples/Samples/Sources/ContentViewControllers/ModalViewController.swift b/Examples/Samples/Sources/ContentViewControllers/ModalViewController.swift new file mode 100644 index 00000000..6a870fee --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/ModalViewController.swift @@ -0,0 +1,76 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +final class ModalViewController: UIViewController, FloatingPanelControllerDelegate { + var fpc: FloatingPanelController! + var consoleVC: DebugTextViewController! + + @IBOutlet weak var safeAreaView: UIView! + + var isNewlayout: Bool = false + + override func viewDidLoad() { + // Initialize FloatingPanelController + fpc = FloatingPanelController() + fpc.delegate = self + + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + fpc.surfaceView.appearance = appearance + + // Set a content view controller and track the scroll view + let consoleVC = storyboard?.instantiateViewController(withIdentifier: "ConsoleViewController") as! DebugTextViewController + fpc.set(contentViewController: consoleVC) + fpc.track(scrollView: consoleVC.textView) + + self.consoleVC = consoleVC + + // Add FloatingPanel to self.view + fpc.addPanel(toParent: self, at: view.subviews.firstIndex(of: safeAreaView) ?? -1) + } + + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + // Remove FloatingPanel from a view + fpc.removePanelFromParent(animated: false) + } + + @IBAction func close(sender: UIButton) { + dismiss(animated: true, completion: nil) + } + + @IBAction func moveToFull(sender: UIButton) { + fpc.move(to: .full, animated: true) + } + @IBAction func moveToHalf(sender: UIButton) { + fpc.move(to: .half, animated: true) + } + @IBAction func moveToTip(sender: UIButton) { + fpc.move(to: .tip, animated: true) + } + @IBAction func moveToHidden(sender: UIButton) { + fpc.move(to: .hidden, animated: true) + } + @IBAction func updateLayout(_ sender: Any) { + isNewlayout = !isNewlayout + UIView.animate(withDuration: 0.5) { + self.fpc.invalidateLayout() + } + } + + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + return (isNewlayout) ? ModalSecondLayout() : FloatingPanelBottomLayout() + } + + class ModalSecondLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .half + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(absoluteInset: 262, edge: .top, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 44.0, edge: .bottom, referenceGuide: .safeArea) + ] + } +} diff --git a/Examples/Samples/Sources/ContentViewControllers/MultiPanelController.swift b/Examples/Samples/Sources/ContentViewControllers/MultiPanelController.swift new file mode 100644 index 00000000..b579e43c --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/MultiPanelController.swift @@ -0,0 +1,65 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit +import FloatingPanel +import WebKit + +final class MultiPanelController: FloatingPanelController, FloatingPanelControllerDelegate { + + private final class FirstPanelContentViewController: UIViewController { + + lazy var webView: WKWebView = WKWebView() + + override func viewDidLoad() { + super.viewDidLoad() + view.addSubview(webView) + webView.frame = view.bounds + webView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + webView.load(URLRequest(url: URL(string: "https://www.apple.com")!)) + + let vc = MultiSecondPanelController() + vc.setUpContent() + vc.addPanel(toParent: self) + } + } + + private final class MultiSecondPanelController: FloatingPanelController { + + private final class SecondPanelContentViewController: DebugTableViewController {} + + func setUpContent() { + contentInsetAdjustmentBehavior = .never + let vc = SecondPanelContentViewController() + vc.loadViewIfNeeded() + vc.title = "Second Panel" + vc.buttonStackView.isHidden = true + let navigationController = UINavigationController(rootViewController: vc) + navigationController.navigationBar.barTintColor = .white + navigationController.navigationBar.titleTextAttributes = [ + .foregroundColor: UIColor.black + ] + set(contentViewController: navigationController) + self.track(scrollView: vc.tableView) + surfaceView.containerMargins = .init(top: 24.0, left: 0.0, bottom: layoutInsets.bottom, right: 0.0) + } + } + + override func viewDidLoad() { + super.viewDidLoad() + layout = FirstViewLayout() + isRemovalInteractionEnabled = true + + let vc = FirstPanelContentViewController() + set(contentViewController: vc) + track(scrollView: vc.webView.scrollView) + } + + private final class FirstViewLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .full + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 40.0, edge: .top, referenceGuide: .superview) + ] + } +} + diff --git a/Examples/Samples/Sources/ContentViewControllers/NestedScrollViewController.swift b/Examples/Samples/Sources/ContentViewControllers/NestedScrollViewController.swift new file mode 100644 index 00000000..d358d7b2 --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/NestedScrollViewController.swift @@ -0,0 +1,18 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit + +final class NestedScrollViewController: UIViewController { + @IBOutlet weak var scrollView: UIScrollView! + @IBOutlet weak var nestedScrollView: UIScrollView! + + @IBAction func longPressed(_ sender: Any) { + print("LongPressed!") + } + @IBAction func swipped(_ sender: Any) { + print("Swipped!") + } + @IBAction func tapped(_ sender: Any) { + print("Tapped!") + } +} diff --git a/Examples/Samples/Sources/ContentViewControllers/SettingsViewController.swift b/Examples/Samples/Sources/ContentViewControllers/SettingsViewController.swift new file mode 100644 index 00000000..d11d0c2a --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/SettingsViewController.swift @@ -0,0 +1,36 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +final class SettingsViewController: InspectableViewController { + @IBOutlet weak var largeTitlesSwicth: UISwitch! + @IBOutlet weak var translucentSwicth: UISwitch! + @IBOutlet weak var versionLabel: UILabel! + + override func viewDidLoad() { + versionLabel.text = "Version: \(Bundle.main.infoDictionary?["CFBundleVersion"] ?? "--")" + } + + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + if #available(iOS 11.0, *) { + let prefersLargeTitles = navigationController!.navigationBar.prefersLargeTitles + largeTitlesSwicth.setOn(prefersLargeTitles, animated: false) + } else { + largeTitlesSwicth.isEnabled = false + } + let isTranslucent = navigationController!.navigationBar.isTranslucent + translucentSwicth.setOn(isTranslucent, animated: false) + } + + @IBAction func toggleLargeTitle(_ sender: UISwitch) { + if #available(iOS 11.0, *) { + navigationController?.navigationBar.prefersLargeTitles = sender.isOn + } + } + @IBAction func toggleTranslucent(_ sender: UISwitch) { + navigationController?.navigationBar.isTranslucent = sender.isOn + } +} + diff --git a/Examples/Samples/Sources/ContentViewControllers/TabBarViewController.swift b/Examples/Samples/Sources/ContentViewControllers/TabBarViewController.swift new file mode 100644 index 00000000..6253e867 --- /dev/null +++ b/Examples/Samples/Sources/ContentViewControllers/TabBarViewController.swift @@ -0,0 +1,250 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +final class TabBarViewController: UITabBarController {} + +final class TabBarContentViewController: UIViewController { + enum Tab3Mode { + case changeOffset + case changeAutoLayout + var label: String { + switch self { + case .changeAutoLayout: return "Use AutoLayout(OK)" + case .changeOffset: return "Use ContentOffset(NG)" + } + } + } + lazy var fpc = FloatingPanelController() + var consoleVC: DebugTextViewController! + + var threeLayout: ThreeTabBarPanelLayout! + var tab3Mode: Tab3Mode = .changeAutoLayout + var switcherLabel: UILabel! + + override func viewDidLoad() { + fpc.delegate = self + + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + fpc.surfaceView.appearance = appearance + + // Set a content view controller and track the scroll view + let consoleVC = storyboard?.instantiateViewController(withIdentifier: "ConsoleViewController") as! DebugTextViewController + fpc.set(contentViewController: consoleVC) + consoleVC.textView.delegate = self // MUST call it before fpc.track(scrollView:) + fpc.track(scrollView: consoleVC.textView) + self.consoleVC = consoleVC + + // Add FloatingPanel to self.view + fpc.addPanel(toParent: self) + + + if #available(iOS 15, *) { + tabBarController?.tabBar.scrollEdgeAppearance = UITabBarAppearance() + } + + switch tabBarItem.tag { + case 1: + fpc.behavior = TwoTabBarPanelBehavior() + case 2: + let switcher = UISwitch() + fpc.view.addSubview(switcher) + switcher.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + switcher.bottomAnchor.constraint(equalTo: fpc.surfaceView.topAnchor, constant: -16.0), + switcher.rightAnchor.constraint(equalTo: fpc.surfaceView.rightAnchor, constant: -16.0), + ]) + switcher.isOn = true + switcher.tintColor = .white + switcher.backgroundColor = .white + switcher.layer.cornerRadius = 16.0 + switcher.addTarget(self, + action: #selector(changeTab3Mode(_:)), + for: .valueChanged) + let label = UILabel() + label.text = tab3Mode.label + fpc.view.addSubview(label) + switcherLabel = label + label.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + label.centerYAnchor.constraint(equalTo: switcher.centerYAnchor, constant: 0.0), + label.rightAnchor.constraint(equalTo: switcher.leftAnchor, constant: -16.0), + ]) + + // Turn off the mask instead of content inset change + consoleVC.textView.clipsToBounds = false + default: + break + } + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + fpc.invalidateLayout() + } + + // MARK: - Action + + @IBAction func close(sender: UIButton) { + dismiss(animated: true, completion: nil) + } + + // MARK: - Private + + @objc + private func changeTab3Mode(_ sender: UISwitch) { + if sender.isOn { + tab3Mode = .changeAutoLayout + } else { + tab3Mode = .changeOffset + } + switcherLabel.text = tab3Mode.label + } +} + +extension TabBarContentViewController: UITextViewDelegate { + func scrollViewDidScroll(_ scrollView: UIScrollView) { + guard self.tabBarItem.tag == 2 else { return } + // Reset an invalid content offset by a user after updating the layout + // of `consoleVC.textView`. + // NOTE: FloatingPanel doesn't implicitly reset the offset(i.e. + // Using KVO of `scrollView.contentOffset`). Because it can lead to an + // infinite loop if a user also resets a content offset as below and, + // in the situation, a user has to modify the library. + if fpc.state != .full, fpc.surfaceLocation.y > fpc.surfaceLocation(for: .full).y { + scrollView.contentOffset = .zero + } + } +} + +extension TabBarContentViewController: FloatingPanelControllerDelegate { + // MARK: - FloatingPanel + + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + switch self.tabBarItem.tag { + case 0: + return OneTabBarPanelLayout() + case 1: + return TwoTabBarPanelLayout() + case 2: + threeLayout = ThreeTabBarPanelLayout(parent: self) + return threeLayout + default: + return FloatingPanelBottomLayout() + } + } + + func floatingPanelDidMove(_ vc: FloatingPanelController) { + guard self.tabBarItem.tag == 2 else { return } + switch tab3Mode { + case .changeAutoLayout: + /* Good solution: Manipulate top constraint */ + assert(consoleVC.textViewTopConstraint != nil) + let safeAreaTop = vc.layoutInsets.top + if vc.surfaceLocation.y + threeLayout.topPadding < safeAreaTop { + consoleVC.textViewTopConstraint?.constant = min(safeAreaTop - vc.surfaceLocation.y, + safeAreaTop) + } else { + consoleVC.textViewTopConstraint?.constant = threeLayout.topPadding + } + case .changeOffset: + /* + Bad solution: Manipulate scroll content inset + + FloatingPanelController keeps a content offset in moving a panel + so that changing content inset or offset causes a buggy behavior. + */ + guard let scrollView = consoleVC.textView else { return } + var insets = vc.adjustedContentInsets + if vc.surfaceView.frame.minY < vc.layoutInsets.top { + insets.top = vc.layoutInsets.top - vc.surfaceView.frame.minY + } else { + insets.top = 0.0 + } + scrollView.contentInset = insets + + if vc.surfaceView.frame.minY > 0 { + scrollView.contentOffset = CGPoint(x: 0.0, + y: 0.0 - scrollView.contentInset.top) + } + } + + if vc.surfaceLocation.y > vc.surfaceLocation(for: .half).y { + let progress = (vc.surfaceLocation.y - vc.surfaceLocation(for: .half).y) + / (vc.surfaceLocation(for: .tip).y - vc.surfaceLocation(for: .half).y) + threeLayout.leftConstraint.constant = max(min(progress, 1.0), 0.0) * threeLayout.sideMargin + threeLayout.rightConstraint.constant = -max(min(progress, 1.0), 0.0) * threeLayout.sideMargin + } else { + threeLayout.leftConstraint.constant = 0.0 + threeLayout.rightConstraint.constant = 0.0 + } + } +} + +class OneTabBarPanelLayout: FloatingPanelLayout { + var initialState: FloatingPanelState { .tip } + var position: FloatingPanelPosition { .bottom } + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 22.0, edge: .bottom, referenceGuide: .safeArea) + ] + } +} + +class TwoTabBarPanelLayout: FloatingPanelLayout { + let initialState: FloatingPanelState = .half + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .top, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(absoluteInset: 261.0, edge: .bottom, referenceGuide: .safeArea) + ] +} + +class TwoTabBarPanelBehavior: FloatingPanelBehavior { + func allowsRubberBanding(for edges: UIRectEdge) -> Bool { + return [UIRectEdge.top, UIRectEdge.bottom].contains(edges) + } +} + + +class ThreeTabBarPanelLayout: FloatingPanelLayout { + weak var parentVC: UIViewController! + + var leftConstraint: NSLayoutConstraint! + var rightConstraint: NSLayoutConstraint! + + let topPadding: CGFloat = 17.0 + let sideMargin: CGFloat = 16.0 + + init(parent: UIViewController) { + parentVC = parent + } + + var initialState: FloatingPanelState { .half } + var position: FloatingPanelPosition { .bottom } + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .top, referenceGuide: .superview), + .half: FloatingPanelLayoutAnchor(absoluteInset: 261.0 + parentVC.layoutInsets.bottom, edge: .bottom, referenceGuide: .superview), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 88.0 + parentVC.layoutInsets.bottom, edge: .bottom, referenceGuide: .superview), + ] + } + + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.3 + } + + func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { + if #available(iOS 11.0, *) { + leftConstraint = surfaceView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0) + rightConstraint = surfaceView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0.0) + } else { + leftConstraint = surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0.0) + rightConstraint = surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0.0) + } + return [ leftConstraint, rightConstraint ] + } +} diff --git a/Examples/Samples/Sources/CustomState.swift b/Examples/Samples/Sources/CustomState.swift new file mode 100644 index 00000000..e20c27fb --- /dev/null +++ b/Examples/Samples/Sources/CustomState.swift @@ -0,0 +1,22 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import Foundation +import FloatingPanel + +extension FloatingPanelState { + static let lastQuart: FloatingPanelState = FloatingPanelState(rawValue: "lastQuart", order: 750) + static let firstQuart: FloatingPanelState = FloatingPanelState(rawValue: "firstQuart", order: 250) +} + +class FloatingPanelLayoutWithCustomState: FloatingPanelBottomLayout { + override var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .top, referenceGuide: .safeArea), + .lastQuart: FloatingPanelLayoutAnchor(fractionalInset: 0.75, edge: .bottom, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + .firstQuart: FloatingPanelLayoutAnchor(fractionalInset: 0.25, edge: .bottom, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 20.0, edge: .bottom, referenceGuide: .safeArea), + ] + } +} + diff --git a/Examples/Samples/Sources/Extensions.swift b/Examples/Samples/Sources/Extensions.swift new file mode 100644 index 00000000..17e15816 --- /dev/null +++ b/Examples/Samples/Sources/Extensions.swift @@ -0,0 +1,40 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit + +protocol LayoutGuideProvider { + var topAnchor: NSLayoutYAxisAnchor { get } + var bottomAnchor: NSLayoutYAxisAnchor { get } +} +extension UILayoutGuide: LayoutGuideProvider {} + +class CustomLayoutGuide: LayoutGuideProvider { + let topAnchor: NSLayoutYAxisAnchor + let bottomAnchor: NSLayoutYAxisAnchor + init(topAnchor: NSLayoutYAxisAnchor, bottomAnchor: NSLayoutYAxisAnchor) { + self.topAnchor = topAnchor + self.bottomAnchor = bottomAnchor + } +} + +extension UIViewController { + var layoutInsets: UIEdgeInsets { + if #available(iOS 11.0, *) { + return view.safeAreaInsets + } else { + return UIEdgeInsets(top: topLayoutGuide.length, + left: 0.0, + bottom: bottomLayoutGuide.length, + right: 0.0) + } + } + + var layoutGuide: LayoutGuideProvider { + if #available(iOS 11.0, *) { + return view!.safeAreaLayoutGuide + } else { + return CustomLayoutGuide(topAnchor: topLayoutGuide.bottomAnchor, + bottomAnchor: bottomLayoutGuide.topAnchor) + } + } +} diff --git a/Examples/Samples/Sources/MainViewController.swift b/Examples/Samples/Sources/MainViewController.swift new file mode 100644 index 00000000..e51de0cc --- /dev/null +++ b/Examples/Samples/Sources/MainViewController.swift @@ -0,0 +1,93 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +final class MainViewController: UIViewController { + @IBOutlet weak var tableView: UITableView! + private var observations: [NSKeyValueObservation] = [] + private lazy var useCaseController = UseCaseController(mainVC: self) +} + +extension MainViewController { + override func viewDidLoad() { + super.viewDidLoad() + tableView.dataSource = self + tableView.delegate = self + tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") + automaticallyAdjustsScrollViewInsets = false + + let searchController = UISearchController(searchResultsController: nil) + if #available(iOS 11.0, *) { + navigationItem.searchController = searchController + navigationItem.hidesSearchBarWhenScrolling = false + navigationItem.largeTitleDisplayMode = .automatic + } + var insets = UIEdgeInsets.zero + insets.bottom += 69.0 + tableView.contentInset = insets + + // Show the initial panel + useCaseController.set(useCase: .trackingTableView) + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + if #available(iOS 11.0, *) { + if let observation = navigationController?.navigationBar.observe(\.prefersLargeTitles, changeHandler: { (bar, _) in + self.tableView.reloadData() + }) { + observations.append(observation) + } + } + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + observations.removeAll() + } +} + +extension MainViewController { + @IBAction func showDebugMenu(_ sender: UIBarButtonItem) { + useCaseController.setUpSettingsPanel(for: self) + } +} + +extension MainViewController: UITableViewDataSource { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + if #available(iOS 11.0, *) { + if navigationController?.navigationBar.prefersLargeTitles == true { + return UseCase.allCases.count + 30 + } else { + return UseCase.allCases.count + } + } else { + return UseCase.allCases.count + } + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) + if UseCase.allCases.count > indexPath.row { + let menu = UseCase.allCases[indexPath.row] + cell.textLabel?.text = menu.name + } else { + cell.textLabel?.text = "\(indexPath.row) row" + } + return cell + } +} + +extension MainViewController: UITableViewDelegate { + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + guard UseCase.allCases.count > indexPath.row else { return } + + // Change panels + useCaseController.set(useCase: UseCase.allCases[indexPath.row]) + } + + @objc func dismissPresentedVC() { + self.presentedViewController?.dismiss(animated: true, completion: nil) + } +} diff --git a/Examples/Samples/Sources/PanelLayouts.swift b/Examples/Samples/Sources/PanelLayouts.swift new file mode 100644 index 00000000..8fae78bb --- /dev/null +++ b/Examples/Samples/Sources/PanelLayouts.swift @@ -0,0 +1,78 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +/** + - Attention: `FloatingPanelLayout` must not be applied by the parent view + controller of a panel. But here `MainViewController` adopts it + purposely to check if the library prints an appropriate warning. + */ +extension MainViewController: FloatingPanelLayout { + var position: FloatingPanelPosition { .bottom } + var initialState: FloatingPanelState { .half } + var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: UIScreen.main.bounds.height == 667.0 ? 18.0 : 16.0, edge: .top, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(absoluteInset: 262.0, edge: .bottom, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 69.0, edge: .bottom, referenceGuide: .safeArea) + ] + } +} + +class TopPositionedPanelLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .top + let initialState: FloatingPanelState = .full + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 88.0, edge: .bottom, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(absoluteInset: 216.0, edge: .top, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 44.0, edge: .top, referenceGuide: .safeArea) + ] +} + +class IntrinsicPanelLayout: FloatingPanelBottomLayout { + override var initialState: FloatingPanelState { .full } + override var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.0, referenceGuide: .safeArea) + ] + } +} + +class RemovablePanelLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .half + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.0, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(absoluteInset: 130.0, edge: .bottom, referenceGuide: .safeArea) + ] + + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.3 + } +} + +class RemovablePanelLandscapeLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .full + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.0, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(absoluteInset: 216.0, edge: .bottom, referenceGuide: .safeArea) + ] + + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.3 + } +} + +class ModalPanelLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .full + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 0.0, referenceGuide: .safeArea), + ] + + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.3 + } +} diff --git a/Examples/Samples/Sources/UIComponents.swift b/Examples/Samples/Sources/SupplementaryViews.swift similarity index 78% rename from Examples/Samples/Sources/UIComponents.swift rename to Examples/Samples/Sources/SupplementaryViews.swift index 836ddbdc..d7e930bf 100644 --- a/Examples/Samples/Sources/UIComponents.swift +++ b/Examples/Samples/Sources/SupplementaryViews.swift @@ -1,12 +1,12 @@ -// -// Created by Shin Yamamoto on 2018/09/19. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. import UIKit @IBDesignable -class CloseButton: UIButton { +final class CloseButton: UIButton { + override var isHighlighted: Bool { didSet { setNeedsDisplay() } } + override var isSelected: Bool { didSet { setNeedsDisplay() } } + required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) render() @@ -20,14 +20,11 @@ class CloseButton: UIButton { self.backgroundColor = .clear } - func p(_ p: CGFloat) -> CGFloat { - return p * (2.0 / 3.0) - } - - override var isHighlighted: Bool { didSet { setNeedsDisplay() } } - override var isSelected: Bool { didSet { setNeedsDisplay() } } - override func draw(_ rect: CGRect) { + func p(_ p: CGFloat) -> CGFloat { + return p * (2.0 / 3.0) + } + guard let context = UIGraphicsGetCurrentContext() else { return } context.setLineWidth(p(1.0)) @@ -74,7 +71,7 @@ class CloseButton: UIButton { } @IBDesignable -class SafeAreaView: UIView { +final class SafeAreaView: UIView { override func prepareForInterfaceBuilder() { let label = UILabel() label.text = "Safe Area" @@ -86,3 +83,18 @@ class SafeAreaView: UIView { ]) } } + + +@IBDesignable +final class OnSafeAreaView: UIView { + override func prepareForInterfaceBuilder() { + let label = UILabel() + label.text = "On Safe Area" + addSubview(label) + label.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + label.centerXAnchor.constraint(equalTo: self.centerXAnchor), + label.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -4.0), + ]) + } +} diff --git a/Examples/Samples/Sources/UIExtensions.swift b/Examples/Samples/Sources/UIExtensions.swift deleted file mode 100644 index 1985edf7..00000000 --- a/Examples/Samples/Sources/UIExtensions.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/10/08. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -extension UIView { - var layoutInsets: UIEdgeInsets { - if #available(iOS 11.0, *) { - return safeAreaInsets - } else { - return layoutMargins - } - } - - var layoutGuide: UILayoutGuide { - if #available(iOS 11.0, *) { - return safeAreaLayoutGuide - } else { - return layoutMarginsGuide - } - } -} - diff --git a/Examples/Samples/Sources/UseCases/PagePanelController.swift b/Examples/Samples/Sources/UseCases/PagePanelController.swift new file mode 100644 index 00000000..58c771a8 --- /dev/null +++ b/Examples/Samples/Sources/UseCases/PagePanelController.swift @@ -0,0 +1,85 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +final class PagePanelController: NSObject { + var pages: [UIViewController] = [] +} + +extension PagePanelController { + func makePageViewControllerForContent() -> UIPageViewController { + pages = [DebugTableViewController(), DebugTableViewController(), DebugTableViewController()] + let pageVC = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:]) + pageVC.dataSource = self + pageVC.delegate = self + pageVC.setViewControllers([pages[0]], direction: .forward, animated: false, completion: nil) + return pageVC + } + + func makePageViewController(for vc: MainViewController) -> UIPageViewController { + pages = [UIColor.blue, .red, .green].compactMap({ (color) -> UIViewController in + let page = FloatingPanelController(delegate: self) + page.view.backgroundColor = color + page.panGestureRecognizer.delegateProxy = self + page.show() + return page + }) + let pageVC = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:]) + let closeButton = UIButton(type: .custom) + pageVC.view.addSubview(closeButton) + closeButton.setTitle("Close", for: .normal) + closeButton.translatesAutoresizingMaskIntoConstraints = false + closeButton.addTarget(vc, action: #selector(MainViewController.dismissPresentedVC), for: .touchUpInside) + NSLayoutConstraint.activate([ + closeButton.topAnchor.constraint(equalTo: pageVC.layoutGuide.topAnchor, constant: 16.0), + closeButton.leftAnchor.constraint(equalTo: pageVC.view.leftAnchor, constant: 16.0), + ]) + pageVC.dataSource = self + pageVC.setViewControllers([pages[0]], direction: .forward, animated: false, completion: nil) + pageVC.modalPresentationStyle = .fullScreen + return pageVC + } +} + +extension PagePanelController: FloatingPanelControllerDelegate { + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + return FloatingPanelBottomLayout() + } +} + +extension PagePanelController: UIGestureRecognizerDelegate { + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return true + } + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return false + } +} + + +extension PagePanelController: UIPageViewControllerDataSource { + func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { + guard + let index = pages.firstIndex(of: viewController), + index + 1 < pages.count + else { return nil } + return pages[index + 1] + } + func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { + guard + let index = pages.firstIndex(of: viewController), + index - 1 >= 0 + else { return nil } + return pages[index - 1] + } +} + +extension PagePanelController: UIPageViewControllerDelegate { + // For showPageContent + func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { + if completed, let page = pageViewController.viewControllers?.first { + (pageViewController.parent as! FloatingPanelController).track(scrollView: (page as! DebugTableViewController).tableView) + } + } +} diff --git a/Examples/Samples/Sources/UseCases/UseCase.swift b/Examples/Samples/Sources/UseCases/UseCase.swift new file mode 100644 index 00000000..8d57bd88 --- /dev/null +++ b/Examples/Samples/Sources/UseCases/UseCase.swift @@ -0,0 +1,92 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +enum UseCase: Int, CaseIterable { + case trackingTableView + case trackingTextView + case showDetail + case showModal + case showPanelModal + case showMultiPanelModal + case showPanelInSheetModal + case showTabBar + case showPageView + case showPageContentView + case showNestedScrollView + case showRemovablePanel + case showIntrinsicView + case showContentInset + case showContainerMargins + case showNavigationController + case showTopPositionedPanel + case showAdaptivePanel + case showAdaptivePanelWithCustomGuide + case showCustomStatePanel +} + +extension UseCase { + var name: String { + switch self { + case .trackingTableView: return "Scroll tracking(TableView)" + case .trackingTextView: return "Scroll tracking(TextView)" + case .showDetail: return "Show Detail Panel" + case .showModal: return "Show Modal" + case .showPanelModal: return "Show Panel Modal" + case .showMultiPanelModal: return "Show Multi Panel Modal" + case .showPanelInSheetModal: return "Show Panel in Sheet Modal" + case .showTabBar: return "Show Tab Bar" + case .showPageView: return "Show Page View" + case .showPageContentView: return "Show Page Content View" + case .showNestedScrollView: return "Show Nested ScrollView" + case .showRemovablePanel: return "Show Removable Panel" + case .showIntrinsicView: return "Show Intrinsic View" + case .showContentInset: return "Show with ContentInset" + case .showContainerMargins: return "Show with ContainerMargins" + case .showNavigationController: return "Show Navigation Controller" + case .showTopPositionedPanel: return "Show Top Positioned Panel" + case .showAdaptivePanel: return "Show Adaptive Panel" + case .showAdaptivePanelWithCustomGuide: return "Show Adaptive Panel (Custom Layout Guide)" + case .showCustomStatePanel: return "Show Panel with Custom state" + } + } + + private enum Content { + case storyboard(String) + case viewController(UIViewController) + } + + private var content: Content { + switch self { + case .trackingTableView: return .viewController(DebugTableViewController()) + case .trackingTextView: return .storyboard("ConsoleViewController") // Storyboard only + case .showDetail: return .storyboard(String(describing: DetailViewController.self)) + case .showModal: return .storyboard(String(describing: ModalViewController.self)) + case .showMultiPanelModal: return .viewController(DebugTableViewController()) + case .showPanelInSheetModal: return .viewController(DebugTableViewController()) + case .showPanelModal: return .viewController(DebugTableViewController()) + case .showTabBar: return .storyboard(String(describing: TabBarViewController.self)) + case .showPageView: return .viewController(DebugTableViewController()) + case .showPageContentView: return .viewController(DebugTableViewController()) + case .showNestedScrollView: return .storyboard(String(describing: NestedScrollViewController.self)) + case .showRemovablePanel: return .storyboard(String(describing: DetailViewController.self)) + case .showIntrinsicView: return .storyboard("IntrinsicViewController") // Storyboard only + case .showContentInset: return .viewController(DebugTableViewController()) + case .showContainerMargins: return .viewController(DebugTableViewController()) + case .showNavigationController: return .storyboard("RootNavigationController") // Storyboard only + case .showTopPositionedPanel: return .viewController(DebugTableViewController()) + case .showAdaptivePanel: return .storyboard(String(describing: ImageViewController.self)) + case .showAdaptivePanelWithCustomGuide: return .storyboard(String(describing: ImageViewController.self)) + case .showCustomStatePanel: return .viewController(DebugTableViewController()) + } + } + + func makeContentViewController(with storyboard: UIStoryboard) -> UIViewController { + switch content { + case .storyboard(let id): + return storyboard.instantiateViewController(withIdentifier: id) + case .viewController(let vc): + return vc + } + } +} diff --git a/Examples/Samples/Sources/UseCases/UseCaseController.swift b/Examples/Samples/Sources/UseCases/UseCaseController.swift new file mode 100644 index 00000000..dac33d43 --- /dev/null +++ b/Examples/Samples/Sources/UseCases/UseCaseController.swift @@ -0,0 +1,384 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +final class UseCaseController: NSObject { + unowned let mainVC: MainViewController + private(set) var useCase: UseCase + + private var mainPanelVC: FloatingPanelController! + private var detailPanelVC: FloatingPanelController! + private var settingsPanelVC: FloatingPanelController! + private lazy var pagePanelController = PagePanelController() + + init(mainVC: MainViewController) { + self.mainVC = mainVC + self.useCase = .trackingTableView + } +} + +extension UseCaseController { + func set(useCase: UseCase) { + self.useCase = useCase + + let contentVC = useCase.makeContentViewController(with: mainVC.storyboard!) + + if let fpc = detailPanelVC { + fpc.removePanelFromParent(animated: true, completion: nil) + self.detailPanelVC = nil + } + + switch useCase { + case .trackingTableView: + let fpc = FloatingPanelController() + fpc.delegate = self + fpc.contentInsetAdjustmentBehavior = .always + fpc.surfaceView.appearance = { + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + return appearance + }() + + let tapGesture = UITapGestureRecognizer(target: self, action: #selector(UseCaseController.handleSurface(tapGesture:))) + tapGesture.cancelsTouchesInView = false + tapGesture.numberOfTapsRequired = 2 + // Prevents a delay to response a tap in menus of DebugTableViewController. + tapGesture.delaysTouchesEnded = false + fpc.surfaceView.addGestureRecognizer(tapGesture) + + fpc.set(contentViewController: contentVC) + fpc.ext_trackScrollView(in: contentVC) + addMain(panel: fpc) + + case .trackingTextView: + let fpc = FloatingPanelController() + fpc.delegate = self + fpc.contentInsetAdjustmentBehavior = .always + fpc.surfaceView.appearance = { + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + return appearance + }() + fpc.set(contentViewController: contentVC) + fpc.ext_trackScrollView(in: contentVC) + addMain(panel: fpc) + + case .showDetail: + // Initialize FloatingPanelController + let fpc = FloatingPanelController() + fpc.delegate = self + fpc.surfaceView.appearance = { + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + return appearance + }() + // Set a content view controller + fpc.set(contentViewController: contentVC) + fpc.contentMode = .fitToBounds + (contentVC as? DetailViewController)?.intrinsicHeightConstraint.isActive = false + + detailPanelVC = fpc + // Add FloatingPanel to self.view + fpc.addPanel(toParent: mainVC, animated: true) + + case .showModal, .showTabBar: + let modalVC = contentVC + modalVC.modalPresentationStyle = .fullScreen + mainVC.present(modalVC, animated: true, completion: nil) + + case .showPageView: + let pageVC = pagePanelController.makePageViewController(for: mainVC) + mainVC.present(pageVC, animated: true, completion: nil) + + case .showPageContentView: + let fpc = FloatingPanelController() + fpc.delegate = self + fpc.contentInsetAdjustmentBehavior = .always + fpc.surfaceView.appearance = { + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + return appearance + }() + let pageVC = pagePanelController.makePageViewControllerForContent() + if let page = (fpc.contentViewController as? UIPageViewController)?.viewControllers?.first { + fpc.track(scrollView: (page as! DebugTableViewController).tableView) + } + fpc.set(contentViewController: pageVC) + addMain(panel: fpc) + + case .showRemovablePanel, .showIntrinsicView: + let fpc = FloatingPanelController() + fpc.isRemovalInteractionEnabled = true + fpc.backdropView.dismissalTapGestureRecognizer.isEnabled = true + fpc.delegate = self + fpc.contentInsetAdjustmentBehavior = .always + fpc.surfaceView.appearance = { + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + return appearance + }() + fpc.set(contentViewController: contentVC) + fpc.ext_trackScrollView(in: contentVC) + addMain(panel: fpc) + + case .showNestedScrollView: + let fpc = FloatingPanelController() + fpc.panGestureRecognizer.delegateProxy = self + fpc.delegate = self + fpc.contentInsetAdjustmentBehavior = .always + fpc.surfaceView.appearance = { + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + return appearance + }() + fpc.set(contentViewController: contentVC) + fpc.ext_trackScrollView(in: contentVC) + addMain(panel: fpc) + + case .showPanelModal: + let fpc = FloatingPanelController() + let contentVC = mainVC.storyboard!.instantiateViewController(withIdentifier: "DetailViewController") + contentVC.loadViewIfNeeded() + (contentVC as? DetailViewController)?.modeChangeView.isHidden = true + fpc.set(contentViewController: contentVC) + fpc.delegate = self + + let appearance = SurfaceAppearance() + appearance.cornerRadius = 38.5 + fpc.surfaceView.appearance = appearance + fpc.backdropView.dismissalTapGestureRecognizer.isEnabled = true + + fpc.isRemovalInteractionEnabled = true + + mainVC.present(fpc, animated: true, completion: nil) + + case .showMultiPanelModal: + let fpc = MultiPanelController() + mainVC.present(fpc, animated: true, completion: nil) + + case .showPanelInSheetModal: + let fpc = FloatingPanelController() + let contentVC = UIViewController() + fpc.set(contentViewController: contentVC) + fpc.delegate = self + + let apprearance = SurfaceAppearance() + apprearance.cornerRadius = 38.5 + apprearance.shadows = [] + fpc.surfaceView.appearance = apprearance + fpc.isRemovalInteractionEnabled = true + + let mvc = UIViewController() + mvc.view.backgroundColor = UIColor(displayP3Red: 2/255, green: 184/255, blue: 117/255, alpha: 1.0) + fpc.addPanel(toParent: mvc) + mainVC.present(mvc, animated: true, completion: nil) + + case .showContentInset: + let contentViewController = UIViewController() + contentViewController.view.backgroundColor = .green + + let fpc = FloatingPanelController() + fpc.set(contentViewController: contentViewController) + fpc.surfaceView.contentPadding = .init(top: 20, left: 20, bottom: 20, right: 20) + + fpc.delegate = self + fpc.isRemovalInteractionEnabled = true + mainVC.present(fpc, animated: true, completion: nil) + + case .showContainerMargins: + let fpc = FloatingPanelController() + let appearance = SurfaceAppearance() + appearance.cornerRadius = 38.5 + fpc.surfaceView.appearance = appearance + + fpc.surfaceView.backgroundColor = .red + fpc.surfaceView.containerMargins = .init(top: 24.0, left: 8.0, bottom: max(mainVC.layoutInsets.bottom, 8.0), right: 8.0) + #if swift(>=5.1) // Actually Xcode 11 or later + if #available(iOS 13.0, *) { + fpc.surfaceView.layer.cornerCurve = .continuous + } + #endif + + fpc.delegate = self + fpc.isRemovalInteractionEnabled = true + mainVC.present(fpc, animated: true, completion: nil) + + case .showNavigationController: + let fpc = FloatingPanelController() + fpc.contentInsetAdjustmentBehavior = .never + fpc.set(contentViewController: contentVC) + fpc.ext_trackScrollView(in: contentVC) + addMain(panel: fpc) + + case .showTopPositionedPanel: // For debug + let fpc = FloatingPanelController(delegate: self) + let contentVC = UIViewController() + contentVC.view.backgroundColor = .red + fpc.set(contentViewController: contentVC) + addMain(panel: fpc) + + case .showAdaptivePanel, .showAdaptivePanelWithCustomGuide: + let fpc = FloatingPanelController() + fpc.isRemovalInteractionEnabled = true + fpc.set(contentViewController: contentVC) + fpc.ext_trackScrollView(in: contentVC) + if case let contentVC as ImageViewController = contentVC { + if #available(iOS 11.0, *) { + let mode: ImageViewController.Mode = (useCase == .showAdaptivePanelWithCustomGuide) ? .withHeaderFooter : .onlyImage + let layoutGuide = contentVC.layoutGuideFor(mode: mode) + fpc.layout = ImageViewController.PanelLayout(targetGuide: layoutGuide) + } else { + fpc.layout = ImageViewController.PanelLayout(targetGuide: nil) + } + } + addMain(panel: fpc) + + case .showCustomStatePanel: + let fpc = FloatingPanelController() + fpc.delegate = self + fpc.contentInsetAdjustmentBehavior = .always + fpc.surfaceView.appearance = { + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + return appearance + }() + fpc.set(contentViewController: contentVC) + fpc.ext_trackScrollView(in: contentVC) + addMain(panel: fpc) + } + } + + func setUpSettingsPanel(for mainVC: MainViewController) { + guard settingsPanelVC == nil else { return } + // Initialize FloatingPanelController + settingsPanelVC = FloatingPanelController() + + let appearance = SurfaceAppearance() + appearance.cornerRadius = 6.0 + settingsPanelVC.surfaceView.appearance = appearance + + settingsPanelVC.isRemovalInteractionEnabled = true + settingsPanelVC.backdropView.dismissalTapGestureRecognizer.isEnabled = true + settingsPanelVC.delegate = self + + let contentVC = mainVC.storyboard?.instantiateViewController(withIdentifier: "SettingsViewController") + + // Set a content view controller + settingsPanelVC.set(contentViewController: contentVC) + + // Add FloatingPanel to self.view + settingsPanelVC.addPanel(toParent: mainVC, animated: true) + } + + private func addMain(panel fpc: FloatingPanelController) { + let oldMainPanelVC = mainPanelVC + mainPanelVC = fpc + if let oldMainPanelVC = oldMainPanelVC { + oldMainPanelVC.removePanelFromParent(animated: true, completion: { + self.mainPanelVC.addPanel(toParent: self.mainVC, animated: true) + }) + } else { + mainPanelVC.addPanel(toParent: mainVC, animated: true) + } + } + + @objc + private func handleSurface(tapGesture: UITapGestureRecognizer) { + switch mainPanelVC.state { + case .full: + mainPanelVC.move(to: .half, animated: true) + default: + mainPanelVC.move(to: .full, animated: true) + } + } +} + +extension UseCaseController: FloatingPanelControllerDelegate { + func floatingPanel(_ vc: FloatingPanelController, contentOffsetForPinning trackingScrollView: UIScrollView) -> CGPoint { + if useCase == .showNavigationController, #available(iOS 11.0, *) { + // 148.0 is the SafeArea's top value for a navigation bar with a large title. + return CGPoint(x: 0.0, y: 0.0 - trackingScrollView.contentInset.top - 148.0) + } + return CGPoint(x: 0.0, y: 0.0 - trackingScrollView.contentInset.top) + } + + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + if vc == settingsPanelVC { + return IntrinsicPanelLayout() + } + + switch useCase { + case .showTopPositionedPanel: + return TopPositionedPanelLayout() + case .showRemovablePanel: + return newCollection.verticalSizeClass == .compact ? RemovablePanelLandscapeLayout() : RemovablePanelLayout() + case .showIntrinsicView: + return IntrinsicPanelLayout() + case .showPanelModal: + if vc != mainPanelVC && vc != detailPanelVC { + return ModalPanelLayout() + } + fallthrough + case .showContentInset: + return FloatingPanelBottomLayout() + case .showCustomStatePanel: + return FloatingPanelLayoutWithCustomState() + default: + return (newCollection.verticalSizeClass == .compact) ? FloatingPanelBottomLayout() : mainVC + } + } + + func floatingPanelDidRemove(_ vc: FloatingPanelController) { + switch vc { + case settingsPanelVC: + settingsPanelVC = nil + default: + break + } + } +} + +extension UseCaseController: UIGestureRecognizerDelegate { + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { + if case .showNestedScrollView = useCase { + return true + } else { + return false + } + } + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { + false + } +} + +private extension FloatingPanelController { + func ext_trackScrollView(in contentVC: UIViewController) { + switch contentVC { + case let consoleVC as DebugTextViewController: + track(scrollView: consoleVC.textView) + + case let contentVC as DebugTableViewController: + let ob = contentVC.tableView.observe(\.isEditing) { [weak self] (tableView, _) in + self?.panGestureRecognizer.isEnabled = !tableView.isEditing + } + contentVC.kvoObservers.append(ob) + track(scrollView: contentVC.tableView) + + case let contentVC as NestedScrollViewController: + track(scrollView: contentVC.scrollView) + + case let navVC as UINavigationController: + if let rootVC = (navVC.topViewController as? MainViewController) { + rootVC.loadViewIfNeeded() + track(scrollView: rootVC.tableView) + } + + case let contentVC as ImageViewController: + track(scrollView: contentVC.scrollView) + + default: + break + } + } +} diff --git a/Examples/Samples/Sources/ViewController.swift b/Examples/Samples/Sources/ViewController.swift deleted file mode 100644 index f91277ee..00000000 --- a/Examples/Samples/Sources/ViewController.swift +++ /dev/null @@ -1,275 +0,0 @@ -// -// ViewController.swift -// FloatingModalSample -// -// Created by Shin Yamamoto on 2018/09/18. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit -import FloatingPanel - -class SampleListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { - @IBOutlet weak var tableView: UITableView! - - enum Menu: Int, CaseIterable { - case trackingTableView - case trackingTextView - case showDetail - case showModal - - var name: String { - switch self { - case .trackingTableView: return "Scroll tracking (UITableView)" - case .trackingTextView: return "Scroll tracking (UITextView)" - case .showDetail: return "Show Detail Panel" - case .showModal: return "Show Modal" - } - } - - var storyboardID: String? { - switch self { - case .trackingTableView: return nil - case .trackingTextView: return "ConsoleViewController" - case .showDetail: return "DetailViewController" - case .showModal: return "ModalViewController" - } - } - } - - var mainPanelVC: FloatingPanelController! - var detailPanelVC: FloatingPanelController! - - override func viewDidLoad() { - super.viewDidLoad() - tableView.dataSource = self - tableView.delegate = self - tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") - - let contentVC = DebugTableViewController(style: .plain) - addMainPanel(with: contentVC) - } - - func addMainPanel(with contentVC: UIViewController) { - // Initialize FloatingPanelController - mainPanelVC = FloatingPanelController() - - // Initialize FloatingPanelController and add the view - mainPanelVC.surfaceView.cornerRadius = 6.0 - mainPanelVC.surfaceView.shadowHidden = false - - // Add a content view controller and connect with the scroll view - mainPanelVC.show(contentVC, sender: self) - - switch contentVC { - case let consoleVC as DebugTextViewController: - mainPanelVC.track(scrollView: consoleVC.textView) - - case let contentVC as DebugTableViewController: - mainPanelVC.track(scrollView: contentVC.tableView) - - default: - fatalError() - } - // Add FloatingPanel to self.view - mainPanelVC.addPanel(toParent: self, belowView: nil, animated: true) - } - - @objc func dismissDetailPanelVC() { - detailPanelVC.removePanelFromParent(animated: true, completion: nil) - } - - // MARK:- TableViewDatasource - - func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return Menu.allCases.count - } - - func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) - let menu = Menu.allCases[indexPath.row] - cell.textLabel?.text = menu.name - return cell - } - - // MARK:- TableViewDelegate - - func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - let menu = Menu.allCases[indexPath.row] - let contentVC: UIViewController = { - guard let storyboardID = menu.storyboardID else { return DebugTableViewController(style: .plain) } - guard let vc = self.storyboard?.instantiateViewController(withIdentifier: storyboardID) else { fatalError() } - return vc - }() - - switch menu { - case .showDetail: - detailPanelVC?.removeFromParent() - - // Initialize FloatingPanelController - detailPanelVC = FloatingPanelController() - - // Initialize FloatingPanelController and add the view - detailPanelVC.surfaceView.cornerRadius = 6.0 - detailPanelVC.surfaceView.shadowHidden = false - - // Add a content view controller and connect with the scroll view - detailPanelVC.show(contentVC, sender: self) - - // (contentVC as? DetailViewController)?.closeButton?.addTarget(self, action: #selector(dismissDetailPanelVC), for: .touchUpInside) - - // Add FloatingPanel to self.view - detailPanelVC.addPanel(toParent: self, belowView: nil, animated: true) - case .showModal: - let modalVC = contentVC - present(modalVC, animated: true, completion: nil) - default: - detailPanelVC?.removePanelFromParent(animated: true, completion: nil) - mainPanelVC?.removePanelFromParent(animated: true) { - self.addMainPanel(with: contentVC) - } - } - } -} - -class DebugTextViewController: UIViewController, UITextViewDelegate { - @IBOutlet weak var textView: UITextView! - - override func viewDidLoad() { - super.viewDidLoad() - textView.delegate = self - if #available(iOS 11.0, *) { - textView.contentInsetAdjustmentBehavior = .never - } - } - - func scrollViewDidScroll(_ scrollView: UIScrollView) { - print("TextView --- ", scrollView.contentOffset, scrollView.contentInset) - if #available(iOS 11.0, *) { - print("TextView --- ", scrollView.adjustedContentInset) - } - } - - @IBAction func close(sender: UIButton) { - // Now impossible - // dismiss(animated: true, completion: nil) - (self.parent as? FloatingPanelController)?.removePanelFromParent(animated: true, completion: nil) - } -} - -class DebugTableViewController: UITableViewController { - var items: [String] = [] - override func viewDidLoad() { - super.viewDidLoad() - for i in 0...100 { - items.append("Items \(i)") - } - tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") - } - - @objc func close(sender: UIButton) { - // Remove FloatingPanel from a view - (self.parent as! FloatingPanelController).removePanelFromParent(animated: true, completion: nil) - } - - override func viewWillLayoutSubviews() { - super.viewWillLayoutSubviews() - //print("Content View: viewWillLayoutSubviews") - } - - override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() - //print("Content View: viewDidLayoutSubviews") - } - - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - print("Content View: viewWillAppear") - } - - override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - print("Content View: viewDidAppear") - } - - override func viewWillDisappear(_ animated: Bool) { - super.viewWillDisappear(animated) - print("Content View: viewWillDisappear") - } - - override func viewDidDisappear(_ animated: Bool) { - super.viewDidDisappear(animated) - print("Content View: viewDidDisappear") - } - - override func willMove(toParent parent: UIViewController?) { - super.willMove(toParent: parent) - print("Content View: willMove(toParent: \(String(describing: parent))") - } - - override func didMove(toParent parent: UIViewController?) { - super.didMove(toParent: parent) - print("Content View: didMove(toParent: \(String(describing: parent))") - } - - public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { - print("Content View: willTransition(to: \(newCollection), with: \(coordinator))") - } - - override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return items.count - } - - override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { - return 66.0 - } - - override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) - cell.textLabel?.text = items[indexPath.row] - return cell - } -} - -class DetailViewController: UIViewController { - @IBOutlet weak var closeButton: UIButton! - @IBAction func close(sender: UIButton) { - // Now impossible - // dismiss(animated: true, completion: nil) - (self.parent as? FloatingPanelController)?.removePanelFromParent(animated: true, completion: nil) - } -} - -class ModalViewController: UIViewController { - var fpc: FloatingPanelController! - var consoleVC: DebugTextViewController! - @IBOutlet weak var safeAreaView: UIView! - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - // Initialize FloatingPanelController - fpc = FloatingPanelController() - - // Initialize FloatingPanelController and add the view - fpc.surfaceView.cornerRadius = 6.0 - fpc.surfaceView.shadowHidden = false - - // Add a content view controller and connect with the scroll view - let consoleVC = storyboard?.instantiateViewController(withIdentifier: "ConsoleViewController") as! DebugTextViewController - fpc.show(consoleVC, sender: self) - self.consoleVC = consoleVC - fpc.track(scrollView: consoleVC.textView) - - // Add FloatingPanel to self.view - fpc.addPanel(toParent: self, belowView: safeAreaView) - } - - override func viewWillDisappear(_ animated: Bool) { - super.viewWillDisappear(animated) - // Remove FloatingPanel from a view - fpc.removePanelFromParent(animated: false) - } - - @IBAction func close(sender: UIButton) { - dismiss(animated: true, completion: nil) - } -} diff --git a/Examples/Samples/Tests/SampleTests.swift b/Examples/Samples/Tests/SampleTests.swift index 84fd3083..437d53f0 100644 --- a/Examples/Samples/Tests/SampleTests.swift +++ b/Examples/Samples/Tests/SampleTests.swift @@ -1,10 +1,4 @@ -// -// FloatingModalSampleTests.swift -// FloatingModalSampleTests -// -// Created by Shin Yamamoto on 2018/09/18. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. import XCTest @testable import FloatingPanelSample diff --git a/Examples/Samples/UITests/SampleUITests.swift b/Examples/Samples/UITests/SampleUITests.swift index b02cb0f5..ba1a3512 100644 --- a/Examples/Samples/UITests/SampleUITests.swift +++ b/Examples/Samples/UITests/SampleUITests.swift @@ -1,7 +1,4 @@ -// -// Created by Shin Yamamoto on 2018/09/18. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. import XCTest diff --git a/Examples/SamplesObjC/SamplesObjC.xcodeproj/project.pbxproj b/Examples/SamplesObjC/SamplesObjC.xcodeproj/project.pbxproj new file mode 100644 index 00000000..48a4a407 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC.xcodeproj/project.pbxproj @@ -0,0 +1,377 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + 545BA70621BA3214007F7846 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 545BA70521BA3214007F7846 /* AppDelegate.m */; }; + 545BA70921BA3214007F7846 /* MainViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 545BA70821BA3214007F7846 /* MainViewController.m */; }; + 545BA70C21BA3214007F7846 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 545BA70A21BA3214007F7846 /* Main.storyboard */; }; + 545BA70E21BA3217007F7846 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 545BA70D21BA3217007F7846 /* Assets.xcassets */; }; + 545BA71121BA3217007F7846 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 545BA70F21BA3217007F7846 /* LaunchScreen.storyboard */; }; + 545BA71421BA3217007F7846 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 545BA71321BA3217007F7846 /* main.m */; }; + 545BA72621BA3BAF007F7846 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 545BA72521BA3BAF007F7846 /* FloatingPanel.framework */; }; + 545BA72721BA3BAF007F7846 /* FloatingPanel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 545BA72521BA3BAF007F7846 /* FloatingPanel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 545BA72821BA3BAF007F7846 /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + 545BA72721BA3BAF007F7846 /* FloatingPanel.framework in Embed Frameworks */, + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 545BA70121BA3214007F7846 /* SamplesObjC.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SamplesObjC.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 545BA70421BA3214007F7846 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; + 545BA70521BA3214007F7846 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; + 545BA70721BA3214007F7846 /* MainViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MainViewController.h; sourceTree = ""; }; + 545BA70821BA3214007F7846 /* MainViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MainViewController.m; sourceTree = ""; }; + 545BA70B21BA3214007F7846 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + 545BA70D21BA3217007F7846 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 545BA71021BA3217007F7846 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + 545BA71221BA3217007F7846 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 545BA71321BA3217007F7846 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + 545BA72221BA3867007F7846 /* SamplesObjC-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SamplesObjC-Bridging-Header.h"; sourceTree = ""; }; + 545BA72521BA3BAF007F7846 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 545BA6FE21BA3214007F7846 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 545BA72621BA3BAF007F7846 /* FloatingPanel.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 545BA6F821BA3214007F7846 = { + isa = PBXGroup; + children = ( + 545BA72521BA3BAF007F7846 /* FloatingPanel.framework */, + 545BA70321BA3214007F7846 /* SamplesObjC */, + 545BA70221BA3214007F7846 /* Products */, + ); + sourceTree = ""; + }; + 545BA70221BA3214007F7846 /* Products */ = { + isa = PBXGroup; + children = ( + 545BA70121BA3214007F7846 /* SamplesObjC.app */, + ); + name = Products; + sourceTree = ""; + }; + 545BA70321BA3214007F7846 /* SamplesObjC */ = { + isa = PBXGroup; + children = ( + 545BA70D21BA3217007F7846 /* Assets.xcassets */, + 545BA70F21BA3217007F7846 /* LaunchScreen.storyboard */, + 545BA70A21BA3214007F7846 /* Main.storyboard */, + 545BA70421BA3214007F7846 /* AppDelegate.h */, + 545BA70521BA3214007F7846 /* AppDelegate.m */, + 545BA70721BA3214007F7846 /* MainViewController.h */, + 545BA70821BA3214007F7846 /* MainViewController.m */, + 545BA71221BA3217007F7846 /* Info.plist */, + 545BA71321BA3217007F7846 /* main.m */, + 545BA72221BA3867007F7846 /* SamplesObjC-Bridging-Header.h */, + ); + path = SamplesObjC; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 545BA70021BA3214007F7846 /* SamplesObjC */ = { + isa = PBXNativeTarget; + buildConfigurationList = 545BA71721BA3217007F7846 /* Build configuration list for PBXNativeTarget "SamplesObjC" */; + buildPhases = ( + 545BA6FD21BA3214007F7846 /* Sources */, + 545BA6FE21BA3214007F7846 /* Frameworks */, + 545BA6FF21BA3214007F7846 /* Resources */, + 545BA72821BA3BAF007F7846 /* Embed Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = SamplesObjC; + productName = SamplesObjC; + productReference = 545BA70121BA3214007F7846 /* SamplesObjC.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 545BA6F921BA3214007F7846 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 1300; + ORGANIZATIONNAME = "Shin Yamamoto"; + TargetAttributes = { + 545BA70021BA3214007F7846 = { + CreatedOnToolsVersion = 10.1; + LastSwiftMigration = 1010; + }; + }; + }; + buildConfigurationList = 545BA6FC21BA3214007F7846 /* Build configuration list for PBXProject "SamplesObjC" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 545BA6F821BA3214007F7846; + productRefGroup = 545BA70221BA3214007F7846 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 545BA70021BA3214007F7846 /* SamplesObjC */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 545BA6FF21BA3214007F7846 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 545BA71121BA3217007F7846 /* LaunchScreen.storyboard in Resources */, + 545BA70E21BA3217007F7846 /* Assets.xcassets in Resources */, + 545BA70C21BA3214007F7846 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 545BA6FD21BA3214007F7846 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 545BA70921BA3214007F7846 /* MainViewController.m in Sources */, + 545BA71421BA3217007F7846 /* main.m in Sources */, + 545BA70621BA3214007F7846 /* AppDelegate.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 545BA70A21BA3214007F7846 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 545BA70B21BA3214007F7846 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + 545BA70F21BA3217007F7846 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 545BA71021BA3217007F7846 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 545BA71521BA3217007F7846 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.1; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + }; + name = Debug; + }; + 545BA71621BA3217007F7846 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.1; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 545BA71821BA3217007F7846 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEFINES_MODULE = NO; + DEVELOPMENT_TEAM = J3D7L9FHSS; + INFOPLIST_FILE = SamplesObjC/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = example.SamplesObjC; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "SamplesObjC/SamplesObjC-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.2; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 545BA71921BA3217007F7846 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEFINES_MODULE = NO; + DEVELOPMENT_TEAM = J3D7L9FHSS; + INFOPLIST_FILE = SamplesObjC/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = example.SamplesObjC; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "SamplesObjC/SamplesObjC-Bridging-Header.h"; + SWIFT_VERSION = 4.2; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 545BA6FC21BA3214007F7846 /* Build configuration list for PBXProject "SamplesObjC" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 545BA71521BA3217007F7846 /* Debug */, + 545BA71621BA3217007F7846 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 545BA71721BA3217007F7846 /* Build configuration list for PBXNativeTarget "SamplesObjC" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 545BA71821BA3217007F7846 /* Debug */, + 545BA71921BA3217007F7846 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 545BA6F921BA3214007F7846 /* Project object */; +} diff --git a/Framework/FloatingPanel.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Examples/SamplesObjC/SamplesObjC.xcodeproj/project.xcworkspace/contents.xcworkspacedata similarity index 64% rename from Framework/FloatingPanel.xcodeproj/project.xcworkspace/contents.xcworkspacedata rename to Examples/SamplesObjC/SamplesObjC.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 8ca79f0c..c35366ae 100644 --- a/Framework/FloatingPanel.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/Examples/SamplesObjC/SamplesObjC.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:SamplesObjC.xcodeproj"> diff --git a/Framework/FloatingPanel.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Examples/SamplesObjC/SamplesObjC.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist similarity index 100% rename from Framework/FloatingPanel.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist rename to Examples/SamplesObjC/SamplesObjC.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/Examples/SamplesObjC/SamplesObjC.xcodeproj/xcshareddata/xcschemes/SamplesObjC.xcscheme b/Examples/SamplesObjC/SamplesObjC.xcodeproj/xcshareddata/xcschemes/SamplesObjC.xcscheme new file mode 100644 index 00000000..1eb246ec --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC.xcodeproj/xcshareddata/xcschemes/SamplesObjC.xcscheme @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/SamplesObjC/SamplesObjC/AppDelegate.h b/Examples/SamplesObjC/SamplesObjC/AppDelegate.h new file mode 100644 index 00000000..f9c2b532 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/AppDelegate.h @@ -0,0 +1,9 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +#import + +@interface AppDelegate : UIResponder + +@property (strong, nonatomic) UIWindow* window; + +@end diff --git a/Examples/SamplesObjC/SamplesObjC/AppDelegate.m b/Examples/SamplesObjC/SamplesObjC/AppDelegate.m new file mode 100644 index 00000000..f9cb8157 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/AppDelegate.m @@ -0,0 +1,9 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +#import "AppDelegate.h" + +@interface AppDelegate () +@end + +@implementation AppDelegate +@end diff --git a/Examples/SamplesObjC/SamplesObjC/Assets.xcassets/AppIcon.appiconset/Contents.json b/Examples/SamplesObjC/SamplesObjC/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 00000000..d8db8d65 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,98 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Examples/SamplesObjC/SamplesObjC/Assets.xcassets/Contents.json b/Examples/SamplesObjC/SamplesObjC/Assets.xcassets/Contents.json new file mode 100644 index 00000000..73c00596 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/SamplesObjC/SamplesObjC/Base.lproj/LaunchScreen.storyboard b/Examples/SamplesObjC/SamplesObjC/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 00000000..bfa36129 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/SamplesObjC/SamplesObjC/Base.lproj/Main.storyboard b/Examples/SamplesObjC/SamplesObjC/Base.lproj/Main.storyboard new file mode 100644 index 00000000..d41effa0 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/Base.lproj/Main.storyboard @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/SamplesObjC/SamplesObjC/Info.plist b/Examples/SamplesObjC/SamplesObjC/Info.plist new file mode 100644 index 00000000..16be3b68 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/Info.plist @@ -0,0 +1,45 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/Examples/SamplesObjC/SamplesObjC/MainViewController.h b/Examples/SamplesObjC/SamplesObjC/MainViewController.h new file mode 100644 index 00000000..43c99dc3 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/MainViewController.h @@ -0,0 +1,13 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +#import +@import FloatingPanel; + +@interface MainViewController : UIViewController +@end + +@interface MyFloatingPanelLayout : NSObject +@end + +@interface MyFloatingPanelBehavior : NSObject +@end diff --git a/Examples/SamplesObjC/SamplesObjC/MainViewController.m b/Examples/SamplesObjC/SamplesObjC/MainViewController.m new file mode 100644 index 00000000..3fda5cc2 --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/MainViewController.m @@ -0,0 +1,96 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +#import "MainViewController.h" +@import FloatingPanel; + +// Defining a custom FloatingPanelState +@interface FloatingPanelState(Extended) ++ (FloatingPanelState *)LastQuart; +@end + +@implementation FloatingPanelState(Extended) +static FloatingPanelState *_lastQuart; ++ (FloatingPanelState *)LastQuart { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _lastQuart = [[FloatingPanelState alloc] initWithRawValue:@"lastquart" order:750]; + }); + return _lastQuart; +} +@end + +@interface MainViewController() +@end + +@implementation MainViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + + FloatingPanelController *fpc = [[FloatingPanelController alloc] init]; + [fpc setContentViewController:nil]; + [fpc setDelegate:self]; + + [fpc setLayout: [MyFloatingPanelLayout new]]; + [fpc setBehavior:[MyFloatingPanelBehavior new]]; + [fpc setRemovalInteractionEnabled:NO]; + + [fpc addPanelToParent:self at:self.view.subviews.count animated:NO completion:nil]; + [fpc moveToState:FloatingPanelState.Tip animated:true completion:nil]; + + [self updateAppearance: fpc]; +} + +- (id)floatingPanel:(FloatingPanelController *)vc layoutFor:(UITraitCollection *)newCollection { + FloatingPanelBottomLayout *layout = [FloatingPanelBottomLayout new]; + return layout; +} + +- (void)updateAppearance: (FloatingPanelController*)fpc +{ + FloatingPanelSurfaceAppearance *appearance = [[FloatingPanelSurfaceAppearance alloc] init]; + appearance.backgroundColor = [UIColor clearColor]; + appearance.cornerRadius = 23.0; + if (@available(iOS 13.0, *)) { + fpc.surfaceView.containerView.layer.cornerCurve = kCACornerCurveContinuous; + } + FloatingPanelSurfaceAppearanceShadow *shadow = [[FloatingPanelSurfaceAppearanceShadow alloc] init]; + shadow.color = [UIColor redColor]; + shadow.radius = 10.0; + shadow.spread = 10.0; + FloatingPanelSurfaceAppearanceShadow *shadow2 = [[FloatingPanelSurfaceAppearanceShadow alloc] init]; + shadow2.color = [UIColor blueColor]; + shadow2.radius = 10.0; + shadow2.spread = 10.0; + + appearance.shadows = @[shadow, shadow2]; + fpc.surfaceView.appearance = appearance; +} +@end + +@implementation MyFloatingPanelLayout +- (FloatingPanelState *)initialState { + return FloatingPanelState.Half; +} +- (NSDictionary> *)anchors { + return @{ + FloatingPanelState.LastQuart: [[FloatingPanelLayoutAnchor alloc] initWithFractionalInset:0.25 + edge:FloatingPanelReferenceEdgeTop + referenceGuide:FloatingPanelLayoutReferenceGuideSafeArea], + FloatingPanelState.Half: [[FloatingPanelLayoutAnchor alloc] initWithFractionalInset:0.5 + edge:FloatingPanelReferenceEdgeTop + referenceGuide:FloatingPanelLayoutReferenceGuideSafeArea], + FloatingPanelState.Tip: [[FloatingPanelLayoutAnchor alloc] initWithAbsoluteInset:44.0 + edge:FloatingPanelReferenceEdgeBottom + referenceGuide:FloatingPanelLayoutReferenceGuideSafeArea], + }; +} + +- (enum FloatingPanelPosition)position { + return FloatingPanelPositionBottom; +} +@end + + +@implementation MyFloatingPanelBehavior +@end diff --git a/Examples/SamplesObjC/SamplesObjC/SamplesObjC-Bridging-Header.h b/Examples/SamplesObjC/SamplesObjC/SamplesObjC-Bridging-Header.h new file mode 100644 index 00000000..e11d920b --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/SamplesObjC-Bridging-Header.h @@ -0,0 +1,3 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// diff --git a/Examples/SamplesObjC/SamplesObjC/main.m b/Examples/SamplesObjC/SamplesObjC/main.m new file mode 100644 index 00000000..a8dd51cb --- /dev/null +++ b/Examples/SamplesObjC/SamplesObjC/main.m @@ -0,0 +1,10 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +#import +#import "AppDelegate.h" + +int main(int argc, char * argv[]) { + @autoreleasepool { + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); + } +} diff --git a/Examples/Stocks/Stocks.xcodeproj/project.pbxproj b/Examples/Stocks/Stocks.xcodeproj/project.pbxproj index 6490673b..ac2c0fb1 100644 --- a/Examples/Stocks/Stocks.xcodeproj/project.pbxproj +++ b/Examples/Stocks/Stocks.xcodeproj/project.pbxproj @@ -7,13 +7,13 @@ objects = { /* Begin PBXBuildFile section */ - 5433F24B21717EA300BDAA5D /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5433F24A21717EA300BDAA5D /* FloatingPanel.framework */; }; - 5433F24C21717EA300BDAA5D /* FloatingPanel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 5433F24A21717EA300BDAA5D /* FloatingPanel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 548DF95421705BE00041922A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 548DF95321705BE00041922A /* AppDelegate.swift */; }; - 548DF95621705BE00041922A /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 548DF95521705BE00041922A /* ViewController.swift */; }; + 548DF95621705BE00041922A /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 548DF95521705BE00041922A /* MainViewController.swift */; }; 548DF95921705BE00041922A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 548DF95721705BE00041922A /* Main.storyboard */; }; 548DF95B21705BE10041922A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 548DF95A21705BE10041922A /* Assets.xcassets */; }; 548DF95E21705BE10041922A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 548DF95C21705BE10041922A /* LaunchScreen.storyboard */; }; + 549D23CF233C77CF008EF4D7 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 549D23CE233C77CF008EF4D7 /* FloatingPanel.framework */; }; + 549D23D0233C77CF008EF4D7 /* FloatingPanel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 549D23CE233C77CF008EF4D7 /* FloatingPanel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -23,7 +23,7 @@ dstPath = ""; dstSubfolderSpec = 10; files = ( - 5433F24C21717EA300BDAA5D /* FloatingPanel.framework in Embed Frameworks */, + 549D23D0233C77CF008EF4D7 /* FloatingPanel.framework in Embed Frameworks */, ); name = "Embed Frameworks"; runOnlyForDeploymentPostprocessing = 0; @@ -31,14 +31,14 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 5433F24A21717EA300BDAA5D /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 548DF95021705BE00041922A /* Stocks.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Stocks.app; sourceTree = BUILT_PRODUCTS_DIR; }; 548DF95321705BE00041922A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 548DF95521705BE00041922A /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + 548DF95521705BE00041922A /* MainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; 548DF95821705BE00041922A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 548DF95A21705BE10041922A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 548DF95D21705BE10041922A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 548DF95F21705BE10041922A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 549D23CE233C77CF008EF4D7 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -46,7 +46,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 5433F24B21717EA300BDAA5D /* FloatingPanel.framework in Frameworks */, + 549D23CF233C77CF008EF4D7 /* FloatingPanel.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -56,7 +56,7 @@ 548DF94721705BE00041922A = { isa = PBXGroup; children = ( - 5433F24A21717EA300BDAA5D /* FloatingPanel.framework */, + 549D23CE233C77CF008EF4D7 /* FloatingPanel.framework */, 548DF95221705BE00041922A /* Stocks */, 548DF95121705BE00041922A /* Products */, ); @@ -73,11 +73,11 @@ 548DF95221705BE00041922A /* Stocks */ = { isa = PBXGroup; children = ( - 548DF95321705BE00041922A /* AppDelegate.swift */, - 548DF95521705BE00041922A /* ViewController.swift */, - 548DF95721705BE00041922A /* Main.storyboard */, 548DF95A21705BE10041922A /* Assets.xcassets */, 548DF95C21705BE10041922A /* LaunchScreen.storyboard */, + 548DF95721705BE00041922A /* Main.storyboard */, + 548DF95321705BE00041922A /* AppDelegate.swift */, + 548DF95521705BE00041922A /* MainViewController.swift */, 548DF95F21705BE10041922A /* Info.plist */, ); path = Stocks; @@ -111,7 +111,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1000; - LastUpgradeCheck = 1000; + LastUpgradeCheck = 1300; ORGANIZATIONNAME = scenee; TargetAttributes = { 548DF94F21705BE00041922A = { @@ -155,7 +155,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 548DF95621705BE00041922A /* ViewController.swift in Sources */, + 548DF95621705BE00041922A /* MainViewController.swift in Sources */, 548DF95421705BE00041922A /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -208,6 +208,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -269,6 +270,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -310,9 +312,9 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.Stocks; + PRODUCT_BUNDLE_IDENTIFIER = example.Stocks; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -329,9 +331,9 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.Stocks; + PRODUCT_BUNDLE_IDENTIFIER = example.Stocks; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/Examples/Stocks/Stocks.xcodeproj/xcshareddata/xcschemes/Stocks.xcscheme b/Examples/Stocks/Stocks.xcodeproj/xcshareddata/xcschemes/Stocks.xcscheme index f6c79bcf..ef0d6c26 100644 --- a/Examples/Stocks/Stocks.xcodeproj/xcshareddata/xcschemes/Stocks.xcscheme +++ b/Examples/Stocks/Stocks.xcodeproj/xcshareddata/xcschemes/Stocks.xcscheme @@ -1,6 +1,6 @@ - - - - + + - - - - - - + - - + - + - + - + - + @@ -34,10 +30,10 @@ - + - + @@ -52,18 +48,17 @@ - - - + + + + + - @@ -121,6 +118,7 @@ + @@ -128,7 +126,6 @@ - diff --git a/Examples/Stocks/Stocks/MainViewController.swift b/Examples/Stocks/Stocks/MainViewController.swift new file mode 100644 index 00000000..1ee7c279 --- /dev/null +++ b/Examples/Stocks/Stocks/MainViewController.swift @@ -0,0 +1,121 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit +import FloatingPanel + +class MainViewController: UIViewController, FloatingPanelControllerDelegate { + @IBOutlet var topBannerView: UIImageView! + @IBOutlet weak var labelStackView: UIStackView! + @IBOutlet weak var bottomToolView: UIView! + + var fpc: FloatingPanelController! + var newsVC: NewsViewController! + + var initialColor: UIColor = .black + + override func viewDidLoad() { + super.viewDidLoad() + initialColor = view.backgroundColor! + // Initialize FloatingPanelController + fpc = FloatingPanelController() + fpc.delegate = self + fpc.behavior = FloatingPanelStocksBehavior() + + // Initialize FloatingPanelController and add the view + fpc.surfaceView.backgroundColor = UIColor(displayP3Red: 30.0/255.0, green: 30.0/255.0, blue: 30.0/255.0, alpha: 1.0) + fpc.surfaceView.appearance.cornerRadius = 24.0 + fpc.surfaceView.appearance.shadows = [] + fpc.surfaceView.appearance.borderWidth = 1.0 / traitCollection.displayScale + fpc.surfaceView.appearance.borderColor = UIColor.black.withAlphaComponent(0.2) + + newsVC = storyboard?.instantiateViewController(withIdentifier: "News") as? NewsViewController + + // Set a content view controller + fpc.set(contentViewController: newsVC) + fpc.track(scrollView: newsVC.scrollView) + + fpc.addPanel(toParent: self, at: view.subviews.firstIndex(of: bottomToolView) ?? -1 , animated: false) + + topBannerView.frame = .zero + topBannerView.alpha = 0.0 + view.addSubview(topBannerView) + topBannerView.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + topBannerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0), + topBannerView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0), + ]) + } + + override var preferredStatusBarStyle: UIStatusBarStyle { + return .lightContent + } + + // MARK: FloatingPanelControllerDelegate + + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + return FloatingPanelStocksLayout() + } + + func floatingPanelDidMove(_ vc: FloatingPanelController) { + if vc.isAttracting == false { + let loc = vc.surfaceLocation + let minY = vc.surfaceLocation(for: .full).y + let maxY = vc.surfaceLocation(for: .tip).y + vc.surfaceLocation = CGPoint(x: loc.x, y: min(max(loc.y, minY), maxY)) + } + + if vc.surfaceLocation.y <= vc.surfaceLocation(for: .full).y + 100 { + showStockTickerBanner() + } else { + hideStockTickerBanner() + } + } + + private func showStockTickerBanner() { + // Present top bar with dissolve animation + UIView.animate(withDuration: 0.25) { + self.topBannerView.alpha = 1.0 + self.labelStackView.alpha = 0.0 + self.view.backgroundColor = .black + } + } + + private func hideStockTickerBanner() { + // Dimiss top bar with dissolve animation + UIView.animate(withDuration: 0.25) { + self.topBannerView.alpha = 0.0 + self.labelStackView.alpha = 1.0 + self.view.backgroundColor = .black + } + } +} + +class NewsViewController: UIViewController { + @IBOutlet weak var scrollView: UIScrollView! +} + + +// MARK: - FloatingPanelLayout + +class FloatingPanelStocksLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .tip + + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 56.0, edge: .top, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(absoluteInset: 262.0, edge: .bottom, referenceGuide: .safeArea), + /* Visible + ToolView */ + .tip: FloatingPanelLayoutAnchor(absoluteInset: 85.0 + 44.0, edge: .bottom, referenceGuide: .safeArea), + ] + + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return 0.0 + } +} + +// MARK: - FloatingPanelBehavior + +class FloatingPanelStocksBehavior: FloatingPanelBehavior { + let springDecelerationRate: CGFloat = UIScrollView.DecelerationRate.fast.rawValue + let springResponseTime: CGFloat = 0.25 +} diff --git a/Examples/Stocks/Stocks/ViewController.swift b/Examples/Stocks/Stocks/ViewController.swift deleted file mode 100644 index 6a678091..00000000 --- a/Examples/Stocks/Stocks/ViewController.swift +++ /dev/null @@ -1,160 +0,0 @@ -// -// ViewController.swift -// Stocks -// -// Created by Shin Yamamoto on 2018/10/12. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit -import FloatingPanel - -class ViewController: UIViewController, FloatingPanelControllerDelegate { - @IBOutlet var topBannerView: UIImageView! - @IBOutlet weak var labelStackView: UIStackView! - @IBOutlet weak var bottomToolView: UIView! - - var fpc: FloatingPanelController! - var newsVC: NewsViewController! - - var initialColor: UIColor = .black - override func viewDidLoad() { - super.viewDidLoad() - initialColor = view.backgroundColor! - // Initialize FloatingPanelController - fpc = FloatingPanelController() - fpc.delegate = self - - // Initialize FloatingPanelController and add the view - fpc.surfaceView.backgroundColor = UIColor(displayP3Red: 30.0/255.0, green: 30.0/255.0, blue: 30.0/255.0, alpha: 1.0) - fpc.surfaceView.cornerRadius = 24.0 - fpc.surfaceView.shadowHidden = true - fpc.surfaceView.borderWidth = 1.0 / traitCollection.displayScale - fpc.surfaceView.borderColor = UIColor.black.withAlphaComponent(0.2) - - newsVC = storyboard?.instantiateViewController(withIdentifier: "News") as? NewsViewController - - // Add a content view controller - fpc.show(newsVC, sender: self) - fpc.track(scrollView: newsVC.scrollView) - - fpc.addPanel(toParent: self, belowView: bottomToolView, animated: false) - - topBannerView.frame = .zero - topBannerView.alpha = 0.0 - view.addSubview(topBannerView) - topBannerView.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate([ - topBannerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0), - topBannerView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0), - ]) - } - - override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - } - - override func viewDidDisappear(_ animated: Bool) { - super.viewDidDisappear(animated) - } - - override var preferredStatusBarStyle: UIStatusBarStyle { - return .lightContent - } - - // MARK: FloatingPanelControllerDelegate - - func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? { - return FloatingPanelStocksLayout() - } - - func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? { - return FloatingPanelStocksBehavior() - } - - func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) { - if vc.position == .full { - // Dimiss top bar with dissolve animation - UIView.animate(withDuration: 0.25) { - self.topBannerView.alpha = 0.0 - self.labelStackView.alpha = 1.0 - self.view.backgroundColor = self.initialColor - } - } - } - func floatingPanelDidEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetPosition: FloatingPanelPosition) { - if targetPosition == .full { - // Present top bar with dissolve animation - UIView.animate(withDuration: 0.25) { - self.topBannerView.alpha = 1.0 - self.labelStackView.alpha = 0.0 - self.view.backgroundColor = .black - } - } - } -} - -class NewsViewController: UIViewController { - @IBOutlet weak var scrollView: UIScrollView! -} - - -// MARK: My custom layout - -class FloatingPanelStocksLayout: FloatingPanelLayout { - public var supportedPositions: [FloatingPanelPosition] { - return [.full, .half, .tip] - } - - public var initialPosition: FloatingPanelPosition { - return .tip - } - - public func insetFor(position: FloatingPanelPosition) -> CGFloat? { - switch position { - case .full: return 56.0 - case .half: return 262.0 - case .tip: return 85.0 + 44.0 // Visible + ToolView - } - } - - public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { - return [ - surfaceView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0), - surfaceView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0.0), - ] - } - - var backdropAlpha: CGFloat = 0.0 -} - -// MARK: My custom behavior - -class FloatingPanelStocksBehavior: FloatingPanelBehavior { - var velocityThreshold: CGFloat { - return 15.0 - } - - func interactionAnimator(_ fpc: FloatingPanelController, to targetPosition: FloatingPanelPosition, with velocity: CGVector) -> UIViewPropertyAnimator { - let timing = timeingCurve(to: targetPosition, with: velocity) - return UIViewPropertyAnimator(duration: 0, timingParameters: timing) - } - - private func timeingCurve(to: FloatingPanelPosition, with velocity: CGVector) -> UITimingCurveProvider { - let damping = self.damping(with: velocity) - return UISpringTimingParameters(dampingRatio: damping, - frequencyResponse: 0.4, - initialVelocity: velocity) - } - - private func damping(with velocity: CGVector) -> CGFloat { - switch velocity.dy { - case ...(-velocityThreshold): - return 0.7 - case velocityThreshold...: - return 0.7 - default: - return 1.0 - } - } -} diff --git a/FloatingPanel.podspec b/FloatingPanel.podspec index 0087f8bf..bb7390f6 100644 --- a/FloatingPanel.podspec +++ b/FloatingPanel.podspec @@ -1,24 +1,22 @@ Pod::Spec.new do |s| s.name = "FloatingPanel" - s.version = "1.0.0" - s.summary = "FloatingPanel is a simple and easy-to-use UI component of a floating panel interface" + s.version = "2.5.5" + s.summary = "FloatingPanel is a clean and easy-to-use UI component of a floating panel interface." s.description = <<-DESC -FloatingPanel is a simple and easy-to-use UI component for a new interface introduced in Apple Maps, Shortcuts and Stocks app. +FloatingPanel is a clean and easy-to-use UI component for a new interface introduced in Apple Maps, Shortcuts and Stocks app. The new interface displays the related contents and utilities in parallel as a user wants. DESC s.homepage = "https://github.com/SCENEE/FloatingPanel" - # s.screenshots = "" + s.author = "Shin Yamamoto" + s.social_media_url = "https://twitter.com/scenee" s.platform = :ios, "10.0" - s.source = { :git => "https://github.com/SCENEE/FloatingPanel.git", :tag => "v#{s.version}" } - s.source_files = "Framework/Sources/*.swift" - s.swift_version = "4.2" - s.pod_target_xcconfig = { 'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES', 'APPLICATION_EXTENSION_API_ONLY' => 'YES' } + s.source = { :git => "https://github.com/SCENEE/FloatingPanel.git", :tag => s.version.to_s } + s.source_files = "Sources/*.swift" + s.swift_versions = ['5.1', '5.2', '5.3'] s.framework = "UIKit" - s.author = { "Shin Yamamoto" => "shin@scenee.com" } s.license = { :type => "MIT", :file => "LICENSE" } - s.social_media_url = "https://twitter.com/scenee" end diff --git a/FloatingPanel.xcodeproj/project.pbxproj b/FloatingPanel.xcodeproj/project.pbxproj new file mode 100644 index 00000000..2f88f64b --- /dev/null +++ b/FloatingPanel.xcodeproj/project.pbxproj @@ -0,0 +1,826 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + 542753C622C49A6E00D17955 /* LayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 542753C522C49A6E00D17955 /* LayoutTests.swift */; }; + 54352E9621A51A2500CBCA08 /* Transitioning.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54352E9521A51A2500CBCA08 /* Transitioning.swift */; }; + 54352E9821A521CA00CBCA08 /* PassthroughView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54352E9721A521CA00CBCA08 /* PassthroughView.swift */; }; + 5450EEE421646DF500135936 /* Behavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5450EEE321646DF500135936 /* Behavior.swift */; }; + 545DB9CB2151169500CA77B8 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 545DB9C12151169500CA77B8 /* FloatingPanel.framework */; }; + 545DB9D02151169500CA77B8 /* ControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DB9CF2151169500CA77B8 /* ControllerTests.swift */; }; + 545DB9D22151169500CA77B8 /* FloatingPanel.h in Headers */ = {isa = PBXBuildFile; fileRef = 545DB9C42151169500CA77B8 /* FloatingPanel.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 545DB9E021511AC100CA77B8 /* Controller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DB9DF21511AC100CA77B8 /* Controller.swift */; }; + 545DBA2B2152383100CA77B8 /* GrabberView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DBA2A2152383100CA77B8 /* GrabberView.swift */; }; + 546055BF2333C4740069F400 /* TestSupports.swift in Sources */ = {isa = PBXBuildFile; fileRef = 542753C722C49A8F00D17955 /* TestSupports.swift */; }; + 5469F4A224B003EF00537F8A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5469F49F24B003EF00537F8A /* LaunchScreen.storyboard */; }; + 5469F4A324B003EF00537F8A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5469F4A024B003EF00537F8A /* AppDelegate.swift */; }; + 5469F4AE24B30D7E00537F8A /* State.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5469F4AD24B30D7E00537F8A /* State.swift */; }; + 5469F4B024B30E1500537F8A /* LayoutAnchoring.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5469F4AF24B30E1500537F8A /* LayoutAnchoring.swift */; }; + 5469F4B224B30F1100537F8A /* Position.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5469F4B124B30F1100537F8A /* Position.swift */; }; + 5469F4B424B30F3500537F8A /* LayoutReferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5469F4B324B30F3500537F8A /* LayoutReferences.swift */; }; + 549C371F2361E15E007D8058 /* ExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 549C371E2361E15D007D8058 /* ExtensionTests.swift */; }; + 549E944522CF295D0050AECF /* StateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 549E944422CF295D0050AECF /* StateTests.swift */; }; + 54A6B6B122968B530077F348 /* CoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54A6B6B022968B530077F348 /* CoreTests.swift */; }; + 54A6B6B82296A8520077F348 /* SurfaceViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54A6B6B72296A8520077F348 /* SurfaceViewTests.swift */; }; + 54ABD7AF216CCFF7002E6C13 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54ABD7AE216CCFF7002E6C13 /* Logger.swift */; }; + 54CDC5D3215B6D5A007D205C /* SurfaceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CDC5D2215B6D5A007D205C /* SurfaceView.swift */; }; + 54CDC5D5215B6D8D007D205C /* BackdropView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CDC5D4215B6D8D007D205C /* BackdropView.swift */; }; + 54CFBFC3215CD045006B5735 /* Layout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CFBFC2215CD045006B5735 /* Layout.swift */; }; + 54CFBFC5215CD09C006B5735 /* Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CFBFC4215CD09C006B5735 /* Core.swift */; }; + 54DBA3DC262E938500D75969 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54DBA3DB262E938500D75969 /* Extensions.swift */; }; + 54E3992727141F5100A8F9ED /* FloatingPanel.docc in Sources */ = {isa = PBXBuildFile; fileRef = 54E3992627141F5100A8F9ED /* FloatingPanel.docc */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + 545DB9CC2151169500CA77B8 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 545DB9B82151169500CA77B8 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 545DB9C02151169500CA77B8; + remoteInfo = FloatingModalController; + }; + 54E740DC218AFE9F005C1A34 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 545DB9B82151169500CA77B8 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 54E740C9218AFD67005C1A34; + remoteInfo = TestingHost; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + 542753C522C49A6E00D17955 /* LayoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutTests.swift; sourceTree = ""; }; + 542753C722C49A8F00D17955 /* TestSupports.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSupports.swift; sourceTree = ""; }; + 54352E9521A51A2500CBCA08 /* Transitioning.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transitioning.swift; sourceTree = ""; }; + 54352E9721A521CA00CBCA08 /* PassthroughView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PassthroughView.swift; sourceTree = ""; }; + 5450EEE321646DF500135936 /* Behavior.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Behavior.swift; sourceTree = ""; }; + 545DB9C12151169500CA77B8 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 545DB9C42151169500CA77B8 /* FloatingPanel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FloatingPanel.h; sourceTree = ""; }; + 545DB9C52151169500CA77B8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 545DB9CA2151169500CA77B8 /* FloatingPanelTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FloatingPanelTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 545DB9CF2151169500CA77B8 /* ControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ControllerTests.swift; sourceTree = ""; }; + 545DB9D12151169500CA77B8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 545DB9DF21511AC100CA77B8 /* Controller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Controller.swift; sourceTree = ""; }; + 545DBA2A2152383100CA77B8 /* GrabberView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GrabberView.swift; sourceTree = ""; }; + 5469F49F24B003EF00537F8A /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = ""; }; + 5469F4A024B003EF00537F8A /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 5469F4A124B003EF00537F8A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 5469F4AD24B30D7E00537F8A /* State.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = ""; }; + 5469F4AF24B30E1500537F8A /* LayoutAnchoring.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutAnchoring.swift; sourceTree = ""; }; + 5469F4B124B30F1100537F8A /* Position.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Position.swift; sourceTree = ""; }; + 5469F4B324B30F3500537F8A /* LayoutReferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutReferences.swift; sourceTree = ""; }; + 549C371E2361E15D007D8058 /* ExtensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionTests.swift; sourceTree = ""; }; + 549E944422CF295D0050AECF /* StateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateTests.swift; sourceTree = ""; }; + 54A6B6B022968B530077F348 /* CoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreTests.swift; sourceTree = ""; }; + 54A6B6B72296A8520077F348 /* SurfaceViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SurfaceViewTests.swift; sourceTree = ""; }; + 54ABD7AE216CCFF7002E6C13 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; + 54CDC5D2215B6D5A007D205C /* SurfaceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SurfaceView.swift; sourceTree = ""; }; + 54CDC5D4215B6D8D007D205C /* BackdropView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackdropView.swift; sourceTree = ""; }; + 54CFBFC2215CD045006B5735 /* Layout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Layout.swift; sourceTree = ""; }; + 54CFBFC4215CD09C006B5735 /* Core.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Core.swift; sourceTree = ""; }; + 54DBA3DB262E938500D75969 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; + 54E3992627141F5100A8F9ED /* FloatingPanel.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = FloatingPanel.docc; sourceTree = ""; }; + 54E740CA218AFD67005C1A34 /* FloatingPanelTesting.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FloatingPanelTesting.app; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 545DB9BE2151169500CA77B8 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 545DB9C72151169500CA77B8 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 545DB9CB2151169500CA77B8 /* FloatingPanel.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 54E740C7218AFD67005C1A34 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 545DB9B72151169500CA77B8 = { + isa = PBXGroup; + children = ( + 545DB9C32151169500CA77B8 /* Sources */, + 545DB9CE2151169500CA77B8 /* Tests */, + 545DB9C22151169500CA77B8 /* Products */, + ); + sourceTree = ""; + }; + 545DB9C22151169500CA77B8 /* Products */ = { + isa = PBXGroup; + children = ( + 545DB9C12151169500CA77B8 /* FloatingPanel.framework */, + 545DB9CA2151169500CA77B8 /* FloatingPanelTests.xctest */, + 54E740CA218AFD67005C1A34 /* FloatingPanelTesting.app */, + ); + name = Products; + sourceTree = ""; + }; + 545DB9C32151169500CA77B8 /* Sources */ = { + isa = PBXGroup; + children = ( + 545DB9DF21511AC100CA77B8 /* Controller.swift */, + 5469F4AD24B30D7E00537F8A /* State.swift */, + 5469F4B124B30F1100537F8A /* Position.swift */, + 54CFBFC4215CD09C006B5735 /* Core.swift */, + 54CFBFC2215CD045006B5735 /* Layout.swift */, + 5469F4B324B30F3500537F8A /* LayoutReferences.swift */, + 5469F4AF24B30E1500537F8A /* LayoutAnchoring.swift */, + 5450EEE321646DF500135936 /* Behavior.swift */, + 54352E9721A521CA00CBCA08 /* PassthroughView.swift */, + 54CDC5D2215B6D5A007D205C /* SurfaceView.swift */, + 54CDC5D4215B6D8D007D205C /* BackdropView.swift */, + 545DBA2A2152383100CA77B8 /* GrabberView.swift */, + 54352E9521A51A2500CBCA08 /* Transitioning.swift */, + 54DBA3DB262E938500D75969 /* Extensions.swift */, + 54ABD7AE216CCFF7002E6C13 /* Logger.swift */, + 545DB9C42151169500CA77B8 /* FloatingPanel.h */, + 545DB9C52151169500CA77B8 /* Info.plist */, + 54E3992627141F5100A8F9ED /* FloatingPanel.docc */, + ); + path = Sources; + sourceTree = ""; + }; + 545DB9CE2151169500CA77B8 /* Tests */ = { + isa = PBXGroup; + children = ( + 5469F49E24B003EF00537F8A /* TestingApp */, + 54A6B6B022968B530077F348 /* CoreTests.swift */, + 545DB9CF2151169500CA77B8 /* ControllerTests.swift */, + 542753C522C49A6E00D17955 /* LayoutTests.swift */, + 54A6B6B72296A8520077F348 /* SurfaceViewTests.swift */, + 549E944422CF295D0050AECF /* StateTests.swift */, + 549C371E2361E15D007D8058 /* ExtensionTests.swift */, + 542753C722C49A8F00D17955 /* TestSupports.swift */, + 545DB9D12151169500CA77B8 /* Info.plist */, + ); + path = Tests; + sourceTree = ""; + }; + 5469F49E24B003EF00537F8A /* TestingApp */ = { + isa = PBXGroup; + children = ( + 5469F49F24B003EF00537F8A /* LaunchScreen.storyboard */, + 5469F4A024B003EF00537F8A /* AppDelegate.swift */, + 5469F4A124B003EF00537F8A /* Info.plist */, + ); + path = TestingApp; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 545DB9BC2151169500CA77B8 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 545DB9D22151169500CA77B8 /* FloatingPanel.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 545DB9C02151169500CA77B8 /* FloatingPanel */ = { + isa = PBXNativeTarget; + buildConfigurationList = 545DB9D52151169500CA77B8 /* Build configuration list for PBXNativeTarget "FloatingPanel" */; + buildPhases = ( + 545DB9BC2151169500CA77B8 /* Headers */, + 545DB9BD2151169500CA77B8 /* Sources */, + 545DB9BE2151169500CA77B8 /* Frameworks */, + 545DB9BF2151169500CA77B8 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = FloatingPanel; + productName = FloatingModalController; + productReference = 545DB9C12151169500CA77B8 /* FloatingPanel.framework */; + productType = "com.apple.product-type.framework"; + }; + 545DB9C92151169500CA77B8 /* FloatingPanelTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = 545DB9D82151169500CA77B8 /* Build configuration list for PBXNativeTarget "FloatingPanelTests" */; + buildPhases = ( + 545DB9C62151169500CA77B8 /* Sources */, + 545DB9C72151169500CA77B8 /* Frameworks */, + 545DB9C82151169500CA77B8 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + 545DB9CD2151169500CA77B8 /* PBXTargetDependency */, + 54E740DD218AFE9F005C1A34 /* PBXTargetDependency */, + ); + name = FloatingPanelTests; + productName = FloatingModalControllerTests; + productReference = 545DB9CA2151169500CA77B8 /* FloatingPanelTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; + 54E740C9218AFD67005C1A34 /* TestingApp */ = { + isa = PBXNativeTarget; + buildConfigurationList = 54E740D9218AFD6A005C1A34 /* Build configuration list for PBXNativeTarget "TestingApp" */; + buildPhases = ( + 54E740C6218AFD67005C1A34 /* Sources */, + 54E740C7218AFD67005C1A34 /* Frameworks */, + 54E740C8218AFD67005C1A34 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = TestingApp; + productName = TestingHost; + productReference = 54E740CA218AFD67005C1A34 /* FloatingPanelTesting.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 545DB9B82151169500CA77B8 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1010; + LastUpgradeCheck = 1300; + ORGANIZATIONNAME = scenee; + TargetAttributes = { + 545DB9C02151169500CA77B8 = { + CreatedOnToolsVersion = 10.0; + LastSwiftMigration = 1110; + }; + 545DB9C92151169500CA77B8 = { + CreatedOnToolsVersion = 10.0; + TestTargetID = 54E740C9218AFD67005C1A34; + }; + 54E740C9218AFD67005C1A34 = { + CreatedOnToolsVersion = 10.1; + }; + }; + }; + buildConfigurationList = 545DB9BB2151169500CA77B8 /* Build configuration list for PBXProject "FloatingPanel" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 545DB9B72151169500CA77B8; + productRefGroup = 545DB9C22151169500CA77B8 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 545DB9C02151169500CA77B8 /* FloatingPanel */, + 545DB9C92151169500CA77B8 /* FloatingPanelTests */, + 54E740C9218AFD67005C1A34 /* TestingApp */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 545DB9BF2151169500CA77B8 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 545DB9C82151169500CA77B8 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 54E740C8218AFD67005C1A34 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 5469F4A224B003EF00537F8A /* LaunchScreen.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 545DB9BD2151169500CA77B8 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 5469F4B224B30F1100537F8A /* Position.swift in Sources */, + 5469F4B024B30E1500537F8A /* LayoutAnchoring.swift in Sources */, + 54CDC5D3215B6D5A007D205C /* SurfaceView.swift in Sources */, + 54CFBFC3215CD045006B5735 /* Layout.swift in Sources */, + 5469F4B424B30F3500537F8A /* LayoutReferences.swift in Sources */, + 54CDC5D5215B6D8D007D205C /* BackdropView.swift in Sources */, + 54352E9821A521CA00CBCA08 /* PassthroughView.swift in Sources */, + 54CFBFC5215CD09C006B5735 /* Core.swift in Sources */, + 54ABD7AF216CCFF7002E6C13 /* Logger.swift in Sources */, + 545DB9E021511AC100CA77B8 /* Controller.swift in Sources */, + 54DBA3DC262E938500D75969 /* Extensions.swift in Sources */, + 5450EEE421646DF500135936 /* Behavior.swift in Sources */, + 545DBA2B2152383100CA77B8 /* GrabberView.swift in Sources */, + 54E3992727141F5100A8F9ED /* FloatingPanel.docc in Sources */, + 54352E9621A51A2500CBCA08 /* Transitioning.swift in Sources */, + 5469F4AE24B30D7E00537F8A /* State.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 545DB9C62151169500CA77B8 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 54A6B6B122968B530077F348 /* CoreTests.swift in Sources */, + 549C371F2361E15E007D8058 /* ExtensionTests.swift in Sources */, + 545DB9D02151169500CA77B8 /* ControllerTests.swift in Sources */, + 549E944522CF295D0050AECF /* StateTests.swift in Sources */, + 542753C622C49A6E00D17955 /* LayoutTests.swift in Sources */, + 54A6B6B82296A8520077F348 /* SurfaceViewTests.swift in Sources */, + 546055BF2333C4740069F400 /* TestSupports.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 54E740C6218AFD67005C1A34 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 5469F4A324B003EF00537F8A /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + 545DB9CD2151169500CA77B8 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 545DB9C02151169500CA77B8 /* FloatingPanel */; + targetProxy = 545DB9CC2151169500CA77B8 /* PBXContainerItemProxy */; + }; + 54E740DD218AFE9F005C1A34 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 54E740C9218AFD67005C1A34 /* TestingApp */; + targetProxy = 54E740DC218AFE9F005C1A34 /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin XCBuildConfiguration section */ + 545DB9D32151169500CA77B8 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Debug; + }; + 545DB9D42151169500CA77B8 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 5.0; + VALIDATE_PRODUCT = YES; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Release; + }; + 545DB9D62151169500CA77B8 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; + BUILD_LIBRARY_FOR_DISTRIBUTION = YES; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Automatic; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = Sources/Info.plist; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanel; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 545DB9D72151169500CA77B8 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; + BUILD_LIBRARY_FOR_DISTRIBUTION = YES; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Automatic; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = Sources/Info.plist; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanel; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; + 545DB9D92151169500CA77B8 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = Tests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingModalControllerTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FloatingPanelTesting.app/FloatingPanelTesting"; + }; + name = Debug; + }; + 545DB9DA2151169500CA77B8 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = Tests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingModalControllerTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FloatingPanelTesting.app/FloatingPanelTesting"; + }; + name = Release; + }; + 54E740DA218AFD6A005C1A34 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = Tests/TestingApp/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanelTesting; + PRODUCT_NAME = FloatingPanelTesting; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 54E740DB218AFD6A005C1A34 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = Tests/TestingApp/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanelTesting; + PRODUCT_NAME = FloatingPanelTesting; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; + 54E79ADF224F6C9800717BC6 /* Test */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Test; + }; + 54E79AE0224F6C9800717BC6 /* Test */ = { + isa = XCBuildConfiguration; + buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Automatic; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = Sources/Info.plist; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanel; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "TEST DEBUG __FP_LOG"; + SWIFT_COMPILATION_MODE = singlefile; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Test; + }; + 54E79AE1224F6C9800717BC6 /* Test */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = Tests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingModalControllerTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FloatingPanelTesting.app/FloatingPanelTesting"; + }; + name = Test; + }; + 54E8AC6A2286CFB6000C5A12 /* Test */ = { + isa = XCBuildConfiguration; + buildSettings = { + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = Tests/TestingApp/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanelTesting; + PRODUCT_NAME = FloatingPanelTesting; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Test; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 545DB9BB2151169500CA77B8 /* Build configuration list for PBXProject "FloatingPanel" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 545DB9D32151169500CA77B8 /* Debug */, + 54E79ADF224F6C9800717BC6 /* Test */, + 545DB9D42151169500CA77B8 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 545DB9D52151169500CA77B8 /* Build configuration list for PBXNativeTarget "FloatingPanel" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 545DB9D62151169500CA77B8 /* Debug */, + 54E79AE0224F6C9800717BC6 /* Test */, + 545DB9D72151169500CA77B8 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 545DB9D82151169500CA77B8 /* Build configuration list for PBXNativeTarget "FloatingPanelTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 545DB9D92151169500CA77B8 /* Debug */, + 54E79AE1224F6C9800717BC6 /* Test */, + 545DB9DA2151169500CA77B8 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 54E740D9218AFD6A005C1A34 /* Build configuration list for PBXNativeTarget "TestingApp" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 54E740DA218AFD6A005C1A34 /* Debug */, + 54E740DB218AFD6A005C1A34 /* Release */, + 54E8AC6A2286CFB6000C5A12 /* Test */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 545DB9B82151169500CA77B8 /* Project object */; +} diff --git a/FloatingPanel.xcodeproj/xcshareddata/IDETemplateMacros.plist b/FloatingPanel.xcodeproj/xcshareddata/IDETemplateMacros.plist new file mode 100644 index 00000000..cfe3b985 --- /dev/null +++ b/FloatingPanel.xcodeproj/xcshareddata/IDETemplateMacros.plist @@ -0,0 +1,8 @@ + + + + + FILEHEADER + Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + + diff --git a/Framework/FloatingPanel.xcodeproj/xcshareddata/xcschemes/FloatingPanel.xcscheme b/FloatingPanel.xcodeproj/xcshareddata/xcschemes/FloatingPanel.xcscheme similarity index 73% rename from Framework/FloatingPanel.xcodeproj/xcshareddata/xcschemes/FloatingPanel.xcscheme rename to FloatingPanel.xcodeproj/xcshareddata/xcschemes/FloatingPanel.xcscheme index 229b3f9c..3d419070 100644 --- a/Framework/FloatingPanel.xcodeproj/xcshareddata/xcschemes/FloatingPanel.xcscheme +++ b/FloatingPanel.xcodeproj/xcshareddata/xcschemes/FloatingPanel.xcscheme @@ -1,6 +1,6 @@ + + + + + + + + - - - - - - - - + + + + @@ -23,5 +22,8 @@ + + diff --git a/Framework/FloatingPanel.xcodeproj/project.pbxproj b/Framework/FloatingPanel.xcodeproj/project.pbxproj deleted file mode 100644 index 3fcdfbcf..00000000 --- a/Framework/FloatingPanel.xcodeproj/project.pbxproj +++ /dev/null @@ -1,501 +0,0 @@ -// !$*UTF8*$! -{ - archiveVersion = 1; - classes = { - }; - objectVersion = 50; - objects = { - -/* Begin PBXBuildFile section */ - 5450EEE421646DF500135936 /* FloatingPanelBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5450EEE321646DF500135936 /* FloatingPanelBehavior.swift */; }; - 545DB9CB2151169500CA77B8 /* FloatingPanel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 545DB9C12151169500CA77B8 /* FloatingPanel.framework */; }; - 545DB9D02151169500CA77B8 /* ViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DB9CF2151169500CA77B8 /* ViewTests.swift */; }; - 545DB9D22151169500CA77B8 /* FloatingPanelController.h in Headers */ = {isa = PBXBuildFile; fileRef = 545DB9C42151169500CA77B8 /* FloatingPanelController.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 545DB9DE215118C800CA77B8 /* UIExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DB9DD215118C800CA77B8 /* UIExtensions.swift */; }; - 545DB9E021511AC100CA77B8 /* FloatingPanelController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DB9DF21511AC100CA77B8 /* FloatingPanelController.swift */; }; - 545DBA2B2152383100CA77B8 /* GrabberHandleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 545DBA2A2152383100CA77B8 /* GrabberHandleView.swift */; }; - 54ABD7AF216CCFF7002E6C13 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54ABD7AE216CCFF7002E6C13 /* Logger.swift */; }; - 54CDC5D3215B6D5A007D205C /* FloatingPanelSurfaceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CDC5D2215B6D5A007D205C /* FloatingPanelSurfaceView.swift */; }; - 54CDC5D5215B6D8D007D205C /* FloatingPanelBackdropView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CDC5D4215B6D8D007D205C /* FloatingPanelBackdropView.swift */; }; - 54CFBFC3215CD045006B5735 /* FloatingPanelLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CFBFC2215CD045006B5735 /* FloatingPanelLayout.swift */; }; - 54CFBFC5215CD09C006B5735 /* FloatingPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CFBFC4215CD09C006B5735 /* FloatingPanel.swift */; }; -/* End PBXBuildFile section */ - -/* Begin PBXContainerItemProxy section */ - 545DB9CC2151169500CA77B8 /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 545DB9B82151169500CA77B8 /* Project object */; - proxyType = 1; - remoteGlobalIDString = 545DB9C02151169500CA77B8; - remoteInfo = FloatingModalController; - }; -/* End PBXContainerItemProxy section */ - -/* Begin PBXFileReference section */ - 5450EEE321646DF500135936 /* FloatingPanelBehavior.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingPanelBehavior.swift; sourceTree = ""; }; - 545DB9C12151169500CA77B8 /* FloatingPanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FloatingPanel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 545DB9C42151169500CA77B8 /* FloatingPanelController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FloatingPanelController.h; sourceTree = ""; }; - 545DB9C52151169500CA77B8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 545DB9CA2151169500CA77B8 /* FloatingPanelTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FloatingPanelTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; - 545DB9CF2151169500CA77B8 /* ViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewTests.swift; sourceTree = ""; }; - 545DB9D12151169500CA77B8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 545DB9DD215118C800CA77B8 /* UIExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIExtensions.swift; sourceTree = ""; }; - 545DB9DF21511AC100CA77B8 /* FloatingPanelController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingPanelController.swift; sourceTree = ""; }; - 545DBA2A2152383100CA77B8 /* GrabberHandleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GrabberHandleView.swift; sourceTree = ""; }; - 54ABD7AE216CCFF7002E6C13 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; - 54CDC5D2215B6D5A007D205C /* FloatingPanelSurfaceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingPanelSurfaceView.swift; sourceTree = ""; }; - 54CDC5D4215B6D8D007D205C /* FloatingPanelBackdropView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingPanelBackdropView.swift; sourceTree = ""; }; - 54CFBFC2215CD045006B5735 /* FloatingPanelLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingPanelLayout.swift; sourceTree = ""; }; - 54CFBFC4215CD09C006B5735 /* FloatingPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingPanel.swift; sourceTree = ""; }; -/* End PBXFileReference section */ - -/* Begin PBXFrameworksBuildPhase section */ - 545DB9BE2151169500CA77B8 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 545DB9C72151169500CA77B8 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - 545DB9CB2151169500CA77B8 /* FloatingPanel.framework in Frameworks */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXFrameworksBuildPhase section */ - -/* Begin PBXGroup section */ - 545DB9B72151169500CA77B8 = { - isa = PBXGroup; - children = ( - 545DB9C32151169500CA77B8 /* Sources */, - 545DB9CE2151169500CA77B8 /* Tests */, - 545DB9C22151169500CA77B8 /* Products */, - ); - sourceTree = ""; - }; - 545DB9C22151169500CA77B8 /* Products */ = { - isa = PBXGroup; - children = ( - 545DB9C12151169500CA77B8 /* FloatingPanel.framework */, - 545DB9CA2151169500CA77B8 /* FloatingPanelTests.xctest */, - ); - name = Products; - sourceTree = ""; - }; - 545DB9C32151169500CA77B8 /* Sources */ = { - isa = PBXGroup; - children = ( - 545DB9C52151169500CA77B8 /* Info.plist */, - 545DB9C42151169500CA77B8 /* FloatingPanelController.h */, - 545DB9DF21511AC100CA77B8 /* FloatingPanelController.swift */, - 54CFBFC4215CD09C006B5735 /* FloatingPanel.swift */, - 54CFBFC2215CD045006B5735 /* FloatingPanelLayout.swift */, - 5450EEE321646DF500135936 /* FloatingPanelBehavior.swift */, - 54CDC5D2215B6D5A007D205C /* FloatingPanelSurfaceView.swift */, - 54CDC5D4215B6D8D007D205C /* FloatingPanelBackdropView.swift */, - 545DBA2A2152383100CA77B8 /* GrabberHandleView.swift */, - 545DB9DD215118C800CA77B8 /* UIExtensions.swift */, - 54ABD7AE216CCFF7002E6C13 /* Logger.swift */, - ); - path = Sources; - sourceTree = ""; - }; - 545DB9CE2151169500CA77B8 /* Tests */ = { - isa = PBXGroup; - children = ( - 545DB9CF2151169500CA77B8 /* ViewTests.swift */, - 545DB9D12151169500CA77B8 /* Info.plist */, - ); - path = Tests; - sourceTree = ""; - }; -/* End PBXGroup section */ - -/* Begin PBXHeadersBuildPhase section */ - 545DB9BC2151169500CA77B8 /* Headers */ = { - isa = PBXHeadersBuildPhase; - buildActionMask = 2147483647; - files = ( - 545DB9D22151169500CA77B8 /* FloatingPanelController.h in Headers */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXHeadersBuildPhase section */ - -/* Begin PBXNativeTarget section */ - 545DB9C02151169500CA77B8 /* FloatingPanel */ = { - isa = PBXNativeTarget; - buildConfigurationList = 545DB9D52151169500CA77B8 /* Build configuration list for PBXNativeTarget "FloatingPanel" */; - buildPhases = ( - 545DB9BC2151169500CA77B8 /* Headers */, - 545DB9BD2151169500CA77B8 /* Sources */, - 545DB9BE2151169500CA77B8 /* Frameworks */, - 545DB9BF2151169500CA77B8 /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - ); - name = FloatingPanel; - productName = FloatingModalController; - productReference = 545DB9C12151169500CA77B8 /* FloatingPanel.framework */; - productType = "com.apple.product-type.framework"; - }; - 545DB9C92151169500CA77B8 /* FloatingPanelTests */ = { - isa = PBXNativeTarget; - buildConfigurationList = 545DB9D82151169500CA77B8 /* Build configuration list for PBXNativeTarget "FloatingPanelTests" */; - buildPhases = ( - 545DB9C62151169500CA77B8 /* Sources */, - 545DB9C72151169500CA77B8 /* Frameworks */, - 545DB9C82151169500CA77B8 /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - 545DB9CD2151169500CA77B8 /* PBXTargetDependency */, - ); - name = FloatingPanelTests; - productName = FloatingModalControllerTests; - productReference = 545DB9CA2151169500CA77B8 /* FloatingPanelTests.xctest */; - productType = "com.apple.product-type.bundle.unit-test"; - }; -/* End PBXNativeTarget section */ - -/* Begin PBXProject section */ - 545DB9B82151169500CA77B8 /* Project object */ = { - isa = PBXProject; - attributes = { - LastSwiftUpdateCheck = 1000; - LastUpgradeCheck = 1000; - ORGANIZATIONNAME = scenee; - TargetAttributes = { - 545DB9C02151169500CA77B8 = { - CreatedOnToolsVersion = 10.0; - LastSwiftMigration = 1000; - }; - 545DB9C92151169500CA77B8 = { - CreatedOnToolsVersion = 10.0; - }; - }; - }; - buildConfigurationList = 545DB9BB2151169500CA77B8 /* Build configuration list for PBXProject "FloatingPanel" */; - compatibilityVersion = "Xcode 9.3"; - developmentRegion = en; - hasScannedForEncodings = 0; - knownRegions = ( - en, - ); - mainGroup = 545DB9B72151169500CA77B8; - productRefGroup = 545DB9C22151169500CA77B8 /* Products */; - projectDirPath = ""; - projectRoot = ""; - targets = ( - 545DB9C02151169500CA77B8 /* FloatingPanel */, - 545DB9C92151169500CA77B8 /* FloatingPanelTests */, - ); - }; -/* End PBXProject section */ - -/* Begin PBXResourcesBuildPhase section */ - 545DB9BF2151169500CA77B8 /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 545DB9C82151169500CA77B8 /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXResourcesBuildPhase section */ - -/* Begin PBXSourcesBuildPhase section */ - 545DB9BD2151169500CA77B8 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 54CDC5D3215B6D5A007D205C /* FloatingPanelSurfaceView.swift in Sources */, - 54CFBFC3215CD045006B5735 /* FloatingPanelLayout.swift in Sources */, - 54CDC5D5215B6D8D007D205C /* FloatingPanelBackdropView.swift in Sources */, - 54CFBFC5215CD09C006B5735 /* FloatingPanel.swift in Sources */, - 54ABD7AF216CCFF7002E6C13 /* Logger.swift in Sources */, - 545DB9E021511AC100CA77B8 /* FloatingPanelController.swift in Sources */, - 5450EEE421646DF500135936 /* FloatingPanelBehavior.swift in Sources */, - 545DBA2B2152383100CA77B8 /* GrabberHandleView.swift in Sources */, - 545DB9DE215118C800CA77B8 /* UIExtensions.swift in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 545DB9C62151169500CA77B8 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 545DB9D02151169500CA77B8 /* ViewTests.swift in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXSourcesBuildPhase section */ - -/* Begin PBXTargetDependency section */ - 545DB9CD2151169500CA77B8 /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 545DB9C02151169500CA77B8 /* FloatingPanel */; - targetProxy = 545DB9CC2151169500CA77B8 /* PBXContainerItemProxy */; - }; -/* End PBXTargetDependency section */ - -/* Begin XCBuildConfiguration section */ - 545DB9D32151169500CA77B8 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - CLANG_ANALYZER_NONNULL = YES; - CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_ENABLE_OBJC_WEAK = YES; - CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_COMMA = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_DOCUMENTATION_COMMENTS = YES; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INFINITE_RECURSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; - CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; - CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; - CLANG_WARN_STRICT_PROTOTYPES = YES; - CLANG_WARN_SUSPICIOUS_MOVE = YES; - CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = "iPhone Developer"; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = dwarf; - ENABLE_STRICT_OBJC_MSGSEND = YES; - ENABLE_TESTABILITY = YES; - GCC_C_LANGUAGE_STANDARD = gnu11; - GCC_DYNAMIC_NO_PIC = NO; - GCC_NO_COMMON_BLOCKS = YES; - GCC_OPTIMIZATION_LEVEL = 0; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)", - ); - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.0; - MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; - MTL_FAST_MATH = YES; - ONLY_ACTIVE_ARCH = YES; - SDKROOT = iphoneos; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - }; - name = Debug; - }; - 545DB9D42151169500CA77B8 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - CLANG_ANALYZER_NONNULL = YES; - CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_ENABLE_OBJC_WEAK = YES; - CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_COMMA = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_DOCUMENTATION_COMMENTS = YES; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INFINITE_RECURSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; - CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; - CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; - CLANG_WARN_STRICT_PROTOTYPES = YES; - CLANG_WARN_SUSPICIOUS_MOVE = YES; - CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = "iPhone Developer"; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - ENABLE_NS_ASSERTIONS = NO; - ENABLE_STRICT_OBJC_MSGSEND = YES; - GCC_C_LANGUAGE_STANDARD = gnu11; - GCC_NO_COMMON_BLOCKS = YES; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.0; - MTL_ENABLE_DEBUG_INFO = NO; - MTL_FAST_MATH = YES; - SDKROOT = iphoneos; - SWIFT_COMPILATION_MODE = wholemodule; - SWIFT_OPTIMIZATION_LEVEL = "-O"; - VALIDATE_PRODUCT = YES; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - }; - name = Release; - }; - 545DB9D62151169500CA77B8 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - CLANG_ENABLE_MODULES = YES; - CODE_SIGN_IDENTITY = ""; - CODE_SIGN_STYLE = Automatic; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - INFOPLIST_FILE = Sources/Info.plist; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/Frameworks", - "@loader_path/Frameworks", - ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanel; - PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; - SKIP_INSTALL = YES; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 4.2; - TARGETED_DEVICE_FAMILY = "1,2"; - }; - name = Debug; - }; - 545DB9D72151169500CA77B8 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - CLANG_ENABLE_MODULES = YES; - CODE_SIGN_IDENTITY = ""; - CODE_SIGN_STYLE = Automatic; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - INFOPLIST_FILE = Sources/Info.plist; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/Frameworks", - "@loader_path/Frameworks", - ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingPanel; - PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; - SKIP_INSTALL = YES; - SWIFT_VERSION = 4.2; - TARGETED_DEVICE_FAMILY = "1,2"; - }; - name = Release; - }; - 545DB9D92151169500CA77B8 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; - CODE_SIGN_STYLE = Automatic; - INFOPLIST_FILE = Tests/Info.plist; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/Frameworks", - "@loader_path/Frameworks", - ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingModalControllerTests; - PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.2; - TARGETED_DEVICE_FAMILY = "1,2"; - }; - name = Debug; - }; - 545DB9DA2151169500CA77B8 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; - CODE_SIGN_STYLE = Automatic; - INFOPLIST_FILE = Tests/Info.plist; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/Frameworks", - "@loader_path/Frameworks", - ); - PRODUCT_BUNDLE_IDENTIFIER = com.scenee.FloatingModalControllerTests; - PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 4.2; - TARGETED_DEVICE_FAMILY = "1,2"; - }; - name = Release; - }; -/* End XCBuildConfiguration section */ - -/* Begin XCConfigurationList section */ - 545DB9BB2151169500CA77B8 /* Build configuration list for PBXProject "FloatingPanel" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 545DB9D32151169500CA77B8 /* Debug */, - 545DB9D42151169500CA77B8 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 545DB9D52151169500CA77B8 /* Build configuration list for PBXNativeTarget "FloatingPanel" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 545DB9D62151169500CA77B8 /* Debug */, - 545DB9D72151169500CA77B8 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 545DB9D82151169500CA77B8 /* Build configuration list for PBXNativeTarget "FloatingPanelTests" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 545DB9D92151169500CA77B8 /* Debug */, - 545DB9DA2151169500CA77B8 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; -/* End XCConfigurationList section */ - }; - rootObject = 545DB9B82151169500CA77B8 /* Project object */; -} diff --git a/Framework/Sources/FloatingPanel.swift b/Framework/Sources/FloatingPanel.swift deleted file mode 100644 index f90a450a..00000000 --- a/Framework/Sources/FloatingPanel.swift +++ /dev/null @@ -1,487 +0,0 @@ -// -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -/// -/// FloatingPanel presentation model -/// -class FloatingPanel: NSObject, UIGestureRecognizerDelegate, UIScrollViewDelegate { - /* Cause 'terminating with uncaught exception of type NSException' error on Swift Playground - unowned let view: UIView - */ - let surfaceView: FloatingPanelSurfaceView - let backdropView: FloatingPanelBackdropView - var layoutAdapter: FloatingPanelLayoutAdapter - var behavior: FloatingPanelBehavior - - weak var scrollView: UIScrollView? { - didSet { - guard let scrollView = scrollView else { return } - scrollView.panGestureRecognizer.addTarget(self, action: #selector(handle(panGesture:))) - scrollBouncable = scrollView.bounces - scrollIndictorVisible = scrollView.showsVerticalScrollIndicator - } - } - weak var userScrollViewDelegate: UIScrollViewDelegate? - - var safeAreaInsets: UIEdgeInsets! { - get { return layoutAdapter.safeAreaInsets } - set { layoutAdapter.safeAreaInsets = newValue } - } - - unowned let viewcontroller: FloatingPanelController - - private(set) var state: FloatingPanelPosition = .tip - - private var animator: UIViewPropertyAnimator? - private let panGesture: UIPanGestureRecognizer - private var initialFrame: CGRect = .zero - private var transOffsetY: CGFloat = 0 - private var interactionInProgress: Bool = false - - // Scroll handling - private var stopScrollDeceleration: Bool = false - private var scrollBouncable = false - private var scrollIndictorVisible = false - - // MARK: - Interface - - init(_ vc: FloatingPanelController, layout: FloatingPanelLayout, behavior: FloatingPanelBehavior) { - viewcontroller = vc - surfaceView = vc.view as! FloatingPanelSurfaceView - backdropView = FloatingPanelBackdropView() - backdropView.backgroundColor = .black - backdropView.alpha = 0.0 - - self.layoutAdapter = FloatingPanelLayoutAdapter(surfaceView: surfaceView, - backdropView: backdropView, - layout: layout) - self.behavior = behavior - - panGesture = UIPanGestureRecognizer() - - if #available(iOS 11.0, *) { - panGesture.name = "FloatingPanelSurface" - } - - super.init() - - surfaceView.addGestureRecognizer(panGesture) - panGesture.addTarget(self, action: #selector(handle(panGesture:))) - panGesture.delegate = self - } - - func layoutViews(in vc: UIViewController) { - unowned let view = vc.view! - - view.insertSubview(backdropView, belowSubview: surfaceView) - backdropView.frame = view.bounds - - layoutAdapter.prepareLayout(toParent: vc) - } - - func move(to: FloatingPanelPosition, animated: Bool, completion: (() -> Void)? = nil) { - if to != .full { - lockScrollView() - } - - if animated { - let animator = behavior.presentAnimator(self.viewcontroller, from: state, to: to) - animator.addAnimations { [weak self] in - guard let self = self else { return } - - self.updateLayout(to: to) - self.state = to - } - animator.addCompletion { _ in - completion?() - } - animator.startAnimation() - } else { - self.updateLayout(to: to) - self.state = to - completion?() - } - } - - func present(animated: Bool, completion: (() -> Void)? = nil) { - self.layoutAdapter.activateLayout(of: nil) - move(to: layoutAdapter.layout.initialPosition, animated: animated, completion: completion) - } - - func dismiss(animated: Bool, completion: (() -> Void)? = nil) { - if animated { - let animator = behavior.dismissAnimator(self.viewcontroller, from: state) - animator.addAnimations { [weak self] in - guard let self = self else { return } - - self.updateLayout(to: nil) - } - animator.addCompletion { _ in - completion?() - } - animator.startAnimation() - } else { - self.updateLayout(to: nil) - completion?() - } - } - - // MARK: - Layout update - - private func updateLayout(to target: FloatingPanelPosition?) { - self.layoutAdapter.activateLayout(of: target) - self.setBackdropAlpha(of: target) - } - - private func setBackdropAlpha(of target: FloatingPanelPosition?) { - switch target { - case .full?: - self.backdropView.alpha = layoutAdapter.layout.backdropAlpha - default: - self.backdropView.alpha = 0.0 - } - } - - private func getBackdropAlpha(with translation: CGPoint) -> CGFloat { - let topY = layoutAdapter.topY - let middleY = layoutAdapter.middleY - let currentY = getCurrentY(from: initialFrame, with: translation) - return (1 - (currentY - topY) / (middleY - topY)) * layoutAdapter.layout.backdropAlpha - } - - // MARK: - UIGestureRecognizerDelegate - - public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, - shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { - guard gestureRecognizer == panGesture else { return false } - - log.debug("shouldRecognizeSimultaneouslyWith", otherGestureRecognizer) - - return otherGestureRecognizer == scrollView?.panGestureRecognizer - } - - public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool { - guard gestureRecognizer == panGesture else { return false } - - // Do not begin any gestures excluding scrollView?.panGestureRecognizer until the pan gesture fails - if otherGestureRecognizer == scrollView?.panGestureRecognizer { - return false - } else { - return true - } - } - - // MARK: - Gesture handling - - @objc func handle(panGesture: UIPanGestureRecognizer) { - log.debug("Gesture >>>>", panGesture) - - switch panGesture { - case scrollView?.panGestureRecognizer: - guard let scrollView = scrollView else { return } - if surfaceView.frame.minY > layoutAdapter.topY { - scrollView.contentOffset.y = scrollView.contentOffsetZero.y - } - case panGesture: - let translation = panGesture.translation(in: panGesture.view!.superview) - let velocity = panGesture.velocity(in: panGesture.view) - let location = panGesture.location(in: panGesture.view) - - log.debug(panGesture.state, ">>>", "{ translation: \(translation), velocity: \(velocity) }") - - if let scrollView = scrollView, scrollView.frame.contains(location) { - log.debug("ScrollView.contentOffset >>>", scrollView.contentOffset) - if state == .full { - if scrollView.contentOffset.y - scrollView.contentOffsetZero.y > 0 { - return - } - if scrollView.isDecelerating { - return - } - if interactionInProgress == false, velocity.y < 0 || velocity.y > 2500.0 { - return - } - } - } - - switch panGesture.state { - case .began: - panningBegan() - case .changed: - panningChange(with: translation) - case .ended, .cancelled, .failed: - panningEnd(with: translation, velocity: velocity) - case .possible: - break - } - default: - return - } - } - - private func panningBegan() { - // A user interaction does not always start from Began state of the pan gesture - // because it can be recognized in scrolling a content in a content view controller. - // So I don't nothing here. - log.debug("panningBegan \(initialFrame)") - } - - private func panningChange(with translation: CGPoint) { - log.debug("panningChange") - if interactionInProgress == false { - startInteraction(with: translation) - } - - let currentY = getCurrentY(from: initialFrame, with: translation) - - var frame = initialFrame - frame.origin.y = currentY - surfaceView.frame = frame - backdropView.alpha = getBackdropAlpha(with: translation) - - viewcontroller.delegate?.floatingPanelDidMove(viewcontroller) - } - - private func panningEnd(with translation: CGPoint, velocity: CGPoint) { - log.debug("panningEnd") - if interactionInProgress == false { - initialFrame = surfaceView.frame - } - - stopScrollDeceleration = (surfaceView.frame.minY > layoutAdapter.topY) // Projecting the dragging to the scroll dragging - - let targetPosition = self.targetPosition(with: translation, velocity: velocity) - let distance = self.distance(to: targetPosition, with: translation) - - endInteraction(for: targetPosition) - - viewcontroller.delegate?.floatingPanelDidEndDragging(viewcontroller, withVelocity: velocity, targetPosition: targetPosition) - viewcontroller.delegate?.floatingPanelWillBeginDecelerating(viewcontroller) - - startAnimation(to: targetPosition, at: distance, with: velocity) - } - - private func startInteraction(with translation: CGPoint) { - log.debug("startInteraction") - initialFrame = surfaceView.frame - transOffsetY = translation.y - viewcontroller.delegate?.floatingPanelWillBeginDragging(viewcontroller) - - lockScrollView() - - interactionInProgress = true - } - - private func endInteraction(for targetPosition: FloatingPanelPosition) { - log.debug("endInteraction for \(targetPosition)") - if targetPosition != .full { - lockScrollView(withBounce: true) - } - interactionInProgress = false - } - - private func getCurrentY(from rect: CGRect, with translation: CGPoint) -> CGFloat { - let dy = translation.y - transOffsetY - let y = rect.offsetBy(dx: 0.0, dy: dy).origin.y - - let topY = layoutAdapter.topY - let topInset = layoutAdapter.topInset - let topBuffer = layoutAdapter.layout.topInteractionBuffer - - let bottomY = layoutAdapter.bottomY - let bottomBuffer = layoutAdapter.layout.bottomInteractionBuffer - - if let scrollView = scrollView, scrollView.panGestureRecognizer.state == .changed { - let preY = surfaceView.frame.origin.y - if preY > 0 && preY > y { - return max(topY, min(bottomY, y)) - } - } - return max(topY - topInset + topBuffer, min(bottomY + bottomBuffer, y)) - } - - private func startAnimation(to targetPosition: FloatingPanelPosition, at distance: CGFloat, with velocity: CGPoint) { - let targetY = layoutAdapter.positionY(for: targetPosition) - let velocityVector = (distance != 0) ? CGVector(dx: 0, dy: max(min(velocity.y/distance, 30.0), -30.0)) : .zero - let animator = behavior.interactionAnimator(self.viewcontroller, to: targetPosition, with: velocityVector) - animator.isInterruptible = false // To prevent a backdrop color's punk - animator.addAnimations { [weak self] in - guard let self = self else { return } - if self.state == targetPosition { - self.surfaceView.frame.origin.y = targetY - self.setBackdropAlpha(of: targetPosition) - } else { - self.updateLayout(to: targetPosition) - } - self.state = targetPosition - } - animator.addCompletion { [weak self] pos in - guard let self = self else { return } - guard - self.interactionInProgress == false, - animator == self.animator, - pos == .end - else { return } - self.finishAnimation(at: targetPosition) - } - animator.startAnimation() - self.animator = animator - } - - private func finishAnimation(at targetPosition: FloatingPanelPosition) { - log.debug("finishAnimation \(targetPosition)") - self.animator = nil - self.viewcontroller.delegate?.floatingPanelDidEndDecelerating(self.viewcontroller) - - // Don't unlock scroll view in animating view when presentation layer != model layer - unlockScrollView() - } - - private func distance(to targetPosition: FloatingPanelPosition, with translation: CGPoint) -> CGFloat { - let topY = layoutAdapter.topY - let middleY = layoutAdapter.middleY - let bottomY = layoutAdapter.bottomY - let currentY = getCurrentY(from: initialFrame, with: translation) - switch targetPosition { - case .full: - return CGFloat(fabs(Double(currentY - topY))) - case .half: - return CGFloat(fabs(Double(currentY - middleY))) - case .tip: - return CGFloat(fabs(Double(currentY - bottomY))) - } - } - - // Distance travelled after decelerating to zero velocity at a constant rate. - // Refer to the slides p176 of [Designing Fluid Interfaces](https://developer.apple.com/videos/play/wwdc2018/803/) - private func project(initialVelocity: CGFloat) -> CGFloat { - let decelerationRate = UIScrollView.DecelerationRate.normal.rawValue - return (initialVelocity / 1000.0) * decelerationRate / (1.0 - decelerationRate) - } - - private func targetPosition(with translation: CGPoint, velocity: CGPoint) -> (FloatingPanelPosition) { - let currentY = getCurrentY(from: initialFrame, with: translation) - let supportedPositions = Set(layoutAdapter.layout.supportedPositions) - - assert(supportedPositions.count > 1) - - switch supportedPositions { - case Set([.full, .half]): - return targetPosition(from: [.half, .tip], at: currentY, velocity: velocity) - case Set([.half, .tip]): - return targetPosition(from: [.half, .tip], at: currentY, velocity: velocity) - case Set([.full, .tip]): - return targetPosition(from: [.full, .tip], at: currentY, velocity: velocity) - default: - /* - [topY|full]---[th1]---[middleY|default]---[th2]---[bottomY|collapsed] - */ - let topY = layoutAdapter.topY - let middleY = layoutAdapter.middleY - let bottomY = layoutAdapter.bottomY - - let th1 = (topY + middleY) / 2 - let th2 = (middleY + bottomY) / 2 - - switch currentY { - case ..= (middleY - currentY) { - return .half - } else { - return .full - } - case ...middleY: - if project(initialVelocity: velocity.y) <= (topY - currentY) { - return .full - } else { - return .half - } - case ..= (bottomY - currentY) { - return .tip - } else { - return .half - } - default: - if project(initialVelocity: velocity.y) <= (middleY - currentY) { - return .half - } else { - return .tip - } - } - } - } - - private func targetPosition(from positions: [FloatingPanelPosition], at currentY: CGFloat, velocity: CGPoint) -> FloatingPanelPosition { - assert(positions.count == 2) - - let top = positions[0] - let bottom = positions[1] - - let topY = layoutAdapter.positionY(for: top) - let bottomY = layoutAdapter.positionY(for: bottom) - - let th = (topY + bottomY) / 2 - - switch currentY { - case ..= (bottomY - currentY) { - return bottom - } else { - return top - } - default: - if project(initialVelocity: velocity.y) <= (topY - currentY) { - return top - } else { - return bottom - } - } - } - - // MARK: - ScrollView handling - - func lockScrollView(withBounce bounce: Bool = false) { - guard let scrollView = scrollView else { return } - - scrollView.isDirectionalLockEnabled = true - if bounce { - scrollView.bounces = false - } - scrollView.showsVerticalScrollIndicator = false - } - - func unlockScrollView() { - guard let scrollView = scrollView else { return } - - scrollView.isDirectionalLockEnabled = false - scrollView.bounces = scrollBouncable - scrollView.showsVerticalScrollIndicator = scrollIndictorVisible - } - - - // MARK: - UIScrollViewDelegate Intermediation - override func responds(to aSelector: Selector!) -> Bool { - return super.responds(to: aSelector) || userScrollViewDelegate?.responds(to: aSelector) == true - } - - override func forwardingTarget(for aSelector: Selector!) -> Any? { - if userScrollViewDelegate?.responds(to: aSelector) == true { - return userScrollViewDelegate - } else { - return super.forwardingTarget(for: aSelector) - } - } - - func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { - if stopScrollDeceleration { - targetContentOffset.pointee = scrollView.contentOffset - stopScrollDeceleration = false - } else { - userScrollViewDelegate?.scrollViewWillEndDragging?(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset) - } - } -} diff --git a/Framework/Sources/FloatingPanelBackdropView.swift b/Framework/Sources/FloatingPanelBackdropView.swift deleted file mode 100644 index 93ad6b0f..00000000 --- a/Framework/Sources/FloatingPanelBackdropView.swift +++ /dev/null @@ -1,9 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/09/26. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -/// A view that presents a backdrop interface behind a floating panel. -public class FloatingPanelBackdropView: UIView { } diff --git a/Framework/Sources/FloatingPanelBehavior.swift b/Framework/Sources/FloatingPanelBehavior.swift deleted file mode 100644 index 6c37dd0c..00000000 --- a/Framework/Sources/FloatingPanelBehavior.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/10/03. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -public protocol FloatingPanelBehavior { - // Returns a UIViewPropertyAnimator object in interacting a floating panel by a user pan gesture - func interactionAnimator(_ fpc: FloatingPanelController, to targetPosition: FloatingPanelPosition, with velocity: CGVector) -> UIViewPropertyAnimator - - // Returns a UIViewPropertyAnimator object to present a floating panel - func presentAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition, to: FloatingPanelPosition) -> UIViewPropertyAnimator - // Returns a UIViewPropertyAnimator object to dismiss a floating panel - func dismissAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition) -> UIViewPropertyAnimator -} - -public extension FloatingPanelBehavior { - func presentAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition, to: FloatingPanelPosition) -> UIViewPropertyAnimator { - return UIViewPropertyAnimator(duration: 0.25, curve: .easeInOut) - } - - func dismissAnimator(_ fpc: FloatingPanelController, from: FloatingPanelPosition) -> UIViewPropertyAnimator { - return UIViewPropertyAnimator(duration: 0.25, curve: .easeInOut) - } -} - -class FloatingPanelDefaultBehavior: FloatingPanelBehavior { - func interactionAnimator(_ fpc: FloatingPanelController, to targetPosition: FloatingPanelPosition, with velocity: CGVector) -> UIViewPropertyAnimator { - let timing = timeingCurve(to: targetPosition, with: velocity) - return UIViewPropertyAnimator(duration: 0, timingParameters: timing) - } - - private func timeingCurve(to: FloatingPanelPosition, with velocity: CGVector) -> UITimingCurveProvider { - log.debug("velocity", velocity) - let damping = self.getDamping(with: velocity) - return UISpringTimingParameters(dampingRatio: damping, - frequencyResponse: 0.3, - initialVelocity: velocity) - } - - private let velocityThreshold: CGFloat = 8.0 - private func getDamping(with velocity: CGVector) -> CGFloat { - let dy = abs(velocity.dy) - if dy > velocityThreshold { - return 0.7 - } else { - return 1.0 - } - } -} diff --git a/Framework/Sources/FloatingPanelController.h b/Framework/Sources/FloatingPanelController.h deleted file mode 100644 index bf3dd585..00000000 --- a/Framework/Sources/FloatingPanelController.h +++ /dev/null @@ -1,9 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/09/18. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -#import - -FOUNDATION_EXPORT double FloatingPanelVersionNumber; -FOUNDATION_EXPORT const unsigned char FloatingPanelVersionString[]; diff --git a/Framework/Sources/FloatingPanelController.swift b/Framework/Sources/FloatingPanelController.swift deleted file mode 100644 index c257f618..00000000 --- a/Framework/Sources/FloatingPanelController.swift +++ /dev/null @@ -1,292 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/09/18. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -public protocol FloatingPanelControllerDelegate: class { - // if it returns nil, FloatingPanelController uses the default layout - func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? - - // if it returns nil, FloatingPanelController uses the default behavior - func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? - - func floatingPanelDidMove(_ vc: FloatingPanelController) // any offset changes - - // called on start of dragging (may require some time and or distance to move) - func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) - // called on finger up if the user dragged. velocity is in points/second. - func floatingPanelDidEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetPosition: FloatingPanelPosition) - func floatingPanelWillBeginDecelerating(_ vc: FloatingPanelController) // called on finger up as we are moving - func floatingPanelDidEndDecelerating(_ vc: FloatingPanelController) // called when scroll view grinds to a halt -} - -public extension FloatingPanelControllerDelegate { - func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? { - return nil - } - func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? { - return nil - } - func floatingPanelDidMove(_ vc: FloatingPanelController) {} - func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) {} - func floatingPanelDidEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetPosition: FloatingPanelPosition) {} - func floatingPanelWillBeginDecelerating(_ vc: FloatingPanelController) {} - func floatingPanelDidEndDecelerating(_ vc: FloatingPanelController) {} -} - -public enum FloatingPanelPosition: Int { - case full - case half - case tip -} - -/// -/// A container view controller to display a floating panel to present contents in parallel as a user wants. -/// -public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate { - - /// Constants indicating how safe area insets are added to the adjusted content inset. - public enum ContentInsetAdjustmentBehavior: Int { - case always - case never - } - - /// The delegate of the floating panel controller object. - public weak var delegate: FloatingPanelControllerDelegate? - - /// Returns the surface view managed by the controller object. It's the same as `self.view`. - public var surfaceView: FloatingPanelSurfaceView! { - return view as? FloatingPanelSurfaceView - } - - /// Returns the backdrop view managed by the controller object. - public var backdropView: FloatingPanelBackdropView! { - return floatingPanel.backdropView - } - - /// Returns the scroll view that the conroller tracks. - public weak var scrollView: UIScrollView? { - return floatingPanel.scrollView - } - - /// The current position of the floating panel controller's contents. - public var position: FloatingPanelPosition { - return floatingPanel.state - } - - /// The content insets of the tracking scroll view derived the safe area of the parent view - public var adjustedContentInsets: UIEdgeInsets { - return floatingPanel.layoutAdapter.adjustedContentInsets - } - - /// The behavior for determining the adjusted content offsets. - /// - /// This property specifies how the content area of the tracking scroll view are modified using `adjustedContentInsets`. The default value of this property is FloatingPanelController.ContentInsetAdjustmentBehavior.always. - public var contentInsetAdjustmentBehavior: ContentInsetAdjustmentBehavior = .always - - private var floatingPanel: FloatingPanel! - - required init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - } - - /// Initialize a newly created a floating panel controller. - public init() { - super.init(nibName: nil, bundle: nil) - } - - /// Creates the view that the controller manages. - override public func loadView() { - assert(self.storyboard == nil, "Storyboard isn't supported") - - let view = FloatingPanelSurfaceView() - view.backgroundColor = .white - - self.view = view as UIView - - let layout = fetchLayout(for: self.traitCollection) - let behavior = fetchBehavior(for: self.traitCollection) - floatingPanel = FloatingPanel(self, - layout: layout, - behavior: behavior) - } - - public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { - super.willTransition(to: newCollection, with: coordinator) - - // Change layout for a new trait collection - floatingPanel.layoutAdapter.layout = fetchLayout(for: newCollection) - floatingPanel.behavior = fetchBehavior(for: newCollection) - - guard let parent = parent else { fatalError() } - - floatingPanel.layoutAdapter.prepareLayout(toParent: parent) - floatingPanel.layoutAdapter.activateLayout(of: floatingPanel.state) - } - - public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { - super.traitCollectionDidChange(previousTraitCollection) - guard previousTraitCollection != traitCollection else { return } - - if let parent = parent { - self.update(safeAreaInsets: parent.layoutInsets) - } - floatingPanel.layoutAdapter.updateHeight() - floatingPanel.backdropView.isHidden = (traitCollection.verticalSizeClass == .compact) - } - - public override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - - // I needs to update safeAreaInsets here to ensure that the `adjustedContentInsets` has a correct value. - // Because the parent VC does not call viewSafeAreaInsetsDidChange() expectedly and - // `view.safeAreaInsets` has a correct value of the bottom inset here. - if let parent = parent { - self.update(safeAreaInsets: parent.layoutInsets) - } - } - - private func fetchLayout(for traitCollection: UITraitCollection) -> FloatingPanelLayout { - switch traitCollection.verticalSizeClass { - case .compact: - return self.delegate?.floatingPanel(self, layoutFor: traitCollection) ?? FloatingPanelDefaultLandscapeLayout() - default: - return self.delegate?.floatingPanel(self, layoutFor: traitCollection) ?? FloatingPanelDefaultLayout() - } - } - - private func fetchBehavior(for traitCollection: UITraitCollection) -> FloatingPanelBehavior { - return self.delegate?.floatingPanel(self, behaviorFor: traitCollection) ?? FloatingPanelDefaultBehavior() - } - - private func update(safeAreaInsets: UIEdgeInsets) { - floatingPanel.safeAreaInsets = safeAreaInsets - switch contentInsetAdjustmentBehavior { - case .always: - scrollView?.contentInset = adjustedContentInsets - scrollView?.scrollIndicatorInsets = adjustedContentInsets - default: - break - } - } - - // MARK: - Container view controller interface - - /// Adds the view managed the controller as a child of the specified view controller. - /// - Parameters: - /// - parent: A parent view controller object that displays FloatingPanelController's view. A conatiner view controller object isn't applicable. - /// - belowView: Insert the surface view managed by the controller below the specified view. As default, the surface view will be added to the end of the parent list of subviews. - /// - animated: Pass true to animate the presentation; otherwise, pass false. - public func addPanel(toParent parent: UIViewController, belowView: UIView? = nil, animated: Bool = false) { - guard self.parent == nil else { - log.warning("Already added to a parent(\(parent))") - return - } - precondition((parent is UINavigationController) == false, "UINavigationController displays only one child view controller at a time.") - precondition((parent is UITableViewController) == false, "UITableViewController should not be the parent because the view hierarchy will be break in reusing cells.") - precondition((parent is UICollectionViewController) == false, "UICollectionViewController should not be the parent because the view hierarchy will be break in reusing cells.") - - view.frame = parent.view.bounds - if let belowView = belowView { - parent.view.insertSubview(self.view, belowSubview: belowView) - } else { - parent.view.addSubview(self.view) - } - - parent.addChild(self) - - // Must set a layout again here because `self.traitCollection` is applied correctly on it's added to a parent VC - floatingPanel.layoutAdapter.layout = fetchLayout(for: traitCollection) - floatingPanel.layoutViews(in: parent) - floatingPanel.present(animated: animated) { [weak self] in - guard let self = self else { return } - self.didMove(toParent: parent) - } - } - - /// Removes the controller and the managed view from its parent view controller - /// - Parameters: - /// - animated: Pass true to animate the presentation; otherwise, pass false. - /// - completion: The block to execute after the view controller is dismissed. This block has no return value and takes no parameters. You may specify nil for this parameter. - public func removePanelFromParent(animated: Bool, completion: (() -> Void)? = nil) { - guard self.parent != nil else { - completion?() - return - } - - floatingPanel.dismiss(animated: animated) { [weak self] in - guard let self = self else { return } - - self.willMove(toParent: nil) - self.view.removeFromSuperview() - self.removeFromParent() - completion?() - } - } - - /// Moves the position to the specified position. - /// - Parameters: - /// - to: Pass a FloatingPanelPosition value to move the surface view to the position. - /// - animated: Pass true to animate the presentation; otherwise, pass false. - /// - completion: The block to execute after the view controller is dismissed. This block has no return value and takes no parameters. You may specify nil for this parameter. - public func move(to: FloatingPanelPosition, animated: Bool, completion: (() -> Void)? = nil) { - floatingPanel.move(to: to, animated: animated, completion: completion) - } - - /// Presents the specified view controller as the content view controller in the surface view interface. - public override func show(_ vc: UIViewController, sender: Any?) { - let surfaceView = self.view as! FloatingPanelSurfaceView - surfaceView.contentView.addSubview(vc.view) - vc.view.frame = surfaceView.contentView.bounds - vc.view.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate([ - vc.view.topAnchor.constraint(equalTo: surfaceView.contentView.topAnchor, constant: 0.0), - vc.view.leftAnchor.constraint(equalTo: surfaceView.contentView.leftAnchor, constant: 0.0), - vc.view.rightAnchor.constraint(equalTo: surfaceView.contentView.rightAnchor, constant: 0.0), - vc.view.bottomAnchor.constraint(equalTo: surfaceView.contentView.bottomAnchor, constant: 0.0), - ]) - addChild(vc) - vc.didMove(toParent: self) - } - - // MARK: - Scroll view tracking - - /// Tracks the specified scroll view to correspond with the scroll. - /// - /// - Attention: - /// The specified scroll view must be already assigned the delegate property because the controller intemediates the several delegate methods. - /// - public func track(scrollView: UIScrollView) { - floatingPanel.scrollView = scrollView - floatingPanel.userScrollViewDelegate = scrollView.delegate - scrollView.delegate = floatingPanel - switch contentInsetAdjustmentBehavior { - case .always: - if #available(iOS 11.0, *) { - scrollView.contentInsetAdjustmentBehavior = .never - } else { - children.forEach { (vc) in - vc.automaticallyAdjustsScrollViewInsets = false - } - } - default: - break - } - } - - // MARK: - Helpers - - /// Returns the y-coordinate of the point at the origin of the surface view - public func originYOfSurface(for pos: FloatingPanelPosition) -> CGFloat { - switch pos { - case .full: - return floatingPanel.layoutAdapter.topY - case .half: - return floatingPanel.layoutAdapter.middleY - case .tip: - return floatingPanel.layoutAdapter.bottomY - } - } -} diff --git a/Framework/Sources/FloatingPanelLayout.swift b/Framework/Sources/FloatingPanelLayout.swift deleted file mode 100644 index c5fee0bf..00000000 --- a/Framework/Sources/FloatingPanelLayout.swift +++ /dev/null @@ -1,243 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/09/27. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -public protocol FloatingPanelLayout: class { - /// Returns the initial position of a floating panel - var initialPosition: FloatingPanelPosition { get } - /// Returns an array of FloatingPanelPosition object to tell the applicable position the floating panel controller - var supportedPositions: [FloatingPanelPosition] { get } - - /// Return the interaction buffer of full position. Default is 6.0. - var topInteractionBuffer: CGFloat { get } - /// Return the interaction buffer of full position. Default is 6.0. - var bottomInteractionBuffer: CGFloat { get } - - /// Returns a CGFloat value for a floating panel position(full, half, tip). - /// A value for full position indicates an inset from the safe area top. - /// On the other hand, values fro half and tip positions indicate insets from the safe area bottom. - /// If a position doesn't contain the supported positions, return nil. - func insetFor(position: FloatingPanelPosition) -> CGFloat? - /// Returns layout constraints for a surface view of a floaitng panel. - /// The layout constraints must not include ones for topAnchor and bottomAnchor - /// because constarints for them will be added by the floating panel controller. - func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] - - /// Return the backdrop alpha of black color in full position. Default is 0.3. - var backdropAlpha: CGFloat { get } -} - -public extension FloatingPanelLayout { - var backdropAlpha: CGFloat { return 0.3 } - var topInteractionBuffer: CGFloat { return 6.0 } - var bottomInteractionBuffer: CGFloat { return 6.0 } -} - -public class FloatingPanelDefaultLayout: FloatingPanelLayout { - public var supportedPositions: [FloatingPanelPosition] { - return [.full, .half, .tip] - } - - public var initialPosition: FloatingPanelPosition { - return .half - } - - public func insetFor(position: FloatingPanelPosition) -> CGFloat? { - switch position { - case .full: return 18.0 - case .half: return 262.0 - case .tip: return 69.0 - } - } - - public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { - return [ - surfaceView.leftAnchor.constraint(equalTo: view.sideLayoutGuide.leftAnchor, constant: 0.0), - surfaceView.rightAnchor.constraint(equalTo: view.sideLayoutGuide.rightAnchor, constant: 0.0), - ] - } -} - -public class FloatingPanelDefaultLandscapeLayout: FloatingPanelLayout { - public var initialPosition: FloatingPanelPosition { - return .tip - } - public var supportedPositions: [FloatingPanelPosition] { - return [.full, .tip] - } - - public func insetFor(position: FloatingPanelPosition) -> CGFloat? { - switch position { - case .full: return 16.0 - case .tip: return 69.0 - default: return nil - } - } - - public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { - return [ - surfaceView.leftAnchor.constraint(equalTo: view.sideLayoutGuide.leftAnchor, constant: 8.0), - surfaceView.widthAnchor.constraint(equalToConstant: 291), - ] - } -} - - -class FloatingPanelLayoutAdapter { - private weak var surfaceView: FloatingPanelSurfaceView! - private weak var backdropVIew: FloatingPanelBackdropView! - - var layout: FloatingPanelLayout - - var safeAreaInsets: UIEdgeInsets = .zero - - private var heightBuffer: CGFloat = 88.0 // For bounce - private var fixedConstraints: [NSLayoutConstraint] = [] - private var fullConstraints: [NSLayoutConstraint] = [] - private var halfConstraints: [NSLayoutConstraint] = [] - private var tipConstraints: [NSLayoutConstraint] = [] - private var offConstraints: [NSLayoutConstraint] = [] - private var heightConstraints: NSLayoutConstraint? = nil - - var topInset: CGFloat { - return layout.insetFor(position: .full) ?? 0.0 - } - var halfInset: CGFloat { - return layout.insetFor(position: .half) ?? 0.0 - } - var tipInset: CGFloat { - return layout.insetFor(position: .tip) ?? 0.0 - } - - var topY: CGFloat { - return (safeAreaInsets.top + topInset) - } - - var middleY: CGFloat { - return surfaceView.superview!.bounds.height - (safeAreaInsets.bottom + halfInset) - } - - var bottomY: CGFloat { - return surfaceView.superview!.bounds.height - (safeAreaInsets.bottom + tipInset) - } - - var adjustedContentInsets: UIEdgeInsets { - return UIEdgeInsets(top: 0.0, - left: 0.0, - bottom: (safeAreaInsets.top + topInset) + (heightBuffer + safeAreaInsets.bottom), - right: 0.0) - } - - func positionY(for pos: FloatingPanelPosition) -> CGFloat { - switch pos { - case .full: - return topY - case .half: - return middleY - case .tip: - return bottomY - } - } - - init(surfaceView: FloatingPanelSurfaceView, backdropView: FloatingPanelBackdropView, layout: FloatingPanelLayout) { - self.layout = layout - self.surfaceView = surfaceView - self.backdropVIew = backdropView - - // Verify layout configurations - assert(layout.supportedPositions.count > 1) - assert(layout.supportedPositions.contains(layout.initialPosition)) - if halfInset > 0 { - assert(halfInset >= tipInset) - } - } - - func prepareLayout(toParent parent: UIViewController) { - surfaceView.translatesAutoresizingMaskIntoConstraints = false - backdropVIew.translatesAutoresizingMaskIntoConstraints = false - - NSLayoutConstraint.deactivate(fixedConstraints + fullConstraints + halfConstraints + tipConstraints + offConstraints) - - // Fixed constraints of surface and backdrop views - let surfaceConstraints = layout.prepareLayout(surfaceView: surfaceView, in: parent.view!) - let backdroptConstraints = [ - backdropVIew.topAnchor.constraint(equalTo: parent.view.topAnchor, - constant: 0.0), - backdropVIew.leftAnchor.constraint(equalTo: parent.view.leftAnchor, - constant: 0.0), - backdropVIew.rightAnchor.constraint(equalTo: parent.view.rightAnchor, - constant: 0.0), - backdropVIew.bottomAnchor.constraint(equalTo: parent.view.bottomAnchor, - constant: 0.0), - ] - fixedConstraints = surfaceConstraints + backdroptConstraints - - // Flexible surface constarints for full, half, tip and off - fullConstraints = [ - surfaceView.topAnchor.constraint(equalTo: parent.layoutGuide.topAnchor, - constant: topInset), - ] - halfConstraints = [ - surfaceView.topAnchor.constraint(equalTo: parent.layoutGuide.bottomAnchor, - constant: -halfInset), - ] - tipConstraints = [ - surfaceView.topAnchor.constraint(equalTo: parent.layoutGuide.bottomAnchor, - constant: -tipInset), - ] - offConstraints = [ - surfaceView.topAnchor.constraint(equalTo: parent.layoutGuide.bottomAnchor, constant: 0.0), - ] - } - - // The method is separated from prepareLayout(to:) for the rotation support - // It must be called in FloatingPanelController.traitCollectionDidChange(_:) - func updateHeight() { - defer { - UIView.performWithoutAnimation { - surfaceView.superview!.layoutIfNeeded() - } - } - - if let heightConstraints = self.heightConstraints { - NSLayoutConstraint.deactivate([heightConstraints]) - } - let heightConstraints = surfaceView.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height + heightBuffer) - NSLayoutConstraint.activate([heightConstraints]) - self.heightConstraints = heightConstraints - } - - func activateLayout(of state: FloatingPanelPosition?) { - defer { - surfaceView.superview!.layoutIfNeeded() - } - - NSLayoutConstraint.activate(fixedConstraints) - - guard var state = state else { - NSLayoutConstraint.deactivate(fullConstraints + halfConstraints + tipConstraints) - NSLayoutConstraint.activate(offConstraints) - return - } - - if layout.supportedPositions.contains(state) == false { - state = layout.initialPosition - } - - NSLayoutConstraint.deactivate(fullConstraints + halfConstraints + tipConstraints + offConstraints) - switch state { - case .full: - NSLayoutConstraint.deactivate(halfConstraints + tipConstraints + offConstraints) - NSLayoutConstraint.activate(fullConstraints) - case .half: - NSLayoutConstraint.deactivate(fullConstraints + tipConstraints + offConstraints) - NSLayoutConstraint.activate(halfConstraints) - case .tip: - NSLayoutConstraint.deactivate(fullConstraints + halfConstraints + offConstraints) - NSLayoutConstraint.activate(tipConstraints) - } - } -} diff --git a/Framework/Sources/FloatingPanelSurfaceView.swift b/Framework/Sources/FloatingPanelSurfaceView.swift deleted file mode 100644 index 4a6c6180..00000000 --- a/Framework/Sources/FloatingPanelSurfaceView.swift +++ /dev/null @@ -1,137 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/09/26. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -class FloatingPanelSurfaceContentView: UIView {} - -/// A view that presents a surface interface in a floating panel. -public class FloatingPanelSurfaceView: UIView { - - /// A GrabberHandleView object displayed at the top of the surface view - public var grabberHandle: GrabberHandleView! - - /// The height of the grabber bar area - public static var topGrabberBarHeight: CGFloat { - return Default.grabberTopPadding * 2 + GrabberHandleView.Default.height // 17.0 - } - - /// A UIView object that can have the surface view added to it. - public var contentView: UIView! - - private var color: UIColor? = .white { didSet { setNeedsDisplay() } } - - public override var backgroundColor: UIColor? { - get { return color } - set { - color = newValue - setNeedsDisplay() - } - } - - /// The radius to use when drawing rounded corners - public var cornerRadius: CGFloat = 0.0 { didSet { setNeedsLayout() } } - - /// A Boolean indicating whether the surface shadow is displayed. - public var shadowHidden: Bool = false { didSet { setNeedsLayout() } } - - /// The color of the surface shadow. - public var shadowColor: UIColor = .black { didSet { setNeedsLayout() } } - - /// The offset (in points) of the surface shadow. - public var shadowOffset: CGSize = CGSize(width: 0.0, height: 1.0) { didSet { setNeedsLayout() } } - - /// The opacity of the surface shadow. - public var shadowOpacity: Float = 0.2 { didSet { setNeedsLayout() } } - - /// The blur radius (in points) used to render the surface shadow. - public var shadowRadius: CGFloat = 3 { didSet { setNeedsLayout() } } - - /// The width of the surface border. - public var borderColor: UIColor? { didSet { setNeedsLayout() } } - - /// The color of the surface border. - public var borderWidth: CGFloat = 0.0 { didSet { setNeedsLayout() } } - - private var shadowLayer: CAShapeLayer! { didSet { setNeedsLayout() } } - - private struct Default { - public static let grabberTopPadding: CGFloat = 6.0 - } - - override init(frame: CGRect) { - super.init(frame: frame) - render() - } - - required init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - render() - } - - private func render() { - super.backgroundColor = .clear - - let contentView = FloatingPanelSurfaceContentView() - addSubview(contentView) - self.contentView = contentView as UIView - // contentView.backgroundColor = .lightGray - contentView.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate([ - contentView.topAnchor.constraint(equalTo: topAnchor, constant: 0.0), - contentView.leftAnchor.constraint(equalTo: leftAnchor, constant: 0.0), - contentView.rightAnchor.constraint(equalTo: rightAnchor, constant: 0.0), - contentView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0), - ]) - - let grabberHandle = GrabberHandleView() - addSubview(grabberHandle) - self.grabberHandle = grabberHandle - - grabberHandle.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate([ - grabberHandle.topAnchor.constraint(equalTo: topAnchor, constant: Default.grabberTopPadding), - grabberHandle.widthAnchor.constraint(equalToConstant: grabberHandle.frame.width), - grabberHandle.heightAnchor.constraint(equalToConstant: grabberHandle.frame.height), - grabberHandle.centerXAnchor.constraint(equalTo: centerXAnchor), - ]) - } - - public override func layoutSubviews() { - super.layoutSubviews() - updateShadowLayer() - // Don't use `contentView.layer.mask` because of UIVisualEffectView issue on ios10, https://forums.developer.apple.com/thread/50854 - contentView.layer.cornerRadius = cornerRadius - contentView.clipsToBounds = true - contentView.layer.borderColor = borderColor?.cgColor - contentView.layer.borderWidth = borderWidth - } - - private func updateShadowLayer() { - if shadowLayer != nil { - shadowLayer.removeFromSuperlayer() - } - shadowLayer = makeShadowLayer() - layer.insertSublayer(shadowLayer, at: 0) - } - - private func makeShadowLayer() -> CAShapeLayer { - log.debug("SurfaceView bounds", bounds) - let shadowLayer = CAShapeLayer() - let path = UIBezierPath(roundedRect: bounds, - byRoundingCorners: [.topLeft, .topRight], - cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)) - shadowLayer.path = path.cgPath - shadowLayer.fillColor = color?.cgColor - if shadowHidden == false { - shadowLayer.shadowPath = shadowLayer.path - shadowLayer.shadowColor = shadowColor.cgColor - shadowLayer.shadowOffset = shadowOffset - shadowLayer.shadowOpacity = shadowOpacity - shadowLayer.shadowRadius = shadowRadius - } - return shadowLayer - } -} diff --git a/Framework/Sources/GrabberHandleView.swift b/Framework/Sources/GrabberHandleView.swift deleted file mode 100644 index 214121c1..00000000 --- a/Framework/Sources/GrabberHandleView.swift +++ /dev/null @@ -1,31 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/09/19. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -public class GrabberHandleView: UIView { - public struct Default { - public static let width: CGFloat = 36.0 - public static let height: CGFloat = 5.0 - public static let barColor = UIColor(displayP3Red: 0.76, green: 0.77, blue: 0.76, alpha: 1.0) - } - - required init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - render() - } - - init() { - let size = CGSize(width: Default.width, - height: Default.height) - super.init(frame: CGRect(origin: .zero, size: size)) - self.backgroundColor = Default.barColor - render() - } - private func render() { - self.layer.masksToBounds = true - self.layer.cornerRadius = frame.size.height * 0.5 - } -} diff --git a/Framework/Sources/Logger.swift b/Framework/Sources/Logger.swift deleted file mode 100644 index 85989d81..00000000 --- a/Framework/Sources/Logger.swift +++ /dev/null @@ -1,106 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/10/09. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import Foundation -import os.log - -var log = { - return Logger() -}() - -struct Logger { - private let osLog: OSLog - private let s = DispatchSemaphore(value: 1) - - enum Level: Int, Comparable { - case debug = 0 - case info = 1 - case warning = 2 - case error = 3 - case fault = 4 - - var name: String { - switch self { - case .debug: return "DEBUG" - case .info: return "INFO" - case .warning: return "WARNING" - case .error: return "ERROR" - case .fault: return "FAULT" - } - } - var shortName: String { - switch self { - case .debug: - return "D/" - case .info: - return "I/" - case .warning: - return "W/" - case .error: - return "E/" - case .fault: - return "F/" - } - } - @available(iOS 10.0, *) - var osLogType: OSLogType { - switch self { - case .debug: return .debug - case .info: return .info - case .warning: return .info - case .error: return .error - case .fault: return .fault - } - } - - public static func < (lhs: Logger.Level, rhs: Logger.Level) -> Bool { - return lhs.rawValue < rhs.rawValue - } - } - - init() { - osLog = OSLog(subsystem: "com.scenee.FloatingPanel", category: "FloatingPanel") - } - - private func log(_ level: Level, _ message: Any, _ arguments: [Any], function: String, line: UInt) { - #if __FP_LOG - _ = s.wait(timeout: .now() + 0.033) - defer { s.signal() } - - let extraMessage: String = arguments.map({ String(describing: $0) }).joined(separator: " ") - let log = "\(level.shortName) \(message) \(extraMessage) (\(function):\(line))" - - os_log("%@", log: osLog, type: level.osLogType, log) - #endif - } - - private func getPrettyFunction(_ function: String, _ file: String) -> String { - if let filename = file.split(separator: "/").last { - return filename + ":" + function - } else { - return file + ":" + function - } - } - - func debug(_ log: Any, _ arguments: Any..., function: String = #function, file: String = #file, line: UInt = #line) { - self.log(.debug, log, arguments, function: getPrettyFunction(function, file), line: line) - } - - func info(_ log: Any, _ arguments: Any..., function: String = #function, file: String = #file, line: UInt = #line) { - self.log(.info, log, arguments, function: getPrettyFunction(function, file), line: line) - } - - func warning(_ log: Any, _ arguments: Any..., function: String = #function, file: String = #file, line: UInt = #line) { - self.log(.warning, log, arguments, function: getPrettyFunction(function, file), line: line) - } - - func error(_ log: Any, _ arguments: Any..., function: String = #function, file: String = #file, line: UInt = #line) { - self.log(.error, log, arguments, function: getPrettyFunction(function, file), line: line) - } - - func fault(_ log: Any, _ arguments: Any..., function: String = #function, file: String = #file, line: UInt = #line) { - self.log(.fault, log, arguments, function: getPrettyFunction(function, file), line: line) - } -} diff --git a/Framework/Sources/UIExtensions.swift b/Framework/Sources/UIExtensions.swift deleted file mode 100644 index 50084230..00000000 --- a/Framework/Sources/UIExtensions.swift +++ /dev/null @@ -1,89 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/09/18. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import UIKit - -protocol LayoutGuideProvider { - var topAnchor: NSLayoutYAxisAnchor { get } - var bottomAnchor: NSLayoutYAxisAnchor { get } -} -extension UILayoutGuide: LayoutGuideProvider {} - -class CustomLayoutGuide: LayoutGuideProvider { - let topAnchor: NSLayoutYAxisAnchor - let bottomAnchor: NSLayoutYAxisAnchor - init(topAnchor: NSLayoutYAxisAnchor, bottomAnchor: NSLayoutYAxisAnchor) { - self.topAnchor = topAnchor - self.bottomAnchor = bottomAnchor - } -} - -extension UIViewController { - var layoutInsets: UIEdgeInsets { - if #available(iOS 11.0, *) { - return view.safeAreaInsets - } else { - return UIEdgeInsets(top: topLayoutGuide.length, - left: 0.0, - bottom: bottomLayoutGuide.length, - right: 0.0) - } - } - - var layoutGuide: LayoutGuideProvider { - if #available(iOS 11.0, *) { - return view!.safeAreaLayoutGuide - } else { - return CustomLayoutGuide(topAnchor: topLayoutGuide.bottomAnchor, - bottomAnchor: bottomLayoutGuide.topAnchor) - } - } -} - -protocol SideLayoutGuideProvider { - var leftAnchor: NSLayoutXAxisAnchor { get } - var rightAnchor: NSLayoutXAxisAnchor { get } -} - -extension UIView: SideLayoutGuideProvider {} -extension UILayoutGuide: SideLayoutGuideProvider {} - -extension UIView { - var sideLayoutGuide: SideLayoutGuideProvider { - if #available(iOS 11.0, *) { - return safeAreaLayoutGuide - } else { - return self - } - } -} - -extension UIGestureRecognizer.State: CustomDebugStringConvertible { - public var debugDescription: String { - switch self { - case .began: return "Began" - case .changed: return "Changed" - case .failed: return "Failed" - case .cancelled: return "Cancelled" - case .ended: return "Endeded" - case .possible: return "Possible" - } - } -} - -extension UIScrollView { - var contentOffsetZero: CGPoint { - return CGPoint(x: 0.0, y: 0.0 - contentInset.top) - } -} - -extension UISpringTimingParameters { - public convenience init(dampingRatio: CGFloat, frequencyResponse: CGFloat, initialVelocity: CGVector = .zero) { - let mass = 1 as CGFloat - let stiffness = pow(2 * .pi / frequencyResponse, 2) * mass - let damp = 4 * .pi * dampingRatio * mass / frequencyResponse - self.init(mass: mass, stiffness: stiffness, damping: damp, initialVelocity: initialVelocity) - } -} diff --git a/Framework/Tests/ViewTests.swift b/Framework/Tests/ViewTests.swift deleted file mode 100644 index 674f63bc..00000000 --- a/Framework/Tests/ViewTests.swift +++ /dev/null @@ -1,14 +0,0 @@ -// -// Created by Shin Yamamoto on 2018/09/18. -// Copyright © 2018 Shin Yamamoto. All rights reserved. -// - -import XCTest -@testable import FloatingPanelController - -class ViewTests: XCTestCase { - - override func setUp() {} - - override func tearDown() {} -} diff --git a/LICENSE b/LICENSE index b0248d13..860fc6fe 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Shin Yamamoto +Copyright (c) 2018-Present Shin Yamamoto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/Package.swift b/Package.swift new file mode 100644 index 00000000..ccfbb5fe --- /dev/null +++ b/Package.swift @@ -0,0 +1,27 @@ +// swift-tools-version:5.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "FloatingPanel", + platforms: [ + .iOS(.v10) + ], + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "FloatingPanel", + targets: ["FloatingPanel"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target(name: "FloatingPanel", path: "Sources"), + ], + swiftLanguageVersions: [.version("5")] +) diff --git a/README.md b/README.md index de4d47fd..52e9c138 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,101 @@ -# FloatingPanel +[![Build Status](https://travis-ci.org/SCENEE/FloatingPanel.svg?branch=master)](https://travis-ci.org/SCENEE/FloatingPanel) +[![Version](https://img.shields.io/cocoapods/v/FloatingPanel.svg)](https://cocoapods.org/pods/FloatingPanel) +[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) +[![Platform](https://img.shields.io/cocoapods/p/FloatingPanel.svg)](https://cocoapods.org/pods/FloatingPanel) +[![Swift 5](https://img.shields.io/badge/Swift-5-orange.svg?style=flat)](https://swift.org/) + +# FloatingPanel FloatingPanel is a simple and easy-to-use UI component for a new interface introduced in Apple Maps, Shortcuts and Stocks app. The new interface displays the related contents and utilities in parallel as a user wants. +📝[Here](https://docs.scenee.com/documentation/floatingpanel) is the API references for the latest version powered by [DocC](https://developer.apple.com/documentation/docc). + ![Maps](https://github.com/SCENEE/FloatingPanel/blob/master/assets/maps.gif) ![Stocks](https://github.com/SCENEE/FloatingPanel/blob/master/assets/stocks.gif) ![Maps(Landscape)](https://github.com/SCENEE/FloatingPanel/blob/master/assets/maps-landscape.gif) + + +- [Features](#features) +- [Requirements](#requirements) +- [Installation](#installation) + - [CocoaPods](#cocoapods) + - [Carthage](#carthage) + - [Swift Package Manager](#swift-package-manager) +- [Getting Started](#getting-started) + - [Add a floating panel as a child view controller](#add-a-floating-panel-as-a-child-view-controller) + - [Present a floating panel as a modality](#present-a-floating-panel-as-a-modality) +- [View hierarchy](#view-hierarchy) +- [Usage](#usage) + - [Show/Hide a floating panel in a view with your view hierarchy](#showhide-a-floating-panel-in-a-view-with-your-view-hierarchy) + - [Scale the content view when the surface position changes](#scale-the-content-view-when-the-surface-position-changes) + - [Customize the layout with `FloatingPanelLayout` protocol](#customize-the-layout-with-floatingpanellayout-protocol) + - [Change the initial layout](#change-the-initial-layout) + - [Update your panel layout](#update-your-panel-layout) + - [Support your landscape layout](#support-your-landscape-layout) + - [Use the intrinsic size of a content in your panel layout](#use-the-intrinsic-size-of-a-content-in-your-panel-layout) + - [Specify an anchor for each state by an inset of the `FloatingPanelController.view` frame](#specify-an-anchor-for-each-state-by-an-inset-of-the-floatingpanelcontrollerview-frame) + - [Change the backdrop alpha](#change-the-backdrop-alpha) + - [Using custome panel states](#using-custome-panel-states) + - [Customize the behavior with `FloatingPanelBehavior` protocol](#customize-the-behavior-with-floatingpanelbehavior-protocol) + - [Modify your floating panel's interaction](#modify-your-floating-panels-interaction) + - [Activate the rubber-band effect on panel edges](#activate-the-rubber-band-effect-on-panel-edges) + - [Manage the projection of a pan gesture momentum](#manage-the-projection-of-a-pan-gesture-momentum) + - [Specify the panel move's boundary](#specify-the-panel-moves-boundary) + - [Customize the surface design](#customize-the-surface-design) + - [Modify your surface appearance](#modify-your-surface-appearance) + - [Use a custom grabber handle](#use-a-custom-grabber-handle) + - [Customize layout of the grabber handle](#customize-layout-of-the-grabber-handle) + - [Customize content padding from surface edges](#customize-content-padding-from-surface-edges) + - [Customize margins of the surface edges](#customize-margins-of-the-surface-edges) + - [Customize gestures](#customize-gestures) + - [Suppress the panel interaction](#suppress-the-panel-interaction) + - [Add tap gestures to the surface view](#add-tap-gestures-to-the-surface-view) + - [Interrupt the delegate methods of `FloatingPanelController.panGestureRecognizer`](#interrupt-the-delegate-methods-of-floatingpanelcontrollerpangesturerecognizer) + - [Create an additional floating panel for a detail](#create-an-additional-floating-panel-for-a-detail) + - [Move a position with an animation](#move-a-position-with-an-animation) + - [Work your contents together with a floating panel behavior](#work-your-contents-together-with-a-floating-panel-behavior) + - [Enabling the tap-to-dismiss action of the backdrop view](#enabling-the-tap-to-dismiss-action-of-the-backdrop-view) +- [Notes](#notes) + - ['Show' or 'Show Detail' Segues from `FloatingPanelController`'s content view controller](#show-or-show-detail-segues-from-floatingpanelcontrollers-content-view-controller) + - [UISearchController issue](#uisearchcontroller-issue) + - [FloatingPanelSurfaceView's issue on iOS 10](#floatingpanelsurfaceviews-issue-on-ios-10) +- [Maintainer](#maintainer) +- [License](#license) + + + ## Features - [x] Simple container view controller -- [x] Fluid animation and gesture handling +- [x] Fluid behavior using numeric springing - [x] Scroll view tracking -- [x] Common UI elements: Grabber handle, Backdrop and Surface rounding corners -- [x] 2 or 3 anchor positions(full, half, tip) -- [x] Layout customization for all trait environments(i.e. Landscape orientation support) -- [x] Behavior customization +- [x] Removal interaction +- [x] Multi panel support +- [x] Modal presentation +- [x] 4 positioning support(top, left, bottom, right) +- [x] 1 or more magnetic anchors(full, half, tip and more) +- [x] Layout support for all trait environments(i.e. Landscape orientation) +- [x] Common UI elements: surface, backdrop and grabber handle - [x] Free from common issues of Auto Layout and gesture handling +- [x] Compatible with Objective-C Examples are here. - [Examples/Maps](https://github.com/SCENEE/FloatingPanel/tree/master/Examples/Maps) like Apple Maps.app. - [Examples/Stocks](https://github.com/SCENEE/FloatingPanel/tree/master/Examples/Stocks) like Apple Stocks.app. +- [Examples/Samples](https://github.com/SCENEE/FloatingPanel/tree/master/Examples/Samples) +- [Examples/SamplesObjC](https://github.com/SCENEE/FloatingPanel/tree/master/Examples/SamplesObjC) ## Requirements -FloatingPanel is written in Swift 4.2. Compatible with iOS 10.0+ +FloatingPanel is written in Swift 5.0+. Compatible with iOS 11.0+. + +The deployment is still iOS 10, but it is recommended to use this library on iOS 11+. + +:pencil2: You would like to use Swift 4.0. Please use FloatingPanel v1. ## Installation @@ -39,6 +108,8 @@ it, simply add the following line to your Podfile: pod 'FloatingPanel' ``` +:pencil2: FloatingPanel v1.7.0 or later requires CocoaPods v1.7.0+ for `swift_versions` support. + ### Carthage For [Carthage](https://github.com/Carthage/Carthage), add the following to your `Cartfile`: @@ -47,9 +118,14 @@ For [Carthage](https://github.com/Carthage/Carthage), add the following to your github "scenee/FloatingPanel" ``` +### Swift Package Manager + +Follow [this doc](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app). ## Getting Started +### Add a floating panel as a child view controller + ```swift import UIKit import FloatingPanel @@ -65,160 +141,586 @@ class ViewController: UIViewController, FloatingPanelControllerDelegate { // Assign self as the delegate of the controller. fpc.delegate = self // Optional - // Add a content view controller. + // Set a content view controller. let contentVC = ContentViewController() - fpc.show(contentVC, sender: nil) + fpc.set(contentViewController: contentVC) // Track a scroll view(or the siblings) in the content view controller. fpc.track(scrollView: contentVC.tableView) - // Add the views managed by the `FloatingPanelController` object to self.view. + // Add and show the views managed by the `FloatingPanelController` object to self.view. fpc.addPanel(toParent: self) } - - override func viewWillDisappear(_ animated: Bool) { - super.viewWillDisappear(animated) - // Remove the views managed by the `FloatingPanelController` object from self.view. - fpc.removePanelFromParent() - } - ... } ``` +### Present a floating panel as a modality + +```swift +let fpc = FloatingPanelController() +let contentVC = ... +fpc.set(contentViewController: contentVC) + +fpc.isRemovalInteractionEnabled = true // Optional: Let it removable by a swipe-down + +self.present(fpc, animated: true, completion: nil) +``` + +You can show a floating panel over UINavigationController from the container view controllers as a modality of `.overCurrentContext` style. + +:pencil2: FloatingPanelController has the custom presentation controller. If you would like to customize the presentation/dismissal, please see [Transitioning](https://github.com/SCENEE/FloatingPanel/blob/master/Sources/Transitioning.swift). + +## View hierarchy + +`FloatingPanelController` manages the views as the following view hierarchy. + +``` +FloatingPanelController.view (FloatingPanelPassThroughView) + ├─ .backdropView (FloatingPanelBackdropView) + └─ .surfaceView (FloatingPanelSurfaceView) + ├─ .containerView (UIView) + │ └─ .contentView (FloatingPanelController.contentViewController.view) + └─ .grabber (FloatingPanelGrabberView) +``` + ## Usage -### Move a positon with an animation +### Show/Hide a floating panel in a view with your view hierarchy -Move a floating panel to the top and middle of a view while opening and closeing a search bar like Apple Maps. +If you need more control over showing and hiding the floating panel, you can forgo the `addPanel` and `removePanelFromParent` methods. These methods are a convenience wrapper for **FloatingPanel**'s `show` and `hide` methods along with some required setup. + +There are two ways to work with the `FloatingPanelController`: +1. Add it to the hierarchy once and then call `show` and `hide` methods to make it appear/disappear. +2. Add it to the hierarchy when needed and remove afterwards. + +The following example shows how to add the controller to your `UIViewController` and how to remove it. Make sure that you never add the same `FloatingPanelController` to the hierarchy before removing it. + +**NOTE**: `self.` prefix is not required, nor recommended. It's used here to make it clearer where do the functions used come from. `self` is an instance of a custom UIViewController in your code. ```swift - func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { - ... - fpc.move(to: .half, animated: true) - } +// Add the floating panel view to the controller's view on top of other views. +self.view.addSubview(fpc.view) + +// REQUIRED. It makes the floating panel view have the same size as the controller's view. +fpc.view.frame = self.view.bounds + +// In addition, Auto Layout constraints are highly recommended. +// Constraint the fpc.view to all four edges of your controller's view. +// It makes the layout more robust on trait collection change. +fpc.view.translatesAutoresizingMaskIntoConstraints = false +NSLayoutConstraint.activate([ + fpc.view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0.0), + fpc.view.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0.0), + fpc.view.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0.0), + fpc.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0.0), +]) + +// Add the floating panel controller to the controller hierarchy. +self.addChild(fpc) + +// Show the floating panel at the initial position defined in your `FloatingPanelLayout` object. +fpc.show(animated: true) { + // Inform the floating panel controller that the transition to the controller hierarchy has completed. + fpc.didMove(toParent: self) +} +``` - func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { - ... - fpc.move(to: .full, animated: true) +After you add the `FloatingPanelController` as seen above, you can call `fpc.show(animated: true) { }` to show the panel and `fpc.hide(animated: true) { }` to hide it. + +To remove the `FloatingPanelController` from the hierarchy, follow the example below. + +```swift +// Inform the panel controller that it will be removed from the hierarchy. +fpc.willMove(toParent: nil) + +// Hide the floating panel. +fpc.hide(animated: true) { + // Remove the floating panel view from your controller's view. + fpc.view.removeFromSuperview() + // Remove the floating panel controller from the controller hierarchy. + fpc.removeFromParent() +} +``` + +### Scale the content view when the surface position changes + +Specify the `contentMode` to `.fitToBounds` if the surface height fits the bounds of `FloatingPanelController.view` when the surface position changes + +```swift +fpc.contentMode = .fitToBounds +``` + +Otherwise, `FloatingPanelController` fixes the content by the height of the top most position. + +:pencil2: In `.fitToBounds` mode, the surface height changes as following a user interaction so that you have a responsibility to configure Auto Layout constrains not to break the layout of a content view by the elastic surface height. + +### Customize the layout with `FloatingPanelLayout` protocol + +#### Change the initial layout + +```swift +class ViewController: UIViewController, FloatingPanelControllerDelegate { + ... { + fpc = FloatingPanelController(delegate: self) + fpc.layout = MyFloatingPanelLayout() } +} + +class MyFloatingPanelLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .tip + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 44.0, edge: .bottom, referenceGuide: .safeArea), + ] +} ``` -### Make your contents correspond with FloatingPanel behavior +### Update your panel layout + +There are 2 ways to update the panel layout. + +1. Manually set `FloatingPanelController.layout` to the new layout object directly. + +```swift +fpc.layout = MyPanelLayout() +fpc.invalidateLayout() // If needed +``` + +Note: If you already set the `delegate` property of your `FloatingPanelController` instance, `invalidateLayout()` overrides the layout object of `FloatingPanelController` with one returned by the delegate object. + +2. Returns an appropriate layout object in one of 2 `floatingPanel(_:layoutFor:)` delegates. ```swift class ViewController: UIViewController, FloatingPanelControllerDelegate { ... - func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) { - if vc.position == .full { - searchVC.searchBar.showsCancelButton = false - searchVC.searchBar.resignFirstResponder() - } + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + return MyFloatingPanelLayout() } - func floatingPanelDidEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetPosition: FloatingPanelPosition) { - if targetPosition != .full { - searchVC.hideHeader() - } - } - ... + // OR + func floatingPanel(_ vc: FloatingPanelController, layoutFor size: CGSize) -> FloatingPanelLayout { + return MyFloatingPanelLayout() + } } ``` -### Support your landscape layout with a `FloatingPanelLayout` object +#### Support your landscape layout ```swift class ViewController: UIViewController, FloatingPanelControllerDelegate { ... - func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? { - return (newCollection.verticalSizeClass == .compact) ? FloatingPanelLandscapeLayout() : nil + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + return (newCollection.verticalSizeClass == .compact) ? LandscapePanelLayout() : FloatingPanelBottomLayout() } - ... } -class FloatingPanelLandscapeLayout: FloatingPanelLayout { - public var initialPosition: FloatingPanelPosition { - return .tip - } - public var supportedPositions: [FloatingPanelPosition] { - return [.full, .tip] +class LandscapePanelLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .tip + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 69.0, edge: .bottom, referenceGuide: .safeArea), + ] + + func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { + return [ + surfaceView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 8.0), + surfaceView.widthAnchor.constraint(equalToConstant: 291), + ] } +} +``` + +#### Use the intrinsic size of a content in your panel layout + +1. Lay out your content View with the intrinsic height size. For example, see "Detail View Controller scene"/"Intrinsic View Controller scene" of [Main.storyboard](https://github.com/SCENEE/FloatingPanel/blob/master/Examples/Samples/Sources/Base.lproj/Main.storyboard). The 'Stack View.bottom' constraint determines the intrinsic height. +2. Specify layout anchors using `FloatingPanelIntrinsicLayoutAnchor`. + +```swift +class IntrinsicPanelLayout: FloatingPanelLayout { + let position: FloatingPanelPosition = .bottom + let initialState: FloatingPanelState = .full + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 0, referenceGuide: .safeArea), + .half: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.5, referenceGuide: .safeArea), + ] + ... +} +``` + +:pencil2: `FloatingPanelIntrinsicLayout` is deprecated on v1. + +#### Specify an anchor for each state by an inset of the `FloatingPanelController.view` frame + +Use `.superview` reference guide in your anchors. + +```swift +class MyFullScreenLayout: FloatingPanelLayout { + ... + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .superview), + .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .superview), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 44.0, edge: .bottom, referenceGuide: .superview), + ] +} +``` + +:pencil2: `FloatingPanelFullScreenLayout` is deprecated on v1. + +#### Change the backdrop alpha + +You can change the backdrop alpha by `FloatingPanelLayout.backdropAlpha(for:)` for each state(`.full`, `.half` and `.tip`). - public func insetFor(position: FloatingPanelPosition) -> CGFloat? { - switch position { - case .full: return 16.0 - case .tip: return 69.0 - default: return nil +For instance, if a panel seems like the backdrop view isn't there on `.half` state, it's time to implement the backdropAlpha API and return a value for the state as below. + +```swift +class MyPanelLayout: FloatingPanelLayout { + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + switch state { + case .full, .half: return 0.3 + default: return 0.0 } } +} +``` - public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { +#### Using custome panel states + +You're able to define custom panel states and use them as the following example. + +```swift +extension FloatingPanelState { + static let lastQuart: FloatingPanelState = FloatingPanelState(rawValue: "lastQuart", order: 750) + static let firstQuart: FloatingPanelState = FloatingPanelState(rawValue: "firstQuart", order: 250) +} + +class FloatingPanelLayoutWithCustomState: FloatingPanelBottomLayout { + override var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] { return [ - surfaceView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuid.leftAnchor, constant: 8.0), - surfaceView.widthAnchor.constraint(equalToConstant: 291), + .full: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .top, referenceGuide: .safeArea), + .lastQuart: FloatingPanelLayoutAnchor(fractionalInset: 0.75, edge: .bottom, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + .firstQuart: FloatingPanelLayoutAnchor(fractionalInset: 0.25, edge: .bottom, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 20.0, edge: .bottom, referenceGuide: .safeArea), ] } } ``` -### Modify your floating panel's interaction with a `FloatingPanelBehavior` object +### Customize the behavior with `FloatingPanelBehavior` protocol + +#### Modify your floating panel's interaction ```swift class ViewController: UIViewController, FloatingPanelControllerDelegate { ... - func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? { - return FloatingPanelStocksBehavior() + func viewDidLoad() { + ... + fpc.behavior = CustomPanelBehavior() + } +} + +class CustomPanelBehavior: FloatingPanelBehavior { + let springDecelerationRate = UIScrollView.DecelerationRate.fast.rawValue + 0.02 + let springResponseTime = 0.4 + func shouldProjectMomentum(_ fpc: FloatingPanelController, to proposedTargetPosition: FloatingPanelState) -> Bool { + return true } +} +``` + +:pencil2: `floatingPanel(_ vc:behaviorFor:)` is deprecated on v1. + +#### Activate the rubber-band effect on panel edges + +```swift +class MyPanelBehavior: FloatingPanelBehavior { ... + func allowsRubberBanding(for edge: UIRectEdge) -> Bool { + return true + } } -... +``` -class FloatingPanelStocksBehavior: FloatingPanelBehavior { - var velocityThreshold: CGFloat { - return 15.0 +#### Manage the projection of a pan gesture momentum + +This allows full projectional panel behavior. For example, a user can swipe up a panel from tip to full nearby the tip position. + +```swift +class MyPanelBehavior: FloatingPanelBehavior { + ... + func shouldProjectMomentum(_ fpc: FloatingPanelController, to proposedTargetPosition: FloatingPanelPosition) -> Bool { + return true } +} +``` + +### Specify the panel move's boundary - func interactionAnimator(to targetPosition: FloatingPanelPosition, with velocity: CGVector) -> UIViewPropertyAnimator { - let damping = self.damping(with: velocity) - let springTiming = UISpringTimingParameters(dampingRatio: damping, initialVelocity: velocity) - return UIViewPropertyAnimator(duration: 0.5, timingParameters: springTiming) +`FloatingPanelController.surfaceLocation` in `floatingPanelDidMove(_:)` delegate method behaves like `UIScrollView.contentOffset` in `scrollViewDidScroll(_:)`. +As a result, you can specify the boundary of a panel move as below. + +```swift +func floatingPanelDidMove(_ vc: FloatingPanelController) { + if vc.isAttracting == false { + let loc = vc.surfaceLocation + let minY = vc.surfaceLocation(for: .full).y - 6.0 + let maxY = vc.surfaceLocation(for: .tip).y + 6.0 + vc.surfaceLocation = CGPoint(x: loc.x, y: min(max(loc.y, minY), maxY)) } +} +``` + +:pencil2: `{top,bottom}InteractionBuffer` property is removed from `FloatingPanelLayout` since v2. + +### Customize the surface design + +#### Modify your surface appearance + +```swift +// Create a new appearance. +let appearance = SurfaceAppearance() + +// Define shadows +let shadow = SurfaceAppearance.Shadow() +shadow.color = UIColor.black +shadow.offset = CGSize(width: 0, height: 16) +shadow.radius = 16 +shadow.spread = 8 +appearance.shadows = [shadow] + +// Define corner radius and background color +appearance.cornerRadius = 8.0 +appearance.backgroundColor = .clear + +// Set the new appearance +fpc.surfaceView.appearance = appearance +```` + +#### Use a custom grabber handle + +```swift +let myGrabberHandleView = MyGrabberHandleView() +fpc.surfaceView.grabberHandle.isHidden = true +fpc.surfaceView.addSubview(myGrabberHandleView) +``` + +#### Customize layout of the grabber handle + +```swift +fpc.surfaceView.grabberHandlePadding = 10.0 +fpc.surfaceView.grabberHandleSize = .init(width: 44.0, height: 12.0) +``` + +:pencil2: Note that `grabberHandleSize` width and height are reversed in the left/right position. + +#### Customize content padding from surface edges + +```swift +fpc.surfaceView.contentPadding = .init(top: 20, left: 20, bottom: 20, right: 20) +``` + +#### Customize margins of the surface edges + +```swift +fpc.surfaceView.containerMargins = .init(top: 20.0, left: 16.0, bottom: 16.0, right: 16.0) +``` + +The feature can be used for these 2 kind panels + +* Facebook/Slack-like panel whose surface top edge is separated from the grabber handle. +* iOS native panel to display AirPods information, for example. + +### Customize gestures + +#### Suppress the panel interaction + +You can disable the pan gesture recognizer directly + +```swift +fpc.panGestureRecognizer.isEnabled = false +``` + +Or use this `FloatingPanelControllerDelegate` method. + +```swift +func floatingPanelShouldBeginDragging(_ vc: FloatingPanelController) -> Bool { + return aCondition ? false : true +} +``` + +#### Add tap gestures to the surface view + +```swift +override func viewDidLoad() { ... + let surfaceTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSurface(tapGesture:))) + fpc.surfaceView.addGestureRecognizer(surfaceTapGesture) + surfaceTapGesture.isEnabled = (fpc.position == .tip) +} + +// Enable `surfaceTapGesture` only at `tip` state +func floatingPanelDidChangeState(_ vc: FloatingPanelController) { + surfaceTapGesture.isEnabled = (vc.position == .tip) } ``` +#### Interrupt the delegate methods of `FloatingPanelController.panGestureRecognizer` + +If you are set `FloatingPanelController.panGestureRecognizer.delegateProxy` to an object adopting `UIGestureRecognizerDelegate`, it overrides delegate methods of the pan gesture recognizer. + +```swift +class MyGestureRecognizerDelegate: UIGestureRecognizerDelegate { + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return false + } +} + +class ViewController: UIViewController { + let myGestureDelegate = MyGestureRecognizerDelegate() + + func setUpFpc() { + .... + fpc.panGestureRecognizer.delegateProxy = myGestureDelegate + } +``` + ### Create an additional floating panel for a detail ```swift -class ViewController: UIViewController, FloatingPanelControllerDelegate { - var searchPanelVC: FloatingPanelController! - var detailPanelVC: FloatingPanelController! +override func viewDidLoad() { + // Setup Search panel + self.searchPanelVC = FloatingPanelController() - override func viewDidLoad() { - // Setup Search panel - self.searchPanelVC = FloatingPanelController() + let searchVC = SearchViewController() + self.searchPanelVC.set(contentViewController: searchVC) + self.searchPanelVC.track(scrollView: contentVC.tableView) - let searchVC = SearchViewController() - self.searchPanelVC.show(searchVC, sender: nil) - self.searchPanelVC.track(scrollView: contentVC.tableView) + self.searchPanelVC.addPanel(toParent: self) - self.searchPanelVC.addPanel(toParent: self) + // Setup Detail panel + self.detailPanelVC = FloatingPanelController() - // Setup Detail panel - self.detailPanelVC = FloatingPanelController() + let contentVC = ContentViewController() + self.detailPanelVC.set(contentViewController: contentVC) + self.detailPanelVC.track(scrollView: contentVC.scrollView) - let contentVC = ContentViewController() - self.detailPanelVC.show(contentVC, sender: nil) - self.detailPanelVC.track(scrollView: contentVC.scrollView) + self.detailPanelVC.addPanel(toParent: self) +} +``` + +### Move a position with an animation + +In the following example, I move a floating panel to full or half position while opening or closing a search bar like Apple Maps. + +```swift +func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { + ... + fpc.move(to: .half, animated: true) +} + +func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { + ... + fpc.move(to: .full, animated: true) +} +``` + +You can also use a view animation to move a panel. + +```swift +UIView.animate(withDuration: 0.25) { + self.fpc.move(to: .half, animated: false) +} +``` - self.detailPanelVC.addPanel(toParent: self) +### Work your contents together with a floating panel behavior + +```swift +class ViewController: UIViewController, FloatingPanelControllerDelegate { + ... + func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) { + if vc.position == .full { + searchVC.searchBar.showsCancelButton = false + searchVC.searchBar.resignFirstResponder() + } } + + func floatingPanelWillEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer) { + if targetState.pointee != .full { + searchVC.hideHeader() + } + } +} +``` + +### Enabling the tap-to-dismiss action of the backdrop view + +The tap-to-dismiss action is disabled by default. So it needs to be enabled as below. + +```swift +fpc.backdropView.dismissalTapGestureRecognizer.isEnabled = true +``` + +## Notes + +### 'Show' or 'Show Detail' Segues from `FloatingPanelController`'s content view controller + +'Show' or 'Show Detail' segues from a content view controller will be managed by a view controller(hereinafter called 'master VC') adding a floating panel. Because a floating panel is just a subview of the master VC(except for modality). + +`FloatingPanelController` has no way to manage a stack of view controllers like `UINavigationController`. If so, it would be so complicated and the interface will become `UINavigationController`. This component should not have the responsibility to manage the stack. + +By the way, a content view controller can present a view controller modally with `present(_:animated:completion:)` or 'Present Modally' segue. + +However, sometimes you want to show a destination view controller of 'Show' or 'Show Detail' segue with another floating panel. It's possible to override `show(_:sender)` of the master VC! + +Here is an example. + +```swift +class ViewController: UIViewController { + var fpc: FloatingPanelController! + var secondFpc: FloatingPanelController! + ... + override func show(_ vc: UIViewController, sender: Any?) { + secondFpc = FloatingPanelController() + + secondFpc.set(contentViewController: vc) + + secondFpc.addPanel(toParent: self) + } +} +``` + +A `FloatingPanelController` object proxies an action for `show(_:sender)` to the master VC. That's why the master VC can handle a destination view controller of a 'Show' or 'Show Detail' segue and you can hook `show(_:sender)` to show a secondary floating panel set the destination view controller to the content. + +It's a great way to decouple between a floating panel and the content VC. + +### UISearchController issue + +`UISearchController` isn't able to be used with `FloatingPanelController` by the system design. + +Because `UISearchController` automatically presents itself modally when a user interacts with the search bar, and then it swaps the superview of the search bar to the view managed by itself while it displays. As a result, `FloatingPanelController` can't control the search bar when it's active, as you can see from [the screen shot](https://github.com/SCENEE/FloatingPanel/issues/248#issuecomment-521263831). + +### FloatingPanelSurfaceView's issue on iOS 10 + +* On iOS 10, `FloatingPanelSurfaceView.cornerRadius` isn't not automatically masked with the top rounded corners because of `UIVisualEffectView` issue. See https://forums.developer.apple.com/thread/50854. +So you need to draw top rounding corners of your content. Here is an example in Examples/Maps. +```swift +override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + if #available(iOS 10, *) { + visualEffectView.layer.cornerRadius = 9.0 + visualEffectView.clipsToBounds = true + } } ``` +* If you sets clear color to `FloatingPanelSurfaceView.backgroundColor`, please note the bottom overflow of your content on bouncing at full position. To prevent it, you need to expand your content. For example, See Example/Maps App's Auto Layout settings of `UIVisualEffectView` in Main.storyboard. -## Author +## Maintainer -Shin Yamamoto +Shin Yamamoto | [@scenee](https://twitter.com/scenee) ## License diff --git a/Sources/BackdropView.swift b/Sources/BackdropView.swift new file mode 100644 index 00000000..21cfc19a --- /dev/null +++ b/Sources/BackdropView.swift @@ -0,0 +1,11 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// A view that presents a backdrop interface behind a panel. +@objc(FloatingPanelBackdropView) +public class BackdropView: UIView { + + /// The gesture recognizer for tap gestures to dismiss a panel. + @objc public var dismissalTapGestureRecognizer: UITapGestureRecognizer! +} diff --git a/Sources/Behavior.swift b/Sources/Behavior.swift new file mode 100644 index 00000000..903807e2 --- /dev/null +++ b/Sources/Behavior.swift @@ -0,0 +1,126 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// An interface for generating behavior information to fine-tune the behavior of a panel. +@objc +public protocol FloatingPanelBehavior { + /// A floating-point value that determines the rate of oscillation magnitude reduction after the user lifts their finger. + /// + /// The oscillation magnitude to attract a panel to an anchor can be adjusted this value between 0.979 and 1.0 + /// in increments of 0.001. When this value is around 0.979, the attraction uses a critically damped spring system. + /// When this value is between 0.978 and 1.0, it uses a underdamped spring system with a damping ratio computed by + /// this value. You shouldn't return less than 0.979 because the system is overdamped. If the pan gesture's velocity + /// is less than 300, this value is ignored and a panel applies a critically damped system. + @objc optional + var springDecelerationRate: CGFloat { get } + + /// A floating-point value that determines the approximate time until a panel stops to an anchor after the user lifts their finger. + @objc optional + var springResponseTime: CGFloat { get } + + /// Returns a deceleration rate to calculate a target position projected a dragging momentum. + /// + /// The default implementation of this method returns the normal deceleration rate of UIScrollView. + @objc optional + var momentumProjectionRate: CGFloat { get } + + /// Asks the behavior if a panel should project a momentum of a user interaction to move the proposed position. + /// + /// The default implementation of this method returns true. This method is called for a layout to support all positions(tip, half and full). + /// Therefore, `proposedTargetPosition` can only be `FloatingPanelState.tip` or `FloatingPanelState.full`. + @objc optional + func shouldProjectMomentum(_ fpc: FloatingPanelController, to proposedTargetPosition: FloatingPanelState) -> Bool + + /// Returns the progress to redirect to the previous position. + /// + /// The progress is represented by a floating-point value between 0.0 and 1.0, inclusive, where 1.0 indicates a panel is impossible to move to the next position. The default value is 0.5. Values less than 0.0 and greater than 1.0 are pinned to those limits. + @objc optional + func redirectionalProgress(_ fpc: FloatingPanelController, from: FloatingPanelState, to: FloatingPanelState) -> CGFloat + + /// Asks the behavior whether the rubber band effect is enabled in moving over a given edge of the surface view. + /// + /// This method allows a panel to activate the rubber band effect to a given edge of the surface view. By default, the effect is disabled. + @objc optional + func allowsRubberBanding(for edge: UIRectEdge) -> Bool + + /// Returns the velocity threshold for the default interactive removal gesture. + /// + /// In case ``FloatingPanel/FloatingPanelControllerDelegate/floatingPanel(_:shouldRemoveAt:with:)`` is implemented, this value will not be used. The default value of ``FloatingPanelDefaultBehavior`` is 5.5 + @objc optional + var removalInteractionVelocityThreshold: CGFloat { get } +} + +/// The default behavior object for a panel +/// +/// This behavior object is fine-tuned to behave as a search panel(card) in Apple Maps on iPhone portrait orientation. +open class FloatingPanelDefaultBehavior: FloatingPanelBehavior { + public init() {} + + open var springDecelerationRate: CGFloat { + return UIScrollView.DecelerationRate.fast.rawValue + 0.001 + } + + open var springResponseTime: CGFloat { + return 0.4 + } + + open var momentumProjectionRate: CGFloat { + return UIScrollView.DecelerationRate.normal.rawValue + } + + open func redirectionalProgress(_ fpc: FloatingPanelController, from: FloatingPanelState, to: FloatingPanelState) -> CGFloat { + return 0.5 + } + + open func allowsRubberBanding(for edge: UIRectEdge) -> Bool { + return false + } + + open var removalInteractionVelocityThreshold: CGFloat = 5.5 +} + +class BehaviorAdapter { + unowned let vc: FloatingPanelController + fileprivate var behavior: FloatingPanelBehavior + + init(vc: FloatingPanelController, behavior: FloatingPanelBehavior) { + self.vc = vc + self.behavior = behavior + } + + var springDecelerationRate: CGFloat { + behavior.springDecelerationRate ?? FloatingPanelDefaultBehavior().springDecelerationRate + } + + var springResponseTime: CGFloat { + behavior.springResponseTime ?? FloatingPanelDefaultBehavior().springResponseTime + } + + var momentumProjectionRate: CGFloat { + behavior.momentumProjectionRate ?? FloatingPanelDefaultBehavior().momentumProjectionRate + } + + var removalInteractionVelocityThreshold: CGFloat { + behavior.removalInteractionVelocityThreshold ?? FloatingPanelDefaultBehavior().removalInteractionVelocityThreshold + } + + func redirectionalProgress(from: FloatingPanelState, to: FloatingPanelState) -> CGFloat { + behavior.redirectionalProgress?(vc, from: from, to: to) ?? FloatingPanelDefaultBehavior().redirectionalProgress(vc,from: from, to: to) + } + + func shouldProjectMomentum(to: FloatingPanelState) -> Bool { + behavior.shouldProjectMomentum?(vc, to: to) ?? false + } + + func allowsRubberBanding(for edge: UIRectEdge) -> Bool { + behavior.allowsRubberBanding?(for: edge) ?? false + } +} + +extension FloatingPanelController { + var _behavior: FloatingPanelBehavior { + get { floatingPanel.behaviorAdapter.behavior } + set { floatingPanel.behaviorAdapter.behavior = newValue} + } +} diff --git a/Sources/Controller.swift b/Sources/Controller.swift new file mode 100644 index 00000000..45bedaa7 --- /dev/null +++ b/Sources/Controller.swift @@ -0,0 +1,720 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// A set of methods implemented by the delegate of a panel controller allows the adopting delegate to respond to +/// messages from the FloatingPanelController class and thus respond to, and in some affect, operations such as +/// dragging, attracting a panel, layout of a panel and the content, and transition animations. +@objc public protocol FloatingPanelControllerDelegate { + /// Returns a FloatingPanelLayout object. If you use the default one, you can use a `FloatingPanelBottomLayout` object. + @objc(floatingPanel:layoutForTraitCollection:) optional + func floatingPanel(_ fpc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout + + /// Returns a FloatingPanelLayout object. If you use the default one, you can use a `FloatingPanelBottomLayout` object. + @objc(floatingPanel:layoutForSize:) optional + func floatingPanel(_ fpc: FloatingPanelController, layoutFor size: CGSize) -> FloatingPanelLayout + + /// Returns a UIViewPropertyAnimator object to add/present the panel to a position. + /// + /// Default is the spring animation with 0.25 secs. + @objc(floatingPanel:animatorForPresentingToState:) optional + func floatingPanel(_ fpc: FloatingPanelController, animatorForPresentingTo state: FloatingPanelState) -> UIViewPropertyAnimator + + /// Returns a UIViewPropertyAnimator object to remove/dismiss a panel from a position. + /// + /// Default is the spring animator with 0.25 secs. + @objc(floatingPanel:animatorForDismissingWithVelocity:) optional + func floatingPanel(_ fpc: FloatingPanelController, animatorForDismissingWith velocity: CGVector) -> UIViewPropertyAnimator + + /// Called when a panel has changed to a new state. + /// + /// This can be called inside an animation block for presenting, dismissing a panel or moving a panel with your + /// animation. So any view properties set inside this function will be automatically animated alongside a panel. + @objc optional + func floatingPanelDidChangeState(_ fpc: FloatingPanelController) + + /// Asks the delegate if dragging should begin by the pan gesture recognizer. + @objc optional + func floatingPanelShouldBeginDragging(_ fpc: FloatingPanelController) -> Bool + + /// Called when the user drags the surface or the surface is attracted to a state anchor. + @objc optional + func floatingPanelDidMove(_ fpc: FloatingPanelController) // any surface frame changes in dragging + + /// Called on start of dragging (may require some time and or distance to move) + @objc optional + func floatingPanelWillBeginDragging(_ fpc: FloatingPanelController) + + /// Called on finger up if the user dragged. velocity is in points/second. + @objc optional + func floatingPanelWillEndDragging(_ fpc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer) + + /// Called on finger up if the user dragged. + /// + /// If `attract` is true, it will continue moving afterwards to a nearby state anchor. + @objc optional + func floatingPanelDidEndDragging(_ fpc: FloatingPanelController, willAttract attract: Bool) + + /// Called when it is about to be attracted to a state anchor. + @objc optional + func floatingPanelWillBeginAttracting(_ fpc: FloatingPanelController, to state: FloatingPanelState) // called on finger up as a panel are moving + + /// Called when attracting it is completed. + @objc optional + func floatingPanelDidEndAttracting(_ fpc: FloatingPanelController) // called when a panel stops + + /// Asks the delegate whether a panel should be removed when dragging ended at the specified location + /// + /// This delegate method is called only where ``FloatingPanel/FloatingPanelController/isRemovalInteractionEnabled`` is `true`. + /// The velocity vector is calculated from the distance to a point of the hidden state and the pan gesture's velocity. + @objc(floatingPanel:shouldRemoveAtLocation:withVelocity:) + optional + func floatingPanel(_ fpc: FloatingPanelController, shouldRemoveAt location: CGPoint, with velocity: CGVector) -> Bool + + /// Called on start to remove its view controller from the parent view controller. + @objc(floatingPanelWillRemove:) + optional + func floatingPanelWillRemove(_ fpc: FloatingPanelController) + + /// Called when a panel is removed from the parent view controller. + @objc optional + func floatingPanelDidRemove(_ fpc: FloatingPanelController) + + /// Asks the delegate for a content offset of the tracking scroll view to be pinned when a panel moves + /// + /// If you do not implement this method, the controller uses a value of the content offset plus the content insets + /// of the tracked scroll view. Your implementation of this method can return a value for a navigation bar with a large + /// title, for example. + /// + /// This method will not be called if the controller doesn't track any scroll view. + @objc(floatingPanel:contentOffsetForPinningScrollView:) + optional + func floatingPanel(_ fpc: FloatingPanelController, contentOffsetForPinning trackingScrollView: UIScrollView) -> CGPoint +} + +/// +/// A container view controller to display a panel to present contents in parallel as a user wants. +/// +@objc +open class FloatingPanelController: UIViewController { + /// Constants indicating how safe area insets are added to the adjusted content inset. + @objc + public enum ContentInsetAdjustmentBehavior: Int { + case always + case never + } + + /// A flag used to determine how the controller object lays out the content view when the surface position changes. + @objc + public enum ContentMode: Int { + /// The option to fix the content to keep the height of the top most position. + case `static` + /// The option to scale the content to fit the bounds of the root view by changing the surface position. + case fitToBounds + } + + /// The delegate of a panel controller object. + @objc + public weak var delegate: FloatingPanelControllerDelegate?{ + didSet{ + didUpdateDelegate() + } + } + + /// Returns the surface view managed by the controller object. It's the same as `self.view`. + @objc + public var surfaceView: SurfaceView! { + return floatingPanel.surfaceView + } + + /// Returns the backdrop view managed by the controller object. + @objc + public var backdropView: BackdropView! { + return floatingPanel.backdropView + } + + /// Returns the scroll view that the controller tracks. + @objc + public weak var trackingScrollView: UIScrollView? { + return floatingPanel.scrollView + } + + // The underlying gesture recognizer for pan gestures + @objc + public var panGestureRecognizer: FloatingPanelPanGestureRecognizer { + return floatingPanel.panGestureRecognizer + } + + /// The current position of a panel controller's contents. + @objc + public var state: FloatingPanelState { + return floatingPanel.state + } + + /// A Boolean value indicating whether a panel controller is attracting the surface to a state anchor. + @objc + public var isAttracting: Bool { + return floatingPanel.isAttracting + } + + /// The layout object that the controller manages + /// + /// You need to call ``invalidateLayout()`` if you want to apply a new layout object into the panel + /// immediately. + @objc + public var layout: FloatingPanelLayout { + get { _layout } + set { + _layout = newValue + if let parent = parent, let layout = newValue as? UIViewController, layout == parent { + log.warning("A memory leak will occur by a retain cycle because \(self) owns the parent view controller(\(parent)) as the layout object. Don't let the parent adopt FloatingPanelLayout.") + } + } + } + + /// The behavior object that the controller manages + @objc + public var behavior: FloatingPanelBehavior { + get { _behavior } + set { + _behavior = newValue + if let parent = parent, let behavior = newValue as? UIViewController, behavior == parent { + log.warning("A memory leak will occur by a retain cycle because \(self) owns the parent view controller(\(parent)) as the behavior object. Don't let the parent adopt FloatingPanelBehavior.") + } + } + } + + /// The content insets of the tracking scroll view derived from this safe area + @objc + public var adjustedContentInsets: UIEdgeInsets { + return floatingPanel.layoutAdapter.adjustedContentInsets + } + + /// The behavior for determining the adjusted content offsets. + /// + /// This property specifies how the content area of the tracking scroll view is modified using ``adjustedContentInsets``. The default value of this property is FloatingPanelController.ContentInsetAdjustmentBehavior.always. + @objc + public var contentInsetAdjustmentBehavior: ContentInsetAdjustmentBehavior = .always + + /// A Boolean value that determines whether the removal interaction is enabled. + @objc + public var isRemovalInteractionEnabled: Bool { + @objc(setRemovalInteractionEnabled:) set { floatingPanel.isRemovalInteractionEnabled = newValue } + @objc(isRemovalInteractionEnabled) get { return floatingPanel.isRemovalInteractionEnabled } + } + + /// The view controller responsible for the content portion of a panel. + @objc + public var contentViewController: UIViewController? { + set { set(contentViewController: newValue) } + get { return _contentViewController } + } + + /// The NearbyState determines that finger's nearby state. + public var nearbyState: FloatingPanelState { + let currentY = surfaceLocation.y + return floatingPanel.targetPosition(from: currentY, with: .zero) + } + + /// Constants that define how a panel content fills in the surface. + @objc + public var contentMode: ContentMode = .static { + didSet { + guard state != .hidden else { return } + activateLayout(forceLayout: false) + } + } + + private var _contentViewController: UIViewController? + + private(set) var floatingPanel: Core! + private var preSafeAreaInsets: UIEdgeInsets = .zero // Capture the latest one + private var safeAreaInsetsObservation: NSKeyValueObservation? + private let modalTransition = ModalTransition() + + required public init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + setUp() + } + + override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { + super.init(nibName: nil, bundle: nil) + setUp() + } + + /// Initialize a newly created panel controller. + @objc + public init(delegate: FloatingPanelControllerDelegate? = nil) { + super.init(nibName: nil, bundle: nil) + self.delegate = delegate + setUp() + } + + private func setUp() { + _ = FloatingPanelController.dismissSwizzling + + modalPresentationStyle = .custom + transitioningDelegate = modalTransition + + let initialLayout: FloatingPanelLayout + if let layout = delegate?.floatingPanel?(self, layoutFor: traitCollection) { + initialLayout = layout + } else { + initialLayout = FloatingPanelBottomLayout() + } + let initialBehavior = FloatingPanelDefaultBehavior() + + floatingPanel = Core(self, layout: initialLayout, behavior: initialBehavior) + } + + private func didUpdateDelegate(){ + if let layout = delegate?.floatingPanel?(self, layoutFor: traitCollection) { + _layout = layout + } + } + + // MARK:- Overrides + + /// Creates the view that the controller manages. + open override func loadView() { + assert(self.storyboard == nil, "Storyboard isn't supported") + + let view = PassthroughView() + view.backgroundColor = .clear + + backdropView.frame = view.bounds + view.addSubview(backdropView) + + surfaceView.frame = view.bounds + view.addSubview(surfaceView) + + self.view = view as UIView + } + + open override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + if #available(iOS 11.0, *) { + // Ensure to update the static constraint of a panel after rotating a device in static mode + if contentMode == .static { + floatingPanel.layoutAdapter.updateStaticConstraint() + } + } else { + // Because {top,bottom}LayoutGuide is managed as a view + if floatingPanel.isAttracting == false { + self.update(safeAreaInsets: fp_safeAreaInsets) + } + } + } + + open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransition(to: size, with: coordinator) + + if self.view.bounds.size == size { + return + } + + // Change a layout for the new view size + if let newLayout = self.delegate?.floatingPanel?(self, layoutFor: size) { + layout = newLayout + activateLayout(forceLayout: false) + } + + if view.translatesAutoresizingMaskIntoConstraints { + view.frame.size = size + view.layoutIfNeeded() + } + } + + open override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { + super.willTransition(to: newCollection, with: coordinator) + + if shouldUpdateLayout(from: traitCollection, to: newCollection) == false { + return + } + + // Change a layout for the new trait collection + if let newLayout = self.delegate?.floatingPanel?(self, layoutFor: newCollection) { + self.layout = newLayout + activateLayout(forceLayout: false) + } + } + + open override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + safeAreaInsetsObservation = nil + } + + // MARK:- Child view controller to consult + open override var childForStatusBarStyle: UIViewController? { + return contentViewController + } + + open override var childForStatusBarHidden: UIViewController? { + return contentViewController + } + + open override var childForScreenEdgesDeferringSystemGestures: UIViewController? { + return contentViewController + } + + open override var childForHomeIndicatorAutoHidden: UIViewController? { + return contentViewController + } + + // MARK:- Privates + + private func shouldUpdateLayout(from previous: UITraitCollection, to new: UITraitCollection) -> Bool { + return previous.horizontalSizeClass != new.horizontalSizeClass + || previous.verticalSizeClass != new.verticalSizeClass + || previous.preferredContentSizeCategory != new.preferredContentSizeCategory + || previous.layoutDirection != new.layoutDirection + } + + private func update(safeAreaInsets: UIEdgeInsets) { + guard + preSafeAreaInsets != safeAreaInsets + else { return } + + log.debug("Update safeAreaInsets", safeAreaInsets) + + // Prevent an infinite loop on iOS 10: setUpLayout() -> viewDidLayoutSubviews() -> setUpLayout() + preSafeAreaInsets = safeAreaInsets + + // preserve the current content offset if contentInsetAdjustmentBehavior is `.always` + var contentOffset: CGPoint? + if contentInsetAdjustmentBehavior == .always { + contentOffset = trackingScrollView?.contentOffset + } + + floatingPanel.layoutAdapter.updateStaticConstraint() + + if let contentOffset = contentOffset { + trackingScrollView?.contentOffset = contentOffset + } + + switch contentInsetAdjustmentBehavior { + case .always: + trackingScrollView?.contentInset = adjustedContentInsets + default: + break + } + } + + private func activateLayout(forceLayout: Bool = false) { + floatingPanel.activateLayout(forceLayout: forceLayout, + contentInsetAdjustmentBehavior: contentInsetAdjustmentBehavior) + } + + func remove() { + if presentingViewController != nil, parent == nil { + delegate?.floatingPanelWillRemove?(self) + dismiss(animated: true) { [weak self] in + guard let self = self else { return } + self.delegate?.floatingPanelDidRemove?(self) + } + } else { + removePanelFromParent(animated: true) + } + } + + // MARK: - Container view controller interface + + /// Shows the surface view at the initial position defined by the current layout + /// - Parameters: + /// - animated: Pass true to animate the presentation; otherwise, pass false. + /// - completion: The block to execute after the presentation finishes. This block has no return value and takes no parameters. You may specify nil for this parameter. + @objc(show:completion:) + public func show(animated: Bool = false, completion: (() -> Void)? = nil) { + // Must apply the current layout here + activateLayout(forceLayout: true) + + if #available(iOS 11.0, *) { + // Must track the safeAreaInsets of `self.view` to update the layout. + // There are 2 reasons. + // 1. This or the parent VC doesn't call viewSafeAreaInsetsDidChange() on the bottom + // inset's update expectedly. + // 2. The safe area top inset can be variable on the large title navigation bar(iOS11+). + // That's why it needs the observation to keep `adjustedContentInsets` correct. + safeAreaInsetsObservation = self.view.observe(\.safeAreaInsets, options: [.initial, .new, .old]) { [weak self] (_, change) in + // Use `self.view.safeAreaInsets` because `change.newValue` can be nil in particular case when + // is reported in https://github.com/SCENEE/FloatingPanel/issues/330 + guard let self = self, change.oldValue != self.view.safeAreaInsets else { return } + self.update(safeAreaInsets: self.view.safeAreaInsets) + } + } else { + // KVOs for topLayoutGuide & bottomLayoutGuide are not effective. + // Instead, update(safeAreaInsets:) is called at `viewDidLayoutSubviews()` + } + + move(to: floatingPanel.layoutAdapter.initialState, + animated: animated, + completion: completion) + } + + /// Hides the surface view to the hidden position + @objc(hide:completion:) + public func hide(animated: Bool = false, completion: (() -> Void)? = nil) { + move(to: .hidden, + animated: animated, + completion: completion) + } + + /// Adds the view managed by the controller as a child of the specified view controller. + /// - Parameters: + /// - parent: A parent view controller object that displays FloatingPanelController's view. A container view controller object isn't applicable. + /// - viewIndex: Insert the surface view managed by the controller below the specified view index. By default, the surface view will be added to the end of the parent list of subviews. + /// - animated: Pass true to animate the presentation; otherwise, pass false. + /// - completion: The block to execute after the presentation finishes. This block has no return value and takes no parameters. You may specify nil for this parameter. + @objc(addPanelToParent:at:animated:completion:) + public func addPanel(toParent parent: UIViewController, at viewIndex: Int = -1, animated: Bool = false, completion: (() -> Void)? = nil) { + guard self.parent == nil else { + log.warning("Already added to a parent(\(parent))") + return + } + assert((parent is UINavigationController) == false, "UINavigationController displays only one child view controller at a time.") + assert((parent is UITabBarController) == false, "UITabBarController displays child view controllers with a radio-style selection interface") + assert((parent is UISplitViewController) == false, "UISplitViewController manages two child view controllers in a master-detail interface") + assert((parent is UITableViewController) == false, "UITableViewController should not be the parent because the view is a table view so that a panel doesn't work well") + assert((parent is UICollectionViewController) == false, "UICollectionViewController should not be the parent because the view is a collection view so that a panel doesn't work well") + + if viewIndex < 0 { + parent.view.addSubview(self.view) + } else { + parent.view.insertSubview(self.view, at: viewIndex) + } + + parent.addChild(self) + + view.frame = parent.view.bounds // Needed for a correct safe area configuration + view.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + self.view.topAnchor.constraint(equalTo: parent.view.topAnchor, constant: 0.0), + self.view.leftAnchor.constraint(equalTo: parent.view.leftAnchor, constant: 0.0), + self.view.rightAnchor.constraint(equalTo: parent.view.rightAnchor, constant: 0.0), + self.view.bottomAnchor.constraint(equalTo: parent.view.bottomAnchor, constant: 0.0), + ]) + + show(animated: animated) { [weak self] in + guard let self = self else { return } + self.didMove(toParent: parent) + completion?() + } + } + + /// Removes the controller and the managed view from its parent view controller + /// - Parameters: + /// - animated: Pass true to animate the presentation; otherwise, pass false. + /// - completion: The block to execute after the view controller is dismissed. This block has no return value and takes no parameters. You may specify nil for this parameter. + @objc(removePanelFromParent:completion:) + public func removePanelFromParent(animated: Bool, completion: (() -> Void)? = nil) { + guard self.parent != nil else { + completion?() + return + } + + delegate?.floatingPanelWillRemove?(self) + + hide(animated: animated) { [weak self] in + guard let self = self else { return } + + self.willMove(toParent: nil) + + self.view.removeFromSuperview() + + self.removeFromParent() + + self.delegate?.floatingPanelDidRemove?(self) + completion?() + } + } + + /// Moves the position to the specified position. + /// + /// - Parameters: + /// - to: Pass a FloatingPanelPosition value to move the surface view to the position. + /// - animated: Pass true to animate the presentation; otherwise, pass false. + /// - completion: The block to execute after the view controller has finished moving. This block has no return value and takes no parameters. You may specify nil for this parameter. + @objc(moveToState:animated:completion:) + public func move(to: FloatingPanelState, animated: Bool, completion: (() -> Void)? = nil) { + floatingPanel.move(to: to, animated: animated, completion: completion) + } + + /// Sets the view controller responsible for the content portion of a panel. + public func set(contentViewController: UIViewController?) { + if let vc = _contentViewController { + vc.willMove(toParent: nil) + vc.view.removeFromSuperview() + vc.removeFromParent() + } + + if let vc = contentViewController { + addChild(vc) + + let surfaceView = floatingPanel.surfaceView + surfaceView.set(contentView: vc.view, mode: contentMode) + + vc.didMove(toParent: self) + } + + _contentViewController = contentViewController + } + + // MARK: - Scroll view tracking + + /// Tracks the specified scroll view to correspond with the scroll. + /// + /// - Parameters: + /// - scrollView: Specify a scroll view to continuously and seamlessly work in concert with interactions of the surface view + @objc(trackScrollView:) + public func track(scrollView: UIScrollView) { + if let scrollView = floatingPanel.scrollView { + untrack(scrollView: scrollView) + } + + floatingPanel.scrollView = scrollView + + switch contentInsetAdjustmentBehavior { + case .always: + if #available(iOS 11.0, *) { + scrollView.contentInsetAdjustmentBehavior = .never + } else { + children.forEach { (vc) in + vc.automaticallyAdjustsScrollViewInsets = false + } + } + default: + break + } + } + + /// Cancel tracking the specify scroll view. + /// + @objc(untrackScrollView:) + public func untrack(scrollView: UIScrollView) { + if floatingPanel.scrollView == scrollView { + floatingPanel.scrollView = nil + } + } + + // MARK: - Accessibility + + open override func accessibilityPerformEscape() -> Bool { + guard isRemovalInteractionEnabled else { return false } + dismiss(animated: true, completion: nil) + return true + } + + // MARK: - Utilities + + /// Invalidates all layout information of the panel and apply the ``layout`` property into it immediately. + /// + /// This lays out subviews of the view that the controller manages with the ``layout`` property by + /// calling the view's `layoutIfNeeded()`. Thus this method can be called in an animation block to + /// animate the panel's changes. + /// + /// If the controller has a delegate object, this will lay them out using the layout object returned by + /// `floatingPanel(_:layoutFor:)` delegate method for the current `UITraitCollection`. + @objc + public func invalidateLayout() { + if let newLayout = self.delegate?.floatingPanel?(self, layoutFor: traitCollection) { + layout = newLayout + } + activateLayout(forceLayout: true) + } + + /// Returns the surface's position in a panel controller's view for the specified state. + /// + /// If a panel is top positioned, this returns a point of the bottom-left corner of the surface. If it is left positioned + /// this returns a point of top-right corner of the surface. If it is bottom or right positioned, this returns a point + /// of the top-left corner of the surface. + @objc + public func surfaceLocation(for state: FloatingPanelState) -> CGPoint { + return floatingPanel.layoutAdapter.surfaceLocation(for: state) + } + + /// The surface's position in a panel controller's view. + /// + /// If a panel is top positioned, this returns a point of the bottom-left corner of the surface. If it is left positioned + /// this returns a point of top-right corner of the surface. If it is bottom or right positioned, this returns a point + /// of the top-left corner of the surface. + @objc + public var surfaceLocation: CGPoint { + get { floatingPanel.layoutAdapter.surfaceLocation } + set { floatingPanel.layoutAdapter.surfaceLocation = newValue } + } +} + +extension FloatingPanelController { + func notifyDidMove() { + #if !TEST + guard self.view.window != nil else { return } + #endif + delegate?.floatingPanelDidMove?(self) + } + + func animatorForPresenting(to: FloatingPanelState) -> UIViewPropertyAnimator { + if let animator = delegate?.floatingPanel?(self, animatorForPresentingTo: to) { + return animator + } + let timingParameters = UISpringTimingParameters(decelerationRate: UIScrollView.DecelerationRate.fast.rawValue, + frequencyResponse: 0.25) + return UIViewPropertyAnimator(duration: 0.0, + timingParameters: timingParameters) + } + + func animatorForDismissing(with velocity: CGVector) -> UIViewPropertyAnimator { + if let animator = delegate?.floatingPanel?(self, animatorForDismissingWith: velocity) { + return animator + } + let timingParameters = UISpringTimingParameters(decelerationRate: UIScrollView.DecelerationRate.fast.rawValue, + frequencyResponse: 0.25, + initialVelocity: velocity) + return UIViewPropertyAnimator(duration: 0.0, + timingParameters: timingParameters) + } +} + +extension FloatingPanelController { + private static let dismissSwizzling: Void = { + let aClass: AnyClass! = UIViewController.self //object_getClass(vc) + if let imp = class_getMethodImplementation(aClass, #selector(dismiss(animated:completion:))), + let originalAltMethod = class_getInstanceMethod(aClass, #selector(fp_original_dismiss(animated:completion:))) { + method_setImplementation(originalAltMethod, imp) + } + let originalMethod = class_getInstanceMethod(aClass, #selector(dismiss(animated:completion:))) + let swizzledMethod = class_getInstanceMethod(aClass, #selector(fp_dismiss(animated:completion:))) + if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod { + method_exchangeImplementations(originalMethod, swizzledMethod) + } + }() +} + +public extension UIViewController { + @objc func fp_original_dismiss(animated flag: Bool, completion: (() -> Void)? = nil) { + // Implementation will be replaced by IMP of self.dismiss(animated:completion:) + } + @objc func fp_dismiss(animated flag: Bool, completion: (() -> Void)? = nil) { + // Call dismiss(animated:completion:) to a content view controller + if let fpc = parent as? FloatingPanelController { + if fpc.presentingViewController != nil { + self.fp_original_dismiss(animated: flag, completion: completion) + } else { + fpc.removePanelFromParent(animated: flag, completion: completion) + } + return + } + // Call dismiss(animated:completion:) to FloatingPanelController directly + if let fpc = self as? FloatingPanelController { + // When a panel is presented modally and it's not a child view controller of the presented view controller. + if fpc.presentingViewController != nil, fpc.parent == nil { + self.fp_original_dismiss(animated: flag, completion: completion) + } else { + fpc.removePanelFromParent(animated: flag, completion: completion) + } + return + } + + // For other view controllers + self.fp_original_dismiss(animated: flag, completion: completion) + } +} diff --git a/Sources/Core.swift b/Sources/Core.swift new file mode 100644 index 00000000..86352db5 --- /dev/null +++ b/Sources/Core.swift @@ -0,0 +1,1266 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// +/// The presentation model of FloatingPanel +/// +class Core: NSObject, UIGestureRecognizerDelegate { + private weak var ownerVC: FloatingPanelController? + + let surfaceView: SurfaceView + let backdropView: BackdropView + let layoutAdapter: LayoutAdapter + let behaviorAdapter: BehaviorAdapter + + weak var scrollView: UIScrollView? { + didSet { + oldValue?.panGestureRecognizer.removeTarget(self, action: nil) + scrollView?.panGestureRecognizer.addTarget(self, action: #selector(handle(panGesture:))) + if let cur = scrollView { + if oldValue == nil { + initialScrollOffset = cur.contentOffset + scrollBounce = cur.bounces + scrollIndictorVisible = cur.showsVerticalScrollIndicator + } + } else { + if let pre = oldValue { + pre.isDirectionalLockEnabled = false + pre.bounces = scrollBounce + pre.showsVerticalScrollIndicator = scrollIndictorVisible + } + } + } + } + + private(set) var state: FloatingPanelState = .hidden { + didSet { + log.debug("state changed: \(oldValue) -> \(state)") + if let vc = ownerVC { + vc.delegate?.floatingPanelDidChangeState?(vc) + } + } + } + + let panGestureRecognizer: FloatingPanelPanGestureRecognizer + var isRemovalInteractionEnabled: Bool = false + + fileprivate var isSuspended: Bool = false // Prevent a memory leak in the modal transition + fileprivate var transitionAnimator: UIViewPropertyAnimator? + fileprivate var moveAnimator: NumericSpringAnimator? + + private var initialSurfaceLocation: CGPoint = .zero + private var initialTranslation: CGPoint = .zero + private var initialLocation: CGPoint { + return panGestureRecognizer.initialLocation + } + + var interactionInProgress: Bool = false + var isAttracting: Bool = false + + // Removal interaction + var removalVector: CGVector = .zero + + // Scroll handling + private var initialScrollOffset: CGPoint = .zero + private var stopScrollDeceleration: Bool = false + private var scrollBounce = false + private var scrollIndictorVisible = false + + // MARK: - Interface + + init(_ vc: FloatingPanelController, layout: FloatingPanelLayout, behavior: FloatingPanelBehavior) { + ownerVC = vc + + surfaceView = SurfaceView() + surfaceView.position = layout.position + surfaceView.backgroundColor = .white + + backdropView = BackdropView() + backdropView.backgroundColor = .black + backdropView.alpha = 0.0 + + layoutAdapter = LayoutAdapter(vc: vc, layout: layout) + behaviorAdapter = BehaviorAdapter(vc: vc, behavior: behavior) + + panGestureRecognizer = FloatingPanelPanGestureRecognizer() + + if #available(iOS 11.0, *) { + panGestureRecognizer.name = "FloatingPanelPanGestureRecognizer" + } + + super.init() + + panGestureRecognizer.floatingPanel = self + surfaceView.addGestureRecognizer(panGestureRecognizer) + panGestureRecognizer.addTarget(self, action: #selector(handle(panGesture:))) + panGestureRecognizer.delegate = self + + // Set tap-to-dismiss in the backdrop view + let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleBackdrop(tapGesture:))) + tapGesture.isEnabled = false + backdropView.dismissalTapGestureRecognizer = tapGesture + backdropView.addGestureRecognizer(tapGesture) + } + + deinit { + // Release `NumericSpringAnimator.displayLink` from the run loop. + self.moveAnimator?.stopAnimation(false) + } + + func move(to: FloatingPanelState, animated: Bool, completion: (() -> Void)? = nil) { + move(from: state, to: to, animated: animated, completion: completion) + } + + private func move(from: FloatingPanelState, to: FloatingPanelState, animated: Bool, completion: (() -> Void)? = nil) { + assert(layoutAdapter.validStates.contains(to), "Can't move to '\(to)' state because it's not valid in the layout") + guard let vc = ownerVC else { + completion?() + return + } + if state != layoutAdapter.mostExpandedState { + lockScrollView() + } + tearDownActiveInteraction() + + interruptAnimationIfNeeded() + + if animated { + let updateScrollView: () -> Void = { [weak self] in + guard let self = self else { return } + if self.state == self.layoutAdapter.mostExpandedState, abs(self.layoutAdapter.offsetFromMostExpandedAnchor) <= 1.0 { + self.unlockScrollView() + } else { + self.lockScrollView() + } + } + + let animator: UIViewPropertyAnimator + switch (from, to) { + case (.hidden, let to): + animator = vc.animatorForPresenting(to: to) + case (_, .hidden): + let animationVector = CGVector(dx: abs(removalVector.dx), dy: abs(removalVector.dy)) + animator = vc.animatorForDismissing(with: animationVector) + default: + move(to: to, with: 0) { [weak self] in + guard let self = self else { return } + + self.moveAnimator = nil + updateScrollView() + completion?() + } + return + } + + let shouldDoubleLayout = from == .hidden + && surfaceView.hasStackView() + && layoutAdapter.isIntrinsicAnchor(state: to) + + animator.addAnimations { [weak self] in + guard let self = self else { return } + + self.state = to + self.updateLayout(to: to) + + if shouldDoubleLayout { + log.info("Lay out the surface again to modify an intrinsic size error according to UIStackView") + self.updateLayout(to: to) + } + } + animator.addCompletion { [weak self] _ in + guard let self = self else { return } + + self.transitionAnimator = nil + updateScrollView() + self.ownerVC?.notifyDidMove() + completion?() + } + self.transitionAnimator = animator + if isSuspended { + return + } + animator.startAnimation() + } else { + self.state = to + self.updateLayout(to: to) + if self.state == self.layoutAdapter.mostExpandedState { + self.unlockScrollView() + } else { + self.lockScrollView() + + } + ownerVC?.notifyDidMove() + completion?() + } + } + + // MARK: - Layout update + + func activateLayout(forceLayout: Bool = false, + contentInsetAdjustmentBehavior: FloatingPanelController.ContentInsetAdjustmentBehavior) { + layoutAdapter.prepareLayout() + + // preserve the current content offset if contentInsetAdjustmentBehavior is `.always` + var contentOffset: CGPoint? + if contentInsetAdjustmentBehavior == .always { + contentOffset = scrollView?.contentOffset + } + + layoutAdapter.updateStaticConstraint() + layoutAdapter.activateLayout(for: state, forceLayout: true) + + // Update the backdrop alpha only when called in `Controller.show(animated:completion:)` + // Because that prevents a backdrop flicking just before presenting a panel(#466). + if forceLayout { + backdropView.alpha = getBackdropAlpha(for: state) + } + + if let contentOffset = contentOffset { + scrollView?.contentOffset = contentOffset + } + } + + private func updateLayout(to target: FloatingPanelState) { + self.layoutAdapter.activateLayout(for: target, forceLayout: true) + self.backdropView.alpha = self.getBackdropAlpha(for: target) + } + + private func getBackdropAlpha(for target: FloatingPanelState) -> CGFloat { + return target == .hidden ? 0.0 : layoutAdapter.backdropAlpha(for: target) + } + + func getBackdropAlpha(at cur: CGFloat, with translation: CGFloat) -> CGFloat { + /* log.debug("currentY: \(currentY) translation: \(translation)") */ + let forwardY = (translation >= 0) + + let segment = layoutAdapter.segment(at: cur, forward: forwardY) + + let lowerState = segment.lower ?? layoutAdapter.mostExpandedState + let upperState = segment.upper ?? layoutAdapter.leastExpandedState + + let preState = forwardY ? lowerState : upperState + let nextState = forwardY ? upperState : lowerState + + let next = value(of: layoutAdapter.surfaceLocation(for: nextState)) + let pre = value(of: layoutAdapter.surfaceLocation(for: preState)) + + let nextAlpha = layoutAdapter.backdropAlpha(for: nextState) + let preAlpha = layoutAdapter.backdropAlpha(for: preState) + + if pre == next { + return preAlpha + } + return preAlpha + max(min(1.0, 1.0 - (next - cur) / (next - pre) ), 0.0) * (nextAlpha - preAlpha) + } + + // MARK: - UIGestureRecognizerDelegate + + public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, + shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { + if let result = panGestureRecognizer.delegateProxy?.gestureRecognizer?(gestureRecognizer, shouldRecognizeSimultaneouslyWith: otherGestureRecognizer) { + return result + } + + guard gestureRecognizer == panGestureRecognizer else { return false } + + /* log.debug("shouldRecognizeSimultaneouslyWith", otherGestureRecognizer) */ + + switch otherGestureRecognizer { + case is FloatingPanelPanGestureRecognizer: + // All visible panels' pan gesture should be recognized simultaneously. + return true + case is UIPanGestureRecognizer, + is UISwipeGestureRecognizer, + is UIRotationGestureRecognizer, + is UIScreenEdgePanGestureRecognizer, + is UIPinchGestureRecognizer: + if surfaceView.grabberAreaContains(gestureRecognizer.location(in: surfaceView)) { + return true + } + // all gestures of the tracking scroll view should be recognized in parallel + // and handle them in self.handle(panGesture:) + return scrollView?.gestureRecognizers?.contains(otherGestureRecognizer) ?? false + default: + // Should recognize tap/long press gestures in parallel when the surface view is at an anchor position. + let adapterY = layoutAdapter.position(for: state) + return abs(value(of: layoutAdapter.surfaceLocation) - adapterY) < (1.0 / surfaceView.fp_displayScale) + } + } + + public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool { + if let result = panGestureRecognizer.delegateProxy?.gestureRecognizer?(gestureRecognizer, shouldBeRequiredToFailBy: otherGestureRecognizer) { + return result + } + + if otherGestureRecognizer is FloatingPanelPanGestureRecognizer { + // If this panel is the farthest descendant of visible panels, + // its ancestors' pan gesture must wait for its pan gesture to fail + if let view = otherGestureRecognizer.view, surfaceView.isDescendant(of: view) { + return true + } + } + if #available(iOS 11.0, *), + otherGestureRecognizer.name == "_UISheetInteractionBackgroundDismissRecognizer" { + // The dismiss gesture of a sheet modal should not begin until the pan gesture fails. + return true + } + + if surfaceView.grabberAreaContains(gestureRecognizer.location(in: surfaceView)) { + return true + } + + return false + } + + public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { + if let result = panGestureRecognizer.delegateProxy?.gestureRecognizer?(gestureRecognizer, shouldRequireFailureOf: otherGestureRecognizer) { + return result + } + + guard gestureRecognizer == panGestureRecognizer else { return false } + + // Should begin the pan gesture without waiting for the tracking scroll view's gestures. + // `scrollView.gestureRecognizers` can contains the following gestures + // * UIScrollViewDelayedTouchesBeganGestureRecognizer + // * UIScrollViewPanGestureRecognizer (scrollView.panGestureRecognizer) + // * _UIDragAutoScrollGestureRecognizer + // * _UISwipeActionPanGestureRecognizer + // * UISwipeDismissalGestureRecognizer + if let scrollView = scrollView { + // On short contents scroll, `_UISwipeActionPanGestureRecognizer` blocks + // the panel's pan gesture if not returns false + if let scrollGestureRecognizers = scrollView.gestureRecognizers, + scrollGestureRecognizers.contains(otherGestureRecognizer) { + switch otherGestureRecognizer { + case scrollView.panGestureRecognizer: + if surfaceView.grabberAreaContains(gestureRecognizer.location(in: surfaceView)) { + return false + } + return allowScrollPanGesture(for: scrollView) + default: + return false + } + } + } + + switch otherGestureRecognizer { + case is FloatingPanelPanGestureRecognizer: + // If this panel is the farthest descendant of visible panels, + // its pan gesture does not require its ancestors' pan gesture to fail + if let view = otherGestureRecognizer.view, surfaceView.isDescendant(of: view) { + return false + } + return true + case is UIPanGestureRecognizer, + is UISwipeGestureRecognizer, + is UIRotationGestureRecognizer, + is UIScreenEdgePanGestureRecognizer, + is UIPinchGestureRecognizer: + if #available(iOS 11.0, *), + otherGestureRecognizer.name == "_UISheetInteractionBackgroundDismissRecognizer" { + // Should begin the pan gesture without waiting the dismiss gesture of a sheet modal. + return false + } + if surfaceView.grabberAreaContains(gestureRecognizer.location(in: surfaceView)) { + return false + } + // Do not begin the pan gesture until these gestures fail + return true + default: + // Should begin the pan gesture without waiting tap/long press gestures fail + return false + } + } + + // MARK: - Gesture handling + + @objc func handleBackdrop(tapGesture: UITapGestureRecognizer) { + removalVector = .zero + ownerVC?.remove() + } + + @objc func handle(panGesture: UIPanGestureRecognizer) { + switch panGesture { + case scrollView?.panGestureRecognizer: + guard let scrollView = scrollView else { return } + + let velocity = value(of: panGesture.velocity(in: panGesture.view)) + let location = panGesture.location(in: surfaceView) + let offsetDiff = value(of: scrollView.contentOffset - contentOffsetForPinning(of: scrollView)) + + let belowEdgeMost = 0 > layoutAdapter.offsetFromMostExpandedAnchor + (1.0 / surfaceView.fp_displayScale) + + log.debug(""" + scroll gesture(\(state):\(panGesture.state)) -- \ + belowTop = \(belowEdgeMost), \ + interactionInProgress = \(interactionInProgress), \ + scroll offset = \(value(of: scrollView.contentOffset)), \ + location = \(value(of: location)), velocity = \(velocity) + """) + + if belowEdgeMost { + // Scroll offset pinning + if state == layoutAdapter.mostExpandedState { + if interactionInProgress { + log.debug("settle offset --", value(of: initialScrollOffset)) + stopScrolling(at: initialScrollOffset) + } else { + if surfaceView.grabberAreaContains(location) { + // Preserve the current content offset in moving from full. + stopScrolling(at: initialScrollOffset) + } + } + } else { + stopScrolling(at: initialScrollOffset) + } + + // Hide a scroll indicator at the non-top in dragging. + if interactionInProgress { + lockScrollView() + } else { + if state == layoutAdapter.mostExpandedState, self.transitionAnimator == nil { + switch layoutAdapter.position { + case .top, .left: + if offsetDiff < 0 && velocity > 0 { + unlockScrollView() + } + case .bottom, .right: + if offsetDiff > 0 && velocity < 0 { + unlockScrollView() + } + } + } + } + } else { + if interactionInProgress { + // Show a scroll indicator at the top in dragging. + switch layoutAdapter.position { + case .top, .left: + if offsetDiff <= 0 && velocity >= 0 { + unlockScrollView() + return + } + case .bottom, .right: + if offsetDiff >= 0 && velocity <= 0 { + unlockScrollView() + return + } + } + if state == layoutAdapter.mostExpandedState { + // Adjust a small gap of the scroll offset just after swiping down starts in the grabber area. + if surfaceView.grabberAreaContains(location), surfaceView.grabberAreaContains(initialLocation) { + stopScrolling(at: initialScrollOffset) + } + } + } else { + if state == layoutAdapter.mostExpandedState { + switch layoutAdapter.position { + case .top, .left: + if velocity < 0, !allowScrollPanGesture(for: scrollView) { + lockScrollView() + } + if velocity > 0, allowScrollPanGesture(for: scrollView) { + unlockScrollView() + } + case .bottom, .right: + // Hide a scroll indicator just before starting an interaction by swiping a panel down. + if velocity > 0, !allowScrollPanGesture(for: scrollView) { + lockScrollView() + } + // Show a scroll indicator when an animation is interrupted at the top and content is scrolled up + if velocity < 0, allowScrollPanGesture(for: scrollView) { + unlockScrollView() + } + } + // Adjust a small gap of the scroll offset just before swiping down starts in the grabber area, + if surfaceView.grabberAreaContains(location), surfaceView.grabberAreaContains(initialLocation) { + stopScrolling(at: initialScrollOffset) + } + } + } + } + case panGestureRecognizer: + let translation = panGesture.translation(in: panGestureRecognizer.view!.superview) + let velocity = panGesture.velocity(in: panGesture.view) + let location = panGesture.location(in: panGesture.view) + if state == .full && translation.y <= 0 { + layoutAdapter.interactionConstraint?.constant = layoutAdapter.initialConst + return + } + + log.debug(""" + panel gesture(\(state):\(panGesture.state)) -- \ + translation = \(value(of: translation)), \ + location = \(value(of: location)), \ + velocity = \(value(of: velocity)) + """) + + if interactionInProgress == false, isAttracting == false, + let vc = ownerVC, vc.delegate?.floatingPanelShouldBeginDragging?(vc) == false { + return + } + + interruptAnimationIfNeeded() + + if panGesture.state == .began { + panningBegan(at: location) + return + } + + if shouldScrollViewHandleTouch(scrollView, point: location, velocity: value(of: velocity)) { + return + } + + switch panGesture.state { + case .changed: + if interactionInProgress == false { + startInteraction(with: translation, at: location) + } + panningChange(with: translation) + case .ended, .cancelled, .failed: + if interactionInProgress == false { + startInteraction(with: translation, at: location) + // Workaround: Prevent stopping the surface view b/w anchors if the pan gesture + // doesn't pass through .changed state after an interruptible animator is interrupted. + let diff = translation - .leastNonzeroMagnitude + layoutAdapter.updateInteractiveEdgeConstraint(diff: value(of: diff), + scrollingContent: true, + allowsRubberBanding: behaviorAdapter.allowsRubberBanding(for:)) + } + panningEnd(with: translation, velocity: velocity) + default: + break + } + default: + return + } + } + + private func interruptAnimationIfNeeded() { + if let animator = self.moveAnimator, animator.isRunning { + log.debug("the attraction animator interrupted!!!") + animator.stopAnimation(true) + endAttraction(false) + } + if let animator = self.transitionAnimator { + guard 0 >= layoutAdapter.offsetFromMostExpandedAnchor else { return } + log.debug("a panel animation(interruptible: \(animator.isInterruptible)) interrupted!!!") + if animator.isInterruptible { + animator.stopAnimation(false) + // A user can stop a panel at the nearest Y of a target position so this fine-tunes + // the a small gap between the presentation layer frame and model layer frame + // to unlock scroll view properly at finishAnimation(at:) + if abs(layoutAdapter.offsetFromMostExpandedAnchor) <= 1.0 { + layoutAdapter.surfaceLocation = layoutAdapter.surfaceLocation(for: layoutAdapter.mostExpandedState) + } + animator.finishAnimation(at: .current) + } else { + animator.stopAnimation(true) + } + } + } + + private func shouldScrollViewHandleTouch(_ scrollView: UIScrollView?, point: CGPoint, velocity: CGFloat) -> Bool { + // When no scrollView, nothing to handle. + guard let scrollView = scrollView else { return false } + + // For _UISwipeActionPanGestureRecognizer + if let scrollGestureRecognizers = scrollView.gestureRecognizers { + for gesture in scrollGestureRecognizers { + guard gesture.state == .began || gesture.state == .changed + else { continue } + + if gesture != scrollView.panGestureRecognizer { + return true + } + } + } + + guard + state == layoutAdapter.mostExpandedState, // When not top most(i.e. .full), don't scroll. + interactionInProgress == false, // When interaction already in progress, don't scroll. + 0 == layoutAdapter.offsetFromMostExpandedAnchor + else { + return false + } + + // When the current point is within grabber area but the initial point is not, do scroll. + if surfaceView.grabberAreaContains(point), !surfaceView.grabberAreaContains(initialLocation) { + return true + } + + // When the initial point is within grabber area and the current point is out of surface, don't scroll. + if surfaceView.grabberAreaContains(initialLocation), !surfaceView.frame.contains(point) { + return false + } + + let scrollViewFrame = scrollView.convert(scrollView.bounds, to: surfaceView) + guard + scrollViewFrame.contains(initialLocation), // When the initial point not in scrollView, don't scroll. + !surfaceView.grabberAreaContains(point) // When point within grabber area, don't scroll. + else { + return false + } + + let offset = value(of: scrollView.contentOffset - contentOffsetForPinning(of: scrollView)) + // The zero offset must be excluded because the offset is usually zero + // after a panel moves from half/tip to full. + switch layoutAdapter.position { + case .top, .left: + if offset < 0.0 { + return true + } + if velocity >= 0 { + return true + } + case .bottom, .right: + if offset > 0.0 { + return true + } + if velocity <= 0 { + return true + } + } + + if scrollView.isDecelerating { + return true + } + if let tableView = (scrollView as? UITableView), tableView.isEditing { + return true + } + + return false + } + + private func panningBegan(at location: CGPoint) { + // A user interaction does not always start from Began state of the pan gesture + // because it can be recognized in scrolling a content in a content view controller. + // So here just preserve the current state if needed. + log.debug("panningBegan -- location = \(value(of: location))") + + guard let scrollView = scrollView else { return } + if state == layoutAdapter.mostExpandedState { + if surfaceView.grabberAreaContains(location) { + initialScrollOffset = scrollView.contentOffset + } + } else { + initialScrollOffset = scrollView.contentOffset + } + } + + private func panningChange(with translation: CGPoint) { + log.debug("panningChange -- translation = \(value(of: translation))") + let pre = value(of: layoutAdapter.surfaceLocation) + let diff = value(of: translation - initialTranslation) + let next = pre + diff + + if !layoutAdapter.canGoAboveTheTopAnchor && pre <= layoutAdapter.surfaceLocation(for: .full).y && translation.y <= 0 { + guard (layoutAdapter.initialConst + diff) >= layoutAdapter.surfaceLocation(for: .full).y else { return } + } + + layoutAdapter.updateInteractiveEdgeConstraint(diff: diff, + scrollingContent: shouldScrollingContentInMoving(from: pre, to: next), + allowsRubberBanding: behaviorAdapter.allowsRubberBanding(for:)) + + let cur = value(of: layoutAdapter.surfaceLocation) + + backdropView.alpha = getBackdropAlpha(at: cur, with: value(of: translation)) + + guard (pre != cur) else { return } + + if let vc = ownerVC { + vc.delegate?.floatingPanelDidMove?(vc) + } + } + + private func shouldScrollingContentInMoving(from pre: CGFloat, to next: CGFloat) -> Bool { + // Don't allow scrolling if the initial panning location is in the grabber area. + if surfaceView.grabberAreaContains(initialLocation) { + return false + } + if let scrollView = scrollView, scrollView.panGestureRecognizer.state == .changed { + switch layoutAdapter.position { + case .top: + if pre > .zero, pre < next, + scrollView.contentSize.height > scrollView.bounds.height || scrollView.alwaysBounceVertical { + return true + } + case .left: + if pre > .zero, pre < next, + scrollView.contentSize.width > scrollView.bounds.width || scrollView.alwaysBounceHorizontal { + return true + } + case .bottom: + if pre > .zero, pre > next, + scrollView.contentSize.height > scrollView.bounds.height || scrollView.alwaysBounceVertical { + return true + } + case .right: + if pre > .zero, pre > next, + scrollView.contentSize.width > scrollView.bounds.width || scrollView.alwaysBounceHorizontal { + return true + } + } + } + return false + } + + private func panningEnd(with translation: CGPoint, velocity: CGPoint) { + log.debug("panningEnd -- translation = \(value(of: translation)), velocity = \(value(of: velocity))") + + if state == .hidden { + log.debug("Already hidden") + return + } + + stopScrollDeceleration = (0 > layoutAdapter.offsetFromMostExpandedAnchor + (1.0 / surfaceView.fp_displayScale)) // Projecting the dragging to the scroll dragging or not + if stopScrollDeceleration { + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + + self.stopScrolling(at: self.initialScrollOffset) + } + } + + let currentPos = value(of: layoutAdapter.surfaceLocation) + let mainVelocity = value(of: velocity) + var targetPosition = self.targetPosition(from: currentPos, with: mainVelocity) + + endInteraction(for: targetPosition) + + if isRemovalInteractionEnabled { + let distToHidden = CGFloat(abs(currentPos - layoutAdapter.position(for: .hidden))) + switch layoutAdapter.position { + case .top, .bottom: + removalVector = (distToHidden != 0) ? CGVector(dx: 0.0, dy: velocity.y/distToHidden) : .zero + case .left, .right: + removalVector = (distToHidden != 0) ? CGVector(dx: velocity.x/distToHidden, dy: 0.0) : .zero + } + if shouldRemove(with: removalVector) { + ownerVC?.remove() + return + } + } + + if let vc = ownerVC { + vc.delegate?.floatingPanelWillEndDragging?(vc, withVelocity: velocity, targetState: &targetPosition) + } + + guard shouldAttract(to: targetPosition) else { + if let vc = ownerVC { + vc.delegate?.floatingPanelDidEndDragging?(vc, willAttract: false) + } + + self.state = targetPosition + self.updateLayout(to: targetPosition) + self.unlockScrollView() + return + } + + if let vc = ownerVC { + vc.delegate?.floatingPanelDidEndDragging?(vc, willAttract: true) + } + + // Workaround: Disable a tracking scroll to prevent bouncing a scroll content in a panel animating + let isScrollEnabled = scrollView?.isScrollEnabled + if let scrollView = scrollView, targetPosition != layoutAdapter.mostExpandedState { + scrollView.isScrollEnabled = false + } + + startAttraction(to: targetPosition, with: velocity) + + // Workaround: Reset `self.scrollView.isScrollEnabled` + if let scrollView = scrollView, targetPosition != layoutAdapter.mostExpandedState, + let isScrollEnabled = isScrollEnabled { + scrollView.isScrollEnabled = isScrollEnabled + } + } + + // MARK: - Behavior + + private func shouldRemove(with velocityVector: CGVector) -> Bool { + guard let vc = ownerVC else { return false } + if let result = vc.delegate?.floatingPanel?(vc, shouldRemoveAt: vc.surfaceLocation, with: velocityVector) { + return result + } + let threshold = behaviorAdapter.removalInteractionVelocityThreshold + switch layoutAdapter.position { + case .top: + return (velocityVector.dy <= -threshold) + case .left: + return (velocityVector.dx <= -threshold) + case .bottom: + return (velocityVector.dy >= threshold) + case .right: + return (velocityVector.dx >= threshold) + } + } + + private func startInteraction(with translation: CGPoint, at location: CGPoint) { + /* Don't lock a scroll view to show a scroll indicator after hitting the top */ + log.debug("startInteraction -- translation = \(value(of: translation)), location = \(value(of: location))") + guard interactionInProgress == false else { return } + + var offset: CGPoint = .zero + + initialSurfaceLocation = layoutAdapter.surfaceLocation + if state == layoutAdapter.mostExpandedState, let scrollView = scrollView { + if surfaceView.grabberAreaContains(location) { + initialScrollOffset = scrollView.contentOffset + } else { + initialScrollOffset = scrollView.contentOffset + let offsetDiff = scrollView.contentOffset - contentOffsetForPinning(of: scrollView) + switch layoutAdapter.position { + case .top, .left: + // Fit the surface bounds to a scroll offset content by startInteraction(at:offset:) + if value(of: offsetDiff) > 0 { + offset = -offsetDiff + } + case .bottom, .right: + // Fit the surface bounds to a scroll offset content by startInteraction(at:offset:) + if value(of: offsetDiff) < 0 { + offset = -offsetDiff + } + } + } + log.debug("initial scroll offset --", initialScrollOffset) + } + + initialTranslation = translation + + if let vc = ownerVC { + vc.delegate?.floatingPanelWillBeginDragging?(vc) + } + + layoutAdapter.startInteraction(at: state, offset: offset) + + interactionInProgress = true + + lockScrollView() + } + + private func endInteraction(for targetPosition: FloatingPanelState) { + log.debug("endInteraction to \(targetPosition)") + + if let scrollView = scrollView { + log.debug("endInteraction -- scroll offset = \(scrollView.contentOffset)") + } + + interactionInProgress = false + + // Prevent to keep a scroll view indicator visible at the half/tip position + if targetPosition != layoutAdapter.mostExpandedState { + lockScrollView() + } + + layoutAdapter.endInteraction(at: targetPosition) + } + + private func tearDownActiveInteraction() { + guard panGestureRecognizer.isEnabled else { return } + // Cancel the pan gesture so that panningEnd(with:velocity:) is called + panGestureRecognizer.isEnabled = false + panGestureRecognizer.isEnabled = true + } + + private func shouldAttract(to targetState: FloatingPanelState) -> Bool { + if layoutAdapter.position(for: targetState) == value(of: layoutAdapter.surfaceLocation) { + return false + } + return true + } + + private func startAttraction(to targetPosition: FloatingPanelState, with velocity: CGPoint) { + log.debug("startAnimation to \(targetPosition) -- velocity = \(value(of: velocity))") + guard let vc = ownerVC else { return } + + isAttracting = true + vc.delegate?.floatingPanelWillBeginAttracting?(vc, to: targetPosition) + move(to: targetPosition, with: value(of: velocity)) { + self.endAttraction(true) + } + } + + private func move(to targetPosition: FloatingPanelState, with velocity: CGFloat, completion: @escaping (() -> Void)) { + let (animationConstraint, target) = layoutAdapter.setUpAttraction(to: targetPosition) + let initialData = NumericSpringAnimator.Data(value: animationConstraint.constant, velocity: velocity) + moveAnimator = NumericSpringAnimator( + initialData: initialData, + target: target, + displayScale: surfaceView.fp_displayScale, + decelerationRate: behaviorAdapter.springDecelerationRate, + responseTime: behaviorAdapter.springResponseTime, + update: { [weak self] data in + guard let self = self, + let ownerVC = self.ownerVC // Ensure the owner vc is existing for `layoutAdapter.surfaceLocation` + else { return } + animationConstraint.constant = data.value + let current = self.value(of: self.layoutAdapter.surfaceLocation) + let translation = data.value - initialData.value + self.backdropView.alpha = self.getBackdropAlpha(at: current, with: translation) + ownerVC.notifyDidMove() + }, + completion: { [weak self] in + guard let self = self, + self.ownerVC != nil else { return } + self.updateLayout(to: targetPosition) + completion() + }) + moveAnimator?.startAnimation() + state = targetPosition + } + + private func endAttraction(_ finished: Bool) { + self.isAttracting = false + self.moveAnimator = nil + + if let vc = ownerVC { + vc.delegate?.floatingPanelDidEndAttracting?(vc) + } + + if let scrollView = scrollView { + log.debug("finishAnimation -- scroll offset = \(scrollView.contentOffset)") + } + + stopScrollDeceleration = false + + log.debug(""" + finishAnimation -- state = \(state) \ + surface location = \(layoutAdapter.surfaceLocation) \ + edge most position = \(layoutAdapter.surfaceLocation(for: layoutAdapter.mostExpandedState)) + """) + if finished, state == layoutAdapter.mostExpandedState, abs(layoutAdapter.offsetFromMostExpandedAnchor) <= 1.0 { + unlockScrollView() + } + } + + func value(of point: CGPoint) -> CGFloat { + return layoutAdapter.position.mainLocation(point) + } + + func setValue(_ newValue: CGPoint, to point: inout CGPoint) { + switch layoutAdapter.position { + case .top, .bottom: + point.y = newValue.y + case .left, .right: + point.x = newValue.x + } + } + + // Distance travelled after decelerating to zero velocity at a constant rate. + // Refer to the slides p176 of [Designing Fluid Interfaces](https://developer.apple.com/videos/play/wwdc2018/803/) + private func project(initialVelocity: CGFloat, decelerationRate: CGFloat) -> CGFloat { + return (initialVelocity / 1000.0) * decelerationRate / (1.0 - decelerationRate) + } + + func targetPosition(from currentY: CGFloat, with velocity: CGFloat) -> (FloatingPanelState) { + log.debug("targetPosition -- currentY = \(currentY), velocity = \(velocity)") + + let sortedPositions = layoutAdapter.sortedAnchorStatesByCoordinate + + guard sortedPositions.count > 1 else { + return state + } + + // Projection + let decelerationRate = behaviorAdapter.momentumProjectionRate + let baseY = abs(layoutAdapter.position(for: layoutAdapter.leastExpandedState) - layoutAdapter.position(for: layoutAdapter.mostExpandedState)) + let vecY = velocity / baseY + var pY = project(initialVelocity: vecY, decelerationRate: decelerationRate) * baseY + currentY + + let distance = (currentY - layoutAdapter.position(for: state)) + let forwardY = velocity == 0 ? distance > 0 : velocity > 0 + + let segment = layoutAdapter.segment(at: pY, forward: forwardY) + + var fromPos: FloatingPanelState + var toPos: FloatingPanelState + + let (lowerPos, upperPos) = (segment.lower ?? sortedPositions.first!, segment.upper ?? sortedPositions.last!) + (fromPos, toPos) = forwardY ? (lowerPos, upperPos) : (upperPos, lowerPos) + + if behaviorAdapter.shouldProjectMomentum(to: toPos) == false { + log.debug("targetPosition -- negate projection: distance = \(distance)") + let segment = layoutAdapter.segment(at: currentY, forward: forwardY) + var (lowerPos, upperPos) = (segment.lower ?? sortedPositions.first!, segment.upper ?? sortedPositions.last!) + // Equate the segment out of {top,bottom} most state to the {top,bottom} most segment + if lowerPos == upperPos { + if forwardY { + upperPos = lowerPos.next(in: sortedPositions) + } else { + lowerPos = lowerPos.pre(in: sortedPositions) + } + } + (fromPos, toPos) = forwardY ? (lowerPos, upperPos) : (upperPos, lowerPos) + // Block a projection to a segment over the next from the current segment + // (= Trim pY with the current segment) + if forwardY { + pY = max(min(pY, layoutAdapter.position(for: toPos.next(in: sortedPositions))), layoutAdapter.position(for: fromPos)) + } else { + pY = max(min(pY, layoutAdapter.position(for: fromPos)), layoutAdapter.position(for: toPos.pre(in: sortedPositions))) + } + } + + // Redirection + let redirectionalProgress = max(min(behaviorAdapter.redirectionalProgress(from: fromPos, to: toPos), 1.0), 0.0) + let progress = abs(pY - layoutAdapter.position(for: fromPos)) / abs(layoutAdapter.position(for: fromPos) - layoutAdapter.position(for: toPos)) + return progress > redirectionalProgress ? toPos : fromPos + } + + // MARK: - ScrollView handling + + private func lockScrollView() { + guard let scrollView = scrollView else { return } + + if scrollView.isLocked { + log.debug("Already scroll locked.") + return + } + log.debug("lock scroll view") + + scrollBounce = scrollView.bounces + scrollIndictorVisible = scrollView.showsVerticalScrollIndicator + + scrollView.isDirectionalLockEnabled = true + scrollView.bounces = false + scrollView.showsVerticalScrollIndicator = false + } + + private func unlockScrollView() { + guard let scrollView = scrollView else { return } + log.debug("unlock scroll view") + + scrollView.isDirectionalLockEnabled = false +// scrollView.bounces = false + scrollView.alwaysBounceVertical = true + scrollView.showsVerticalScrollIndicator = scrollIndictorVisible + } + + private func stopScrolling(at contentOffset: CGPoint) { + // Must use setContentOffset(_:animated) to force-stop deceleration + guard let scrollView = scrollView else { return } + var offset = scrollView.contentOffset + setValue(contentOffset, to: &offset) + scrollView.setContentOffset(offset, animated: false) + } + + private func contentOffsetForPinning(of scrollView: UIScrollView) -> CGPoint { + if let vc = ownerVC, let origin = vc.delegate?.floatingPanel?(vc, contentOffsetForPinning: scrollView) { + return origin + } + switch layoutAdapter.position { + case .top: + return CGPoint(x: 0.0, y: scrollView.fp_contentOffsetMax.y) + case .left: + return CGPoint(x: scrollView.fp_contentOffsetMax.x, y: 0.0) + case .bottom: + return CGPoint(x: 0.0, y: 0.0 - scrollView.fp_contentInset.top) + case .right: + return CGPoint(x: 0.0 - scrollView.fp_contentInset.left, y: 0.0) + } + } + + private func allowScrollPanGesture(for scrollView: UIScrollView) -> Bool { + guard state == layoutAdapter.mostExpandedState else { return false } + var offsetY: CGFloat = 0 + switch layoutAdapter.position { + case .top, .left: + offsetY = value(of: scrollView.fp_contentOffsetMax - scrollView.contentOffset) + case .bottom, .right: + offsetY = value(of: scrollView.contentOffset - contentOffsetForPinning(of: scrollView)) + } + return offsetY <= -30.0 || offsetY > 0 + } + + // MARK: - UIPanGestureRecognizer Intermediation + override func responds(to aSelector: Selector!) -> Bool { + return super.responds(to: aSelector) || panGestureRecognizer.delegateProxy?.responds(to: aSelector) == true + } + + override func forwardingTarget(for aSelector: Selector!) -> Any? { + if panGestureRecognizer.delegateProxy?.responds(to: aSelector) == true { + return panGestureRecognizer.delegateProxy + } + return super.forwardingTarget(for: aSelector) + } +} + +/// A gesture recognizer that looks for panning (dragging) gestures in a panel. +public final class FloatingPanelPanGestureRecognizer: UIPanGestureRecognizer { + fileprivate weak var floatingPanel: Core? + fileprivate var initialLocation: CGPoint = .zero + + public override func touchesBegan(_ touches: Set, with event: UIEvent) { + super.touchesBegan(touches, with: event) + initialLocation = touches.first?.location(in: view) ?? .zero + if floatingPanel?.transitionAnimator != nil || floatingPanel?.moveAnimator != nil { + self.state = .began + } + } + /// The delegate of the gesture recognizer. + /// + /// - Note: The delegate is used by FloatingPanel itself. If you set your own delegate object, an + /// exception is raised. If you want to handle the methods of UIGestureRecognizerDelegate, you can use `delegateProxy`. + public override weak var delegate: UIGestureRecognizerDelegate? { + get { + return super.delegate + } + set { + guard newValue is Core else { + let exception = NSException(name: .invalidArgumentException, + reason: "FloatingPanelController's built-in pan gesture recognizer must have its controller as its delegate. Use 'delegateProxy' property.", + userInfo: nil) + exception.raise() + return + } + super.delegate = newValue + } + } + + /// An object to intercept the delegate of the gesture recognizer. + /// + /// If an object adopting `UIGestureRecognizerDelegate` is set, the delegate methods are proxied to it. + public weak var delegateProxy: UIGestureRecognizerDelegate? { + didSet { + self.delegate = floatingPanel // Update the cached IMP + } + } +} + +// MARK: - Animator + +private class NumericSpringAnimator: NSObject { + struct Data { + let value: CGFloat + let velocity: CGFloat + } + + private class UnfairLock { + var unfairLock = os_unfair_lock() + func lock() { + os_unfair_lock_lock(&unfairLock); + } + func tryLock() -> Bool { + return os_unfair_lock_trylock(&unfairLock); + } + func unlock() { + os_unfair_lock_unlock(&unfairLock); + } + } + + private(set) var isRunning = false + + private var lock = UnfairLock() + + private lazy var displayLink = CADisplayLink(target: self, selector: #selector(update(_:))) + + private var data: Data + + private let target: CGFloat + private let displayScale: CGFloat + private let zeta: CGFloat + private let omega: CGFloat + + private let update: ((_ data: Data) -> Void) + private let completion: (() -> Void) + + init(initialData: Data, + target: CGFloat, + displayScale: CGFloat, + decelerationRate: CGFloat, + responseTime: CGFloat, + update: @escaping ((_ data: Data) -> Void), + completion: @escaping (() -> Void)) { + + self.data = initialData + self.target = target + self.displayScale = displayScale + + let frequency = 1 / responseTime // oscillation frequency + let duration: CGFloat = 0.001 // millisecond + self.zeta = abs(initialData.velocity) > 300 ? CoreGraphics.log(decelerationRate) / (-2.0 * .pi * frequency * duration) : 1.0 + self.omega = 2.0 * .pi * frequency + + self.update = update + self.completion = completion + } + + @discardableResult + func startAnimation() -> Bool{ + lock.lock() + defer { lock.unlock() } + + if isRunning { + return false + } + log.debug("startAnimation --", displayLink) + isRunning = true + displayLink.add(to: RunLoop.main, forMode: .common) + return true + } + + func stopAnimation(_ withoutFinishing: Bool) { + let locked = lock.tryLock() + defer { + if locked { lock.unlock() } + } + + log.debug("stopAnimation --", displayLink) + isRunning = false + displayLink.invalidate() + if withoutFinishing { + return + } + completion() + } + + @objc + func update(_ displayLink: CADisplayLink) { + guard lock.tryLock() else { return } + defer { lock.unlock() } + + let pre = data.value + var cur = pre + var velocity = data.velocity + spring(x: &cur, + v: &velocity, + xt: target, + zeta: zeta, + omega: omega, + h: CGFloat(displayLink.targetTimestamp - displayLink.timestamp)) + data = Data(value: cur, velocity: velocity) + update(data) + if abs(target - data.value) <= (1 / displayScale), + abs(pre - data.value) / (1 / displayScale) <= 1 { + stopAnimation(false) + } + } + + /** + - Parameters: + - x: value + - v: velocity + - xt: target value + - zeta: damping ratio + - omega: angular frequency + - h: time step + */ + private func spring(x: inout CGFloat, v: inout CGFloat, xt: CGFloat, zeta: CGFloat, omega: CGFloat, h: CGFloat) { + let f = 1.0 + 2.0 * h * zeta * omega + let h2 = pow(h, 2) + let o2 = pow(omega, 2) + let det = f + h2 * o2 + x = (f * x + h * v + h2 * o2 * xt) / det + v = (v + h * o2 * (xt - x)) / det + } +} + +extension FloatingPanelController { + func suspendTransitionAnimator(_ suspended: Bool) { + self.floatingPanel.isSuspended = suspended + } + var transitionAnimator: UIViewPropertyAnimator? { + return self.floatingPanel.transitionAnimator + } +} diff --git a/Sources/Extensions.swift b/Sources/Extensions.swift new file mode 100644 index 00000000..229e798c --- /dev/null +++ b/Sources/Extensions.swift @@ -0,0 +1,274 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +// MARK: - CoreGraphics + +extension CGFloat { + /// Returns this value rounded to an logical pixel value by a display scale + func rounded(by displayScale: CGFloat) -> CGFloat { + return (self * displayScale).rounded(.toNearestOrAwayFromZero) / displayScale + } + func isEqual(to: CGFloat, on displayScale: CGFloat) -> Bool { + return self.rounded(by: displayScale) == to.rounded(by: displayScale) + } +} + +extension CGPoint { + static var leastNonzeroMagnitude: CGPoint { + return CGPoint(x: CGFloat.leastNonzeroMagnitude, y: CGFloat.leastNonzeroMagnitude) + } + + static func + (lhs: CGPoint, rhs: CGPoint) -> CGPoint { + return CGPoint(x: lhs.x + rhs.x, y: lhs.y + rhs.y) + } + + static func - (lhs: CGPoint, rhs: CGPoint) -> CGPoint { + return CGPoint(x: lhs.x - rhs.x, y: lhs.y - rhs.y) + } + + static prefix func - (point: CGPoint) -> CGPoint { + return CGPoint(x: -point.x, y: -point.y) + } +} + +// MARK: - UIKit + +protocol LayoutGuideProvider { + var topAnchor: NSLayoutYAxisAnchor { get } + var leftAnchor: NSLayoutXAxisAnchor { get } + var bottomAnchor: NSLayoutYAxisAnchor { get } + var rightAnchor: NSLayoutXAxisAnchor { get } + var widthAnchor: NSLayoutDimension { get } + var heightAnchor: NSLayoutDimension { get } +} +extension UILayoutGuide: LayoutGuideProvider {} +extension UIView: LayoutGuideProvider {} + +private class CustomLayoutGuide: LayoutGuideProvider { + let topAnchor: NSLayoutYAxisAnchor + let leftAnchor: NSLayoutXAxisAnchor + let bottomAnchor: NSLayoutYAxisAnchor + let rightAnchor: NSLayoutXAxisAnchor + let widthAnchor: NSLayoutDimension + let heightAnchor: NSLayoutDimension + init(topAnchor: NSLayoutYAxisAnchor, + leftAnchor: NSLayoutXAxisAnchor, + bottomAnchor: NSLayoutYAxisAnchor, + rightAnchor: NSLayoutXAxisAnchor, + widthAnchor: NSLayoutDimension, + heightAnchor: NSLayoutDimension) { + self.topAnchor = topAnchor + self.leftAnchor = leftAnchor + self.bottomAnchor = bottomAnchor + self.rightAnchor = rightAnchor + self.widthAnchor = widthAnchor + self.heightAnchor = heightAnchor + } +} + +extension UIViewController { + @objc var fp_safeAreaInsets: UIEdgeInsets { + if #available(iOS 11.0, *) { + return view.safeAreaInsets + } else { + return UIEdgeInsets(top: topLayoutGuide.length, + left: 0.0, + bottom: bottomLayoutGuide.length, + right: 0.0) + } + } + + var fp_safeAreaLayoutGuide: LayoutGuideProvider { + if #available(iOS 11.0, *) { + return view!.safeAreaLayoutGuide + } else { + return CustomLayoutGuide(topAnchor: topLayoutGuide.bottomAnchor, + leftAnchor: view.leftAnchor, + bottomAnchor: bottomLayoutGuide.topAnchor, + rightAnchor: view.rightAnchor, + widthAnchor: view.widthAnchor, + heightAnchor: topLayoutGuide.bottomAnchor.anchorWithOffset(to: bottomLayoutGuide.topAnchor)) + } + } +} + +// The reason why UIView has no extensions of safe area insets and top/bottom guides +// is for iOS10 compatibility. +extension UIView { + var fp_safeAreaLayoutGuide: LayoutGuideProvider { + if #available(iOS 11.0, *) { + return safeAreaLayoutGuide + } else { + return self + } + } + + var presentationFrame: CGRect { + return layer.presentation()?.frame ?? frame + } + + /// Returns non-zero displayScale + /// + /// On iOS 11 or earlier the `traitCollection.displayScale` of a view can be + /// 0.0(indicating unspecified) when its view hasn't been added yet into a view tree in a window. + /// So this method returns `UIScreen.main` scale if the scale value is zero, for testing mainly. + var fp_displayScale: CGFloat { + let ret = traitCollection.displayScale + if ret == 0.0 { + return UIScreen.main.scale + } + return ret + } +} + +extension UIView { + func disableAutoLayout() { + let frame = self.frame + translatesAutoresizingMaskIntoConstraints = true + self.frame = frame + } + func enableAutoLayout() { + translatesAutoresizingMaskIntoConstraints = false + } + + static func performWithLinear(startTime: Double = 0.0, relativeDuration: Double = 1.0, _ animations: @escaping (() -> Void)) { + UIView.animateKeyframes(withDuration: 0.0, delay: 0.0, options: [.calculationModeCubic], animations: { + UIView.addKeyframe(withRelativeStartTime: startTime, relativeDuration: relativeDuration, animations: animations) + }, completion: nil) + } +} + +#if __FP_LOG +extension UIGestureRecognizer.State: CustomDebugStringConvertible { + public var debugDescription: String { + switch self { + case .began: return "began" + case .changed: return "changed" + case .failed: return "failed" + case .cancelled: return "cancelled" + case .ended: return "ended" + case .possible: return "possible" + @unknown default: return "" + } + } +} +#endif + +extension UIScrollView { + var isLocked: Bool { + return !showsVerticalScrollIndicator && !bounces && isDirectionalLockEnabled + } + var fp_contentInset: UIEdgeInsets { + if #available(iOS 11.0, *) { + return adjustedContentInset + } else { + return contentInset + } + } + var fp_contentOffsetMax: CGPoint { + return CGPoint(x: max((contentSize.width + fp_contentInset.right) - bounds.width, 0.0), + y: max((contentSize.height + fp_contentInset.bottom) - bounds.height, 0.0)) + } +} + +extension UISpringTimingParameters { + public convenience init(decelerationRate: CGFloat, frequencyResponse: CGFloat, initialVelocity: CGVector = .zero) { + let dampingRatio = CoreGraphics.log(decelerationRate) / (-4 * .pi * 0.001) + self.init(dampingRatio: dampingRatio, frequencyResponse: frequencyResponse, initialVelocity: initialVelocity) + } + + public convenience init(dampingRatio: CGFloat, frequencyResponse: CGFloat, initialVelocity: CGVector = .zero) { + let mass = 1 as CGFloat + let stiffness = pow(2 * .pi / frequencyResponse, 2) * mass + let damp = 4 * .pi * dampingRatio * mass / frequencyResponse + self.init(mass: mass, stiffness: stiffness, damping: damp, initialVelocity: initialVelocity) + } +} + +extension NSLayoutConstraint { + static func activate(constraint: NSLayoutConstraint?) { + guard let constraint = constraint else { return } + self.activate([constraint]) + } + static func deactivate(constraint: NSLayoutConstraint?) { + guard let constraint = constraint else { return } + self.deactivate([constraint]) + } +} + +extension UIEdgeInsets { + var horizontalInset: CGFloat { + return self.left + self.right + } + var verticalInset: CGFloat { + return self.top + self.bottom + } +} + +extension UIBezierPath { + static func path(roundedRect rect: CGRect, appearance: SurfaceAppearance) -> UIBezierPath { + let cornerRadius = appearance.cornerRadius; + if #available(iOS 13.0, *) { + if appearance.cornerCurve == .circular { + let path = UIBezierPath() + let start = CGPoint(x: rect.minX + cornerRadius, y: rect.minY) + + path.move(to: start) + + path.addLine(to: CGPoint(x: rect.maxX - cornerRadius, y: rect.minY)) + if cornerRadius > 0 { + path .addArc(withCenter: CGPoint(x: rect.maxX - cornerRadius, + y: rect.minY + cornerRadius), + radius: cornerRadius, + startAngle: -0.5 * .pi, + endAngle: 0, + clockwise: true) + } + + path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - cornerRadius)) + + if cornerRadius > 0 { + path.addArc(withCenter: CGPoint(x: rect.maxX - cornerRadius, + y: rect.maxY - cornerRadius), + radius: cornerRadius, + startAngle: 0, + endAngle: .pi * 0.5, + clockwise: true) + } + + path.addLine(to: CGPoint(x: rect.minX + cornerRadius, y: rect.maxY)) + + if cornerRadius > 0 { + path.addArc(withCenter: CGPoint(x: rect.minX + cornerRadius, + y: rect.maxY - cornerRadius), + radius: cornerRadius, + startAngle: .pi * 0.5, + endAngle: .pi, + clockwise: true) + } + + path.addLine(to: CGPoint(x: rect.minX, y: rect.minY + cornerRadius)) + + if cornerRadius > 0 { + path.addArc(withCenter: CGPoint(x: rect.minX + cornerRadius, + y: rect.minY + cornerRadius), + radius: cornerRadius, + startAngle: .pi, + endAngle: .pi * 1.5, + clockwise: true) + } + + path.addLine(to: start) + + path.close() + + return path + } + } + return UIBezierPath(roundedRect: rect, + byRoundingCorners: [.allCorners], + cornerRadii: CGSize(width: cornerRadius, + height: cornerRadius)) + } +} diff --git a/Sources/FloatingPanel.docc/FloatingPanel.md b/Sources/FloatingPanel.docc/FloatingPanel.md new file mode 100644 index 00000000..a1e08abd --- /dev/null +++ b/Sources/FloatingPanel.docc/FloatingPanel.md @@ -0,0 +1,51 @@ +# ``FloatingPanel`` + +The new interface displays the related contents and utilities in parallel as a user wants. + +## Overview + +FloatingPanel is a simple and easy-to-use UI component for a new interface introduced in Apple Maps, Shortcuts and Stocks app. +The new interface displays the related contents and utilities in parallel as a user wants. + + +## Topics + +### Essentials + +- ``FloatingPanelController`` +- ``FloatingPanelControllerDelegate`` + +### Views + +- ``SurfaceView`` +- ``SurfaceAppearance`` +- ``BackdropView`` +- ``GrabberView`` + +### Gestures + +- ``FloatingPanelPanGestureRecognizer`` + +### Layouts and Anchors + +- ``FloatingPanelLayout`` +- ``FloatingPanelBottomLayout`` +- ``FloatingPanelLayoutAnchoring`` +- ``FloatingPanelLayoutAnchor`` +- ``FloatingPanelAdaptiveLayoutAnchor`` +- ``FloatingPanelIntrinsicLayoutAnchor`` + +### States + +- ``FloatingPanelState`` + +### Positions + +- ``FloatingPanelPosition`` +- ``FloatingPanelReferenceEdge`` +- ``FloatingPanelLayoutReferenceGuide`` + +### Behaviors + +- ``FloatingPanelBehavior`` +- ``FloatingPanelDefaultBehavior`` diff --git a/Sources/FloatingPanel.h b/Sources/FloatingPanel.h new file mode 100644 index 00000000..b72569ea --- /dev/null +++ b/Sources/FloatingPanel.h @@ -0,0 +1,11 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +#ifndef FloatingPanel_h +#define FloatingPanel_h + +#import + +FOUNDATION_EXPORT double FloatingPanelVersionNumber; +FOUNDATION_EXPORT const unsigned char FloatingPanelVersionString[]; + +#endif /* FloatingPanel_h */ diff --git a/Sources/GrabberView.swift b/Sources/GrabberView.swift new file mode 100644 index 00000000..75934fd2 --- /dev/null +++ b/Sources/GrabberView.swift @@ -0,0 +1,33 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// A view that presents a grabber handle in the surface of a panel. +@objc(FloatingPanelGrabberView) +public class GrabberView: UIView { + + public var barColor = UIColor(displayP3Red: 0.76, green: 0.77, blue: 0.76, alpha: 1.0) { didSet { backgroundColor = barColor } } + + required public init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + } + + init() { + super.init(frame: .zero) + backgroundColor = barColor + } + + public override func layoutSubviews() { + super.layoutSubviews() + render() + } + + public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { + let view = super.hitTest(point, with: event) + return view == self ? nil : view + } + + private func render() { + self.layer.masksToBounds = true + } +} diff --git a/Framework/Sources/Info.plist b/Sources/Info.plist similarity index 96% rename from Framework/Sources/Info.plist rename to Sources/Info.plist index e1fe4cfb..d04aed99 100644 --- a/Framework/Sources/Info.plist +++ b/Sources/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.0 + 2.5.4 CFBundleVersion $(CURRENT_PROJECT_VERSION) diff --git a/Sources/Layout.swift b/Sources/Layout.swift new file mode 100644 index 00000000..2aeeccbf --- /dev/null +++ b/Sources/Layout.swift @@ -0,0 +1,839 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// An interface for generating layout information for a panel. +@objc public protocol FloatingPanelLayout { + /// Returns the position of a panel in a `FloatingPanelController` view . + @objc var position: FloatingPanelPosition { get } + + /// Returns the initial state when a panel is presented. + @objc var initialState: FloatingPanelState { get } + + /// Returns the layout anchors to specify the snapping locations for each state. + @objc var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] { get } + + /// Returns layout constraints to determine the cross dimension of a panel. + @objc optional func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] + + /// Returns the alpha value of the backdrop of a panel for each state. + @objc optional func backdropAlpha(for state: FloatingPanelState) -> CGFloat + + /// Allow the panel sheet to go above the upper position when it is in `.full` state. + @objc optional var canGoAboveTheTopAnchor: Bool { get } +} + +/// A layout object that lays out a panel in bottom sheet style. +@objcMembers +open class FloatingPanelBottomLayout: NSObject, FloatingPanelLayout { + public override init() { + super.init() + } + open var initialState: FloatingPanelState { + return .half + } + + open var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 18.0, edge: .top, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 69.0, edge: .bottom, referenceGuide: .safeArea), + ] + } + + open var position: FloatingPanelPosition { + return .bottom + } + + open func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] { + return [ + surfaceView.leftAnchor.constraint(equalTo: view.fp_safeAreaLayoutGuide.leftAnchor, constant: 0.0), + surfaceView.rightAnchor.constraint(equalTo: view.fp_safeAreaLayoutGuide.rightAnchor, constant: 0.0), + ] + } + + open func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return state == .full ? 0.3 : 0.0 + } + + public var canGoAboveTheTopAnchor: Bool { + true + } +} + +struct LayoutSegment { + let lower: FloatingPanelState? + let upper: FloatingPanelState? +} + +class LayoutAdapter { + private unowned var vc: FloatingPanelController + private let defaultLayout = FloatingPanelBottomLayout() + + fileprivate var layout: FloatingPanelLayout { + didSet { + surfaceView.position = position + } + } + + private var surfaceView: SurfaceView { + return vc.surfaceView + } + private var backdropView: BackdropView { + return vc.backdropView + } + private var safeAreaInsets: UIEdgeInsets { + return vc.fp_safeAreaInsets + } + + var initialConst: CGFloat = 0.0 + + private var fixedConstraints: [NSLayoutConstraint] = [] + + private var stateConstraints: [FloatingPanelState: [NSLayoutConstraint]] = [:] + private var offConstraints: [NSLayoutConstraint] = [] + private var fitToBoundsConstraint: NSLayoutConstraint? + + private(set) var interactionConstraint: NSLayoutConstraint? + private(set) var attractionConstraint: NSLayoutConstraint? + + private var staticConstraint: NSLayoutConstraint? + + private var anchorStates: Set { + return Set(layout.anchors.keys) + } + + private var sortedAnchorStates: [FloatingPanelState] { + return anchorStates.sorted(by: { + return $0.order < $1.order + }) + } + + var initialState: FloatingPanelState { + layout.initialState + } + + var position: FloatingPanelPosition { + layout.position + } + + var validStates: Set { + return anchorStates.union([.hidden]) + } + + var sortedAnchorStatesByCoordinate: [FloatingPanelState] { + return anchorStates.sorted(by: { + switch position { + case .top, .left: + return $0.order < $1.order + case .bottom, .right: + return $0.order > $1.order + } + }) + } + + private var leastCoordinateState: FloatingPanelState { + return sortedAnchorStatesByCoordinate.first ?? .hidden + } + + private var mostCoordinateState: FloatingPanelState { + return sortedAnchorStatesByCoordinate.last ?? .hidden + } + + var canGoAboveTheTopAnchor: Bool { + layout.canGoAboveTheTopAnchor ?? true + } + + var leastExpandedState: FloatingPanelState { + if sortedAnchorStates.count == 1 { + return .hidden + } + return sortedAnchorStates.first ?? .hidden + } + + var mostExpandedState: FloatingPanelState { + if sortedAnchorStates.count == 1 { + return sortedAnchorStates[0] + } + return sortedAnchorStates.last ?? .hidden + } + + var adjustedContentInsets: UIEdgeInsets { + switch position { + case .top: + return UIEdgeInsets(top: safeAreaInsets.top, + left: 0.0, + bottom: 0.0, + right: 0.0) + case .left: + return UIEdgeInsets(top: 0.0, + left: safeAreaInsets.left, + bottom: 0.0, + right: 0.0) + case .bottom: + return UIEdgeInsets(top: 0.0, + left: 0.0, + bottom: safeAreaInsets.bottom, + right: 0.0) + case .right: + return UIEdgeInsets(top: 0.0, + left: 0.0, + bottom: 0.0, + right: safeAreaInsets.right) + } + } + + /* + Returns a constraint based value in the interaction and animation. + + So that it doesn't need to call `surfaceView.layoutIfNeeded()` + after every interaction and animation update. It has an effect on + the smooth interaction because the content view doesn't need to update + its layout frequently. + */ + var surfaceLocation: CGPoint { + get { + var pos: CGFloat + if let constraint = interactionConstraint { + pos = constraint.constant + } else if let animationConstraint = attractionConstraint, let anchor = layout.anchors[vc.state] { + switch position { + case .top, .bottom: + switch referenceEdge(of: anchor) { + case .top: + pos = animationConstraint.constant + if anchor.referenceGuide == .safeArea { + pos += safeAreaInsets.top + } + case .bottom: + pos = vc.view.bounds.height + animationConstraint.constant + if anchor.referenceGuide == .safeArea { + pos -= safeAreaInsets.bottom + } + default: + fatalError("Unsupported reference edges") + } + case .left, .right: + switch referenceEdge(of: anchor) { + case .left: + pos = animationConstraint.constant + if anchor.referenceGuide == .safeArea { + pos += safeAreaInsets.left + } + case .right: + pos = vc.view.bounds.width + animationConstraint.constant + if anchor.referenceGuide == .safeArea { + pos -= safeAreaInsets.right + } + default: + fatalError("Unsupported reference edges") + } + } + } else { + pos = edgePosition(surfaceView.frame).rounded(by: surfaceView.fp_displayScale) + } + switch position { + case .top, .bottom: + return CGPoint(x: 0.0, y: pos) + case .left, .right: + return CGPoint(x: pos, y: 0.0) + } + } + set { + let pos = position.mainLocation(newValue) + if let constraint = interactionConstraint { + constraint.constant = pos + } else if let animationConstraint = attractionConstraint, let anchor = layout.anchors[vc.state] { + let refEdge = referenceEdge(of: anchor) + switch refEdge { + case .top, .left: + animationConstraint.constant = pos + if anchor.referenceGuide == .safeArea { + animationConstraint.constant -= refEdge.inset(of: safeAreaInsets) + } + case .bottom, .right: + animationConstraint.constant = pos - position.mainDimension(vc.view.bounds.size) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant += refEdge.inset(of: safeAreaInsets) + } + } + } else { + switch position { + case .top: + return surfaceView.frame.origin.y = pos - surfaceView.bounds.height + case .left: + return surfaceView.frame.origin.x = pos - surfaceView.bounds.width + case .bottom: + return surfaceView.frame.origin.y = pos + case .right: + return surfaceView.frame.origin.x = pos + } + } + } + } + + var offsetFromMostExpandedAnchor: CGFloat { + switch position { + case .top, .left: + return edgePosition(surfaceView.presentationFrame) - position(for: mostExpandedState) + case .bottom, .right: + return position(for: mostExpandedState) - edgePosition(surfaceView.presentationFrame) + } + } + + private var hiddenAnchor: FloatingPanelLayoutAnchoring { + switch position { + case .top: + return FloatingPanelLayoutAnchor(absoluteInset: -100, edge: .top, referenceGuide: .superview) + case .left: + return FloatingPanelLayoutAnchor(absoluteInset: -100, edge: .left, referenceGuide: .superview) + case .bottom: + return FloatingPanelLayoutAnchor(absoluteInset: -100, edge: .bottom, referenceGuide: .superview) + case .right: + return FloatingPanelLayoutAnchor(absoluteInset: -100, edge: .right, referenceGuide: .superview) + } + } + + init(vc: FloatingPanelController, layout: FloatingPanelLayout) { + self.vc = vc + self.layout = layout + } + + func surfaceLocation(for state: FloatingPanelState) -> CGPoint { + let pos = position(for: state).rounded(by: surfaceView.fp_displayScale) + switch layout.position { + case .top, .bottom: + return CGPoint(x: 0.0, y: pos) + case .left, .right: + return CGPoint(x: pos, y: 0.0) + } + } + + func position(for state: FloatingPanelState) -> CGFloat { + let bounds = vc.view.bounds + let anchor = layout.anchors[state] ?? self.hiddenAnchor + + switch anchor { + case let anchor as FloatingPanelIntrinsicLayoutAnchor: + let intrinsicLength = position.mainDimension(surfaceView.intrinsicContentSize) + let diff = anchor.isAbsolute ? anchor.offset : intrinsicLength * anchor.offset + + switch position { + case .top, .left: + var base: CGFloat = 0.0 + if anchor.referenceGuide == .safeArea { + base += position.inset(safeAreaInsets) + } + return base + intrinsicLength - diff + case .bottom, .right: + var base = position.mainDimension(bounds.size) + if anchor.referenceGuide == .safeArea { + base -= position.inset(safeAreaInsets) + } + return base - intrinsicLength + diff + } + case let anchor as FloatingPanelAdaptiveLayoutAnchor: + let dimension = layout.position.mainDimension(anchor.contentLayoutGuide.layoutFrame.size) + let diff = anchor.distance(from: dimension) + var referenceBoundsLength = layout.position.mainDimension(bounds.size) + switch layout.position { + case .top, .left: + if anchor.referenceGuide == .safeArea { + referenceBoundsLength += position.inset(safeAreaInsets) + } + return dimension - diff + case .bottom, .right: + if anchor.referenceGuide == .safeArea { + referenceBoundsLength -= position.inset(safeAreaInsets) + } + return referenceBoundsLength - dimension + diff + } + case let anchor as FloatingPanelLayoutAnchor: + let referenceBounds = anchor.referenceGuide == .safeArea ? bounds.inset(by: safeAreaInsets) : bounds + let diff = anchor.isAbsolute ? anchor.inset : position.mainDimension(referenceBounds.size) * anchor.inset + switch anchor.referenceEdge { + case .top: + return referenceBounds.minY + diff + case .left: + return referenceBounds.minX + diff + case .bottom: + return referenceBounds.maxY - diff + case .right: + return referenceBounds.maxX - diff + } + default: + fatalError("Unsupported a FloatingPanelLayoutAnchoring object") + } + } + + func isIntrinsicAnchor(state: FloatingPanelState) -> Bool { + return layout.anchors[state] is FloatingPanelIntrinsicLayoutAnchor + } + + private func edgePosition(_ frame: CGRect) -> CGFloat { + switch position { + case .top: + return frame.maxY + case .left: + return frame.maxX + case .bottom: + return frame.minY + case .right: + return frame.minX + } + } + + private func referenceEdge(of anchor: FloatingPanelLayoutAnchoring) -> FloatingPanelReferenceEdge { + switch anchor { + case is FloatingPanelIntrinsicLayoutAnchor, + is FloatingPanelAdaptiveLayoutAnchor: + switch position { + case .top: return .top + case .left: return .left + case .bottom: return .bottom + case .right: return .right + } + case let anchor as FloatingPanelLayoutAnchor: + return anchor.referenceEdge + default: + fatalError("Unsupported a FloatingPanelLayoutAnchoring object") + } + } + + func prepareLayout() { + NSLayoutConstraint.deactivate(fixedConstraints) + + surfaceView.translatesAutoresizingMaskIntoConstraints = false + backdropView.translatesAutoresizingMaskIntoConstraints = false + + // Fixed constraints of surface and backdrop views + let surfaceConstraints: [NSLayoutConstraint] + if let constraints = layout.prepareLayout?(surfaceView: surfaceView, in: vc.view) { + surfaceConstraints = constraints + } else { + switch position { + case .top, .bottom: + surfaceConstraints = [ + surfaceView.leftAnchor.constraint(equalTo: vc.fp_safeAreaLayoutGuide.leftAnchor, constant: 0.0), + surfaceView.rightAnchor.constraint(equalTo: vc.fp_safeAreaLayoutGuide.rightAnchor, constant: 0.0), + ] + case .left, .right: + surfaceConstraints = [ + surfaceView.topAnchor.constraint(equalTo: vc.fp_safeAreaLayoutGuide.topAnchor, constant: 0.0), + surfaceView.bottomAnchor.constraint(equalTo: vc.fp_safeAreaLayoutGuide.bottomAnchor, constant: 0.0), + ] + } + } + let backdropConstraints = [ + backdropView.topAnchor.constraint(equalTo: vc.view.topAnchor, constant: 0.0), + backdropView.leftAnchor.constraint(equalTo: vc.view.leftAnchor,constant: 0.0), + backdropView.rightAnchor.constraint(equalTo: vc.view.rightAnchor, constant: 0.0), + backdropView.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor, constant: 0.0), + ] + + fixedConstraints = surfaceConstraints + backdropConstraints + + NSLayoutConstraint.deactivate(constraint: self.fitToBoundsConstraint) + self.fitToBoundsConstraint = nil + + if vc.contentMode == .fitToBounds { + switch position { + case .top: + fitToBoundsConstraint = surfaceView.topAnchor.constraint(equalTo: vc.view.topAnchor, constant: 0.0) + fitToBoundsConstraint?.identifier = "FloatingPanel-fit-to-top" + case .left: + fitToBoundsConstraint = surfaceView.leftAnchor.constraint(equalTo: vc.view.leftAnchor, constant: 0.0) + fitToBoundsConstraint?.identifier = "FloatingPanel-fit-to-left" + case .bottom: + fitToBoundsConstraint = surfaceView.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor, constant: 0.0) + fitToBoundsConstraint?.identifier = "FloatingPanel-fit-to-bottom" + case .right: + fitToBoundsConstraint = surfaceView.rightAnchor.constraint(equalTo: vc.view.rightAnchor, constant: 0.0) + fitToBoundsConstraint?.identifier = "FloatingPanel-fit-to-right" + } + fitToBoundsConstraint?.priority = .defaultHigh + } + + updateStateConstraints() + } + + private func updateStateConstraints() { + let allStateConstraints = stateConstraints.flatMap { $1 } + NSLayoutConstraint.deactivate(allStateConstraints + offConstraints) + stateConstraints.removeAll() + for state in layout.anchors.keys { + stateConstraints[state] = layout.anchors[state]? + .layoutConstraints(vc, for: position) + .map{ $0.identifier = "FloatingPanel-\(state)-constraint"; return $0 } + } + let hiddenAnchor = layout.anchors[.hidden] ?? self.hiddenAnchor + offConstraints = hiddenAnchor.layoutConstraints(vc, for: position) + offConstraints.forEach { + $0.identifier = "FloatingPanel-hidden-constraint" + } + } + + func startInteraction(at state: FloatingPanelState, offset: CGPoint = .zero) { + if let constraint = interactionConstraint { + initialConst = constraint.constant + return + } + + tearDownAttraction() + + NSLayoutConstraint.deactivate(stateConstraints.flatMap { $1 } + offConstraints) + + initialConst = edgePosition(surfaceView.frame) + offset.y + + let constraint: NSLayoutConstraint + switch position { + case .top: + constraint = surfaceView.bottomAnchor.constraint(equalTo: vc.view.topAnchor, constant: initialConst) + case .left: + constraint = surfaceView.rightAnchor.constraint(equalTo: vc.view.leftAnchor, constant: initialConst) + case .bottom: + constraint = surfaceView.topAnchor.constraint(equalTo: vc.view.topAnchor, constant: initialConst) + case .right: + constraint = surfaceView.leftAnchor.constraint(equalTo: vc.view.leftAnchor, constant: initialConst) + } + + constraint.priority = .required + constraint.identifier = "FloatingPanel-interaction" + + NSLayoutConstraint.activate([constraint]) + self.interactionConstraint = constraint + } + + func endInteraction(at state: FloatingPanelState) { + // Don't deactivate `interactiveTopConstraint` here because it leads to + // unsatisfiable constraints + + if self.interactionConstraint == nil { + // Activate `interactiveTopConstraint` for `fitToBounds` mode. + // It goes through this path when the pan gesture state jumps + // from .begin to .end. + startInteraction(at: state) + } + } + + func setUpAttraction(to state: FloatingPanelState) -> (NSLayoutConstraint, CGFloat) { + NSLayoutConstraint.deactivate(constraint: attractionConstraint) + + let anchor = layout.anchors[state] ?? self.hiddenAnchor + + NSLayoutConstraint.deactivate(stateConstraints.flatMap { $1 } + offConstraints) + NSLayoutConstraint.deactivate(constraint: interactionConstraint) + interactionConstraint = nil + + let layoutGuideProvider: LayoutGuideProvider + switch anchor.referenceGuide { + case .safeArea: + layoutGuideProvider = vc.fp_safeAreaLayoutGuide + case .superview: + layoutGuideProvider = vc.view + } + let currentY = position.mainLocation(surfaceLocation) + let baseHeight = position.mainDimension(vc.view.bounds.size) + + let animationConstraint: NSLayoutConstraint + var targetY = position(for: state) + + switch position { + case .top: + switch referenceEdge(of: anchor) { + case .top: + animationConstraint = surfaceView.bottomAnchor.constraint(equalTo: layoutGuideProvider.topAnchor, + constant: currentY) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant -= safeAreaInsets.top + targetY -= safeAreaInsets.top + } + case .bottom: + let baseHeight = vc.view.bounds.height + targetY = -(baseHeight - targetY) + animationConstraint = surfaceView.bottomAnchor.constraint(equalTo: layoutGuideProvider.bottomAnchor, + constant: -(baseHeight - currentY)) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant += safeAreaInsets.bottom + targetY += safeAreaInsets.bottom + + } + default: + fatalError("Unsupported reference edges") + } + case .left: + switch referenceEdge(of: anchor) { + case .left: + animationConstraint = surfaceView.rightAnchor.constraint(equalTo: layoutGuideProvider.leftAnchor, + constant: currentY) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant -= safeAreaInsets.right + targetY -= safeAreaInsets.right + } + case .right: + targetY = -(baseHeight - targetY) + animationConstraint = surfaceView.rightAnchor.constraint(equalTo: layoutGuideProvider.rightAnchor, + constant: -(baseHeight - currentY)) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant += safeAreaInsets.left + targetY += safeAreaInsets.left + } + default: + fatalError("Unsupported reference edges") + } + case .bottom: + switch referenceEdge(of: anchor) { + case .top: + animationConstraint = surfaceView.topAnchor.constraint(equalTo: layoutGuideProvider.topAnchor, + constant: currentY) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant -= safeAreaInsets.top + targetY -= safeAreaInsets.top + } + case .bottom: + targetY = -(baseHeight - targetY) + animationConstraint = surfaceView.topAnchor.constraint(equalTo: layoutGuideProvider.bottomAnchor, + constant: -(baseHeight - currentY)) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant += safeAreaInsets.bottom + targetY += safeAreaInsets.bottom + + } + default: + fatalError("Unsupported reference edges") + } + case .right: + switch referenceEdge(of: anchor) { + case .left: + animationConstraint = surfaceView.leftAnchor.constraint(equalTo: layoutGuideProvider.leftAnchor, + constant: currentY) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant -= safeAreaInsets.left + targetY -= safeAreaInsets.left + } + case .right: + targetY = -(baseHeight - targetY) + animationConstraint = surfaceView.leftAnchor.constraint(equalTo: layoutGuideProvider.rightAnchor, + constant: -(baseHeight - currentY)) + if anchor.referenceGuide == .safeArea { + animationConstraint.constant += safeAreaInsets.right + targetY += safeAreaInsets.right + } + default: + fatalError("Unsupported reference edges") + } + } + + animationConstraint.priority = .defaultHigh + animationConstraint.identifier = "FloatingPanel-attraction" + + NSLayoutConstraint.activate([animationConstraint]) + self.attractionConstraint = animationConstraint + return (animationConstraint, targetY) + } + + private func tearDownAttraction() { + NSLayoutConstraint.deactivate(constraint: attractionConstraint) + attractionConstraint = nil + } + + // The method is separated from prepareLayout(to:) for the rotation support + // It must be called in FloatingPanelController.traitCollectionDidChange(_:) + func updateStaticConstraint() { + NSLayoutConstraint.deactivate(constraint: staticConstraint) + staticConstraint = nil + + if vc.contentMode == .fitToBounds { + surfaceView.containerOverflow = 0 + return + } + + let anchor = layout.anchors[self.mostExpandedState]! + let surfaceAnchor = position.mainDimensionAnchor(surfaceView) + switch anchor { + case let anchor as FloatingPanelIntrinsicLayoutAnchor: + var constant = position.mainDimension(surfaceView.intrinsicContentSize) + if anchor.referenceGuide == .safeArea { + constant += position.inset(safeAreaInsets) + } + staticConstraint = surfaceAnchor.constraint(equalToConstant: constant) + case let anchor as FloatingPanelAdaptiveLayoutAnchor: + let constant: CGFloat + if anchor.referenceGuide == .safeArea { + constant = position.inset(safeAreaInsets) + } else { + constant = 0.0 + } + let baseAnchor = position.mainDimensionAnchor(anchor.contentLayoutGuide) + staticConstraint = surfaceAnchor.constraint(equalTo: baseAnchor, constant: constant) + default: + switch position { + case .top, .left: + staticConstraint = surfaceAnchor.constraint(equalToConstant: position(for: self.mostCoordinateState)) + case .bottom, .right: + let rootViewAnchor = position.mainDimensionAnchor(vc.view) + staticConstraint = rootViewAnchor.constraint(equalTo: surfaceAnchor, + constant: position(for: self.leastCoordinateState)) + } + } + + switch position { + case .top, .bottom: + staticConstraint?.identifier = "FloatingPanel-static-height" + case .left, .right: + staticConstraint?.identifier = "FloatingPanel-static-width" + } + + NSLayoutConstraint.activate(constraint: staticConstraint) + + surfaceView.containerOverflow = position.mainDimension(vc.view.bounds.size) + } + + func updateInteractiveEdgeConstraint(diff: CGFloat, scrollingContent: Bool, allowsRubberBanding: (UIRectEdge) -> Bool) { + defer { + log.debug("update surface location = \(surfaceLocation)") + } + + let minConst: CGFloat = position(for: leastCoordinateState) + let maxConst: CGFloat = position(for: mostCoordinateState) + + var const = initialConst + diff + + let base = position.mainDimension(vc.view.bounds.size) + // Rubber-banding top buffer + if allowsRubberBanding(.top), const < minConst { + let buffer = minConst - const + const = minConst - rubberBandEffect(for: buffer, base: base) + } + + // Rubber-banding bottom buffer + if allowsRubberBanding(.bottom), const > maxConst { + let buffer = const - maxConst + const = maxConst + rubberBandEffect(for: buffer, base: base) + } + + if scrollingContent { + const = min(max(const, minConst), maxConst) + } + + interactionConstraint?.constant = const + } + + // According to @chpwn's tweet: https://twitter.com/chpwn/status/285540192096497664 + // x = distance from the edge + // c = constant value, UIScrollView uses 0.55 + // d = dimension, either width or height + private func rubberBandEffect(for buffer: CGFloat, base: CGFloat) -> CGFloat { + return (1.0 - (1.0 / ((buffer * 0.55 / base) + 1.0))) * base + } + + func activateLayout(for state: FloatingPanelState, forceLayout: Bool = false) { + defer { + if forceLayout { + layoutSurfaceIfNeeded() + log.debug("activateLayout for \(state) -- surface.presentation = \(self.surfaceView.presentationFrame) surface.frame = \(self.surfaceView.frame)") + } else { + log.debug("activateLayout for \(state)") + } + } + + // Must deactivate `interactiveTopConstraint` here + NSLayoutConstraint.deactivate(constraint: self.interactionConstraint) + self.interactionConstraint = nil + + tearDownAttraction() + + NSLayoutConstraint.activate(fixedConstraints) + + if vc.contentMode == .fitToBounds { + NSLayoutConstraint.activate(constraint: self.fitToBoundsConstraint) + } + + var state = state + + if validStates.contains(state) == false { + state = layout.initialState + } + + // Recalculate the intrinsic size of a content view. This is because + // UIView.systemLayoutSizeFitting() returns a different size between an + // on-screen and off-screen view which includes + // UIStackView(i.e. Settings view in Samples.app) + updateStateConstraints() + + switch state { + case .hidden: + NSLayoutConstraint.activate(offConstraints) + default: + if let constraints = stateConstraints[state] { + NSLayoutConstraint.activate(constraints) + } else { + log.error("Couldn't find any constraints for \(state)") + } + } + } + + private func layoutSurfaceIfNeeded() { + #if !TEST + guard surfaceView.window != nil else { return } + #endif + surfaceView.superview?.layoutIfNeeded() + } + + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + return layout.backdropAlpha?(for: state) ?? defaultLayout.backdropAlpha(for: state) + } + + fileprivate func checkLayout() { + // Verify layout configurations + assert(anchorStates.count > 0) + assert(validStates.contains(layout.initialState), + "Does not include an initial state (\(layout.initialState)) in (\(validStates))") + /* This assertion does not work in a device rotating + let statePosOrder = activeStates.sorted(by: { position(for: $0) < position(for: $1) }) + assert(sortedDirectionalStates == statePosOrder, + "Check your layout anchors because the state order(\(statePosOrder)) must be (\(sortedDirectionalStates))).") + */ + } +} + +extension LayoutAdapter { + func segment(at pos: CGFloat, forward: Bool) -> LayoutSegment { + /// ----------------------->Y + /// --> forward <-- backward + /// |-------|===o===|-------| |-------|-------|===o===| + /// |-------|-------x=======| |-------|=======x-------| + /// |-------|-------|===o===| |-------|===o===|-------| + /// pos: o/x, segment: = + + let sortedStates = sortedAnchorStatesByCoordinate + + let upperIndex: Int? + if forward { + upperIndex = sortedStates.firstIndex(where: { pos < position(for: $0) }) + } else { + upperIndex = sortedStates.firstIndex(where: { pos <= position(for: $0) }) + } + + switch upperIndex { + case 0: + return LayoutSegment(lower: nil, upper: sortedStates.first) + case let upperIndex?: + return LayoutSegment(lower: sortedStates[upperIndex - 1], upper: sortedStates[upperIndex]) + default: + return LayoutSegment(lower: sortedStates[sortedStates.endIndex - 1], upper: nil) + } + } +} + +extension FloatingPanelController { + var _layout: FloatingPanelLayout { + get { + floatingPanel.layoutAdapter.layout + } + set { + floatingPanel.layoutAdapter.layout = newValue + floatingPanel.layoutAdapter.checkLayout() + } + } +} diff --git a/Sources/LayoutAnchoring.swift b/Sources/LayoutAnchoring.swift new file mode 100644 index 00000000..c20ad87a --- /dev/null +++ b/Sources/LayoutAnchoring.swift @@ -0,0 +1,201 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// An interface for implementing custom layout anchor objects. +@objc public protocol FloatingPanelLayoutAnchoring { + var referenceGuide: FloatingPanelLayoutReferenceGuide { get } + func layoutConstraints(_ fpc: FloatingPanelController, for position: FloatingPanelPosition) -> [NSLayoutConstraint] +} + +/// An object that defines how to settles a panel with insets from an edge of a reference rectangle. +@objc final public class FloatingPanelLayoutAnchor: NSObject, FloatingPanelLayoutAnchoring /*, NSCopying */ { + + /// Returns a layout anchor with the specified inset by an absolute value, edge and reference guide for a panel. + /// + /// The inset is an amount to inset a panel from an edge of the reference guide. The edge refers to a panel + /// positioning. + @objc public init(absoluteInset: CGFloat, edge: FloatingPanelReferenceEdge, referenceGuide: FloatingPanelLayoutReferenceGuide) { + self.inset = absoluteInset + self.referenceGuide = referenceGuide + self.referenceEdge = edge + self.isAbsolute = true + } + + /// Returns a layout anchor with the specified inset by a fractional value, edge and reference guide for a panel. + /// + /// The inset is an amount to inset a panel from the edge of the specified reference guide. The value is + /// a floating-point number in the range 0.0 to 1.0, where 0.0 represents zero distance from the edge and + /// 1.0 represents a distance to the opposite edge. + @objc public init(fractionalInset: CGFloat, edge: FloatingPanelReferenceEdge, referenceGuide: FloatingPanelLayoutReferenceGuide) { + self.inset = fractionalInset + self.referenceGuide = referenceGuide + self.referenceEdge = edge + self.isAbsolute = false + } + let inset: CGFloat + let isAbsolute: Bool + /// The reference rectangle area for the inset. + @objc public let referenceGuide: FloatingPanelLayoutReferenceGuide + @objc let referenceEdge: FloatingPanelReferenceEdge +} + +public extension FloatingPanelLayoutAnchor { + func layoutConstraints(_ vc: FloatingPanelController, for position: FloatingPanelPosition) -> [NSLayoutConstraint] { + let layoutGuide = referenceGuide.layoutGuide(vc: vc) + switch position { + case .top: + return layoutConstraints(layoutGuide, for: vc.surfaceView.bottomAnchor) + case .left: + return layoutConstraints(layoutGuide, for: vc.surfaceView.rightAnchor) + case .bottom: + return layoutConstraints(layoutGuide, for: vc.surfaceView.topAnchor) + case .right: + return layoutConstraints(layoutGuide, for: vc.surfaceView.leftAnchor) + } + } + + private func layoutConstraints(_ layoutGuide: LayoutGuideProvider, for edgeAnchor: NSLayoutYAxisAnchor) -> [NSLayoutConstraint] { + switch referenceEdge { + case .top: + if isAbsolute { + return [edgeAnchor.constraint(equalTo: layoutGuide.topAnchor, constant: inset)] + } + let offsetAnchor = layoutGuide.topAnchor.anchorWithOffset(to: edgeAnchor) + return [offsetAnchor.constraint(equalTo:layoutGuide.heightAnchor, multiplier: inset)] + case .bottom: + if isAbsolute { + return [layoutGuide.bottomAnchor.constraint(equalTo: edgeAnchor, constant: inset)] + } + let offsetAnchor = edgeAnchor.anchorWithOffset(to: layoutGuide.bottomAnchor) + return [offsetAnchor.constraint(equalTo: layoutGuide.heightAnchor, multiplier: inset)] + default: + fatalError("Unsupported reference edges") + } + } + + private func layoutConstraints(_ layoutGuide: LayoutGuideProvider, for edgeAnchor: NSLayoutXAxisAnchor) -> [NSLayoutConstraint] { + switch referenceEdge { + case .left: + if isAbsolute { + return [edgeAnchor.constraint(equalTo: layoutGuide.leftAnchor, constant: inset)] + } + let offsetAnchor = layoutGuide.leftAnchor.anchorWithOffset(to: edgeAnchor) + return [offsetAnchor.constraint(equalTo: layoutGuide.widthAnchor, multiplier: inset)] + case .right: + if isAbsolute { + return [layoutGuide.rightAnchor.constraint(equalTo: edgeAnchor, constant: inset)] + } + let offsetAnchor = edgeAnchor.anchorWithOffset(to: layoutGuide.rightAnchor) + return [offsetAnchor.constraint(equalTo: layoutGuide.widthAnchor, multiplier: inset)] + default: + fatalError("Unsupported reference edges") + } + } +} + +/// An object that defines how to settles a panel with the intrinsic size for a content. +@objc final public class FloatingPanelIntrinsicLayoutAnchor: NSObject, FloatingPanelLayoutAnchoring /*, NSCopying */ { + + /// Returns a layout anchor with the specified offset by an absolute value and reference guide for a panel. + /// + /// The offset is an amount to offset a position of panel that displays the entire content from an edge of + /// the reference guide. The edge refers to a panel positioning. + @objc public init(absoluteOffset offset: CGFloat, referenceGuide: FloatingPanelLayoutReferenceGuide = .safeArea) { + self.offset = offset + self.referenceGuide = referenceGuide + self.isAbsolute = true + } + + /// Returns a layout anchor with the specified offset by a fractional value and reference guide for a panel. + /// + /// The offset value is a floating-point number in the range 0.0 to 1.0, where 0.0 represents the full content + /// is displayed and 0.5 represents the half of content is displayed. + @objc public init(fractionalOffset offset: CGFloat, referenceGuide: FloatingPanelLayoutReferenceGuide = .safeArea) { + self.offset = offset + self.referenceGuide = referenceGuide + self.isAbsolute = false + } + let offset: CGFloat + let isAbsolute: Bool + + /// The reference rectangle area for the offset + @objc public let referenceGuide: FloatingPanelLayoutReferenceGuide +} + +public extension FloatingPanelIntrinsicLayoutAnchor { + func layoutConstraints(_ vc: FloatingPanelController, for position: FloatingPanelPosition) -> [NSLayoutConstraint] { + let surfaceIntrinsicLength = position.mainDimension(vc.surfaceView.intrinsicContentSize) + let constant = isAbsolute ? surfaceIntrinsicLength - offset : surfaceIntrinsicLength * (1 - offset) + let layoutGuide = referenceGuide.layoutGuide(vc: vc) + switch position { + case .top: + return [vc.surfaceView.bottomAnchor.constraint(equalTo: layoutGuide.topAnchor, constant: constant)] + case .left: + return [vc.surfaceView.rightAnchor.constraint(equalTo: layoutGuide.leftAnchor, constant: constant)] + case .bottom: + return [vc.surfaceView.topAnchor.constraint(equalTo: layoutGuide.bottomAnchor, constant: -constant)] + case .right: + return [vc.surfaceView.leftAnchor.constraint(equalTo: layoutGuide.rightAnchor, constant: -constant)] + } + } +} + +/// An object that defines how to settles a panel with a layout guide of a content view. +@objc final public class FloatingPanelAdaptiveLayoutAnchor: NSObject, FloatingPanelLayoutAnchoring /*, NSCopying */ { + + /// Returns a layout anchor with the specified offset by an absolute value, layout guide to display content and reference guide for a panel. + /// + /// The offset is an amount to offset a position of panel that displays the entire content of the specified guide from an edge of + /// the reference guide. The edge refers to a panel positioning. + @objc public init(absoluteOffset offset: CGFloat, contentLayout: UILayoutGuide, referenceGuide: FloatingPanelLayoutReferenceGuide = .safeArea) { + self.offset = offset + self.contentLayoutGuide = contentLayout + self.referenceGuide = referenceGuide + self.isAbsolute = true + + } + + /// Returns a layout anchor with the specified offset by a fractional value, layout guide to display content and reference guide for a panel. + /// + /// The offset value is a floating-point number in the range 0.0 to 1.0, where 0.0 represents the full content + /// is displayed and 0.5 represents the half of content is displayed. + @objc public init(fractionalOffset offset: CGFloat, contentLayout: UILayoutGuide, referenceGuide: FloatingPanelLayoutReferenceGuide = .safeArea) { + self.offset = offset + self.contentLayoutGuide = contentLayout + self.referenceGuide = referenceGuide + self.isAbsolute = false + } + fileprivate let offset: CGFloat + fileprivate let isAbsolute: Bool + let contentLayoutGuide: UILayoutGuide + @objc public let referenceGuide: FloatingPanelLayoutReferenceGuide +} + +public extension FloatingPanelAdaptiveLayoutAnchor { + func layoutConstraints(_ vc: FloatingPanelController, for position: FloatingPanelPosition) -> [NSLayoutConstraint] { + let layoutGuide = referenceGuide.layoutGuide(vc: vc) + let offsetAnchor: NSLayoutDimension + switch position { + case .top: + offsetAnchor = layoutGuide.topAnchor.anchorWithOffset(to: vc.surfaceView.bottomAnchor) + case .left: + offsetAnchor = layoutGuide.leftAnchor.anchorWithOffset(to: vc.surfaceView.rightAnchor) + case .bottom: + offsetAnchor = vc.surfaceView.topAnchor.anchorWithOffset(to: layoutGuide.bottomAnchor) + case .right: + offsetAnchor = vc.surfaceView.leftAnchor.anchorWithOffset(to: layoutGuide.rightAnchor) + } + if isAbsolute { + return [offsetAnchor.constraint(equalTo: position.mainDimensionAnchor(contentLayoutGuide), constant: -offset)] + } else { + return [offsetAnchor.constraint(equalTo: position.mainDimensionAnchor(contentLayoutGuide), multiplier: (1 - offset))] + } + } +} + +extension FloatingPanelAdaptiveLayoutAnchor { + func distance(from dimension: CGFloat) -> CGFloat { + return isAbsolute ? offset : dimension * offset + } +} diff --git a/Sources/LayoutReferences.swift b/Sources/LayoutReferences.swift new file mode 100644 index 00000000..ff9f874a --- /dev/null +++ b/Sources/LayoutReferences.swift @@ -0,0 +1,45 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// Constants that specify the edge of the container of a panel. +@objc public enum FloatingPanelReferenceEdge: Int { + case top + case left + case bottom + case right +} + +extension FloatingPanelReferenceEdge { + func inset(of insets: UIEdgeInsets) -> CGFloat { + switch self { + case .top: return insets.top + case .left: return insets.left + case .bottom: return insets.bottom + case .right: return insets.right + } + } + func mainDimension(_ size: CGSize) -> CGFloat { + switch self { + case .top, .bottom: return size.height + case .left, .right: return size.width + } + } +} + +/// Constants that specify a layout guide to lay out a panel. +@objc public enum FloatingPanelLayoutReferenceGuide: Int { + case superview = 0 + case safeArea = 1 +} + +extension FloatingPanelLayoutReferenceGuide { + func layoutGuide(vc: UIViewController) -> LayoutGuideProvider { + switch self { + case .safeArea: + return vc.fp_safeAreaLayoutGuide + case .superview: + return vc.view + } + } +} diff --git a/Sources/Logger.swift b/Sources/Logger.swift new file mode 100644 index 00000000..4e28839e --- /dev/null +++ b/Sources/Logger.swift @@ -0,0 +1,100 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import Foundation +import os.log + +// Must be a variable to use `hook` property in testing +var log = { + return Logger() +}() + +struct Logger { + private let osLog: OSLog + private let s = DispatchSemaphore(value: 1) + + enum Level: Int, Comparable { + case debug = 0 + case info = 1 + case warning = 2 + case error = 3 + + var displayName: String { + switch self { + case .debug: + return "Debug:" + case .info: + return "Info:" + case .warning: + return "Warning:" + case .error: + return "Error:" + } + } + @available(iOS 10.0, *) + var osLogType: OSLogType { + switch self { + case .debug: return .debug + case .info: return .info + case .warning: return .default + case .error: return .error + } + } + + static func < (lhs: Logger.Level, rhs: Logger.Level) -> Bool { + return lhs.rawValue < rhs.rawValue + } + } + + typealias Hook = ((String, Level) -> Void) + var hook: Hook? + + fileprivate init() { + osLog = OSLog(subsystem: "com.scenee.FloatingPanel", category: "FloatingPanel") + } + + private func log(_ level: Level, _ message: Any, _ arguments: [Any], tag: String, function: String, line: UInt) { + _ = s.wait(timeout: .now() + 0.033) + defer { s.signal() } + + let extraMessage: String = arguments.map({ String(describing: $0) }).joined(separator: " ") + let _tag = tag.isEmpty ? "" : "\(tag):" + let log: String = { + switch level { + case .debug: + return "\(level.displayName)\(_tag) \(message) \(extraMessage) (\(function):\(line))" + default: + return "\(level.displayName)\(_tag) \(message) \(extraMessage)" + } + }() + + hook?(log, level) + + os_log("%{public}@", log: osLog, type: level.osLogType, log) + } + + private func getPrettyFunction(_ function: String, _ file: String) -> String { + if let filename = file.split(separator: "/").last { + return filename + ":" + function + } else { + return file + ":" + function + } + } + + func debug(_ log: Any, _ arguments: Any..., tag: String = "", function: String = #function, file: String = #file, line: UInt = #line) { + #if __FP_LOG + self.log(.debug, log, arguments, tag: tag, function: getPrettyFunction(function, file), line: line) + #endif + } + + func info(_ log: Any, _ arguments: Any..., tag: String = "", function: String = #function, file: String = #file, line: UInt = #line) { + self.log(.info, log, arguments, tag: tag, function: getPrettyFunction(function, file), line: line) + } + + func warning(_ log: Any, _ arguments: Any..., function: String = #function, file: String = #file, line: UInt = #line) { + self.log(.warning, log, arguments, tag: "", function: getPrettyFunction(function, file), line: line) + } + + func error(_ log: Any, _ arguments: Any..., function: String = #function, file: String = #file, line: UInt = #line) { + self.log(.error, log, arguments, tag: "", function: getPrettyFunction(function, file), line: line) + } +} diff --git a/Sources/PassthroughView.swift b/Sources/PassthroughView.swift new file mode 100644 index 00000000..eef74209 --- /dev/null +++ b/Sources/PassthroughView.swift @@ -0,0 +1,17 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +@objc(FloatingPanelPassthroughView) +class PassthroughView: UIView { + public weak var eventForwardingView: UIView? + public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { + let hitView = super.hitTest(point, with: event) + switch hitView { + case self: + return eventForwardingView?.hitTest(self.convert(point, to: eventForwardingView), with: event) + default: + return hitView + } + } +} diff --git a/Sources/Position.swift b/Sources/Position.swift new file mode 100644 index 00000000..973bc55f --- /dev/null +++ b/Sources/Position.swift @@ -0,0 +1,50 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// Constants describing the position of a panel in a screen +@objc public enum FloatingPanelPosition: Int { + case top + case left + case bottom + case right +} + +extension FloatingPanelPosition { + func mainLocation(_ point: CGPoint) -> CGFloat { + switch self { + case .top, .bottom: return point.y + case .left, .right: return point.x + } + } + + func mainDimension(_ size: CGSize) -> CGFloat { + switch self { + case .top, .bottom: return size.height + case .left, .right: return size.width + } + } + + func mainDimensionAnchor(_ layoutGuide: LayoutGuideProvider) -> NSLayoutDimension { + switch self { + case .top, .bottom: return layoutGuide.heightAnchor + case .left, .right: return layoutGuide.widthAnchor + } + } + + func crossDimension(_ size: CGSize) -> CGFloat { + switch self { + case .top, .bottom: return size.width + case .left, .right: return size.height + } + } + + func inset(_ insets: UIEdgeInsets) -> CGFloat { + switch self { + case .top: return insets.top + case .left: return insets.left + case .bottom: return insets.bottom + case .right: return insets.right + } + } +} diff --git a/Sources/State.swift b/Sources/State.swift new file mode 100644 index 00000000..25d57b9d --- /dev/null +++ b/Sources/State.swift @@ -0,0 +1,64 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import Foundation + +/// An object that represents the display state of a panel in a screen. +@objc +open class FloatingPanelState: NSObject, NSCopying, RawRepresentable { + public typealias RawValue = String + + required public init?(rawValue: RawValue) { + self.order = 0 + self.rawValue = rawValue + super.init() + } + + @objc + public init(rawValue: RawValue, order: Int) { + self.rawValue = rawValue + self.order = order + super.init() + } + + /// The corresponding value of the raw type. + public let rawValue: RawValue + /// The sorting order for states + public let order: Int + + public func copy(with zone: NSZone? = nil) -> Any { + return self + } + + public override var description: String { + return rawValue + } + + public override var debugDescription: String { + return "" + } + + /// A panel state indicates the entire panel is shown. + @objc(Full) public static let full: FloatingPanelState = FloatingPanelState(rawValue: "full", order: 1000) + /// A panel state indicates the half of a panel is shown. + @objc(Half) public static let half: FloatingPanelState = FloatingPanelState(rawValue: "half", order: 500) + /// A panel state indicates the tip of a panel is shown. + @objc(Tip) public static let tip: FloatingPanelState = FloatingPanelState(rawValue: "tip", order: 100) + /// A panel state indicates it is hidden. + @objc(Hidden) public static let hidden: FloatingPanelState = FloatingPanelState(rawValue: "hidden", order: 0) +} + +extension FloatingPanelState { + func next(in states: [FloatingPanelState]) -> FloatingPanelState { + if let index = states.firstIndex(of: self), states.indices.contains(index + 1) { + return states[index + 1] + } + return self + } + + func pre(in states: [FloatingPanelState]) -> FloatingPanelState { + if let index = states.firstIndex(of: self), states.indices.contains(index - 1) { + return states[index - 1] + } + return self + } +} diff --git a/Sources/SurfaceView.swift b/Sources/SurfaceView.swift new file mode 100644 index 00000000..5c9404b1 --- /dev/null +++ b/Sources/SurfaceView.swift @@ -0,0 +1,468 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +/// An object for customizing the appearance of a surface view +@objc(FloatingPanelSurfaceAppearance) +@objcMembers +public class SurfaceAppearance: NSObject { + + /// An object that represents information to render a shadow + @objc(FloatingPanelSurfaceAppearanceShadow) + public class Shadow: NSObject { + /// A Boolean indicating whether a shadow is displayed. + @objc + public var hidden: Bool = false + + /// The color of a shadow. + @objc + public var color: UIColor = .black + + /// The offset (in points) of a shadow. + @objc + public var offset: CGSize = CGSize(width: 0.0, height: 1.0) + + /// The opacity of a shadow. + @objc + public var opacity: Float = 0.2 + + /// The blur radius (in points) used to render a shadow. + @objc + public var radius: CGFloat = 3 + + /// The inflated amount of a shadow prior to applying the blur. + @objc + public var spread: CGFloat = 0 + + } + /// The background color of a surface view + public var backgroundColor: UIColor? = { + if #available(iOS 13, *) { + return UIColor.systemBackground + } else { + return UIColor.white + } + }() + + /// The radius to use when drawing the top rounded corners. + /// + /// `self.contentView` is masked with the top rounded corners automatically on iOS 11 and later. + /// On iOS 10, they are not automatically masked because of a UIVisualEffectView issue. See https://forums.developer.apple.com/thread/50854 + public var cornerRadius: CGFloat = 0.0 + + /// Defines the curve used for rendering the rounded corners of the layer. + /// + /// Defaults to `.circular`. + @available(iOS 13.0, *) + public var cornerCurve: CALayerCornerCurve { + get { _cornerCurve ?? .circular } + set { _cornerCurve = newValue } + } + + private var _cornerCurve: CALayerCornerCurve? + + /// An array of shadows used to create drop shadows underneath a surface view. + public var shadows: [Shadow] = [Shadow()] + + /// The border width of a surface view. + public var borderColor: UIColor? + + /// The border color of a surface view. + public var borderWidth: CGFloat = 0.0 +} + +/// A view that presents a surface interface in a panel. +@objc(FloatingPanelSurfaceView) +@objcMembers +public class SurfaceView: UIView { + /// A `FloatingPanelGrabberView` object displayed at the top of the surface view. + /// + /// To use a custom grabber, hide this and then add it to the surface view at appropriate point. + public let grabberHandle = GrabberView() + + /// Offset of the grabber handle from the interactive edge. + public var grabberHandlePadding: CGFloat = 6.0 { didSet { + setNeedsUpdateConstraints() + } } + + /// The offset from the move edge to prevent the content scroll + public var grabberAreaOffset: CGFloat = 36.0 + + /// The grabber handle size + /// + /// On left/right positioned panel the width dimension is used as the height of ``grabberHandle``, and vice versa. + public var grabberHandleSize: CGSize = CGSize(width: 36.0, height: 5.0) { didSet { + setNeedsUpdateConstraints() + } } + + /// The content view to be assigned a view of the content view controller of `FloatingPanelController` + public weak var contentView: UIView? + + /// The content insets specifying the insets around the content view. + public var contentPadding: UIEdgeInsets = .zero { + didSet { + // Needs update constraints + self.setNeedsUpdateConstraints() + } + } + + public override var backgroundColor: UIColor? { + get { return appearance.backgroundColor } + set { appearance.backgroundColor = newValue; setNeedsLayout() } + } + + /// The appearance settings for a surface view. + public var appearance = SurfaceAppearance() { didSet { + shadowLayers = appearance.shadows.map { _ in CAShapeLayer() } + setNeedsLayout() + }} + + /// The margins to use when laying out the container view wrapping content. + public var containerMargins: UIEdgeInsets = .zero { didSet { + setNeedsUpdateConstraints() + } } + + /// The view that displays an actual surface shape. + /// + /// It renders the background color, border line and top rounded corners, + /// specified by other properties. The reason why they're not be applied to + /// a content view directly is because it avoids any side-effects to the + /// content view. + public let containerView: UIView = UIView() + + var containerOverflow: CGFloat = 0.0 { // Must not call setNeedsLayout() + didSet { + // Calling setNeedsUpdateConstraints() is necessary to fix a layout break + // when the contentMode is changed after laying out a panel, for instance, + // after calling viewDidAppear(_:) of the parent view controller. + setNeedsUpdateConstraints() + } + } + + var position: FloatingPanelPosition = .bottom { + didSet { + guard position != oldValue else { return } + NSLayoutConstraint.deactivate([grabberHandleEdgePaddingConstraint, + grabberHandleCenterConstraint, + grabberHandleWidthConstraint, + grabberHandleHeightConstraint]) + switch position { + case .top: + grabberHandleEdgePaddingConstraint = grabberHandle.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -grabberHandlePadding) + grabberHandleCenterConstraint = grabberHandle.centerXAnchor.constraint(equalTo: centerXAnchor) + grabberHandleWidthConstraint = grabberHandle.widthAnchor.constraint(equalToConstant: grabberHandleSize.width) + grabberHandleHeightConstraint = grabberHandle.heightAnchor.constraint(equalToConstant: grabberHandleSize.height) + case .left: + grabberHandleEdgePaddingConstraint = grabberHandle.rightAnchor.constraint(equalTo: rightAnchor, constant: -grabberHandlePadding) + grabberHandleCenterConstraint = grabberHandle.centerYAnchor.constraint(equalTo: centerYAnchor) + grabberHandleWidthConstraint = grabberHandle.widthAnchor.constraint(equalToConstant: grabberHandleSize.height) + grabberHandleHeightConstraint = grabberHandle.heightAnchor.constraint(equalToConstant: grabberHandleSize.width) + case .bottom: + grabberHandleEdgePaddingConstraint = grabberHandle.topAnchor.constraint(equalTo: topAnchor, constant: grabberHandlePadding) + grabberHandleCenterConstraint = grabberHandle.centerXAnchor.constraint(equalTo: centerXAnchor) + grabberHandleWidthConstraint = grabberHandle.widthAnchor.constraint(equalToConstant: grabberHandleSize.width) + grabberHandleHeightConstraint = grabberHandle.heightAnchor.constraint(equalToConstant: grabberHandleSize.height) + case .right: + grabberHandleEdgePaddingConstraint = grabberHandle.leftAnchor.constraint(equalTo: leftAnchor, constant: grabberHandlePadding) + grabberHandleCenterConstraint = grabberHandle.centerYAnchor.constraint(equalTo: centerYAnchor) + grabberHandleWidthConstraint = grabberHandle.widthAnchor.constraint(equalToConstant: grabberHandleSize.height) + grabberHandleHeightConstraint = grabberHandle.heightAnchor.constraint(equalToConstant: grabberHandleSize.width) + } + NSLayoutConstraint.activate([grabberHandleEdgePaddingConstraint, + grabberHandleCenterConstraint, + grabberHandleWidthConstraint, + grabberHandleHeightConstraint]) + setNeedsUpdateConstraints() + } + } + + var grabberAreaFrame: CGRect { + switch position { + case .top: + return CGRect(origin: .init(x: bounds.minX, y: bounds.maxY - grabberAreaOffset), + size: .init(width: bounds.width, height: grabberAreaOffset)) + case .left: + return CGRect(origin: .init(x: bounds.maxX - grabberAreaOffset, y: bounds.minY), + size: .init(width: grabberAreaOffset, height: bounds.height)) + case .bottom: + return CGRect(origin: CGPoint(x: bounds.minX, y: bounds.minY), + size: CGSize(width: bounds.width, height: grabberAreaOffset)) + case .right: + return CGRect(origin: .init(x: bounds.minX, y: bounds.minY), + size: .init(width: grabberAreaOffset, height: bounds.height)) + } + } + + private lazy var containerViewTopConstraint = containerView.topAnchor.constraint(equalTo: topAnchor, constant: 0.0) + private lazy var containerViewLeftConstraint = containerView.leftAnchor.constraint(equalTo: leftAnchor, constant: 0.0) + private lazy var containerViewBottomConstraint = containerView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0) + private lazy var containerViewRightConstraint = containerView.rightAnchor.constraint(equalTo: rightAnchor, constant: 0.0) + + /// The content view's top constraint + private var contentViewTopConstraint: NSLayoutConstraint? + /// The content view's left constraint + private var contentViewLeftConstraint: NSLayoutConstraint? + /// The content view's right constraint + private var contentViewRightConstraint: NSLayoutConstraint? + /// The content view's bottom constraint + private var contentViewBottomConstraint: NSLayoutConstraint? + + private lazy var grabberHandleWidthConstraint = grabberHandle.widthAnchor.constraint(equalToConstant: grabberHandleSize.width) + private lazy var grabberHandleHeightConstraint = grabberHandle.heightAnchor.constraint(equalToConstant: grabberHandleSize.height) + private lazy var grabberHandleCenterConstraint = grabberHandle.centerXAnchor.constraint(equalTo: centerXAnchor) + private lazy var grabberHandleEdgePaddingConstraint = grabberHandle.topAnchor.constraint(equalTo: topAnchor, constant: grabberHandlePadding) + + private var shadowLayers: [CALayer] = [] { + willSet { + for shadowLayer in shadowLayers { + shadowLayer.removeFromSuperlayer() + } + } + didSet { + for shadowLayer in shadowLayers { + layer.insertSublayer(shadowLayer, at: 0) + } + } + } + + public override class var requiresConstraintBasedLayout: Bool { return true } + + override init(frame: CGRect) { + super.init(frame: frame) + addSubViews() + } + + required public init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + addSubViews() + } + + private func addSubViews() { + super.backgroundColor = .clear + self.clipsToBounds = false + + addSubview(containerView) + containerView.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + containerViewTopConstraint, + containerViewLeftConstraint, + containerViewBottomConstraint, + containerViewRightConstraint, + ].map { + $0.identifier = "FloatingPanel-surface-container" + return $0; + }) + + addSubview(grabberHandle) + grabberHandle.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + grabberHandleEdgePaddingConstraint, + grabberHandleCenterConstraint, + grabberHandleWidthConstraint, + grabberHandleHeightConstraint, + ].map { + $0.identifier = "FloatingPanel-surface-grabber" + return $0; + }) + + shadowLayers = appearance.shadows.map { _ in CALayer() } + } + + public override func updateConstraints() { + switch position { + case .top: + containerViewTopConstraint.constant = (containerMargins.top == 0) ? -containerOverflow : containerMargins.top + containerViewLeftConstraint.constant = containerMargins.left + containerViewRightConstraint.constant = -containerMargins.right + containerViewBottomConstraint.constant = -containerMargins.bottom + case .left: + containerViewTopConstraint.constant = containerMargins.top + containerViewLeftConstraint.constant = (containerMargins.left == 0) ? -containerOverflow : containerMargins.left + containerViewRightConstraint.constant = -containerMargins.right + containerViewBottomConstraint.constant = -containerMargins.bottom + case .bottom: + containerViewTopConstraint.constant = containerMargins.top + containerViewLeftConstraint.constant = containerMargins.left + containerViewRightConstraint.constant = -containerMargins.right + containerViewBottomConstraint.constant = (containerMargins.bottom == 0) ? containerOverflow : -containerMargins.bottom + case .right: + containerViewTopConstraint.constant = containerMargins.top + containerViewLeftConstraint.constant = containerMargins.left + containerViewRightConstraint.constant = (containerMargins.right == 0) ? containerOverflow : -containerMargins.right + containerViewBottomConstraint.constant = -containerMargins.bottom + } + + contentViewTopConstraint?.constant = containerMargins.top + contentPadding.top + contentViewLeftConstraint?.constant = containerMargins.left + contentPadding.left + contentViewRightConstraint?.constant = containerMargins.right + contentPadding.right + contentViewBottomConstraint?.constant = containerMargins.bottom + contentPadding.bottom + + switch position { + case .top, .left: + grabberHandleEdgePaddingConstraint.constant = -grabberHandlePadding + case .bottom, .right: + grabberHandleEdgePaddingConstraint.constant = grabberHandlePadding + } + + switch position { + case .top, .bottom: + grabberHandleWidthConstraint.constant = grabberHandleSize.width + grabberHandleHeightConstraint.constant = grabberHandleSize.height + case .left, .right: + grabberHandleWidthConstraint.constant = grabberHandleSize.height + grabberHandleHeightConstraint.constant = grabberHandleSize.width + } + + super.updateConstraints() + } + + public override func layoutSubviews() { + super.layoutSubviews() + log.debug("surface view frame = \(frame)") + + containerView.backgroundColor = appearance.backgroundColor + + updateCornerRadius() + updateShadow() + updateBorder() + + grabberHandle.layer.cornerRadius = grabberHandleSize.height / 2 + } + + public override var intrinsicContentSize: CGSize { + let fittingSize = UIView.layoutFittingCompressedSize + let contentSize = contentView?.systemLayoutSizeFitting(fittingSize) ?? .zero + return CGSize(width: containerMargins.horizontalInset + contentPadding.horizontalInset + contentSize.width, + height: containerMargins.verticalInset + contentPadding.verticalInset + contentSize.height) + } + + private func updateShadow() { + // Disable shadow animation when the surface's frame jumps to a new value. + CATransaction.begin() + CATransaction.setDisableActions(true) + for (i, shadow) in appearance.shadows.enumerated() { + let shadowLayer = shadowLayers[i] + + shadowLayer.backgroundColor = UIColor.clear.cgColor + shadowLayer.frame = layer.bounds + + let spread = shadow.spread + let shadowRect = containerView.frame.insetBy(dx: -spread, dy: -spread) + let shadowPath = UIBezierPath.path(roundedRect: shadowRect, + appearance: appearance) + shadowLayer.shadowPath = shadowPath.cgPath + shadowLayer.shadowColor = shadow.color.cgColor + shadowLayer.shadowOffset = shadow.offset + // A shadow.radius value isn't manipulated by a scale(i.e. the display scale). It should be applied to the value by itself. + shadowLayer.shadowRadius = shadow.radius + shadowLayer.shadowOpacity = shadow.opacity + + let mask = CAShapeLayer() + let path = UIBezierPath.path(roundedRect: containerView.frame, + appearance: appearance) + let size = window?.bounds.size ?? CGSize(width: 1000.0, height: 1000.0) + path.append(UIBezierPath(rect: layer.bounds.insetBy(dx: -size.width, + dy: -size.height))) + mask.fillRule = .evenOdd + mask.path = path.cgPath + if #available(iOS 13.0, *) { + containerView.layer.cornerCurve = appearance.cornerCurve + mask.cornerCurve = appearance.cornerCurve + } + shadowLayer.mask = mask + } + CATransaction.commit() + } + + private func updateCornerRadius() { + containerView.layer.cornerRadius = appearance.cornerRadius + guard containerView.layer.cornerRadius != 0.0 else { + containerView.layer.masksToBounds = false + return + } + containerView.layer.masksToBounds = true + if position.inset(containerMargins) != 0 { + if #available(iOS 11, *) { + containerView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner, + .layerMinXMaxYCorner, .layerMaxXMaxYCorner] + } + return + } + if #available(iOS 11, *) { + // Don't use `contentView.clipToBounds` because it prevents content view from expanding the height of a subview of it + // for the bottom overflow like Auto Layout settings of UIVisualEffectView in Main.storyboard of Example/Maps. + // Because the bottom of contentView must be fit to the bottom of a screen to work the `safeLayoutGuide` of a content VC. + switch position { + case .top: + containerView.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner] + case .left: + containerView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner] + case .bottom: + containerView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] + case .right: + containerView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner] + } + } else { + // Can't use `containerView.layer.mask` because of a UIVisualEffectView issue in iOS 10, https://forums.developer.apple.com/thread/50854 + // Instead, a user should display rounding corners appropriately. + } + } + + private func updateBorder() { + containerView.layer.borderColor = appearance.borderColor?.cgColor + containerView.layer.borderWidth = appearance.borderWidth + } + + func set(contentView: UIView, mode: FloatingPanelController.ContentMode) { + containerView.addSubview(contentView) + self.contentView = contentView + /* contentView.frame = bounds */ // MUST NOT: Because the top safe area inset of a content VC will be incorrect. + contentView.translatesAutoresizingMaskIntoConstraints = false + let topConstraint = contentView.topAnchor.constraint(equalTo: topAnchor, constant: containerMargins.top + contentPadding.top) + let leftConstraint = contentView.leftAnchor.constraint(equalTo: leftAnchor, constant: containerMargins.left + contentPadding.left) + let rightConstraint = rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: containerMargins.right + contentPadding.right) + let bottomConstraint = bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: containerMargins.bottom + contentPadding.bottom) + NSLayoutConstraint.activate([ + topConstraint, + leftConstraint, + rightConstraint, + bottomConstraint, + ].map { + switch mode { + case .static: + $0.priority = .required + // The reason why this priority is set to .required - 1 is #359, which fixed #294. + case .fitToBounds: + $0.priority = .required - 1 + } + $0.identifier = "FloatingPanel-surface-content" + return $0 + }) + self.contentViewTopConstraint = topConstraint + self.contentViewLeftConstraint = leftConstraint + self.contentViewRightConstraint = rightConstraint + self.contentViewBottomConstraint = bottomConstraint + } + + func hasStackView() -> Bool { + return contentView?.subviews.reduce(false) { $0 || ($1 is UIStackView) } ?? false + } + + func grabberAreaContains(_ location: CGPoint) -> Bool { + // Sometimes a dragging finger's location is out of surface frame. + let cappedLocation: CGPoint + // Because the maximum width / height is out of bounds in CGRect.contains(_:) + let adjustment = 1 / fp_displayScale + switch position { + case .top: + cappedLocation = CGPoint(x: location.x, y: min(location.y, bounds.height - adjustment)) + case .left: + cappedLocation = CGPoint(x: min(location.x, bounds.width - adjustment), y: location.y) + case .bottom: + cappedLocation = CGPoint(x: location.x, y: max(location.y, 0)) + case .right: + cappedLocation = CGPoint(x: max(location.x, 0), y: location.y) + } + return grabberAreaFrame.contains(cappedLocation) + } +} diff --git a/Sources/Transitioning.swift b/Sources/Transitioning.swift new file mode 100644 index 00000000..88ab2348 --- /dev/null +++ b/Sources/Transitioning.swift @@ -0,0 +1,149 @@ +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. + +import UIKit + +class ModalTransition: NSObject, UIViewControllerTransitioningDelegate { + func animationController(forPresented presented: UIViewController, + presenting: UIViewController, + source: UIViewController) -> UIViewControllerAnimatedTransitioning? { + return ModalPresentTransition() + } + + func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { + return ModalDismissTransition() + } + + func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? { + return PresentationController(presentedViewController: presented, presenting: presenting) + } +} + +class PresentationController: UIPresentationController { + override func presentationTransitionWillBegin() { + // Must call here even if duplicating on in containerViewWillLayoutSubviews() + // Because it let the panel present correctly with the presentation animation + addFloatingPanel() + } + + override func presentationTransitionDidEnd(_ completed: Bool) { + // For non-animated presentation + if let fpc = presentedViewController as? FloatingPanelController, fpc.state == .hidden { + fpc.show(animated: false, completion: nil) + } + } + + override func dismissalTransitionDidEnd(_ completed: Bool) { + if let fpc = presentedViewController as? FloatingPanelController { + // For non-animated dismissal + if fpc.state != .hidden { + fpc.hide(animated: false, completion: nil) + } + fpc.view.removeFromSuperview() + } + } + + override func containerViewWillLayoutSubviews() { + guard + let fpc = presentedViewController as? FloatingPanelController, + /** + This condition fixes https://github.com/SCENEE/FloatingPanel/issues/369. + The issue is that this method is called in presenting a + UIImagePickerViewController and then a FloatingPanelController + view is added unnecessarily. + */ + fpc.presentedViewController == nil + else { return } + + /* + * Layout the views managed by `FloatingPanelController` here for the + * sake of the presentation and dismissal modally from the controller. + */ + addFloatingPanel() + + // Forward touch events to the presenting view controller + (fpc.view as? PassthroughView)?.eventForwardingView = presentingViewController.view + } + + @objc func handleBackdrop(tapGesture: UITapGestureRecognizer) { + presentedViewController.dismiss(animated: true, completion: nil) + } + + private func addFloatingPanel() { + guard + let containerView = self.containerView, + let fpc = presentedViewController as? FloatingPanelController + else { fatalError() } + + containerView.addSubview(fpc.view) + fpc.view.frame = containerView.bounds + fpc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] + } +} + +class ModalPresentTransition: NSObject, UIViewControllerAnimatedTransitioning { + func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { + guard + let fpc = transitionContext?.viewController(forKey: .to) as? FloatingPanelController + else { fatalError()} + + let animator = fpc.animatorForPresenting(to: fpc.layout.initialState) + return TimeInterval(animator.duration) + } + + func interruptibleAnimator(using transitionContext: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating { + guard + let fpc = transitionContext.viewController(forKey: .to) as? FloatingPanelController + else { fatalError() } + + if let animator = fpc.transitionAnimator { + return animator + } + + fpc.suspendTransitionAnimator(true) + fpc.show(animated: true) { [weak fpc] in + fpc?.suspendTransitionAnimator(false) + transitionContext.completeTransition(!transitionContext.transitionWasCancelled) + } + guard let transitionAnimator = fpc.transitionAnimator else { + fatalError("The panel state must be `hidden` but it is `\(fpc.state)`") + } + return transitionAnimator + } + + func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { + self.interruptibleAnimator(using: transitionContext).startAnimation() + } +} + +class ModalDismissTransition: NSObject, UIViewControllerAnimatedTransitioning { + func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { + guard + let fpc = transitionContext?.viewController(forKey: .from) as? FloatingPanelController + else { fatalError()} + + let animator = fpc.animatorForDismissing(with: .zero) + return TimeInterval(animator.duration) + } + + func interruptibleAnimator(using transitionContext: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating { + guard + let fpc = transitionContext.viewController(forKey: .from) as? FloatingPanelController + else { fatalError() } + + if let animator = fpc.transitionAnimator { + return animator + } + + fpc.suspendTransitionAnimator(true) + fpc.hide(animated: true) { [weak fpc] in + fpc?.suspendTransitionAnimator(false) + transitionContext.completeTransition(!transitionContext.transitionWasCancelled) + } + return fpc.transitionAnimator! + } + + func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { + self.interruptibleAnimator(using: transitionContext).startAnimation() + } +} + diff --git a/Tests/ControllerTests.swift b/Tests/ControllerTests.swift new file mode 100644 index 00000000..480534f7 --- /dev/null +++ b/Tests/ControllerTests.swift @@ -0,0 +1,350 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import XCTest +@testable import FloatingPanel + +class ControllerTests: XCTestCase { + override func setUp() {} + override func tearDown() {} + + func test_warningRetainCycle() { + let exp = expectation(description: "Warning retain cycle") + exp.expectedFulfillmentCount = 2 // For layout & behavior logs + log.hook = {(log, level) in + if log.contains("A memory leak will occur by a retain cycle because") { + XCTAssert(level == .warning) + exp.fulfill() + } + } + let myVC = MyZombieViewController(nibName: nil, bundle: nil) + myVC.loadViewIfNeeded() + wait(for: [exp], timeout: 10) + } + + func test_addPanel() { + guard let rootVC = UIApplication.shared.keyWindow?.rootViewController else { fatalError() } + let fpc = FloatingPanelController() + fpc.addPanel(toParent: rootVC) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .half).y) + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .tip).y) + } + + @available(iOS 12.0, *) + func test_updateLayout_willTransition() { + class MyDelegate: FloatingPanelControllerDelegate { + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout { + if newCollection.userInterfaceStyle == .dark { + XCTFail() + } + return FloatingPanelBottomLayout() + } + } + let myDelegate = MyDelegate() + let fpc = FloatingPanelController(delegate: myDelegate) + let traitCollection = UITraitCollection(traitsFrom: [fpc.traitCollection, + UITraitCollection(userInterfaceStyle: .dark)]) + XCTAssertEqual(traitCollection.userInterfaceStyle, .dark) + } + + func test_moveTo() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + XCTAssertEqual(delegate.position, .hidden) + fpc.showForTest() + XCTAssertEqual(delegate.position, .half) + + fpc.hide() + XCTAssertEqual(delegate.position, .hidden) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.state, .full) + XCTAssertEqual(delegate.position, .full) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .full).y) + + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.state, .half) + XCTAssertEqual(delegate.position, .half) + + XCTAssertEqual(fpc.surfaceLocation, fpc.surfaceLocation(for: .half)) + + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.state, .tip) + XCTAssertEqual(delegate.position, .tip) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .tip).y) + + fpc.move(to: .hidden, animated: false) + XCTAssertEqual(fpc.state, .hidden) + XCTAssertEqual(delegate.position, .hidden) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .hidden).y) + + XCTContext.runActivity(named: "move to full(animated)") { act in + let exp = expectation(description: act.name) + fpc.move(to: .full, animated: true) { + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .full).y) + exp.fulfill() + } + XCTAssertEqual(fpc.state, .full) + XCTAssertEqual(delegate.position, .full) + wait(for: [exp], timeout: 1.0) + } + + XCTContext.runActivity(named: "move to half(animated)") { act in + let exp = expectation(description: act.name) + fpc.move(to: .half, animated: true) { + XCTAssertEqual(fpc.surfaceLocation, fpc.surfaceLocation(for: .half)) + exp.fulfill() + } + XCTAssertEqual(fpc.state, .half) + XCTAssertEqual(delegate.position, .half) + wait(for: [exp], timeout: 1.0) + } + + XCTContext.runActivity(named: "move to tip(animated)") { act in + let exp = expectation(description: act.name) + fpc.move(to: .tip, animated: true) { + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .tip).y) + exp.fulfill() + } + XCTAssertEqual(fpc.state, .tip) + XCTAssertEqual(delegate.position, .tip) + wait(for: [exp], timeout: 1.0) + } + + fpc.move(to: .hidden, animated: true) + XCTAssertEqual(fpc.state, .hidden) + XCTAssertEqual(delegate.position, .hidden) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .hidden).y) + } + + func test_moveTo_bottomEdge() { + class MyFloatingPanelTop2BottomLayout: FloatingPanelTop2BottomTestLayout { + override var initialState: FloatingPanelState { return .half } + } + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = MyFloatingPanelTop2BottomLayout() + XCTAssertEqual(delegate.position, .hidden) + fpc.showForTest() + XCTAssertEqual(delegate.position, .half) + + fpc.hide() + XCTAssertEqual(delegate.position, .hidden) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.state, .full) + XCTAssertEqual(delegate.position, .full) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .full).y) + + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.state, .half) + XCTAssertEqual(delegate.position, .half) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .half).y) + + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.state, .tip) + XCTAssertEqual(delegate.position, .tip) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .tip).y) + + fpc.move(to: .hidden, animated: false) + XCTAssertEqual(fpc.state, .hidden) + XCTAssertEqual(delegate.position, .hidden) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .hidden).y) + + XCTContext.runActivity(named: "move to full(animated)") { act in + let exp = expectation(description: act.name) + fpc.move(to: .full, animated: true) { + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .full).y) + exp.fulfill() + } + XCTAssertEqual(fpc.state, .full) + XCTAssertEqual(delegate.position, .full) + wait(for: [exp], timeout: 1.0) + } + + XCTContext.runActivity(named: "move to half(animated)") { act in + let exp = expectation(description: act.name) + fpc.move(to: .half, animated: true) { + XCTAssertEqual(fpc.surfaceLocation, fpc.surfaceLocation(for: .half)) + exp.fulfill() + } + XCTAssertEqual(fpc.state, .half) + XCTAssertEqual(delegate.position, .half) + wait(for: [exp], timeout: 1.0) + } + + XCTContext.runActivity(named: "move to tip(animated)") { act in + let exp = expectation(description: act.name) + fpc.move(to: .tip, animated: true) { + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .tip).y) + exp.fulfill() + } + XCTAssertEqual(fpc.state, .tip) + XCTAssertEqual(delegate.position, .tip) + wait(for: [exp], timeout: 1.0) + } + + fpc.move(to: .hidden, animated: true) + XCTAssertEqual(fpc.state, .hidden) + XCTAssertEqual(delegate.position, .hidden) + XCTAssertEqual(fpc.surfaceLocation.y, fpc.surfaceLocation(for: .hidden).y) + } + + func test_moveWithNearbyPosition() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + XCTAssertEqual(delegate.position, .hidden) + fpc.showForTest() + + XCTAssertEqual(fpc.nearbyState, .half) + + fpc.hide() + XCTAssertEqual(fpc.nearbyState, .tip) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.nearbyState, .full) + XCTAssertEqual(fpc.surfaceView.frame.minY, fpc.surfaceLocation(for: .full).y) + } + + func test_moveTo_didMoveDelegate() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + XCTAssertEqual(delegate.position, .hidden) + fpc.showForTest() + + XCTContext.runActivity(named: "move(to:animated:false") { act in + let exp = expectation(description: act.name) + exp.expectedFulfillmentCount = 1 + var count = 0 + delegate.didMoveCallback = { _ in + count += 1 + exp.fulfill() + } + fpc.move(to: .full, animated: false) + wait(for: [exp], timeout: 1.0) + + XCTAssertEqual(count, 1) + } + + XCTContext.runActivity(named: "move(to:animated:true)") { act in + let exp = expectation(description: act.name) + exp.assertForOverFulfill = false + exp.expectedFulfillmentCount = 1 + var count = 0 + delegate.didMoveCallback = { _ in + count += 1 + } + fpc.move(to: .half, animated: true) { + exp.fulfill() + } + wait(for: [exp], timeout: 1.0) + + XCTAssertGreaterThan(count, 1) + } + + XCTContext.runActivity(named: "move(to:animated:false) with animation") { act in + let exp = expectation(description: act.name) + exp.expectedFulfillmentCount = 1 + var count = 0 + delegate.didMoveCallback = { _ in + count += 1 + } + UIView.animate(withDuration: 0.3) { + fpc.move(to: .full, animated: false) { + exp.fulfill() + } + } + wait(for: [exp], timeout: 1.0) + + XCTAssertEqual(count, 1) + } + + XCTContext.runActivity(named: "move(to:animated:true) with animation") { act in + let exp = expectation(description: act.name) + exp.assertForOverFulfill = false + exp.expectedFulfillmentCount = 1 + var count = 0 + delegate.didMoveCallback = { _ in + count += 1 + } + UIView.animate(withDuration: 0.3) { + fpc.move(to: .half, animated: true) { + exp.fulfill() + } + } + wait(for: [exp], timeout: 1.0) + + XCTAssertGreaterThan(count, 1) + } + } + + func test_originSurfaceY() { + let fpc = FloatingPanelController(delegate: nil) + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + fpc.show(animated: false, completion: nil) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.surfaceLocation, fpc.surfaceLocation(for: .full)) + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.surfaceLocation, fpc.surfaceLocation(for: .half)) + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.surfaceLocation, fpc.surfaceLocation(for: .tip)) + fpc.move(to: .hidden, animated: false) + XCTAssertEqual(fpc.surfaceLocation, fpc.surfaceLocation(for: .hidden)) + } + + func test_contentMode() { + let fpc = FloatingPanelController(delegate: nil) + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + fpc.show(animated: false, completion: nil) + + fpc.contentMode = .static + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.surfaceView.frame.height, fpc.view.bounds.height - fpc.surfaceLocation(for: .full).y) + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.surfaceView.frame.height, fpc.view.bounds.height - fpc.surfaceLocation(for: .full).y) + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.surfaceView.frame.height, fpc.view.bounds.height - fpc.surfaceLocation(for: .full).y) + + fpc.contentMode = .fitToBounds + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.surfaceView.frame.height, fpc.view.bounds.height - fpc.surfaceLocation(for: .full).y) + fpc.move(to: .half, animated: false) + print(1 / fpc.surfaceView.fp_displayScale) + XCTAssertEqual(fpc.surfaceView.frame.height, fpc.view.bounds.height - fpc.surfaceLocation(for: .half).y) + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.surfaceView.frame.height, fpc.view.bounds.height - fpc.surfaceLocation(for: .tip).y) + } +} + +private class MyZombieViewController: UIViewController, FloatingPanelLayout, FloatingPanelBehavior, FloatingPanelControllerDelegate { + var fpc: FloatingPanelController? + override func viewDidLoad() { + fpc = FloatingPanelController(delegate: self) + fpc?.addPanel(toParent: self) + fpc?.layout = self + fpc?.behavior = self + } + var position: FloatingPanelPosition { + return .bottom + } + var initialState: FloatingPanelState { + return .half + } + + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: UIScreen.main.bounds.height == 667.0 ? 18.0 : 16.0, + edge: .top, + referenceGuide: .superview), + .half: FloatingPanelLayoutAnchor(absoluteInset: 250.0, + edge: .bottom, + referenceGuide: .superview), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 60.0, + edge: .bottom, + referenceGuide: .superview), + ] +} diff --git a/Tests/CoreTests.swift b/Tests/CoreTests.swift new file mode 100644 index 00000000..77b3c83c --- /dev/null +++ b/Tests/CoreTests.swift @@ -0,0 +1,784 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import XCTest +@testable import FloatingPanel + +class CoreTests: XCTestCase { + override func setUp() {} + override func tearDown() {} + + func test_scrolllock() { + let fpc = FloatingPanelController() + + let contentVC1 = UITableViewController(nibName: nil, bundle: nil) + XCTAssertEqual(contentVC1.tableView.showsVerticalScrollIndicator, true) + XCTAssertEqual(contentVC1.tableView.bounces, true) + fpc.set(contentViewController: contentVC1) + fpc.track(scrollView: contentVC1.tableView) + fpc.showForTest() + + XCTAssertEqual(fpc.state, .half) + XCTAssertEqual(contentVC1.tableView.showsVerticalScrollIndicator, false) + XCTAssertEqual(contentVC1.tableView.bounces, false) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(contentVC1.tableView.showsVerticalScrollIndicator, true) + XCTAssertEqual(contentVC1.tableView.bounces, true) + + fpc.move(to: .tip, animated: false) + XCTAssertEqual(contentVC1.tableView.showsVerticalScrollIndicator, false) + XCTAssertEqual(contentVC1.tableView.bounces, false) + + let exp1 = expectation(description: "move to full with animation") + fpc.move(to: .full, animated: true) { + XCTAssertEqual(contentVC1.tableView.showsVerticalScrollIndicator, true) + XCTAssertEqual(contentVC1.tableView.bounces, true) + exp1.fulfill() + } + wait(for: [exp1], timeout: 1.0) + + let exp2 = expectation(description: "move to tip with animation") + fpc.move(to: .tip, animated: false) { + XCTAssertEqual(contentVC1.tableView.showsVerticalScrollIndicator, false) + XCTAssertEqual(contentVC1.tableView.bounces, false) + exp2.fulfill() + } + wait(for: [exp2], timeout: 1.0) + + // Reset the content vc + let contentVC2 = UITableViewController(nibName: nil, bundle: nil) + XCTAssertEqual(contentVC2.tableView.showsVerticalScrollIndicator, true) + XCTAssertEqual(contentVC2.tableView.bounces, true) + fpc.set(contentViewController: contentVC2) + fpc.track(scrollView: contentVC2.tableView) + fpc.show(animated: false, completion: nil) + XCTAssertEqual(fpc.state, .half) + XCTAssertEqual(contentVC2.tableView.showsVerticalScrollIndicator, false) + XCTAssertEqual(contentVC2.tableView.bounces, false) + } + + func test_getBackdropAlpha_1positions() { + class FloatingPanelLayout1Positions: FloatingPanelLayout { + let initialState: FloatingPanelState = .full + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = + [.full: FloatingPanelLayoutAnchor(absoluteInset: 20.0, edge: .top, referenceGuide: .superview)] + } + + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout1Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos - 100.0, with: -100.0), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: 0), 0.3) + XCTAssertLessThan(fpc.floatingPanel.getBackdropAlpha(at: fullPos + 100.0, with: 100.0), 0.3) + } + + func test_getBackdropAlpha_1positionsWithInitialHidden() { + class FloatingPanelLayout2Positions: FloatingPanelTestLayout { + override var initialState: FloatingPanelState { .hidden } + override var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: fullInset, edge: .top, referenceGuide: referenceGuide), + ] + } + } + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout2Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let hiddenPos = fpc.surfaceLocation(for: .hidden).y + + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos - 100.0, with: -100.0), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: 0.0), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: hiddenPos, with: 100.0), 0.0) + } + + func test_getBackdropAlpha_2positions() { + class FloatingPanelLayout2Positions: FloatingPanelLayout { + let initialState: FloatingPanelState = .half + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 20.0, edge: .top, referenceGuide: .superview), + .half: FloatingPanelLayoutAnchor(absoluteInset: 250.0, edge: .bottom, referenceGuide: .superview), + ] + } + + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout2Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let distance1 = abs(halfPos - fullPos) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: 0.0), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + distance1 * 0.5, with: distance1), 0.3 * 0.5) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: distance1), 0.0) + + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: 0.0), 0.0) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + distance1 * 0.5, with: -0.5 * distance1), 0.3 * 0.5) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: -1 * distance1), 0.3) + } + + func test_getBackdropAlpha_2positionsWithHidden() { + class FloatingPanelLayout2Positions: FloatingPanelTestLayout { + override var initialState: FloatingPanelState { .hidden } + override var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: fullInset, edge: .top, referenceGuide: referenceGuide), + .hidden: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .bottom, referenceGuide: referenceGuide), + ] + } + } + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout2Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let hiddenPos = fpc.surfaceLocation(for: .hidden).y + + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos - 100.0, with: -100.0), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: 0.0), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: hiddenPos, with: 100.0), 0.0) + } + + func test_getBackdropAlpha_3positions() { + let fpc = FloatingPanelController() + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + let distance1 = abs(halfPos - fullPos) + let distance2 = abs(tipPos - halfPos) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: 0.0), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + distance1 * 0.5, with: distance1 * 0.5), 0.3 * 0.5) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: distance1), 0.0) + + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: 0.0), 0.0) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + distance1 * 0.5, with: -0.5 * distance1), 0.3 * 0.5) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: -1 * distance1), 0.3) + + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: tipPos, with: 0.0), 0.0) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos + distance2 * 0.5, with: -0.5 * distance2), 0.0) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: -1 * distance2), 0.0) + } + + + func test_updateBackdropAlpha() { + class BackdropTestLayout: FloatingPanelTestLayout { + override var initialState: FloatingPanelState { .hidden } + func backdropAlpha(for state: FloatingPanelState) -> CGFloat { + switch state { + case .full: return 0.3 + case .half: return 0.0 + case .tip: return 0.3 + default: return 0.0 + } + } + } + let fpc = FloatingPanelController() + fpc.layout = BackdropTestLayout() + + fpc.showForTest() + + fpc.move(to: .full, animated: false) + XCTAssertEqual(floor(fpc.backdropView.alpha * 1000_000) / 1000_000, 0.3) + + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.backdropView.alpha, 0.0) + + fpc.move(to: .tip, animated: false) + XCTAssertEqual(floor(fpc.backdropView.alpha * 1000_000) / 1000_000, 0.3) + + let exp1 = expectation(description: "move to full with animation") + fpc.move(to: .full, animated: true) { + exp1.fulfill() + } + wait(for: [exp1], timeout: 1.0) + XCTAssertEqual(floor(fpc.backdropView.alpha * 1000_000) / 1000_000, 0.3) + + let exp2 = expectation(description: "move to half with animation") + fpc.move(to: .half, animated: true) { + exp2.fulfill() + } + wait(for: [exp2], timeout: 1.0) + XCTAssertEqual(fpc.backdropView.alpha, 0.0) + + let exp3 = expectation(description: "move to tip with animation") + fpc.move(to: .tip, animated: true) { + exp3.fulfill() + } + fpc.contentMode = .fitToBounds + XCTAssertEqual(fpc.backdropView.alpha, 0.0) // Must not affect the backdrop alpha by changing the content mode + wait(for: [exp3], timeout: 1.0) + XCTAssertEqual(floor(fpc.backdropView.alpha * 1000_000) / 1000_000, 0.3) + } + + func test_targetPosition_1positions() { + class FloatingPanelLayout1Positions: FloatingPanelLayout { + let initialState: FloatingPanelState = .full + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] + = [.full: FloatingPanelLayoutAnchor(absoluteInset: 20.0, edge: .top, referenceGuide: .superview)] + } + + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout1Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + + fpc.move(to: .full, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 1000.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .full), // redirect + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + ]) + } + + func test_targetPosition_2positions() { + class FloatingPanelLayout2Positions: FloatingPanelLayout { + let initialState: FloatingPanelState = .half + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 20.0, edge: .top, referenceGuide: .superview), + .half: FloatingPanelLayoutAnchor(absoluteInset: 250.0, edge: .bottom, referenceGuide: .superview), + ] + } + + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout2Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + + fpc.move(to: .full, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 1000.0), .half), // project to half + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .half), // project to half + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, halfPos - 10.0, CGPoint(x: 0.0, y: -100.0), .half), // redirect + (#line, halfPos, CGPoint(x: 0.0, y: -1000.0), .full), // project to full + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .half), // redirect + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: -1000.0), .full), // project to full + ]) + fpc.move(to: .half, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 1000.0), .half), // project to half + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .half), // project to half + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, halfPos - 10.0, CGPoint(x: 0.0, y: -100.0), .half), // redirect + (#line, halfPos, CGPoint(x: 0.0, y: -1000.0), .full), // project to full + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .half), // redirect + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: -1000.0), .full), // project to full + ]) + } + + func test_targetPosition_2positionsWithHidden() { + class FloatingPanelLayout2Positions: FloatingPanelLayout { + let initialState: FloatingPanelState = .hidden + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 20.0, edge: .top, referenceGuide: .superview), + .hidden: FloatingPanelLayoutAnchor(absoluteInset: 0, edge: .bottom, referenceGuide: .superview), + ] + } + + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout2Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let hiddenPos = fpc.surfaceLocation(for: .hidden).y + + fpc.move(to: .full, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 1000.0), .hidden), // project to hidden + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .hidden), // project to hidden + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, hiddenPos - 10.0, CGPoint(x: 0.0, y: -100.0), .hidden), // redirect + (#line, hiddenPos, CGPoint(x: 0.0, y: -1000.0), .full), // project to full + (#line, hiddenPos, CGPoint(x: 0.0, y: -100.0), .hidden), + (#line, hiddenPos, CGPoint(x: 0.0, y: 0.0), .hidden), + (#line, hiddenPos, CGPoint(x: 0.0, y: 100.0), .hidden), + (#line, hiddenPos, CGPoint(x: 0.0, y: 1000.0), .hidden), // redirect + (#line, hiddenPos + 10.0, CGPoint(x: 0.0, y: -1000.0), .full), // project to full + ]) + fpc.move(to: .hidden, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 1000.0), .hidden), // project to hidden + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .hidden), // project to hidden + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, hiddenPos - 10.0, CGPoint(x: 0.0, y: -100.0), .hidden), // redirect + (#line, hiddenPos, CGPoint(x: 0.0, y: -1000.0), .full), // project to full + (#line, hiddenPos, CGPoint(x: 0.0, y: -100.0), .hidden), + (#line, hiddenPos, CGPoint(x: 0.0, y: 0.0), .hidden), + (#line, hiddenPos, CGPoint(x: 0.0, y: 100.0), .hidden), + (#line, hiddenPos, CGPoint(x: 0.0, y: 1000.0), .hidden), // redirect + (#line, hiddenPos + 10.0, CGPoint(x: 0.0, y: -1000.0), .full), // project to full + ]) + } + + func test_targetPosition_3positionsFromFull() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + // From .full + fpc.move(to: .full, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: -100.0), .full), // far from topMostState + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: 0.0), .full), // far from topMostState + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: 100.0), .full), // far from topMostState + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 3000.0), .half), // block projecting to tip at half + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 500.0), .half), // project to half + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .half), // block projecting to tip at half + (#line, fullPos, CGPoint(x: 0.0, y: 3000.0), .half), // block projecting to tip at half + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, halfPos - 10.0, CGPoint(x: 0.0, y: -100.0), .half), // redirect + (#line, halfPos, CGPoint(x: 0.0, y: -1000.0), .full), //project to full + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .tip), // project to tip + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: 100.0), .half), // redirect + (#line, tipPos - 10.0, CGPoint(x: 0.0, y: -100.0), .tip), // redirect + (#line, tipPos, CGPoint(x: 0.0, y: -3000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: -1000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: -500.0), .half), // project to half + (#line, tipPos, CGPoint(x: 0.0, y: -100.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 0.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 100.0), .tip), + (#line, tipPos + 10.0, CGPoint(x: 0.0, y: -3000.0), .half), // block projecting to full at half + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: -100.0), .tip), // far from bottomMostState + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: 0.0), .tip), // far from bottomMostState + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: 100.0), .tip), // far from bottomMostState + ]) + } + + func test_targetPosition_3positionsFromFull_bottomEdge() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3PositionsBottomEdge() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + // From .full + fpc.move(to: .full, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: -100.0), .tip), // far from topMostState + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: 0.0), .tip), // far from topMostState + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: 100.0), .tip), // far from topMostState + (#line, tipPos - 10.0, CGPoint(x: 0.0, y: 3000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: -100.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 0.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 500.0), .half), // project to half + (#line, tipPos, CGPoint(x: 0.0, y: 1000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: 3000.0), .half), // block projecting to full at half + (#line, tipPos + 10.0, CGPoint(x: 0.0, y: 100.0), .tip), // redirect + (#line, halfPos - 10.0, CGPoint(x: 0.0, y: -100.0), .half), // redirect + (#line, halfPos, CGPoint(x: 0.0, y: -1000.0), .tip), //project to tip + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .full), // project to full + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: 100.0), .half), // redirect + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: -100.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -3000.0), .half), // block projecting to tip at half + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .half), // block projecting to tip at half + (#line, fullPos, CGPoint(x: 0.0, y: -500.0), .half), // project to half + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: -3000.0), .half), // block projecting to tip at half + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: -100.0), .full), // far from bottomMostState + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: 0.0), .full), // far from bottomMostState + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: 100.0), .full), // far from bottomMostState + ]) + } + + func test_targetPosition_3positionsFromHalf() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + // From .half + fpc.move(to: .half, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: -100.0), .full), // far from topMostState + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: 0.0), .full), // far from topMostState + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: 100.0), .full), // far from topMostState + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 500.0), .half), // project to half + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .half), // block projecting to tip at half + (#line, fullPos, CGPoint(x: 0.0, y: 3000.0), .half), // block projecting to tip at half + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, halfPos - 10.0, CGPoint(x: 0.0, y: -100.0), .half), // redirect + (#line, halfPos, CGPoint(x: 0.0, y: -1000.0), .full),// project to full + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .tip), // project to tip + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: 100.0), .half), // redirect + (#line, tipPos - 10.0, CGPoint(x: 0.0, y: -100.0), .tip), // redirect + (#line, tipPos, CGPoint(x: 0.0, y: -3000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: -1000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: -500.0), .half), // project to half + (#line, tipPos, CGPoint(x: 0.0, y: -100.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 0.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 100.0), .tip), + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: -100.0), .tip), // far from bottomMostState + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: 0.0), .tip), // far from bottomMostState + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: 100.0), .tip), // far from bottomMostState + ]) + } + + func test_targetPosition_3positionsFromHalf_bottomEdge() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3PositionsBottomEdge() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + // From .half + fpc.move(to: .half, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: -100.0), .tip), // far from topMostState + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: 0.0), .tip), // far from topMostState + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: 100.0), .tip), // far from topMostState + (#line, tipPos, CGPoint(x: 0.0, y: -100.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 0.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 500.0), .half), // project to half + (#line, tipPos, CGPoint(x: 0.0, y: 1000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: 3000.0), .half), // block projecting to full at half + (#line, tipPos + 10.0, CGPoint(x: 0.0, y: 100.0), .tip), // redirect + (#line, halfPos - 10.0, CGPoint(x: 0.0, y: -100.0), .half), // redirect + (#line, halfPos, CGPoint(x: 0.0, y: -1000.0), .tip),// project to tip + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .full), // project to full + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: 100.0), .half), // redirect + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: -100.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -3000.0), .half), // block projecting to tip at half + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .half), // block projecting to tip at half + (#line, fullPos, CGPoint(x: 0.0, y: -500.0), .half), // project to half + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: -100.0), .full), // far from bottomMostState + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: 0.0), .full), // far from bottomMostState + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: 100.0), .full), // far from bottomMostState + ]) + } + + func test_targetPosition_3positionsFromTip() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3Positions() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + + // From .tip + fpc.move(to: .tip, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: -100.0), .full), // far from topMostState + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: 0.0), .full), // far from topMostState + (#line, fullPos - 500.0, CGPoint(x: 0.0, y: 100.0), .full), // far from topMostState + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 500.0), .half), // project to half + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .half), // block projecting to tip at half + (#line, fullPos, CGPoint(x: 0.0, y: 3000.0), .half), // block projecting to tip at half + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, halfPos - 10.0, CGPoint(x: 0.0, y: -100.0), .half), // redirect + (#line, halfPos, CGPoint(x: 0.0, y: -3000.0), .full), // project to full + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .tip), // project to tip + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: 100.0), .half), // redirect + (#line, tipPos - 10.0, CGPoint(x: 0.0, y: -100.0), .tip), // redirect + (#line, tipPos, CGPoint(x: 0.0, y: -3000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: -1000.0), .half), // block projecting to full at half + (#line, tipPos, CGPoint(x: 0.0, y: -500.0), .half), // project to half + (#line, tipPos, CGPoint(x: 0.0, y: -100.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 0.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 100.0), .tip), + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: -100.0), .tip), // far from bottomMostState + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: 0.0), .tip), // far from bottomMostState + (#line, tipPos + 500.0, CGPoint(x: 0.0, y: 100.0), .tip), // far from bottomMostState + ]) + } + + func test_targetPosition_3positionsFromTip_bottomEdge() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3PositionsBottomEdge() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + + // From .tip + fpc.move(to: .tip, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: -100.0), .tip), // far from topMostState + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: 0.0), .tip), // far from topMostState + (#line, tipPos - 500.0, CGPoint(x: 0.0, y: 100.0), .tip), // far from topMostState + (#line, tipPos, CGPoint(x: 0.0, y: -100.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 0.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 500.0), .half), // project to half + (#line, tipPos, CGPoint(x: 0.0, y: 1000.0), .half), // block projecting to tip at half + (#line, tipPos, CGPoint(x: 0.0, y: 3000.0), .half), // block projecting to tip at half + (#line, tipPos + 10.0, CGPoint(x: 0.0, y: 100.0), .tip), // redirect + (#line, halfPos - 10.0, CGPoint(x: 0.0, y: -100.0), .half), // redirect + (#line, halfPos, CGPoint(x: 0.0, y: -3000.0), .tip), // project to full + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .full), // project to tip + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: 100.0), .half), // redirect + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: -100.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -3000.0), .half), // block projecting to full at half + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .half), // block projecting to full at half + (#line, fullPos, CGPoint(x: 0.0, y: -500.0), .half), // project to half + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: -100.0), .full), // far from bottomMostState + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: 0.0), .full), // far from bottomMostState + (#line, fullPos + 500.0, CGPoint(x: 0.0, y: 100.0), .full), // far from bottomMostState + ]) + } + + func test_targetPosition_3positionsAllProjection() { + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3Positions() + fpc.behavior = FloatingPanelProjectableBehavior() + + fpc.showForTest() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + + // From .full + fpc.move(to: .full, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 3000.0), .tip), + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .tip), + (#line, fullPos, CGPoint(x: 0.0, y: 3000.0), .tip), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .tip), + (#line, halfPos, CGPoint(x: 0.0, y: -1000.0), .full), + (#line, tipPos, CGPoint(x: 0.0, y: -3000.0), .full), + (#line, tipPos, CGPoint(x: 0.0, y: -1000.0), .full), + (#line, tipPos + 10.0, CGPoint(x: 0.0, y: -3000.0), .full), + ]) + + // From .half + fpc.move(to: .tip, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .tip), + (#line, fullPos, CGPoint(x: 0.0, y: 3000.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: -3000.0), .full), + (#line, tipPos, CGPoint(x: 0.0, y: -1000.0), .full), + ]) + + // From .tip + fpc.move(to: .tip, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 3000.0), .tip), + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .tip), + (#line, fullPos, CGPoint(x: 0.0, y: 3000.0), .tip), + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .tip), + (#line, halfPos, CGPoint(x: 0.0, y: -1000.0), .full), + (#line, tipPos, CGPoint(x: 0.0, y: -3000.0), .full), + (#line, tipPos, CGPoint(x: 0.0, y: -1000.0), .full), + (#line, tipPos + 10.0, CGPoint(x: 0.0, y: -3000.0), .full), + ]) + } + + func test_targetPosition_3positionsWithHidden() { + class FloatingPanelLayout3PositionsWithHidden: FloatingPanelLayout { + let initialState: FloatingPanelState = .hidden + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 20.0, edge: .top, referenceGuide: .superview), + .half: FloatingPanelLayoutAnchor(absoluteInset: 250.0, edge: .bottom, referenceGuide: .superview), + .hidden: FloatingPanelLayoutAnchor(absoluteInset: 0, edge: .bottom, referenceGuide: .superview), + ] + } + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3PositionsWithHidden() + + fpc.showForTest() + XCTAssertEqual(fpc.state, .hidden) + + fpc.move(to: .full, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fpc.surfaceView.frame.minY, CGPoint(x: 0.0, y: 1000.0), .half), + ]) + fpc.move(to: .half, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fpc.surfaceView.frame.minY, CGPoint(x: 0.0, y: -100.0), .half), + (#line, fpc.surfaceView.frame.minY, CGPoint(x: 0.0, y: -1000.0), .full), + (#line, fpc.surfaceView.frame.minY, CGPoint(x: 0.0, y: 0.0), .half), + (#line, fpc.surfaceView.frame.minY, CGPoint(x: 0.0, y: 1000.0), .hidden), + ]) + } + + func test_targetPosition_3positionsWithHiddenWithoutFull() { + class FloatingPanelLayout3Positions: FloatingPanelLayout { + let initialState: FloatingPanelState = .hidden + let position: FloatingPanelPosition = .bottom + let anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] = [ + .half: FloatingPanelLayoutAnchor(absoluteInset: 250.0, edge: .bottom, referenceGuide: .superview), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 60.0, edge: .bottom, referenceGuide: .superview), + .hidden: FloatingPanelLayoutAnchor(absoluteInset: 0, edge: .bottom, referenceGuide: .superview), + ] + } + + let delegate = FloatingPanelTestDelegate() + let fpc = FloatingPanelController(delegate: delegate) + fpc.layout = FloatingPanelLayout3Positions() + + fpc.showForTest() + fpc.behavior = FloatingPanelProjectableBehavior() + XCTAssertEqual(fpc.state, .hidden) + + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + //let hiddenPos = fpc.surfaceLocation(for: .hidden) + + fpc.move(to: .half, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, halfPos, CGPoint(x: 0.0, y: -100.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 0.0), .half), + (#line, halfPos, CGPoint(x: 0.0, y: 385.0), .tip), // projection + (#line, halfPos, CGPoint(x: 0.0, y: 1000.0), .hidden), // projection + (#line, halfPos + 10.0, CGPoint(x: 0.0, y: 100.0), .half), // redirection + (#line, tipPos - 10.0, CGPoint(x: 0.0, y: -100.0), .tip), // redirection + (#line, tipPos, CGPoint(x: 0.0, y: -3000.0), .half), //projection + (#line, tipPos, CGPoint(x: 0.0, y: -10.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 0.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 10.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 1000.0), .hidden), //projection + (#line, tipPos + 10.0, CGPoint(x: 0.0, y: 10.0), .tip), // redirection + (#line, tipPos - 10.0, CGPoint(x: 0.0, y: 10.0), .tip), // redirection + ]) + fpc.move(to: .tip, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, tipPos, CGPoint(x: 0.0, y: -100.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: -1000.0), .half), + (#line, tipPos, CGPoint(x: 0.0, y: 0.0), .tip), + (#line, tipPos, CGPoint(x: 0.0, y: 1000.0), .hidden), + ]) + } + + func test_keep_pan_gesture_disabled() { + let fpc = FloatingPanelController() + fpc.panGestureRecognizer.isEnabled = false + fpc.showForTest() + XCTAssertFalse(fpc.panGestureRecognizer.isEnabled) + } +} + +private class FloatingPanelLayout3Positions: FloatingPanelTestLayout { + override var initialState: FloatingPanelState { + return .tip + } +} + +private class FloatingPanelLayout3PositionsBottomEdge: FloatingPanelTop2BottomTestLayout { + override var initialState: FloatingPanelState { + return .tip + } +} + +private typealias TestParameter = (UInt, CGFloat, CGPoint, FloatingPanelState) +private func assertTargetPosition(_ floatingPanel: Core, with params: [TestParameter]) { + params.forEach { (line, pos, velocity, result) in + floatingPanel.surfaceView.frame.origin.y = pos + XCTAssertEqual(floatingPanel.targetPosition(from: pos, with: velocity.y), result, line: line) + } +} diff --git a/Tests/ExtensionTests.swift b/Tests/ExtensionTests.swift new file mode 100644 index 00000000..b2409abf --- /dev/null +++ b/Tests/ExtensionTests.swift @@ -0,0 +1,12 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import XCTest +@testable import FloatingPanel + +class ExtensionTests: XCTestCase { + func test_roundedByDisplayScale() { + XCTAssertEqual(CGFloat(333.222).rounded(by: 3), 333.3333333333333) + XCTAssertNotEqual(CGFloat(333.5).rounded(by: 3), 333.66666666666674) + XCTAssertTrue(CGFloat(333.5).isEqual(to: 333.66666666666674, on: 3.0)) + } +} diff --git a/Framework/Tests/Info.plist b/Tests/Info.plist similarity index 100% rename from Framework/Tests/Info.plist rename to Tests/Info.plist diff --git a/Tests/LayoutTests.swift b/Tests/LayoutTests.swift new file mode 100644 index 00000000..c94a2bb6 --- /dev/null +++ b/Tests/LayoutTests.swift @@ -0,0 +1,652 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import XCTest +@testable import FloatingPanel + +class LayoutTests: XCTestCase { + var fpc: FloatingPanelController! + override func setUp() { + fpc = FloatingPanelController(delegate: nil) + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + } + override func tearDown() {} + + func test_layoutAdapter_topAndBottomMostState() { + XCTAssertEqual(fpc.floatingPanel.layoutAdapter.mostExpandedState, .full) + XCTAssertEqual(fpc.floatingPanel.layoutAdapter.leastExpandedState, .tip) + + class FloatingPanelLayoutWithHidden: FloatingPanelLayout { + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 18.0, edge: .top, referenceGuide: .safeArea), + .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + .hidden: FloatingPanelLayoutAnchor(absoluteInset: 0, edge: .bottom, referenceGuide: .superview) + ] + let initialState: FloatingPanelState = .hidden + let position: FloatingPanelPosition = .bottom + } + class FloatingPanelLayout2Positions: FloatingPanelLayout { + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: 69.0, edge: .bottom, referenceGuide: .safeArea), + ] + let initialState: FloatingPanelState = .tip + let position: FloatingPanelPosition = .bottom + } + fpc.layout = FloatingPanelLayoutWithHidden() + XCTAssertEqual(fpc.floatingPanel.layoutAdapter.mostExpandedState, .full) + XCTAssertEqual(fpc.floatingPanel.layoutAdapter.leastExpandedState, .hidden) + + fpc.layout = FloatingPanelLayout2Positions() + XCTAssertEqual(fpc.floatingPanel.layoutAdapter.mostExpandedState, .half) + XCTAssertEqual(fpc.floatingPanel.layoutAdapter.leastExpandedState, .tip) + } + + func test_layoutSegment_3position() { + class FloatingPanelLayout3Positions: FloatingPanelTestLayout { + override var initialState: FloatingPanelState { .half } + } + + fpc.layout = FloatingPanelLayout3Positions() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + let tipPos = fpc.surfaceLocation(for: .tip).y + + let minPos = CGFloat.leastNormalMagnitude + let maxPos = CGFloat.greatestFiniteMagnitude + + assertLayoutSegment(fpc.floatingPanel, with: [ + (#line, pos: minPos, forwardY: true, lower: nil, upper: .full), + (#line, pos: minPos, forwardY: false, lower: nil, upper: .full), + (#line, pos: fullPos, forwardY: true, lower: .full, upper: .half), + (#line, pos: fullPos, forwardY: false, lower: nil, upper: .full), + (#line, pos: halfPos, forwardY: true, lower: .half, upper: .tip), + (#line, pos: halfPos, forwardY: false, lower: .full, upper: .half), + (#line, pos: tipPos, forwardY: true, lower: .tip, upper: nil), + (#line, pos: tipPos, forwardY: false, lower: .half, upper: .tip), + (#line, pos: maxPos, forwardY: true, lower: .tip, upper: nil), + (#line, pos: maxPos, forwardY: false, lower: .tip, upper: nil), + ]) + } + + func test_layoutSegment_2positions() { + class FloatingPanelLayout2Positions: FloatingPanelTestLayout { + override var initialState: FloatingPanelState { .half } + override var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] + { super.anchors.filter { (key, _) in key != .tip } } + } + + fpc.layout = FloatingPanelLayout2Positions() + + let fullPos = fpc.surfaceLocation(for: .full).y + let halfPos = fpc.surfaceLocation(for: .half).y + + let minPos = CGFloat.leastNormalMagnitude + let maxPos = CGFloat.greatestFiniteMagnitude + + assertLayoutSegment(fpc.floatingPanel, with: [ + (#line, pos: minPos, forwardY: true, lower: nil, upper: .full), + (#line, pos: minPos, forwardY: false, lower: nil, upper: .full), + (#line, pos: fullPos, forwardY: true, lower: .full, upper: .half), + (#line, pos: fullPos, forwardY: false, lower: nil, upper: .full), + (#line, pos: halfPos, forwardY: true, lower: .half, upper: nil), + (#line, pos: halfPos, forwardY: false, lower: .full, upper: .half), + (#line, pos: maxPos, forwardY: true, lower: .half, upper: nil), + (#line, pos: maxPos, forwardY: false, lower: .half, upper: nil), + ]) + } + + func test_layoutSegment_1positions() { + class FloatingPanelLayout1Positions: FloatingPanelTestLayout { + override var initialState: FloatingPanelState { .full } + override var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] + { super.anchors.filter { (key, _) in key == .full } } + } + + fpc.layout = FloatingPanelLayout1Positions() + + let fullPos = fpc.surfaceLocation(for: .full).y + + let minPos = CGFloat.leastNormalMagnitude + let maxPos = CGFloat.greatestFiniteMagnitude + + assertLayoutSegment(fpc.floatingPanel, with: [ + (#line, pos: minPos, forwardY: true, lower: nil, upper: .full), + (#line, pos: minPos, forwardY: false, lower: nil, upper: .full), + (#line, pos: fullPos, forwardY: true, lower: .full, upper: nil), + (#line, pos: fullPos, forwardY: false, lower: nil, upper: .full), + (#line, pos: maxPos, forwardY: true, lower: .full, upper: nil), + (#line, pos: maxPos, forwardY: false, lower: .full, upper: nil), + ]) + } + + func test_updateInteractiveEdgeConstraint() { + fpc.showForTest() + fpc.move(to: .full, animated: false) + + fpc.floatingPanel.layoutAdapter.startInteraction(at: fpc.state) + fpc.floatingPanel.layoutAdapter.startInteraction(at: fpc.state) // Should be ignore + + let fullPos = fpc.surfaceLocation(for: .full).y + let tipPos = fpc.surfaceLocation(for: .tip).y + + var next: CGFloat + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: -100.0, + scrollingContent: true, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: 100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos + 100.0) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: tipPos - fullPos, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, tipPos) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: tipPos - fullPos + 100.0, + scrollingContent: true, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, tipPos) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: tipPos - fullPos + 100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, tipPos + 100.0) + + fpc.floatingPanel.layoutAdapter.endInteraction(at: fpc.state) + } + + func test_updateInteractiveEdgeConstraint_bottomEdge() { + fpc.layout = FloatingPanelTop2BottomTestLayout() + fpc.showForTest() + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.surfaceView.frame, CGRect(x: 0.0, y: -667.0 + 60.0, width: 375.0, height: 667)) + XCTAssertEqual(fpc.surfaceView.containerView.frame, CGRect(x: 0.0, y: -667.0, + width: 375.0, height: 667 * 2.0)) + + fpc.floatingPanel.layoutAdapter.startInteraction(at: fpc.state) + fpc.floatingPanel.layoutAdapter.startInteraction(at: fpc.state) // Should be ignore + + XCTAssertEqual(fpc.floatingPanel.layoutAdapter.interactionConstraint?.constant, 60.0) + + let fullPos = fpc.surfaceLocation(for: .full).y + let tipPos = fpc.surfaceLocation(for: .tip).y + + var pre: CGFloat + var next: CGFloat + pre = fpc.surfaceLocation.y + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: -100.0, + scrollingContent: true, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, pre) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: 100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, tipPos + 100.0) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: fullPos - tipPos, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: fullPos - tipPos + 100, + scrollingContent: true, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: fullPos - tipPos + 100, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos + 100.0) + + fpc.floatingPanel.layoutAdapter.endInteraction(at: fpc.state) + } + + func test_updateInteractiveEdgeConstraintWithHidden() { + class FloatingPanelLayout2Positions: FloatingPanelLayout { + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 18.0, edge: .bottom, referenceGuide: .safeArea), + .hidden: FloatingPanelLayoutAnchor(absoluteInset: 0, edge: .bottom, referenceGuide: .superview), + ] + let initialState: FloatingPanelState = .hidden + let position: FloatingPanelPosition = .bottom + } + fpc.layout = FloatingPanelLayout2Positions() + fpc.showForTest() + fpc.move(to: .full, animated: false) + + fpc.floatingPanel.layoutAdapter.startInteraction(at: fpc.state) + + let fullPos = fpc.surfaceLocation(for: .full).y + let hiddenPos = fpc.surfaceLocation(for: .hidden).y + + var next: CGFloat + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: -100.0, + scrollingContent: true, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: -100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos - 100.0) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: hiddenPos - fullPos + 100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, hiddenPos + 100.0) + + fpc.floatingPanel.layoutAdapter.endInteraction(at: fpc.state) + } + + func test_updateInteractiveEdgeConstraintWithHidden_bottomEdge() { + class FloatingPanelLayout2Positions: FloatingPanelLayout { + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: 18.0, edge: .bottom, referenceGuide: .safeArea), + .hidden: FloatingPanelLayoutAnchor(absoluteInset: 0, edge: .top, referenceGuide: .superview), + ] + let initialState: FloatingPanelState = .hidden + let position: FloatingPanelPosition = .top + } + fpc.layout = FloatingPanelLayout2Positions() + fpc.showForTest() + fpc.move(to: .full, animated: false) + + fpc.floatingPanel.layoutAdapter.startInteraction(at: fpc.state) + + let fullPos = fpc.surfaceLocation(for: .full).y + let hiddenPos = fpc.surfaceLocation(for: .hidden).y + + var next: CGFloat + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: 100.0, + scrollingContent: true, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: 100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos + 100.0) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: hiddenPos - fullPos + 100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, hiddenPos + 100.0) + + fpc.floatingPanel.layoutAdapter.endInteraction(at: fpc.state) + } + + func test_updateInteractiveTopConstraintWithMinusInsets() { + class FloatingPanelLayoutMinusInsets: FloatingPanelLayout { + let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [ + .full: FloatingPanelLayoutAnchor(absoluteInset: -200, edge: .top, referenceGuide: .safeArea), + .tip: FloatingPanelLayoutAnchor(absoluteInset: -200, edge: .bottom, referenceGuide: .safeArea), + ] + let initialState: FloatingPanelState = .full + let position: FloatingPanelPosition = .bottom + } + fpc.layout = FloatingPanelLayoutMinusInsets() + fpc.showForTest() + fpc.floatingPanel.layoutAdapter.startInteraction(at: fpc.state) + + let fullPos = fpc.surfaceLocation(for: .full).y + let tipPos = fpc.surfaceLocation(for: .tip).y + + var next: CGFloat + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: -100.0, + scrollingContent: true, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: -100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, fullPos - 100) + + fpc.floatingPanel.layoutAdapter.updateInteractiveEdgeConstraint(diff: tipPos - fullPos + 100.0, + scrollingContent: false, + allowsRubberBanding: fpc.floatingPanel.behaviorAdapter.allowsRubberBanding(for:)) + next = fpc.surfaceLocation.y + XCTAssertEqual(next, tipPos + 100) + + fpc.floatingPanel.layoutAdapter.endInteraction(at: fpc.state) + } + + func test_surfaceLocation() { + fpc = CustomSafeAreaFloatingPanelController() + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + + class MyFloatingPanelFullLayout: FloatingPanelTestLayout {} + class MyFloatingPanelSafeAreaLayout: FloatingPanelTestLayout { + override var referenceGuide: FloatingPanelLayoutReferenceGuide { + return .safeArea + } + } + + let myLayout = MyFloatingPanelFullLayout() + fpc.layout = myLayout + fpc.showForTest() + + let bounds = fpc.view!.bounds + XCTAssertEqual(fpc.layout.anchors.filter({ $0.value.referenceGuide != .superview }).count, 0) + XCTAssertEqual(fpc.surfaceLocation(for: .full).y, myLayout.fullInset) + XCTAssertEqual(fpc.surfaceLocation(for: .half).y, bounds.height - myLayout.halfInset) + XCTAssertEqual(fpc.surfaceLocation(for: .tip).y, bounds.height - myLayout.tipInset) + XCTAssertEqual(fpc.surfaceLocation(for: .hidden).y, bounds.height + 100.0) + + fpc.layout = MyFloatingPanelSafeAreaLayout() + + XCTAssertEqual(fpc.layout.anchors.filter({ $0.value.referenceGuide != .safeArea }).count, 0) + XCTAssertEqual(fpc.surfaceLocation(for: .full).y, myLayout.fullInset + fpc.fp_safeAreaInsets.top) + XCTAssertEqual(fpc.surfaceLocation(for: .half).y, bounds.height - myLayout.halfInset + fpc.fp_safeAreaInsets.bottom) + XCTAssertEqual(fpc.surfaceLocation(for: .tip).y, bounds.height - myLayout.tipInset + fpc.fp_safeAreaInsets.bottom) + XCTAssertEqual(fpc.surfaceLocation(for: .hidden).y, bounds.height + 100.0) + } + + func test_surfaceLocation_bottomEdge() { + fpc = CustomSafeAreaFloatingPanelController() + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + + class MyFloatingPanelFullLayout: FloatingPanelTop2BottomTestLayout { } + class MyFloatingPanelSafeAreaLayout: FloatingPanelTop2BottomTestLayout { + override var referenceGuide: FloatingPanelLayoutReferenceGuide { + return .safeArea + } + } + let myLayout = MyFloatingPanelFullLayout() + fpc.layout = myLayout + fpc.showForTest() + + let bounds = fpc.view!.bounds + XCTAssertEqual(fpc.layout.anchors.filter({ $0.value.referenceGuide != .superview }).count, 0) + XCTAssertEqual(fpc.surfaceLocation(for: .full).y, bounds.height - myLayout.fullInset) + XCTAssertEqual(fpc.surfaceLocation(for: .half).y, myLayout.halfInset) + XCTAssertEqual(fpc.surfaceLocation(for: .tip).y, myLayout.tipInset) + XCTAssertEqual(fpc.surfaceLocation(for: .hidden).y, -100.0) + + + fpc.layout = MyFloatingPanelSafeAreaLayout() + + XCTAssertEqual(fpc.layout.anchors.filter({ $0.value.referenceGuide != .safeArea }).count, 0) + XCTAssertEqual(fpc.surfaceLocation(for: .full).y, bounds.height - myLayout.fullInset + fpc.fp_safeAreaInsets.bottom) + XCTAssertEqual(fpc.surfaceLocation(for: .half).y, myLayout.halfInset + fpc.fp_safeAreaInsets.top) + XCTAssertEqual(fpc.surfaceLocation(for: .tip).y, myLayout.tipInset + fpc.fp_safeAreaInsets.top) + XCTAssertEqual(fpc.surfaceLocation(for: .hidden).y, -100.0) + } + + func test_layoutAnchor_topPosition() { + let position: FloatingPanelPosition = .top + fpc = CustomSafeAreaFloatingPanelController() + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + + for prop in [ + // from top edge + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .top, referenceGuide: .safeArea), + result: (#line, constant: 0.0, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .top, referenceGuide: .safeArea), + result: (#line, constant: 100.0, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .top, referenceGuide: .superview), + result: (#line, constant: 0.0, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.view.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .top, referenceGuide: .superview), + result: (#line, constant: 100.0, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.view.topAnchor)), + // from bottom edge + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .bottom, referenceGuide: .safeArea), + result: (#line, constant: 0.0, firstAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor, secondAnchor: fpc.surfaceView.bottomAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .bottom, referenceGuide: .safeArea), + result: (#line, constant: 100.0, firstAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor, secondAnchor: fpc.surfaceView.bottomAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .bottom, referenceGuide: .superview), + result: (#line, constant: 0.0, firstAnchor: fpc.view.bottomAnchor, secondAnchor: fpc.surfaceView.bottomAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .bottom, referenceGuide: .superview), + result: (#line, constant: 100.0, firstAnchor: fpc.view.bottomAnchor, secondAnchor: fpc.surfaceView.bottomAnchor)), + ] { + let c = prop.anchor.layoutConstraints(fpc, for: position)[0] + XCTAssertEqual(c.constant, CGFloat(prop.result.constant), line: UInt(prop.result.0)) + XCTAssertEqual(c.firstAnchor, prop.result.firstAnchor, line: UInt(prop.result.0)) + XCTAssertEqual(c.secondAnchor, prop.result.secondAnchor, line: UInt(prop.result.0)) + } + + // fractional + for prop in [ + // from top edge + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.0, edge: .top, referenceGuide: .safeArea), + result: (#line, multiplier: 1.0, secondAnchor: nil)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .top, referenceGuide: .safeArea), + result: (#line, multiplier: 0.5, secondAnchor: fpc.fp_safeAreaLayoutGuide.heightAnchor)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.0, edge: .top, referenceGuide: .superview), + result: (#line, multiplier: 1.0, secondAnchor: nil)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .top, referenceGuide: .superview), + result: (#line, multiplier: 0.5, secondAnchor: fpc.view.heightAnchor)), + + // from bottom edge + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.0, edge: .bottom, referenceGuide: .safeArea), + result: (#line, multiplier: 1.0, secondAnchor: nil)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + result: (#line, multiplier: 0.5, secondAnchor: fpc.fp_safeAreaLayoutGuide.heightAnchor)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.0, edge: .bottom, referenceGuide: .superview), + result: (#line, multiplier: 1.0, secondAnchor: nil)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .superview), + result: (#line, multiplier: 0.5, secondAnchor: fpc.view.heightAnchor)), + ] { + let c = prop.anchor.layoutConstraints(fpc, for: position)[0] + XCTAssertEqual(c.multiplier, CGFloat(prop.result.multiplier), line: UInt(prop.result.0)) + XCTAssertTrue(c.firstAnchor is NSLayoutAnchor, line: UInt(prop.result.0)) + // On iOS 10, `c.secondAnchor` can't be equal object to `prop.result.secondAnchor` + // because there is no safe area on iOS 10 and `fp_safeAreaLayoutGuide` emulates it. + if #available(iOS 11, *) { + XCTAssertEqual(c.secondAnchor, prop.result.secondAnchor, line: UInt(prop.result.0)) + } + print(c) + } + } + func test_layoutAnchor_bottomPosition() { + let position: FloatingPanelPosition = .bottom + + fpc = CustomSafeAreaFloatingPanelController() + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + + for prop in [ + // from top edge + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .top, referenceGuide: .safeArea), + result: (#line, constant: 0.0, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .top, referenceGuide: .safeArea), + result: (#line, constant: 100.0, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .top, referenceGuide: .superview), + result: (#line, constant: 0.0, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.view.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .top, referenceGuide: .superview), + result: (#line, constant: 100.0, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.view.topAnchor)), + + // from bottom edge + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .bottom, referenceGuide: .safeArea), + result: (#line, constant: 0.0, firstAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor, secondAnchor: fpc.surfaceView.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .bottom, referenceGuide: .safeArea), + result: (#line, constant: 100.0, firstAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor, secondAnchor: fpc.surfaceView.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 0.0, edge: .bottom, referenceGuide: .superview), + result: (#line, constant: 0.0, firstAnchor: fpc.view.bottomAnchor, secondAnchor: fpc.surfaceView.topAnchor)), + (anchor: FloatingPanelLayoutAnchor(absoluteInset: 100.0, edge: .bottom, referenceGuide: .superview), + result: (#line, constant: 100.0, firstAnchor: fpc.view.bottomAnchor, secondAnchor: fpc.surfaceView.topAnchor)), + ] { + let c = prop.anchor.layoutConstraints(fpc, for: position)[0] + XCTAssertEqual(c.constant, CGFloat(prop.result.constant), line: UInt(prop.result.0)) + XCTAssertEqual(c.firstAnchor, prop.result.firstAnchor, line: UInt(prop.result.0)) + XCTAssertEqual(c.secondAnchor, prop.result.secondAnchor, line: UInt(prop.result.0)) + } + + // fractional + for prop in [ + // from top edge + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.0, edge: .top, referenceGuide: .safeArea), + result: (#line, multiplier: 1.0, secondAnchor: nil)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .top, referenceGuide: .safeArea), + result: (#line, multiplier: 0.5, secondAnchor: fpc.fp_safeAreaLayoutGuide.heightAnchor)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.0, edge: .top, referenceGuide: .superview), + result: (#line, multiplier: 1.0, secondAnchor: nil)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .top, referenceGuide: .superview), + result: (#line, multiplier: 0.5, secondAnchor: fpc.view.heightAnchor)), + + // from bottom edge + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.0, edge: .bottom, referenceGuide: .safeArea), + result: (#line, multiplier: 1.0, secondAnchor: nil)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .safeArea), + result: (#line, multiplier: 0.5, secondAnchor: fpc.fp_safeAreaLayoutGuide.heightAnchor)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.0, edge: .bottom, referenceGuide: .superview), + result: (#line, multiplier: 1.0, secondAnchor: nil)), + (anchor: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom, referenceGuide: .superview), + result: (#line, multiplier: 0.5, secondAnchor: fpc.view.heightAnchor)), + ] { + let c = prop.anchor.layoutConstraints(fpc, for: position)[0] + XCTAssertEqual(c.multiplier, CGFloat(prop.result.multiplier), line: UInt(prop.result.0)) + XCTAssertTrue(c.firstAnchor is NSLayoutAnchor, line: UInt(prop.result.0)) + // On iOS 10, `c.secondAnchor` can't be equal object to `prop.result.secondAnchor` + // because there is no safe area on iOS 10 and `fp_safeAreaLayoutGuide` emulates it. + if #available(iOS 11, *) { + XCTAssertEqual(c.secondAnchor, prop.result.secondAnchor, line: UInt(prop.result.0)) + } + print(c) + } + } + + func test_intrinsicLayoutAnchor_topPosition() { + class ContentViewController: UIViewController { + class IntrinsicView: UIView { + override var intrinsicContentSize: CGSize { + return CGSize(width: UIView.noIntrinsicMetric, height: 420) + } + } + override func loadView() { + self.view = IntrinsicView() + } + } + let position: FloatingPanelPosition = .top + + fpc = CustomSafeAreaFloatingPanelController() + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + let contentVC = ContentViewController() + contentVC.loadViewIfNeeded() + fpc.set(contentViewController: contentVC) + + for prop in [ + (anchor: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 0.0, referenceGuide: .safeArea), + result: (#line, constant: 420, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 42.0, referenceGuide: .safeArea), + result: (#line, constant: 420 - 42, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 0.0, referenceGuide: .superview), + result: (#line, constant: 420, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.view.topAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 42.0, referenceGuide: .superview), + result: (#line, constant: 420 - 42, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.view.topAnchor)), + + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.0, referenceGuide: .safeArea), + result: (#line, constant: 420, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.5, referenceGuide: .safeArea), + result: (#line, constant: 210, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 1.0, referenceGuide: .safeArea), + result: (#line, constant: 0, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.topAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.0, referenceGuide: .superview), + result: (#line, constant: 420, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.view.topAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.5, referenceGuide: .superview), + result: (#line, constant: 210, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.view.topAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 1.0, referenceGuide: .superview), + result: (#line, constant: 0, firstAnchor: fpc.surfaceView.bottomAnchor, secondAnchor: fpc.view.topAnchor)), + ] { + let c = prop.anchor.layoutConstraints(fpc, for: position)[0] + XCTAssertEqual(c.constant, CGFloat(prop.result.constant), line: UInt(prop.result.0)) + XCTAssertEqual(c.firstAnchor, prop.result.firstAnchor, line: UInt(prop.result.0)) + XCTAssertEqual(c.secondAnchor, prop.result.secondAnchor, line: UInt(prop.result.0)) + } + } + + func test_intrinsicLayoutAnchor_bottomPosition() { + class ContentViewController: UIViewController { + class IntrinsicView: UIView { + override var intrinsicContentSize: CGSize { + return CGSize(width: UIView.noIntrinsicMetric, height: 420) + } + } + override func loadView() { + self.view = IntrinsicView() + } + } + let position: FloatingPanelPosition = .bottom + + fpc = CustomSafeAreaFloatingPanelController() + fpc.loadViewIfNeeded() + fpc.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + let contentVC = ContentViewController() + contentVC.loadViewIfNeeded() + fpc.set(contentViewController: contentVC) + + for prop in [ + (anchor: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 0.0, referenceGuide: .safeArea), + result: (#line, constant: -420, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 42.0, referenceGuide: .safeArea), + result: (#line, constant: -420 + 42, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 0.0, referenceGuide: .superview), + result: (#line, constant: -420, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.view.bottomAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 42.0, referenceGuide: .superview), + result: (#line, constant: -420 + 42, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.view.bottomAnchor)), + + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.0, referenceGuide: .safeArea), + result: (#line, constant: -420, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.5, referenceGuide: .safeArea), + result: (#line, constant: -210, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 1.0, referenceGuide: .safeArea), + result: (#line, constant: 0, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.fp_safeAreaLayoutGuide.bottomAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.0, referenceGuide: .superview), + result: (#line, constant: -420, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.view.bottomAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 0.5, referenceGuide: .superview), + result: (#line, constant: -210, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.view.bottomAnchor)), + (anchor: FloatingPanelIntrinsicLayoutAnchor(fractionalOffset: 1.0, referenceGuide: .superview), + result: (#line, constant: 0, firstAnchor: fpc.surfaceView.topAnchor, secondAnchor: fpc.view.bottomAnchor)), + ] { + let c = prop.anchor.layoutConstraints(fpc, for: position)[0] + XCTAssertEqual(c.constant, CGFloat(prop.result.constant), line: UInt(prop.result.0)) + XCTAssertEqual(c.firstAnchor, prop.result.firstAnchor, line: UInt(prop.result.0)) + XCTAssertEqual(c.secondAnchor, prop.result.secondAnchor, line: UInt(prop.result.0)) + } + } +} + +private typealias LayoutSegmentTestParameter = (UInt, pos: CGFloat, forwardY: Bool, lower: FloatingPanelState?, upper: FloatingPanelState?) +private func assertLayoutSegment(_ floatingPanel: Core, with params: [LayoutSegmentTestParameter]) { + params.forEach { (line, pos, forwardY, lowr, upper) in + let segment = floatingPanel.layoutAdapter.segment(at: pos, forward: forwardY) + XCTAssertEqual(segment.lower, lowr, line: line) + XCTAssertEqual(segment.upper, upper, line: line) + } +} + +private class CustomSafeAreaFloatingPanelController: FloatingPanelController { + override var fp_safeAreaInsets: UIEdgeInsets { + return UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 34.0) + } +} diff --git a/Tests/StateTests.swift b/Tests/StateTests.swift new file mode 100644 index 00000000..46bcf9b9 --- /dev/null +++ b/Tests/StateTests.swift @@ -0,0 +1,24 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import XCTest +@testable import FloatingPanel + +class StateTests: XCTestCase { + override func setUp() { } + override func tearDown() { } + + func test_nextAndPre() { + var positions: [FloatingPanelState] + positions = [.full, .half, .tip, .hidden] + XCTAssertEqual(FloatingPanelState.full.next(in: positions), .half) + XCTAssertEqual(FloatingPanelState.full.pre(in: positions), .full) + XCTAssertEqual(FloatingPanelState.hidden.next(in: positions), .hidden) + XCTAssertEqual(FloatingPanelState.hidden.pre(in: positions), .tip) + + positions = [.full, .hidden] + XCTAssertEqual(FloatingPanelState.full.next(in: positions), .hidden) + XCTAssertEqual(FloatingPanelState.full.pre(in: positions), .full) + XCTAssertEqual(FloatingPanelState.hidden.next(in: positions), .hidden) + XCTAssertEqual(FloatingPanelState.hidden.pre(in: positions), .full) + } +} diff --git a/Tests/SurfaceViewTests.swift b/Tests/SurfaceViewTests.swift new file mode 100644 index 00000000..7b9018fd --- /dev/null +++ b/Tests/SurfaceViewTests.swift @@ -0,0 +1,315 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import XCTest +@testable import FloatingPanel + +class SurfaceViewTests: XCTestCase { + override func setUp() {} + override func tearDown() {} + + func test_surfaceView() { + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + XCTAssertTrue(SurfaceView.requiresConstraintBasedLayout) + XCTAssert(surface.contentView == nil) + surface.layoutIfNeeded() + XCTAssert(surface.grabberHandle.frame.minY == 6.0) + XCTAssert(surface.grabberHandle.frame.width == surface.grabberHandleSize.width) + XCTAssert(surface.grabberHandle.frame.height == surface.grabberHandleSize.height) + surface.backgroundColor = .red + surface.layoutIfNeeded() + XCTAssert(surface.backgroundColor == surface.containerView.backgroundColor) + } + + func test_surfaceView_containerView() { + XCTContext.runActivity(named: "Bottom sheet") { _ in + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + XCTAssertNil(surface.contentView) + surface.layoutIfNeeded() + + let height = surface.bounds.height * 2 + surface.containerOverflow = height + surface.setNeedsLayout() + surface.layoutIfNeeded() + XCTAssertEqual(surface.containerView.frame, CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0 * 3)) + } + + XCTContext.runActivity(named: "Top sheet") { _ in + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + surface.position = .top + XCTAssertNil(surface.contentView) + surface.layoutIfNeeded() + + let height = surface.bounds.height * 2 + surface.containerOverflow = height + surface.setNeedsLayout() + surface.layoutIfNeeded() + XCTAssertEqual(surface.containerView.frame, CGRect(x: 0.0, y: -height, width: 320.0, height: 480.0 * 3)) + } + } + + func test_surfaceView_contentView() { + for (position, mode, line) in [ + (.top, .static, #line), + (.top, .fitToBounds, #line), + (.bottom, .static, #line), + (.bottom, .fitToBounds, #line), + ] as [(FloatingPanelPosition, FloatingPanelController.ContentMode, UInt)] { + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + surface.position = position + surface.layoutIfNeeded() + + let contentView = UIView() + surface.set(contentView: contentView, mode: mode) + + let height = surface.bounds.height * 2 + surface.containerOverflow = height + surface.setNeedsLayout() + surface.layoutIfNeeded() + + switch position { + case .top: + XCTAssertEqual(surface.containerView.frame, + CGRect(x: 0.0, y: -height, width: 320.0, height: 480.0 * 3), + line: line) + XCTAssertEqual(surface.convert(surface.contentView?.frame ?? .zero, from: surface.containerView), + surface.bounds, + line: line) + case .bottom: + XCTAssertEqual(surface.contentView?.frame ?? .zero, surface.bounds, line: line) + default: + break + } + } + } + + + func test_surfaceView_grabberHandle() { + XCTContext.runActivity(named: "Bottom sheet") { _ in + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + XCTAssertNil(surface.contentView) + surface.layoutIfNeeded() + + XCTAssertEqual(surface.grabberHandle.frame.minY, 6.0) + XCTAssertEqual(surface.grabberHandle.frame.width, surface.grabberHandleSize.width) + XCTAssertEqual(surface.grabberHandle.frame.height, surface.grabberHandleSize.height) + + surface.grabberHandlePadding = 10.0 + surface.grabberHandleSize = CGSize(width: 44.0, height: 12.0) + surface.setNeedsLayout() + surface.layoutIfNeeded() + XCTAssertEqual(surface.grabberHandle.frame.minY, surface.grabberHandlePadding) + XCTAssertEqual(surface.grabberHandle.frame.width, surface.grabberHandleSize.width) + XCTAssertEqual(surface.grabberHandle.frame.height, surface.grabberHandleSize.height) + } + + XCTContext.runActivity(named: "Top sheet") { _ in + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + surface.position = .top + XCTAssertNil(surface.contentView) + surface.layoutIfNeeded() + + XCTAssertEqual(surface.grabberHandle.frame.maxY, (surface.bounds.maxY - 6.0)) + XCTAssertEqual(surface.grabberHandle.frame.width, surface.grabberHandleSize.width) + XCTAssertEqual(surface.grabberHandle.frame.height, surface.grabberHandleSize.height) + + surface.grabberHandlePadding = 10.0 + surface.setNeedsLayout() + surface.layoutIfNeeded() + XCTAssertEqual(surface.grabberHandle.frame.maxY, surface.bounds.maxY - surface.grabberHandlePadding) + } + } + + func test_surfaceView_contentMargins() { + XCTContext.runActivity(named: "Top sheet") { _ in + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + surface.position = .top + surface.layoutIfNeeded() + XCTAssertEqual(surface.containerView.frame, surface.bounds) + surface.containerMargins = UIEdgeInsets(top: 16.0, left: 16.0, bottom: 16.0, right: 16.0) + surface.setNeedsLayout() + surface.layoutIfNeeded() + XCTAssertEqual(surface.containerView.frame, surface.bounds.inset(by: surface.containerMargins)) + } + XCTContext.runActivity(named: "Bottom sheet") { _ in + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + surface.layoutIfNeeded() + XCTAssertEqual(surface.containerView.frame, surface.bounds) + surface.containerMargins = UIEdgeInsets(top: 16.0, left: 16.0, bottom: 16.0, right: 16.0) + surface.setNeedsLayout() + surface.layoutIfNeeded() + XCTAssertEqual(surface.containerView.frame, surface.bounds.inset(by: surface.containerMargins)) + } + } + + func test_surfaceView_contentInsets() { + for (position, mode, line) in [ + (.top, .static, #line), + (.top, .fitToBounds, #line), + (.bottom, .static, #line), + (.bottom, .fitToBounds, #line), + ] as [(FloatingPanelPosition, FloatingPanelController.ContentMode, UInt)] { + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + surface.position = position + let contentView = UIView() + surface.set(contentView: contentView, mode: mode) + surface.layoutIfNeeded() + XCTAssertEqual(surface.contentView?.frame ?? .zero, surface.bounds, line: line) + surface.contentPadding = UIEdgeInsets(top: 16.0, left: 16.0, bottom: 16.0, right: 16.0) + surface.setNeedsLayout() + surface.layoutIfNeeded() + XCTAssertEqual(surface.contentView?.frame ?? .zero, surface.bounds.inset(by: surface.contentPadding), line: line) + } + } + + func test_surfaceView_containerMargins_and_contentInsets() { + for (mode, line) in [ + (.static, #line), + (.fitToBounds, #line), + ] as [(FloatingPanelController.ContentMode, UInt)] { + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + let contentView = UIView() + surface.set(contentView: contentView, mode: mode) + surface.layoutIfNeeded() + XCTAssertEqual(surface.contentView?.frame ?? .zero, surface.bounds, line: line) + surface.containerMargins = UIEdgeInsets(top: 16.0, left: 16.0, bottom: 16.0, right: 16.0) + surface.contentPadding = UIEdgeInsets(top: 16.0, left: 16.0, bottom: 16.0, right: 16.0) + surface.setNeedsLayout() + surface.layoutIfNeeded() + XCTAssertEqual(surface.containerView.frame, surface.bounds.inset(by: surface.containerMargins), line: line) + XCTAssertEqual(surface.contentView?.frame ?? .zero, surface.containerView.bounds.inset(by: surface.contentPadding), line: line) + } + } + + func test_surfaceView_cornerRadius() { + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + XCTAssert(surface.containerView.layer.cornerRadius == 0.0) + XCTAssert(surface.containerView.layer.masksToBounds == false) + + let appearance = SurfaceAppearance() + + appearance.cornerRadius = 10.0 + surface.appearance = appearance + surface.layoutIfNeeded() + XCTAssert(surface.containerView.layer.cornerRadius == 10.0) + XCTAssert(surface.containerView.layer.masksToBounds == true) + + surface.containerView.layer.cornerRadius = 12.0 + surface.layoutIfNeeded() + XCTAssert(surface.containerView.layer.cornerRadius == 12.0) + XCTAssert(surface.containerView.layer.masksToBounds == true) + + appearance.cornerRadius = 0.0 + surface.appearance = appearance + surface.layoutIfNeeded() + XCTAssert(surface.containerView.layer.cornerRadius == 0.0) + XCTAssert(surface.containerView.layer.masksToBounds == false) + + surface.containerView.layer.cornerRadius = 12.0 // Don't change it directly + XCTAssert(surface.containerView.layer.cornerRadius == 12.0) + XCTAssertFalse(surface.containerView.layer.masksToBounds == true) + + surface.setNeedsLayout() + surface.layoutIfNeeded() + // Reset corner radius by the current appearance + XCTAssert(surface.containerView.layer.cornerRadius == 0.0) + XCTAssert(surface.containerView.layer.masksToBounds == false) + + } + + func test_surfaceView_border() { + let surface = SurfaceView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)) + XCTAssert(surface.containerView.layer.borderWidth == 0.0) + + let appearance = SurfaceAppearance() + appearance.borderColor = .red + appearance.borderWidth = 3.0 + surface.appearance = appearance + surface.layoutIfNeeded() + XCTAssert(surface.containerView.layer.borderColor == UIColor.red.cgColor) + XCTAssert(surface.containerView.layer.borderWidth == 3.0) + } + + func test_surfaceView_grabberArea() { + let sv = SurfaceView() + sv.bounds = .init(x: 0, y: 0, width: 375, height: 500) + sv.grabberAreaOffset = 44.0 + // Top + do { + sv.position = .top + XCTAssertEqual(sv.grabberAreaFrame, + .init(x: 0, + y: sv.bounds.height - sv.grabberAreaOffset, + width: sv.bounds.width, + height: sv.grabberAreaOffset)) + } + // Bottom + do { + sv.position = .bottom + XCTAssertEqual(sv.grabberAreaFrame, + .init(x: 0, + y: 0, + width: sv.bounds.width, + height: sv.grabberAreaOffset)) + } + // Left + do { + sv.position = .left + XCTAssertEqual(sv.grabberAreaFrame, + .init(x: sv.bounds.width - sv.grabberAreaOffset, + y: 0, + width: sv.grabberAreaOffset, + height: sv.bounds.height)) + } + // Right + do { + sv.position = .right + XCTAssertEqual(sv.grabberAreaFrame, + .init(x: 0, + y: 0, + width: sv.grabberAreaOffset, + height: sv.bounds.height)) + } + } + + func test_surfaceView_grabberAreaDetection() { + let sv = SurfaceView() + sv.bounds = .init(x: 0, y: 0, width: 375, height: 500) + // Top + do { + sv.position = .top + XCTAssertTrue(sv.grabberAreaContains(.init(x: 0, y: sv.bounds.height))) + XCTAssertTrue(sv.grabberAreaContains(.init(x: sv.bounds.width / 2, y: sv.bounds.height))) + XCTAssertTrue(sv.grabberAreaContains(.init(x: sv.bounds.width / 2, y: sv.bounds.height + 2))) + XCTAssertFalse(sv.grabberAreaContains(.init(x: -2, y: sv.bounds.height))) + XCTAssertFalse(sv.grabberAreaContains(.init(x: -2, y: -2))) + } + // Bottom + do { + sv.position = .bottom + XCTAssertTrue(sv.grabberAreaContains(.init(x: 0, y: 0))) + XCTAssertTrue(sv.grabberAreaContains(.init(x: sv.bounds.width / 2, y: 0))) + XCTAssertTrue(sv.grabberAreaContains(.init(x: sv.bounds.width / 2, y: -2))) + XCTAssertFalse(sv.grabberAreaContains(.init(x: -2, y: 0))) + XCTAssertFalse(sv.grabberAreaContains(.init(x: -2, y: -2))) + } + // Left + do { + sv.position = .left + XCTAssertTrue(sv.grabberAreaContains(.init(x: sv.bounds.width, y: 0))) + XCTAssertTrue(sv.grabberAreaContains(.init(x: sv.bounds.width, y: sv.bounds.height / 2))) + XCTAssertTrue(sv.grabberAreaContains(.init(x: sv.bounds.width + 2, y: sv.bounds.height / 2))) + XCTAssertFalse(sv.grabberAreaContains(.init(x: sv.bounds.width, y: -2))) + XCTAssertFalse(sv.grabberAreaContains(.init(x: -2, y: -2))) + } + // Right + do { + sv.position = .right + XCTAssertTrue(sv.grabberAreaContains(.init(x: 0, y: 0))) + XCTAssertTrue(sv.grabberAreaContains(.init(x: 0, y: sv.bounds.height / 2))) + XCTAssertTrue(sv.grabberAreaContains(.init(x: -2, y: sv.bounds.height / 2))) + XCTAssertFalse(sv.grabberAreaContains(.init(x: 0, y: -2))) + XCTAssertFalse(sv.grabberAreaContains(.init(x: -2, y: -2))) + } + } + +} diff --git a/Tests/TestSupports.swift b/Tests/TestSupports.swift new file mode 100644 index 00000000..ffa354b4 --- /dev/null +++ b/Tests/TestSupports.swift @@ -0,0 +1,79 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import Foundation +@testable import FloatingPanel + +func waitRunLoop(secs: TimeInterval = 0) { + RunLoop.main.run(until: Date(timeIntervalSinceNow: secs)) +} + +extension FloatingPanelController { + func showForTest() { + loadViewIfNeeded() + view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) + show(animated: false, completion: nil) + } +} + +class FloatingPanelTestDelegate: FloatingPanelControllerDelegate { + var position: FloatingPanelState = .hidden + var didMoveCallback: ((FloatingPanelController) -> Void)? + func floatingPanelDidChangeState(_ vc: FloatingPanelController) { + position = vc.state + } + func floatingPanelDidMove(_ vc: FloatingPanelController) { + didMoveCallback?(vc) + } +} + +class FloatingPanelTestLayout: FloatingPanelLayout { + let fullInset: CGFloat = 20.0 + let halfInset: CGFloat = 250.0 + let tipInset: CGFloat = 60.0 + + var initialState: FloatingPanelState { + return .half + } + var position: FloatingPanelPosition { + return .bottom + } + var referenceGuide: FloatingPanelLayoutReferenceGuide { + return .superview + } + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: fullInset, edge: .top, referenceGuide: referenceGuide), + .half: FloatingPanelLayoutAnchor(absoluteInset: halfInset, edge: .bottom, referenceGuide: referenceGuide), + .tip: FloatingPanelLayoutAnchor(absoluteInset: tipInset, edge: .bottom, referenceGuide: referenceGuide), + ] + } +} + +class FloatingPanelTop2BottomTestLayout: FloatingPanelLayout { + let fullInset: CGFloat = 0.0 + let halfInset: CGFloat = 250.0 + let tipInset: CGFloat = 60.0 + + var initialState: FloatingPanelState { + return .half + } + var position: FloatingPanelPosition { + return .top + } + var referenceGuide: FloatingPanelLayoutReferenceGuide { + return .superview + } + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { + return [ + .full: FloatingPanelLayoutAnchor(absoluteInset: fullInset, edge: .bottom, referenceGuide: referenceGuide), + .half: FloatingPanelLayoutAnchor(absoluteInset: halfInset, edge: .top, referenceGuide: referenceGuide), + .tip: FloatingPanelLayoutAnchor(absoluteInset: tipInset, edge: .top, referenceGuide: referenceGuide), + ] + } +} + +class FloatingPanelProjectableBehavior: FloatingPanelBehavior { + func shouldProjectMomentum(_ fpc: FloatingPanelController, to proposedTargetPosition: FloatingPanelState) -> Bool { + return true + } +} diff --git a/Tests/TestingApp/AppDelegate.swift b/Tests/TestingApp/AppDelegate.swift new file mode 100644 index 00000000..a725a22f --- /dev/null +++ b/Tests/TestingApp/AppDelegate.swift @@ -0,0 +1,19 @@ +// Copyright 2018 the FloatingPanel authors. All rights reserved. MIT license. + +import UIKit +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + var window: UIWindow? + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { + let rootVC = UIViewController(nibName: nil, bundle: nil) + rootVC.view.backgroundColor = .gray + + let window = UIWindow() + window.rootViewController = rootVC + window.makeKeyAndVisible() + self.window = window + + return true + } +} diff --git a/Tests/TestingApp/Info.plist b/Tests/TestingApp/Info.plist new file mode 100644 index 00000000..4222ac2d --- /dev/null +++ b/Tests/TestingApp/Info.plist @@ -0,0 +1,43 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/Tests/TestingApp/LaunchScreen.storyboard b/Tests/TestingApp/LaunchScreen.storyboard new file mode 100644 index 00000000..92708cd2 --- /dev/null +++ b/Tests/TestingApp/LaunchScreen.storyboard @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +