Skip to content

Commit

Permalink
增加自定义属性API
Browse files Browse the repository at this point in the history
  • Loading branch information
JmoVxia committed Dec 16, 2021
1 parent 7dbc11c commit 62932dd
Show file tree
Hide file tree
Showing 21 changed files with 871 additions and 87 deletions.
31 changes: 30 additions & 1 deletion CLAutolayoutController/CLAutolayoutController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ class CLAutolayoutController: CLController {
}

private lazy var player: CLPlayer = {
let view = CLPlayer()
let view = CLPlayer { config in
config.isAutoRotate = true
config.isGestureInteractionEnabled = false
config.autoFadeOut = 8
config.topBarHiddenStyle = .onlySmall
config.maskImage = UIImage(named: "placeholder")
}
view.delegate = self
return view
}()

Expand Down Expand Up @@ -104,7 +111,13 @@ private extension CLAutolayoutController {

private extension CLAutolayoutController {
func initData() {
player.title = NSMutableAttributedString("Apple", attributes: { $0
.font(.systemFont(ofSize: 16))
.foregroundColor(.white)
.alignment(.center)
})
player.url = URL(string: "https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4")
player.play()
}
}

Expand All @@ -125,6 +138,22 @@ extension CLAutolayoutController {

@objc private extension CLAutolayoutController {
func changeAction() {
player.title = NSMutableAttributedString("这是一个标题", attributes: { $0
.font(.systemFont(ofSize: 16))
.foregroundColor(.orange)
.alignment(.left)
})
player.url = URL(string: "https://www.apple.com/105/media/us/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/peter/mac-peter-tpl-cc-us-2018_1280x720h.mp4")
player.play()
}
}

extension CLAutolayoutController: CLPlayerDelegate {
func didClickBackButton(in _: CLPlayer) {
print("didClickBackButton")
}

func didPlayToEndTime(in _: CLPlayer) {
print("didPlayToEndTime")
}
}
198 changes: 198 additions & 0 deletions CLCollectionViewController/CLCollectionViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
//
// CLCollectionViewController.swift
// CLPlayer
//
// Created by Chen JmoVxia on 2021/12/16.
//

import SnapKit
import UIKit

// MARK: - JmoVxia---枚举

extension CLCollectionViewController {}

// MARK: - JmoVxia---类-属性

class CLCollectionViewController: CLController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit {}

private lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 10
layout.minimumInteritemSpacing = 10
layout.itemSize = CGSize(width: view.bounds.width - 20, height: (view.bounds.width - 20) * 9.0 / 16.0)
let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
view.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
view.backgroundColor = .clear
view.delegate = self
view.dataSource = self
view.register(CLCollectionViewCell.self, forCellWithReuseIdentifier: "CLCollectionViewCell")
return view
}()

private var player: CLPlayer?

let array = [
"http://vfx.mtime.cn/Video/2019/03/19/mp4/190319212559089721.mp4",
"http://vfx.mtime.cn/Video/2019/03/18/mp4/190318231014076505.mp4",
"http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4",
"http://vfx.mtime.cn/Video/2019/03/21/mp4/190321153853126488.mp4",
"http://vfx.mtime.cn/Video/2019/03/19/mp4/190319222227698228.mp4",
"http://vfx.mtime.cn/Video/2019/03/19/mp4/190319212559089721.mp4",
"http://vfx.mtime.cn/Video/2019/03/18/mp4/190318231014076505.mp4",
"http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4",
"http://vfx.mtime.cn/Video/2019/03/19/mp4/190319104618910544.mp4",
"http://vfx.mtime.cn/Video/2019/03/19/mp4/190319125415785691.mp4",
"http://vfx.mtime.cn/Video/2019/03/17/mp4/190317150237409904.mp4",
"http://vfx.mtime.cn/Video/2019/03/14/mp4/190314223540373995.mp4",
"http://vfx.mtime.cn/Video/2019/03/14/mp4/190314102306987969.mp4",
"http://vfx.mtime.cn/Video/2019/03/13/mp4/190313094901111138.mp4",
"http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4",
"http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4",
"http://vfx.mtime.cn/Video/2019/03/09/mp4/190309153658147087.mp4",
"https://www.apple.com/105/media/us/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/grimes/mac-grimes-tpl-cc-us-2018_1280x720h.mp4",
"https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4",
"https://www.apple.com/105/media/us/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/peter/mac-peter-tpl-cc-us-2018_1280x720h.mp4",
"http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4",
]
}

// MARK: - JmoVxia---生命周期

extension CLCollectionViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}

override func viewDidLoad() {
super.viewDidLoad()
initUI()
makeConstraints()
initData()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
}

// MARK: - JmoVxia---布局

private extension CLCollectionViewController {
func initUI() {
updateTitleLabel { $0.text = "CollectionView" }
view.addSubview(collectionView)
}

func makeConstraints() {
collectionView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
if #available(iOS 11.0, *) {
make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
} else {
make.top.equalTo(topLayoutGuide.snp.bottom)
}
}
}
}

