Skip to content

Commit

Permalink
code optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Aug 23, 2021
1 parent a3f3af5 commit 9ac1848
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Demo/demo-iOS/demo-iOS/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ extension RootViewController: UITableViewDataSource {
}

extension RootViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard let cell = tableView.cellForRow(at: indexPath) as? TableViewCell else {
return
}
playerView.set(resource: objects[indexPath.row])
cell.videoView.addSubview(playerView)
}
func scrollViewDidEndDecelerating(_: UIScrollView) {
beginVideoAction()
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/KSPlayer/AVPlayer/PlayerDefines.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,24 @@ public extension MediaPlayerProtocol {
guard let superview = view.superview, naturalSize != .zero else {
return
}
view.constraints.forEach { view.removeConstraint($0) }
view.widthConstraint.flatMap { view.removeConstraint($0) }
view.heightConstraint.flatMap { view.removeConstraint($0) }
view.centerXConstraint.flatMap { view.removeConstraint($0) }
view.centerYConstraint.flatMap { view.removeConstraint($0) }
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
view.centerXAnchor.constraint(equalTo: superview.centerXAnchor),
view.centerYAnchor.constraint(equalTo: superview.centerYAnchor),
view.widthAnchor.constraint(equalTo: view.heightAnchor, multiplier: naturalSize.width / naturalSize.height),
])
if naturalSize.isHorizonal {
NSLayoutConstraint.activate([
view.widthAnchor.constraint(equalTo: superview.widthAnchor),
view.heightAnchor.constraint(equalTo: view.widthAnchor, multiplier: naturalSize.height / naturalSize.width),
])
} else {
NSLayoutConstraint.activate([
view.heightAnchor.constraint(equalTo: superview.heightAnchor),
view.widthAnchor.constraint(equalTo: view.heightAnchor, multiplier: naturalSize.width / naturalSize.height),
])
}
}
Expand Down
10 changes: 7 additions & 3 deletions Sources/KSPlayer/MEPlayer/MetalPlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ final class MetalPlayView: UIView {
view.delegate = self
addSubview(view)
view.isHidden = true
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
topAnchor.constraint(equalTo: view.topAnchor),
leadingAnchor.constraint(equalTo: view.leadingAnchor),
trailingAnchor.constraint(equalTo: view.trailingAnchor),
bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
var controlTimebase: CMTimebase?
CMTimebaseCreateWithMasterClock(allocator: kCFAllocatorDefault, masterClock: CMClockGetHostTimeClock(), timebaseOut: &controlTimebase)
if let controlTimebase = controlTimebase {
Expand Down Expand Up @@ -177,9 +184,6 @@ extension MetalPlayView: MTKViewDelegate {
}

func mtkView(_ view: MTKView, drawableSizeWillChange _: CGSize) {
if view.frame != bounds {
view.frame = bounds
}
}
}
extension MetalPlayView: FrameOutput {
Expand Down

0 comments on commit 9ac1848

Please sign in to comment.