//
|
// ListenFight_Game_Pocket_CCell.swift
|
// DolphinEnglishLearnStudent
|
//
|
// Created by 无故事王国 on 2024/5/29.
|
//
|
|
import UIKit
|
import JQTools
|
|
class ListenFight_Game_Pocket_CCell: UICollectionViewCell {
|
|
enum PocketEnum{
|
case toFrom
|
case toback
|
}
|
|
lazy var mask_img:UIImageView = {
|
let image = UIImageView(image: UIImage(named: "bg_card"))
|
return image
|
}()
|
|
lazy var answer_img:UIImageView = {
|
let image = UIImageView()
|
image.jq_cornerRadius = 10
|
image.backgroundColor = .red
|
return image
|
}()
|
|
|
lazy var voice_view:UIView = {
|
let voiceView = UIView()
|
voiceView.backgroundColor = UIColor(hexString: "#41A2EB")
|
voiceView.jq_cornerRadius = 10
|
return voiceView
|
}()
|
|
let img1 = UIImageView(image: UIImage(named: "icon_play_1")!.withTintColor(UIColor(hexString: "#41A2EB")!))
|
let img2 = UIImageView(image: UIImage(named: "icon_playing")!.withTintColor(UIColor(hexString: "#41A2EB")!))
|
|
private var pocketEnum = PocketEnum.toback
|
|
let stateImg = UIImageView()
|
|
let playBtn = UIButton(type: .custom)
|
|
private var model:SimpleListenDataModel!
|
|
var indexPath:IndexPath!
|
private var playIndexClouse:((IndexPath)->Void)?
|
|
override init(frame: CGRect) {
|
super.init(frame: frame)
|
addSubview(answer_img)
|
answer_img.snp.makeConstraints { make in
|
make.edges.equalToSuperview()
|
}
|
|
|
addSubview(voice_view)
|
voice_view.snp.makeConstraints { make in
|
make.edges.equalToSuperview()
|
}
|
|
addSubview(mask_img)
|
mask_img.snp.makeConstraints { make in
|
make.edges.equalToSuperview()
|
}
|
|
let contentVoiceView = UIView()
|
contentVoiceView.backgroundColor = .white
|
contentVoiceView.jq_cornerRadius = 8
|
voice_view.addSubview(contentVoiceView)
|
contentVoiceView.snp.makeConstraints { make in
|
make.center.equalToSuperview()
|
make.width.equalTo(self.width * 0.66)
|
make.height.equalTo(self.height * 0.32)
|
}
|
|
contentVoiceView.addSubview(img1)
|
img1.snp.makeConstraints { make in
|
make.left.equalTo(self.width * 0.0958)
|
make.width.height.equalTo(self.width * 0.133)
|
make.centerY.equalToSuperview()
|
}
|
|
|
contentVoiceView.addSubview(img2)
|
img2.snp.makeConstraints { make in
|
make.width.equalTo(self.width * 0.1875)
|
make.height.equalTo(self.width * 0.1292)
|
make.center.equalToSuperview()
|
}
|
|
playBtn.addTarget(self, action: #selector(playAction), for: .touchUpInside)
|
playBtn.setBackgroundImage(UIImage(named: "icon_play")?.withTintColor(UIColor(hexString: "#41A2EB")!), for: .normal)
|
playBtn.isEnabled = false
|
contentVoiceView.addSubview(playBtn)
|
playBtn.snp.makeConstraints { make in
|
make.right.equalTo(-self.width * 0.0958)
|
make.width.height.equalTo(self.width * 0.133)
|
make.centerY.equalToSuperview()
|
}
|
|
addSubview(stateImg)
|
stateImg.snp.makeConstraints { make in
|
make.center.equalToSuperview()
|
make.width.height.equalTo(self.width * 0.316)
|
}
|
|
cellResotePay()
|
}
|
|
func cellResotePay(){
|
img2.isHidden = true
|
img1.isHidden = false
|
playBtn.isHidden = false
|
layoutIfNeeded()
|
}
|
|
func cellPlaying(){
|
img2.isHidden = false
|
img1.isHidden = true
|
playBtn.isHidden = true
|
playIndexClouse?(indexPath)
|
}
|
|
func setState(success:Bool){
|
if success{
|
stateImg.image = UIImage(named: "icon_success")
|
}else{
|
stateImg.image = UIImage(named: "icon_fail")
|
}
|
|
|
stateImg.alpha = 0
|
stateImg.transform = .init(scaleX: 0.1, y: 0.1)
|
layoutIfNeeded()
|
|
UIView.animate(withDuration: 0.4) {
|
self.stateImg.alpha = 1
|
self.stateImg.transform = .init(scaleX: 1.0, y: 1.0)
|
} completion: { state in
|
if state{
|
DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.5){
|
UIView.animate(withDuration: 0.5) {
|
self.stateImg.alpha = 0
|
self.stateImg.transform = .init(scaleX: 0.1, y: 0.1)
|
}
|
}
|
}
|
}
|
|
}
|
|
func cellPayatIndex(_ clouse:@escaping (IndexPath)->Void){
|
self.playIndexClouse = clouse
|
}
|
|
@objc func playAction(){
|
//防止穿透点击播放
|
if pocketEnum == .toFrom{
|
VoicePlayer.share().playerAt(url: model.voice)
|
cellPlaying()
|
}
|
}
|
|
func setSimpleModel(_ model:SimpleListenDataModel){
|
self.model = model
|
if model.type == 1{
|
voice_view.isHidden = true
|
answer_img.sd_setImage(with: URL(string: model.photo))
|
}else{
|
voice_view.isHidden = false
|
}
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
func toFromAction(_ superView:UIView? = nil,c:Bool? = nil){
|
superView?.isUserInteractionEnabled = false
|
pocketEnum = .toFrom
|
playBtn.isEnabled = true
|
UIView.transition(from: mask_img, to: answer_img, duration: 0.6, options: [.transitionFlipFromLeft,.showHideTransitionViews]) { _ in
|
if c == true{
|
superView?.isUserInteractionEnabled = true
|
}
|
}
|
}
|
|
func toBackAction(_ superView:UIView? = nil,c:Bool? = nil){
|
superView?.isUserInteractionEnabled = false
|
pocketEnum = .toback
|
playBtn.isEnabled = false
|
UIView.transition(from: answer_img, to: mask_img, duration: 0.6,options: [.transitionFlipFromRight,.showHideTransitionViews]){ _ in
|
if c == true{
|
superView?.isUserInteractionEnabled = true
|
}
|
}
|
}
|
}
|