123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- //
- // KMAdvertisementShowView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/10/26.
- //
- import Cocoa
- typealias KMAdvertisementShowViewDidSelect = (_ view: KMAdvertisementShowView, _ item: KMAdvertisementItemInfo) -> Void
- class KMAdvertisementShowView: NSView {
- @IBOutlet var contentView: NSView!
- @IBOutlet weak var iconImage: NSImageView!
- @IBOutlet weak var closeButton: NSButton!
- @IBOutlet weak var contentButton: NSButton!
-
- var didSelect: KMAdvertisementShowViewDidSelect?
-
- open var inputData: KMAdvertisementItem? {
- didSet {
- self.reloadData()
- }
- }
-
- convenience init (data: [KMAdvertisementItem], superView: NSView) {
- self.init(frame: superView.bounds)
- superView.addSubview(self)
- self.autoresizingMask = [.height , .width]
- }
-
- public override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- initContentView()
- setup()
- }
-
- // MARK: 初始化
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
- initContentView()
- setup()
- }
- private func initContentView() {
- //绑定xib
- let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
- bundle: Bundle(for: self.classForCoder.self))!
- resource.instantiate(withOwner: self, topLevelObjects: nil)
- addSubview(contentView)
- contentView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- contentView.topAnchor.constraint(equalTo: topAnchor),
- contentView.leftAnchor.constraint(equalTo: leftAnchor),
- contentView.rightAnchor.constraint(equalTo: rightAnchor),
- contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
- contentView.updateConstraintsForSubtreeIfNeeded()
- }
-
- func setup() {
- self.contentView.isHidden = true
- // self.backgroundColor(NSColor.black)
- }
-
- func reloadData() {
- guard let model = inputData?.content?.first else {
- self.contentView.isHidden = true
- return
- }
- self.contentView.isHidden = false
-
- let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.image, highlight: false))
- self.iconImage.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
- self?.iconImage.image = image
- })
- }
-
- @IBAction func contentButtonAction(_ sender: Any) {
- guard let model = inputData?.content?.first else { return }
-
- self.didSelect?(self, model)
- }
-
- @IBAction func closeButtonAction(_ sender: Any) {
- self.removeFromSuperview()
- }
- }
|