Skip to content

Commit

Permalink
添加来源位置超出屏幕宽度的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
DancewithPeng committed Jun 17, 2021
1 parent f0405a1 commit 134bace
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 5 deletions.
2 changes: 1 addition & 1 deletion KBPopup.podspec
Original file line number Diff line number Diff line change
@@ -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带箭头的起泡弹窗,使用于需要在页面内弹窗的场景
Expand Down
4 changes: 4 additions & 0 deletions KBPopup.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
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 = "<group>"; };
6E09B751267B26AC009D3E88 /* ListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListViewController.swift; sourceTree = "<group>"; };
6E2B7CD92673395400197BEE /* KBPopupExample-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "KBPopupExample-Bridging-Header.h"; sourceTree = "<group>"; };
6E2B7CDA2673395400197BEE /* ObjCViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ObjCViewController.h; sourceTree = "<group>"; };
6E2B7CDB2673395400197BEE /* ObjCViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ObjCViewController.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -158,6 +160,7 @@
6E2B7CDA2673395400197BEE /* ObjCViewController.h */,
6E2B7CDB2673395400197BEE /* ObjCViewController.m */,
6E2B7CD92673395400197BEE /* KBPopupExample-Bridging-Header.h */,
6E09B751267B26AC009D3E88 /* ListViewController.swift */,
);
path = KBPopupExample;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 26 additions & 2 deletions KBPopup/Sources/KBPopupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
56 changes: 55 additions & 1 deletion KBPopupExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="wIj-jH-KaV">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="collection view cell content view" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Swift-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="KBPopupControllerExample" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="KBPopupExample" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand Down Expand Up @@ -106,6 +108,57 @@
</objects>
<point key="canvasLocation" x="950.72463768115949" y="-364.28571428571428"/>
</scene>
<!--List-->
<scene sceneID="0pt-OP-rix">
<objects>
<viewController id="IQ2-7a-nMl" customClass="ListViewController" customModule="KBPopupExample" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="mv4-DY-kB6">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="Rxi-aM-AeE">
<rect key="frame" x="0.0" y="0.0" width="414" height="813"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<collectionViewFlowLayout key="collectionViewLayout" automaticEstimatedItemSize="YES" minimumLineSpacing="10" minimumInteritemSpacing="10" id="6Ea-D8-SUy">
<size key="itemSize" width="128" height="128"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
<size key="footerReferenceSize" width="0.0" height="0.0"/>
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
</collectionViewFlowLayout>
<cells>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="MyCell" id="h9o-Q2-r8p">
<rect key="frame" x="0.0" y="0.0" width="128" height="128"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<collectionViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" id="znp-GK-hYM">
<rect key="frame" x="0.0" y="0.0" width="128" height="128"/>
<autoresizingMask key="autoresizingMask"/>
</collectionViewCellContentView>
</collectionViewCell>
</cells>
<connections>
<outlet property="dataSource" destination="IQ2-7a-nMl" id="auD-12-f4z"/>
<outlet property="delegate" destination="IQ2-7a-nMl" id="21S-MY-SuZ"/>
</connections>
</collectionView>
</subviews>
<viewLayoutGuide key="safeArea" id="ppo-yk-2f6"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="Rxi-aM-AeE" firstAttribute="top" secondItem="mv4-DY-kB6" secondAttribute="top" id="1fh-r5-P5e"/>
<constraint firstItem="Rxi-aM-AeE" firstAttribute="leading" secondItem="ppo-yk-2f6" secondAttribute="leading" id="gkp-30-KhD"/>
<constraint firstItem="Rxi-aM-AeE" firstAttribute="bottom" secondItem="ppo-yk-2f6" secondAttribute="bottom" id="lE2-DH-EDs"/>
<constraint firstItem="Rxi-aM-AeE" firstAttribute="trailing" secondItem="ppo-yk-2f6" secondAttribute="trailing" id="mUj-Lu-DMp"/>
</constraints>
</view>
<tabBarItem key="tabBarItem" title="List" id="HJx-gF-UHz"/>
<connections>
<outlet property="listView" destination="Rxi-aM-AeE" id="Hn0-1G-bh7"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="W1J-FB-ZQn" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1708.6956521739132" y="-364.28571428571428"/>
</scene>
<!--Tab Bar Controller-->
<scene sceneID="EVq-Wu-1Xv">
<objects>
Expand All @@ -118,6 +171,7 @@
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="viewControllers" id="8bE-ke-3Hn"/>
<segue destination="gfg-sS-SzG" kind="relationship" relationship="viewControllers" id="xUV-Vq-Zi1"/>
<segue destination="IQ2-7a-nMl" kind="relationship" relationship="viewControllers" id="g7w-KD-MZ7"/>
</connections>
</tabBarController>
<placeholder placeholderIdentifier="IBFirstResponder" id="2p7-AL-2P5" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
Expand Down
58 changes: 58 additions & 0 deletions KBPopupExample/ListViewController.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ iOS带箭头的起泡弹窗
`Podfile`文件中加入

```ruby
pod 'KBPopup', '~> 1.0.2'
pod 'KBPopup', '~> 1.1.0'
```


Expand Down

0 comments on commit 134bace

Please sign in to comment.