CNLiveShopGoodsDetailsViewController.swift
16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// CNLiveShopGoodsDetailsViewController.swift
// metaCode
//
// Created by zx on 2023/11/20.
//
import Foundation
import YYImage
import YYWebImage
import SKPhotoBrowser
import CNLiveShareToolKit
import BKCycleScrollView
///分类类型
enum CNLiveShopGoodsDetailsVCType:Int {
///正常
case normal
///预览
case preview
}
class CNLiveShopGoodsDetailsViewController:CNLiveBaseViewController,UITableViewDelegate,UITableViewDataSource, BKCycleScrollViewDelegate{
let kDSGoodsDetailsTableViewCellID = "DSGoodsDetailsTableViewCell"
let kDSPlayerAnimateTimeInterval:CGFloat = 0.25
/// 格式
var type:CNLiveShopGoodsDetailsVCType?
/// 商品id
var goodsId:String?
/// 编辑商品预览使用
var detailsModel:CNLiveShopGoodsDetailsModel?
var leftCircleBgView:CNLiveRoundedRectView?
var leftImageView:YYAnimatedImageView?
var shareCircleBgView:CNLiveRoundedRectView?
var shareImageView:YYAnimatedImageView?
var CSV_playerView:CNLivePlayerView?
weak var cycleScrollView :BKCycleScrollView? = {
let cycleScrollView = BKCycleScrollView()
return cycleScrollView
}()
lazy var tableView : UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: self.view.height), style: .plain)
tableView.dataSource = self
tableView.delegate = self
tableView.separatorStyle = .none
tableView.tableFooterView = UIView.init()
tableView.backgroundColor = HexColor("#f1f2f6")
tableView.register(CNLiveGoodsDetailsTableViewCell.self, forCellReuseIdentifier: kDSGoodsDetailsTableViewCellID)
return tableView
}()
//图片集合
lazy var imgs :NSMutableArray = {
let imgs = NSMutableArray()
return imgs
}()
override func viewDidLoad() {
super.viewDidLoad()
self.initNav()
self.view.insertSubview(self.tableView, belowSubview: self.topNavView)
navigationBarHidden = false
if self.type == .preview{
self.insertData()
}else{
self.requestGoodsInfo()
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.tableView.frame = CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height)
}
var isPrefersStatusBarHidden:Bool = false
override var prefersStatusBarHidden: Bool {
return self.isPrefersStatusBarHidden
}
deinit {
if (self.cycleScrollView != nil){
self.cycleScrollView = nil
}
self.destroyPlayerView()
}
func initNav(){
self.topNavView.backgroundColor = UIColor(white: 1, alpha: 0)
self.topLine.alpha = 0
self.leftNavBtns.forEach { $0.removeFromSuperview() }
let backNavBtn = UIButton(type: .custom)
backNavBtn.frame = CGRect(x: 0, y: get_system_statusBar_height(), width: get_system_nav_ui_height(), height: get_system_nav_ui_height())
backNavBtn.addTarget(self, action: #selector(backNavBtnClick), for: .touchUpInside)
self.topNavView.addSubview(backNavBtn)
self.leftCircleBgView = CNLiveRoundedRectView(frame: CGRect(x: 15, y: (backNavBtn.height - 32)/2, width: 32, height: 32))
self.leftCircleBgView?.fillColor = HexColor("#848484", alpha: 0.45)
self.leftCircleBgView?.radius = self.leftCircleBgView!.height/2
self.leftCircleBgView?.isUserInteractionEnabled = false
backNavBtn.addSubview(self.leftCircleBgView!)
let wLeftImageView = YYAnimatedImageView(frame: CGRect(x: 15, y: (backNavBtn.height - 32)/2, width: 32, height: 32))
wLeftImageView.clipsToBounds = true
wLeftImageView.contentMode = .scaleAspectFit
wLeftImageView.image = UIImage(named: "DS_goods_back_g")
backNavBtn.addSubview(wLeftImageView)
self.leftImageView = YYAnimatedImageView(frame: CGRect(x: 15, y: (backNavBtn.height - 32)/2, width: 32, height: 32))
self.leftImageView?.clipsToBounds = true
self.leftImageView?.contentMode = .scaleAspectFit
self.leftImageView?.image = UIImage(named: "DS_goods_back_g")
self.leftImageView?.alpha = 0
backNavBtn.addSubview(self.leftImageView!)
if self.goodsId?.count ?? 0 > 0{
let shareNavBtn = UIButton(type: .custom)
shareNavBtn.frame = CGRect(x: KSreenWidth-62, y: get_system_statusBar_height(), width: get_system_nav_ui_height(), height: get_system_nav_ui_height())
shareNavBtn.addTarget(self, action: #selector(shareNavBtnClick), for: .touchUpInside)
self.topNavView.addSubview(shareNavBtn)
self.shareCircleBgView = CNLiveRoundedRectView(frame: CGRect(x: 15, y: (shareNavBtn.height - 32)/2, width: 32, height: 32))
self.shareCircleBgView?.fillColor = HexColor("#848484", alpha: 0.45)
self.shareCircleBgView?.radius = self.shareCircleBgView!.height/2
self.shareCircleBgView?.isUserInteractionEnabled = false
shareNavBtn.addSubview(self.shareCircleBgView!)
let wShareImageView = YYAnimatedImageView(frame: CGRect(x: 15, y: (shareNavBtn.height - 32), width: 32, height: 32))
wShareImageView.clipsToBounds = true
wShareImageView.contentMode = .scaleAspectFit
wShareImageView.image = UIImage(named: "DS_goods_share_g")
wShareImageView.alpha = 0
shareNavBtn.addSubview(wShareImageView)
self.shareImageView = YYAnimatedImageView(frame: CGRect(x: 15, y: (shareNavBtn.height - 32), width: 32, height: 32))
self.shareImageView?.clipsToBounds = true
self.shareImageView?.contentMode = .scaleAspectFit
self.shareImageView?.image = UIImage(named: "DS_goods_share_g")
self.shareImageView?.alpha = 0
shareNavBtn.addSubview(self.shareImageView!)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.detailsModel?.detail_img.count ?? 0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let detailsContentModel = self.detailsModel?.detail_img[indexPath.row]
if detailsContentModel?.w ?? 0 > 0{
return floor(self.view.width / (detailsContentModel?.w ?? 0) * (detailsContentModel?.h ?? 0))
}else{
return 200
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: kDSGoodsDetailsTableViewCellID) as! CNLiveGoodsDetailsTableViewCell
let detailsContentModel = self.detailsModel?.detail_img[indexPath.row]
var escapedUrl = detailsContentModel?.simg?.removingPercentEncoding
escapedUrl = escapedUrl?.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
cell.displayImageView?.yy_setImage(with: URL(string: escapedUrl!), placeholder: nil, options: [.showNetworkActivity , .setImageWithFadeAnimation], completion: { image, url, from, stage, error in
if (image == nil) {
return
}
if (image!.size.height == detailsContentModel?.h) {
return
}
detailsContentModel?.w = image!.size.width
detailsContentModel?.h = image!.size.height
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let browser = SKPhotoBrowser(photos: self.imgs as! [SKPhotoProtocol])
browser.initializePageIndex(indexPath.row)
currentViewController()?.present(browser, animated: true)
}
// MARK: - UIScrollViewDelegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if self.tableView == scrollView{
if self.tableView.contentOffset.y < 0{
self.topNavView.backgroundColor = UIColor(white: 1, alpha: 0)
self.leftCircleBgView?.alpha = 1
self.leftImageView?.alpha = 0
self.shareCircleBgView?.alpha = 1
self.shareImageView?.alpha = 0
self.topLine.alpha = 0
}else if self.tableView.contentOffset.y >= 128{
self.topNavView.backgroundColor = UIColor(white: 1, alpha: 1)
self.leftCircleBgView?.alpha = 0
self.leftImageView?.alpha = 1
self.shareCircleBgView?.alpha = 0
self.shareImageView?.alpha = 1
self.topLine.alpha = 1
}else{
let indexAlpha = self.tableView.contentOffset.y/128.0
self.leftCircleBgView?.alpha = 1 - indexAlpha
self.leftImageView?.alpha = indexAlpha
self.shareCircleBgView?.alpha = 1 - indexAlpha
self.shareImageView?.alpha = indexAlpha
self.topLine.alpha = indexAlpha
self.topNavView.backgroundColor = UIColor(white: 1, alpha: indexAlpha)
}
}
}
// MARK: - 获取商品详情
func requestGoodsInfo(){
showLoading(message: "")
CNLiveShopGoodListViewModel.getGoodsInfoWithGoodsId(goodsId: self.goodsId ?? "") { result, status in
hiddenLoading()
if status == .failed{
showMessage(message: "获取失败")
return
}
if status == .success{
let model = result as! CNLiveShopGoodsDetailsModel
self.detailsModel = model
self.insertData()
}
}
}
func insertData(){
var headerView:CNLiveShopGoodsDetailsTableHeaderView?
if self.tableView.tableHeaderView == nil{
headerView = CNLiveShopGoodsDetailsTableHeaderView(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: 0))
headerView!.bannerView.delegate = self
headerView!.clickGiveGiftBlock = {
}
}else{
headerView = self.tableView.tableHeaderView as? CNLiveShopGoodsDetailsTableHeaderView
}
headerView!.detailsModel = self.detailsModel
self.tableView.tableHeaderView = headerView
self.tableView.reloadData()
self.tableView.setContentOffset(CGPointZero, animated: true)
for model in self.detailsModel!.detail_img {
let photo = SKPhoto.photoWithImageURL(model.simg ?? "")
photo.shouldCachePhotoURLImage = false
photo.photoURL = model.simg
self.imgs.add(photo)
}
}
// MARK: - 销毁播放器
func destroyPlayerView(){
self.CSV_playerView?.stop()
if self.CSV_playerView?.currentOrientation != .portrait{
self.CSV_playerView?.currentOrientation = .portrait
}
self.CSV_playerView?.superview?.removeFromSuperview()
self.CSV_playerView?.removeFromSuperview()
self.CSV_playerView = nil
}
// MARK: - BKCycleScrollViewDelegate
func cycleScrollView(_ cycleScrollView: BKCycleScrollView, play playIndex: UInt, superView: UIView) {
let dataModel = cycleScrollView.displayDataArr![Int(playIndex)] as BKCycleScrollDataModel
if (self.CSV_playerView == nil){
let playerView = CNLivePlayerView()
self.CSV_playerView = playerView
if dataModel.imageUrl.count > 0{
self.CSV_playerView?.controlView.coverImageUrl = dataModel.imageUrl
}else if dataModel.image.size != CGSizeZero{
self.CSV_playerView?.controlView.coverImage = dataModel.image
}
self.CSV_playerView?.controlView.fullScreenType = .inWindowOrientation
self.CSV_playerView?.controlView.topNaviView?.backBtn?.isHidden = true
self.CSV_playerView?.isMonitorDeviceOrientation = false
self.CSV_playerView?.controlView.gestureView?.panGestureRecognizer?.isEnabled = false
self.CSV_playerView?.controlView.coverImageViewContentMode = .scaleAspectFill
self.CSV_playerView?.switchInterfaceOrientationCallBack = { playerView in
if playerView.controlView.isFullScreen!{
playerView.controlView.bottomProgressView?.isHidden = true
}else{
playerView.controlView.bottomProgressView?.isHidden = false
}
if #available(iOS 11.0, *) {
self.setNeedsUpdateOfHomeIndicatorAutoHidden()
} else {
}
UIView.animate(withDuration: self.kDSPlayerAnimateTimeInterval) {
//iPhoneX系列在横屏时隐藏状态栏
if playerView.controlView.isAlphaMessage == false{
if (isiPhoneXSeries || Double(UIDevice.current.systemVersion) ?? 0 >= 13) && (playerView.controlView.isFullScreen != nil) {
self.isPrefersStatusBarHidden = true
}else{
self.isPrefersStatusBarHidden = false
}
self.setNeedsStatusBarAppearanceUpdate()
}
if playerView.controlView.isFullScreen ?? false{
playerView.isMonitorDeviceOrientation = true
}else{
playerView.isMonitorDeviceOrientation = false
}
}
if playerView.currentOrientation == .portrait{
playerView.controlView.gestureView?.panGestureRecognizer?.isEnabled = false
}else{
if playerView.controlView.isLockScreen{
playerView.controlView.gestureView?.panGestureRecognizer?.isEnabled = false
}else{
playerView.controlView.gestureView?.panGestureRecognizer?.isEnabled = true
}
}
}
self.CSV_playerView?.playerPlayingCallBack = {
playerView in
self.cycleScrollView?.playVideo()
}
self.CSV_playerView?.playerPausedCallBack = {
playerView in
self.cycleScrollView?.pauseVideo()
}
self.CSV_playerView?.playerDidToEndCallBack = {
playerView in
self.cycleScrollView?.stopVideo()
self.destroyPlayerView()
}
}
superView.addSubview(self.CSV_playerView!)
self.CSV_playerView!.frame = superView.bounds
let model = cycleScrollView.displayDataArr?[Int(playIndex)]
self.CSV_playerView?.setPlayUrl(playUrl: URL(string: model?.videoUrl ?? "") ?? URL(fileURLWithPath: ""), isLive: false)
self.CSV_playerView?.play()
}
func cycleScrollView(_ cycleScrollView: BKCycleScrollView, pause pauseIndex: UInt) {
self.CSV_playerView?.pauseWithIsRemoveKVOsAndNotifications(isRemove: false)
}
func cycleScrollView(_ cycleScrollView: BKCycleScrollView, stop stopIndex: UInt) {
self.destroyPlayerView()
}
// MARK: - BtnAction
@objc func backNavBtnClick(){
self.navigationController?.popViewController(animated: true)
}
@objc func shareNavBtnClick(){
CNLiveShopGoodListViewModel.postGetShareDataWithType(type: 2, targetId: self.goodsId!, ext: "") {[weak self] result, status in
let model = result as! CNLiveShopCommentRequestShareModel
CNLiveShareManager.showShareViewWithParam(forShareTitle: model.share_title, shareUrl: model.url, shareDesc: model.share_desc, shareImage: model.share_img, screenFull: false, hiddenWjj: true, hiddenQQ: true, hiddenWB: true, hiddenLifeCircle: true, hiddenWechatCircle: false, hiddenWechat: false, hiddenSafari: true, formVC: self, topImage: nil, topTitles: nil, platformType: .all) { str in
} completerBlock: { resultType, platformType, typtString in
if (resultType == .succ) {
showMessage(message: "分享成功")
}
if (resultType == .fail) {
showMessage(message: "分享失败")
}
}
}
}
}