// MARK: - JmoVxia---数据

private extension CLCollectionViewController {
func initData() {}
}

// MARK: - JmoVxia---override

extension CLCollectionViewController {}

// MARK: - JmoVxia---objc

@objc private extension CLCollectionViewController {}

// MARK: - JmoVxia---私有方法

private extension CLCollectionViewController {
func playWithIndexPath(_ indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) else { return }

if player == nil {
player = CLPlayer()
}
player?.title = NSMutableAttributedString("这是一个标题", attributes: { $0
.font(.systemFont(ofSize: 16))
.foregroundColor(.orange)
.alignment(.center)
})
player?.url = URL(string: array[indexPath.row])
cell.contentView.addSubview(player!)
player?.snp.remakeConstraints { make in
make.top.left.equalToSuperview()
make.size.equalTo(CGSize(width: cell.bounds.width, height: cell.bounds.height - 10))
}
player?.play()
}
}

// MARK: - JmoVxia---公共方法

extension CLCollectionViewController {}

extension CLCollectionViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CLCollectionViewCell", for: indexPath)
return cell
}

func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
return array.count
}
}

extension CLCollectionViewController: UICollectionViewDelegate {
func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
playWithIndexPath(indexPath)
}

func collectionView(_: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let player = player else { return }
guard array[indexPath.row] == player.url?.absoluteString else { return }

cell.contentView.addSubview(player)
player.snp.remakeConstraints { make in
make.top.left.equalToSuperview()
make.size.equalTo(CGSize(width: cell.bounds.width, height: cell.bounds.height - 10))
}
player.play()
}

func collectionView(_: UICollectionView, didEndDisplaying _: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let player = player else { return }
guard array[indexPath.row] == player.url?.absoluteString else { return }

player.removeFromSuperview()
player.pause()
}
}
51 changes: 51 additions & 0 deletions CLCollectionViewController/View/CLCollectionViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// CLCollectionViewCell.swift
// CLPlayer
//
// Created by Chen JmoVxia on 2021/12/16.
//

import UIKit

class CLCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
initUI()
makeConstraints()
}

@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private lazy var iconImageView: UIImageView = {
let view = UIImageView()
view.image = UIImage(named: "placeholder")
return view
}()

private lazy var playImageView: UIImageView = {
let view = UIImageView()
view.image = UIImage(named: "play")
return view
}()
}

// MARK: - JmoVxia---布局

private extension CLCollectionViewCell {
func initUI() {
contentView.addSubview(iconImageView)
iconImageView.addSubview(playImageView)
}

func makeConstraints() {
iconImageView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
playImageView.snp.makeConstraints { make in
make.center.equalToSuperview()
}
}
}
12 changes: 12 additions & 0 deletions CLFrameController/CLFrameController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ private extension CLFrameController {

private extension CLFrameController {
func initData() {
player.title = NSMutableAttributedString("Apple", attributes: { $0
.font(.systemFont(ofSize: 16))
.foregroundColor(.white)
.alignment(.center)
})
player.url = URL(string: "https://www.apple.com/105/media/cn/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/bruce/mac-bruce-tpl-cn-2018_1280x720h.mp4")
player.play()
}
}

Expand All @@ -112,6 +118,12 @@ extension CLFrameController {

@objc private extension CLFrameController {
func changeAction() {
player.title = NSMutableAttributedString("这是一个标题", attributes: { $0
.font(.systemFont(ofSize: 16))
.foregroundColor(.white)
.alignment(.left)
})
player.url = URL(string: "http://vfx.mtime.cn/Video/2019/03/09/mp4/190309153658147087.mp4")
player.play()
}
}
13 changes: 13 additions & 0 deletions CLHomeController/Controller/CLHomeController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ private extension CLHomeController {
}
tableViewHepler.dataSource.append(item)
}
do {
let item = CLListItem()
item.title = "UICollectionView"
item.didSelectCellCallback = { [weak self] _ in
guard let self = self else { return }
self.pushToCollectionView()
}
tableViewHepler.dataSource.append(item)
}
tableView.reloadData()
}
}
Expand All @@ -149,6 +158,10 @@ private extension CLHomeController {
func pushToTableView() {
navigationController?.pushViewController(CLTableViewController(), animated: true)
}

func pushToCollectionView() {
navigationController?.pushViewController(CLCollectionViewController(), animated: true)
}
}

// MARK: - JmoVxia---公共方法
Expand Down
2 changes: 1 addition & 1 deletion CLPlayer.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'CLPlayer'
s.version = '2.0.1'
s.version = '2.0.2'
s.summary = 'Swift版自定义AVPlayer'
s.description = <<-DESC
CLPlayer是基于系统AVPlayer封装的视频播放器.
Expand Down
Loading

0 comments on commit 62932dd

Please sign in to comment.