Skip to content

Commit c8e5c34

Browse files
author
Jasper
committed
【优化】webViewController优化
1 parent 5e3f8a3 commit c8e5c34

File tree

7 files changed

+401
-88
lines changed

7 files changed

+401
-88
lines changed

CYUtilProjectSwift/CYUtilProjectSwift/CYUtilProjectSwift.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
61091D721CDAFCDA008B7EA3 /* ImageDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61091D711CDAFCDA008B7EA3 /* ImageDownloader.swift */; };
1616
83D7016B52B862258E58891D /* Pods_CYUtilProjectSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2D53746AC48DAC068C766891 /* Pods_CYUtilProjectSwift.framework */; };
1717
A306D5631CE330A8008341F3 /* CYAlertViewAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = A306D5621CE330A8008341F3 /* CYAlertViewAction.swift */; };
18+
A30D07D01FCBBFE800373F1C /* WebViewControllerStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = A30D07CF1FCBBFE800373F1C /* WebViewControllerStyle.swift */; };
1819
A30EA3D21CFED40F006FEF23 /* CYAudioPlayUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = A30EA3D11CFED40F006FEF23 /* CYAudioPlayUtils.swift */; };
1920
A30EA3D41CFEE023006FEF23 /* CYAudioRecordUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = A30EA3D31CFEE023006FEF23 /* CYAudioRecordUtils.swift */; };
2021
A32F8C9B1F31C0AA003BA93B /* FloatingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A32F8C9A1F31C0AA003BA93B /* FloatingView.swift */; };
@@ -111,6 +112,7 @@
111112
7703B57890D7A5E3633F6303 /* Pods-CYUtilProjectSwiftTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CYUtilProjectSwiftTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-CYUtilProjectSwiftTests/Pods-CYUtilProjectSwiftTests.release.xcconfig"; sourceTree = "<group>"; };
112113
9213E6929DBD2C2D204EFCA7 /* Pods-CYUtilProjectSwiftTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CYUtilProjectSwiftTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CYUtilProjectSwiftTests/Pods-CYUtilProjectSwiftTests.debug.xcconfig"; sourceTree = "<group>"; };
113114
A306D5621CE330A8008341F3 /* CYAlertViewAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CYAlertViewAction.swift; sourceTree = "<group>"; };
115+
A30D07CF1FCBBFE800373F1C /* WebViewControllerStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewControllerStyle.swift; sourceTree = "<group>"; };
114116
A30EA3D11CFED40F006FEF23 /* CYAudioPlayUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CYAudioPlayUtils.swift; sourceTree = "<group>"; };
115117
A30EA3D31CFEE023006FEF23 /* CYAudioRecordUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CYAudioRecordUtils.swift; sourceTree = "<group>"; };
116118
A32F8C9A1F31C0AA003BA93B /* FloatingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FloatingView.swift; sourceTree = "<group>"; };
@@ -390,6 +392,7 @@
390392
children = (
391393
A371504A1F385C020079E9C5 /* WebViewController.swift */,
392394
A371504C1F3875350079E9C5 /* WebViewProgress.swift */,
395+
A30D07CF1FCBBFE800373F1C /* WebViewControllerStyle.swift */,
393396
);
394397
path = WebViewController;
395398
sourceTree = "<group>";
@@ -718,6 +721,7 @@
718721
A36B943B1F4E7F8900F14AAB /* CYAnimatedImageView.swift in Sources */,
719722
A345C5FC1DEBDE2F0094AF5A /* CYBaseProgressBar.swift in Sources */,
720723
A385A6AE1ED16C240022C2EB /* CYRedPacketDrawViewModel.swift in Sources */,
724+
A30D07D01FCBBFE800373F1C /* WebViewControllerStyle.swift in Sources */,
721725
A33E4B3B1D069B0C001B0AB1 /* CYJSLocalRequestUtils.swift in Sources */,
722726
A34F784A1FBC28C000A3D895 /* HTTPClient.swift in Sources */,
723727
A385A6A91ED168BE0022C2EB /* CYRedPacketDetailViewController.swift in Sources */,

CYUtilProjectSwift/CYUtilProjectSwift/CYUtilProjectSwift/ViewController.swift

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
//
88

99
import UIKit
10+
import WebKit
1011

