From 134bace1f8bd9c4643bf6c11f3a202827d50245c Mon Sep 17 00:00:00 2001 From: DancewithPeng Date: Thu, 17 Jun 2021 15:10:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9D=A5=E6=BA=90=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E8=B6=85=E5=87=BA=E5=B1=8F=E5=B9=95=E5=AE=BD=E5=BA=A6?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KBPopup.podspec | 2 +- KBPopup.xcodeproj/project.pbxproj | 4 ++ KBPopup/Sources/KBPopupView.swift | 28 ++++++++++- KBPopupExample/Base.lproj/Main.storyboard | 56 +++++++++++++++++++++- KBPopupExample/ListViewController.swift | 58 +++++++++++++++++++++++ README.md | 2 +- 6 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 KBPopupExample/ListViewController.swift diff --git a/KBPopup.podspec b/KBPopup.podspec index 67c28c4..5558cc2 100644 --- a/KBPopup.podspec +++ b/KBPopup.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "KBPopup" - spec.version = "1.0.2" + spec.version = "1.1" spec.summary = "iOS带箭头的起泡弹窗" spec.description = <<-DESC iOS带箭头的起泡弹窗,使用于需要在页面内弹窗的场景 diff --git a/KBPopup.xcodeproj/project.pbxproj b/KBPopup.xcodeproj/project.pbxproj index 40b1d2f..9c483fc 100644 --- a/KBPopup.xcodeproj/project.pbxproj +++ b/KBPopup.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 6E09B752267B26AC009D3E88 /* ListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E09B751267B26AC009D3E88 /* ListViewController.swift */; }; 6E2B7CDC2673395400197BEE /* ObjCViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E2B7CDB2673395400197BEE /* ObjCViewController.m */; }; 6E7FD13F2670AECF008289B9 /* KBPopup.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E7FD13D2670AECF008289B9 /* KBPopup.h */; settings = {ATTRIBUTES = (Public, ); }; }; 6E7FD1492670B70F008289B9 /* KBPopupView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E7FD1482670B70F008289B9 /* KBPopupView.swift */; }; @@ -49,6 +50,7 @@ 12B6146BD5D2A71A7BA358F2 /* Pods_KBPopupExample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_KBPopupExample.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 1FBDC366B575BB10E64B0D59 /* Pods-KBPopupController.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KBPopupController.debug.xcconfig"; path = "Target Support Files/Pods-KBPopupController/Pods-KBPopupController.debug.xcconfig"; sourceTree = ""; }; 24914E2EB29E1584D56999D9 /* Pods-KBPopupControllerExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KBPopupControllerExample.debug.xcconfig"; path = "Target Support Files/Pods-KBPopupControllerExample/Pods-KBPopupControllerExample.debug.xcconfig"; sourceTree = ""; }; + 6E09B751267B26AC009D3E88 /* ListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListViewController.swift; sourceTree = ""; }; 6E2B7CD92673395400197BEE /* KBPopupExample-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "KBPopupExample-Bridging-Header.h"; sourceTree = ""; }; 6E2B7CDA2673395400197BEE /* ObjCViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ObjCViewController.h; sourceTree = ""; }; 6E2B7CDB2673395400197BEE /* ObjCViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ObjCViewController.m; sourceTree = ""; }; @@ -158,6 +160,7 @@ 6E2B7CDA2673395400197BEE /* ObjCViewController.h */, 6E2B7CDB2673395400197BEE /* ObjCViewController.m */, 6E2B7CD92673395400197BEE /* KBPopupExample-Bridging-Header.h */, + 6E09B751267B26AC009D3E88 /* ListViewController.swift */, ); path = KBPopupExample; sourceTree = ""; @@ -362,6 +365,7 @@ files = ( 6E2B7CDC2673395400197BEE /* ObjCViewController.m in Sources */, 6E7FD1552670EA56008289B9 /* ViewController.swift in Sources */, + 6E09B752267B26AC009D3E88 /* ListViewController.swift in Sources */, 6E7FD1512670EA56008289B9 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/KBPopup/Sources/KBPopupView.swift b/KBPopup/Sources/KBPopupView.swift index 6d6bcb9..2983eb3 100644 --- a/KBPopup/Sources/KBPopupView.swift +++ b/KBPopup/Sources/KBPopupView.swift @@ -360,6 +360,16 @@ extension KBPopupView { guard sourceViewFrame != .zero else { return } + + let windowSourceViewFrame: CGRect + let windowBounds: CGRect + if let window = containerView.window { + windowSourceViewFrame = window.convert(sourceViewFrame, from: containerView) + windowBounds = window.bounds + } else { + windowSourceViewFrame = sourceViewFrame + windowBounds = containerView.bounds + } self.bounds = CGRect(x: 0, y: 0, width: self.intrinsicContentSize.width, height: self.intrinsicContentSize.height) @@ -382,13 +392,27 @@ extension KBPopupView { positionX = sourceViewFrame.midX } - if (sourceViewFrame.origin.y - self.bounds.height > margin.top) { // 弹窗在上,箭头朝下 + if (windowSourceViewFrame.origin.y - self.bounds.height >= margin.top) { // 弹窗在上,箭头朝下 self.arrowDirection = .down positionY = sourceViewFrame.origin.y - } else { // 弹窗在下,箭头朝上 + } else if ((windowSourceViewFrame.maxY + self.bounds.height) <= (windowBounds.height - margin.bottom)) { // 弹窗在下,箭头朝上 self.arrowDirection = .up positionY = sourceViewFrame.origin.y + sourceViewFrame.height + } else { // 弹窗在屏幕中间,箭头朝下 + self.arrowDirection = .down + if let window = containerView.window { + positionY = containerView.convert(CGPoint(x: windowBounds.midX, y: windowBounds.midY), from: window).y + } else { + positionY = containerView.convert(CGPoint(x: sourceFrame.midX, y: sourceFrame.midX), from: window).y + } } +// if (sourceViewFrame.origin.y - self.bounds.height > margin.top) { // 弹窗在上,箭头朝下 +// self.arrowDirection = .down +// positionY = sourceViewFrame.origin.y +// } else { // 弹窗在下,箭头朝上 +// self.arrowDirection = .up +// positionY = sourceViewFrame.origin.y + sourceViewFrame.height +// } let anchorX: CGFloat = arrowVertexPosition.x / bounds.width; let anchorY: CGFloat = arrowDirection == .down ? 1 : 0 diff --git a/KBPopupExample/Base.lproj/Main.storyboard b/KBPopupExample/Base.lproj/Main.storyboard index 6028b93..da5dddb 100644 --- a/KBPopupExample/Base.lproj/Main.storyboard +++ b/KBPopupExample/Base.lproj/Main.storyboard @@ -2,16 +2,18 @@ + + - + @@ -106,6 +108,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -118,6 +171,7 @@ + diff --git a/KBPopupExample/ListViewController.swift b/KBPopupExample/ListViewController.swift new file mode 100644 index 0000000..2ada882 --- /dev/null +++ b/KBPopupExample/ListViewController.swift @@ -0,0 +1,58 @@ +// +// ListViewController.swift +// KBPopupExample +// +// Created by 张鹏 on 2021/6/17. +// + +import UIKit +import KBPopup + +class ListViewController: UIViewController { + + lazy var popupView: KBPopupView = { + let myView = UIView(frame: .zero) + myView.backgroundColor = .red + + let popupView = KBPopupView(contentView: myView) + popupView.contentSize = CGSize(width: 180, height: 66) + popupView.margin = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50) + return popupView + }() + + @IBOutlet weak var listView: UICollectionView! + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } +} + +extension ListViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { + + func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + collectionView.deselectItem(at: indexPath, animated: true) + +// popupView.sourceView = collectionView.cellForItem(at: indexPath) + guard let cell = collectionView.cellForItem(at: indexPath) else { + return + } + popupView.sourceFrame = cell.frame + popupView.show(in: collectionView) + } + + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + return 100 + } + + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) + cell.backgroundColor = UIColor(red: CGFloat.random(in: 0...1), green: CGFloat.random(in: 0...1), blue: CGFloat.random(in: 0...1), alpha: 1.0) + return cell + } + + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { + return CGSize(width: CGFloat.random(in: 100...300), height: CGFloat.random(in: 600...900)) + } +} diff --git a/README.md b/README.md index bbec65c..6b00547 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ iOS带箭头的起泡弹窗 在`Podfile`文件中加入 ```ruby -pod 'KBPopup', '~> 1.0.2' +pod 'KBPopup', '~> 1.1.0' ```