11-
class ViewController: UITableViewController {
12+
class ViewController: UITableViewController, WKNavigationDelegate, WKUIDelegate {
1213

1314
@IBOutlet weak var recordPauseButton: UIButton!
1415
@IBOutlet weak var recordStopButton: UIButton!
@@ -164,14 +165,39 @@ class ViewController: UITableViewController {
164165
} else if indexPath.row == 4 {
165166
navigationController?.pushViewController(FloatingViewTestViewController(), animated: true)
166167
} else if indexPath.row == 5 {
167-
let web = WebViewController()
168-
_ = web.load(URLRequest(url: URL(string: "http://www.qq.com")!))
168+
169+
let webStyle = WebViewControllerStyle(closeBarButtonItemMode: .`default`, progressBarColor: UIColor.red)
170+
171+
let web = WebViewController(style: webStyle)
172+
web.delegate = self
173+
_ = web.load(URLRequest(url: URL(string: "http://www.qguanzi.com")!))
174+
175+
web.addNavigationRightItem(UIBarButtonItem(title: "更多", style: .plain, target: nil, action: nil)) { (item) in
176+
print("点击更多啦啦啦啦啦啦啦啦啦啦啦")
177+
}
178+
web.addNavigationRightItem(UIBarButtonItem(title: "Funy", style: .plain, target: nil, action: nil)) { (item) in
179+
print("点击Funy啦啦啦啦啦啦啦啦啦啦啦")
180+
}
181+
169182
navigationController?.pushViewController(web, animated: true)
170183
} else if indexPath.row == 6 {
171184
let animation = AnimationTestTableViewController(style: .plain)
172185
navigationController?.pushViewController(animation, animated: true)
173186
}
174187
}
175188
}
189+
190+
// MARK: WKNavigationDelegate
191+
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
192+
if (navigationAction.request.url?.absoluteString.starts(with: "http://www.qq.com") ?? false) {
193+
decisionHandler(.cancel)
194+
} else {
195+
decisionHandler(.allow)
196+
}
197+
}
198+
199+
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
200+
print("didFinish 就是到这里啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦")
201+
}
176202
}
177203

CYUtilProjectSwift/CYUtilProjectSwift/CYUtils/CYWebViewUtils/CYJSParametersSerialization/CYJSJSONParametersSerialization.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import UIKit
1010

1111
class CYJSJSONParametersSerialization: CYJSParametersSerialization {
1212

13-
override func serializeParameters( paramsString: String?) -> AnyObject? {
13+
override func serializeParameters(_ paramsString: String?) -> AnyObject? {
1414

1515
if let paramsStr = paramsString {
1616

17-
let jsonData = paramsStr.dataUsingEncoding(<#T##encoding: NSStringEncoding##NSStringEncoding#>)
1817
let jsonData = paramsStr.data(using: String.Encoding.utf8)
1918
if let data = jsonData {
2019

CYUtilProjectSwift/CYUtilProjectSwift/CYUtils/CycleBanner/CycleBannerView.swift

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import UIKit
1515
@objc optional func cycleBannerViewDidLayoutItems(_ cycleBannerView: CycleBannerView)
1616
}
1717

18-
open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDelegate {
18+
open class CycleBannerView: UIView, UIScrollViewDelegate {
1919

2020
@IBOutlet weak var delegate: CycleBannerViewDelegate?
2121

@@ -46,10 +46,7 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
4646
}
4747
}
4848
}
49-
// // 所有显示的Items,
50-
// // 如果cycleScrolling=false,则此属性与items值相同
51-
// // 否则,此属性值比Items多两个,其中internalItems[0] == items.last,internalItems.last == items[0],中间的都一样
52-
// private var internalItems: [CycleBannerViewItem] = []
49+
5350
// items count
5451
public var numberOfItems: Int {
5552

@@ -65,26 +62,13 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
6562
public var itemFooterWidth: CGFloat = 0
6663

6764
// 自动滚动,小于0表示不自动滚动,否则每次按时间滚动一页,默认为0
68-
public var autoplayDuration: Double = 0 {
69-
didSet {
70-
if autoplayDuration > 0
71-
&& numberOfItems > 0 {
72-
startAutoplay()
73-
} else {
74-
stopAutoplay()
75-
}
76-
}
77-
}
65+
public var autoplayDuration: Double = 0
7866

7967
// 是否循环滚动, 默认false
80-
public var cycleScrolling: Bool = false {
81-
didSet {
82-
83-
}
84-
}
68+
public var cycleScrolling: Bool = false
8569

8670
// select index
87-
private(set) var currentIndex: Int = 0
71+
fileprivate(set) var currentIndex: Int = 0
8872

8973
public func setCurrentIndex(_ index: Int, animated: Bool) {
9074
if index < 0
@@ -101,10 +85,6 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
10185
// scroll items
10286
private func _scrollToCurrentIndexItem_(_ animated: Bool) {
10387

104-
// if currentIndex == 0 {
105-
//
106-
// scrollView.setContentOffset(.zero, animated: animated)
107-
// } else
10888
if let item = item(at: currentIndex) {
10989

11090
let offsetX = item.center.x - scrollView.frame.width / 2.0
@@ -228,7 +208,6 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
228208
scrollView.addSubview(item)
229209

230210
let tap = UITapGestureRecognizer(target: self, action: #selector(_itemTapped_(sender:)))
231-
tap.delegate = self
232211
item.addGestureRecognizer(tap)
233212
}
234213
}
@@ -253,14 +232,6 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
253232

254233
public func indexOfItem(at point: CGPoint) -> Int? {
255234

256-
// // 第一个item的x
257-
// var enumertatedItems: [CycleBannerViewItem] = items
258-
// if (point.x < scrollView.contentSize.width - (scrollView.frame.width - itemSize.width) / 2.0 - itemFooterWidth
259-
// && point.x > scrollView.contentSize.width / 2.0)
260-
// || (point.x < (scrollView.frame.width - itemSize.width) / 2.0 - itemHeaderWidth){
261-
// // 从最后一个开始遍历
262-
// enumertatedItems = items.reversed()
263-
// }
264235
for (index, item) in items.enumerated() {
265236
if point.x < item.frame.maxX + itemFooterWidth
266237
&& point.x > item.frame.minX - itemHeaderWidth {
@@ -323,10 +294,10 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
323294
delegate?.scrollViewDidScroll?(scrollView)
324295
}
325296

326-
// MARK: UIGestureRecognizerDelegate
327-
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
328-
return true
329-
}
297+
// // MARK: UIGestureRecognizerDelegate
298+
// public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
299+
// return true
300+
// }
330301

331302
// event
332303
@objc private func _itemTapped_(sender: UITapGestureRecognizer?) {
@@ -346,7 +317,8 @@ extension CycleBannerView {
346317
// 开始之前,先停止之前的操作
347318
stopAutoplay()
348319
// 时间小于0,不自动滚动
349-
if autoplayDuration <= 0 {
320+
if autoplayDuration <= 0
321+
|| numberOfItems == 0 {
350322
return
351323
}
352324
perform(#selector(autoplayBanner),
@@ -360,6 +332,14 @@ extension CycleBannerView {
360332
object: nil)
361333
}
362334

335+
public func pauseAutoplay() {
336+
stopAutoplay()
337+
}
338+
339+
public func resumeAutoplay() {
340+
startAutoplay()
341+
}
342+
363343
@objc fileprivate func autoplayBanner() {
364344
var nextItemIndex = currentIndex + 1
365345
if cycleScrolling
@@ -384,11 +364,11 @@ extension CycleBannerView {
384364

385365
let contentMinX = (scrollView.frame.width - itemSize.width) / 2.0 - itemHeaderWidth
386366
let contentMaxX = scrollView.contentSize.width - (scrollView.frame.width - itemSize.width) / 2.0 + itemFooterWidth
387-
if scrollView.contentOffset.x < contentMinX {
367+
if scrollView.contentOffset.x <= contentMinX {
388368
// 滑到了最左边,把最右边的item挪到最左边
389369
items[0].center.x = scrollView.frame.width / 2.0
390370
items[numberOfItems - 1].center.x = contentMinX - itemFooterWidth - itemSize.width / 2.0
391-
} else if scrollView.contentOffset.x > contentMaxX - scrollView.frame.width {
371+
} else if scrollView.contentOffset.x >= contentMaxX - scrollView.frame.width {
392372
// 滑到最右边,把最左边的item挪到最右边
393373
items[numberOfItems - 1].center.x = scrollView.contentSize.width - scrollView.frame.width / 2.0
394374
items[0].center.x = contentMaxX + itemHeaderWidth + itemSize.width / 2.0
@@ -398,11 +378,15 @@ extension CycleBannerView {
398378
}
399379

400380
if scrollView.contentOffset.x <= -itemSize.width - itemHeaderWidth - itemFooterWidth {
401-
// 循环滚动的时候,滑到最后一个item(此时最后一个item在第一个item的左边),则调回开头
381+
// 循环滚动的时候,滑到最左边item,此时最左边item.index=numberOfItems-1,则跳回到numberOfItems-1本来的位置
402382
scrollView.setContentOffset(CGPoint(x: scrollView.contentSize.width - scrollView.frame.width, y: 0), animated: false)
383+
currentIndex = numberOfItems - 1
384+
pageControl.currentPage = currentIndex
403385
} else if scrollView.contentOffset.x >= scrollView.contentSize.width - scrollView.frame.width + itemSize.width + itemFooterWidth + itemHeaderWidth {
404-
// 循环滚动的时候,滑到第一个item(此时第一个item在最后一个item的右边),则调回开头
386+
// 循环滚动的时候,滑到最右边item,此时最右边Item.index=0,则调回开头
405387
scrollView.setContentOffset(.zero, animated: false)
388+
currentIndex = 0
389+
pageControl.currentPage = currentIndex
406390
}
407391
}
408392
}

CYUtilProjectSwift/CYUtilProjectSwift/CYUtils/HTTPUtils/HTTPClient.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import Alamofire
1111

1212
class HTTPClient: NSObject {
1313
func test() {
14-
Alamofire.download("http://www.baidu.com").downloadProgress { (progress) in
15-
16-
}.response { (<#DefaultDownloadResponse#>) in
17-
<#code#>
18-
}
14+
// Alamofire.download("http://www.baidu.com").downloadProgress { (progress) in
15+
//
16+
// }.response { (<#DefaultDownloadResponse#>) in
17+
// <#code#>
18+
// }
1919
}
2020
}

0 commit comments

Comments
 (0)