杨锴
3 天以前 876bce3064536c036112dd1834fb0d0013a57d16
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
//
//  VoiceHandleView.swift
//  DolphinEnglishLearnStudent
//
//  Created by 无故事王国 on 2024/6/11.
//
 
import UIKit
 
class VoiceHandleView: UIView {
 
    private lazy var img_hint:UIImageView = {
        let img = UIImageView(image: UIImage(named: "icon_play_1"))
        return img
    }()
 
    private lazy var img_hint_playing:UIImageView = {
        let img = UIImageView(image: UIImage(named: "icon_playing")?.themeGreen)
        img.isHidden = true
        return img
    }()
 
    private lazy var btn_play:UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "icon_play"), for: .normal)
        return btn
    }()
 
    let playBtn = UIButton(type: .custom)
 
    var isPlayed:Bool = false //是否已播放
    var playUrl:String?
    var listenType:ListenType?
    private var playAtClouse:((Int)->Void)?
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
 
        VoicePlayer.share().playEnd {
            self.resetView()
        }
    }
 
    private func setUI(){
        //                                backgroundColor = UIColor(hexString: "#41A2EB")
        backgroundColor = UIColor.white
        jq_cornerRadius = 8
        addSubview(img_hint_playing)
        addSubview(img_hint)
        addSubview(btn_play)
 
        img_hint_playing.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(45)
            make.height.equalTo(31)
        }
 
        btn_play.isUserInteractionEnabled = false
        btn_play.snp.makeConstraints { make in
            make.left.equalTo(img_hint_playing.snp.right).offset(0)
            make.centerY.equalToSuperview()
            make.width.equalTo(32)
            make.height.equalTo(32)
        }
 
        img_hint.snp.makeConstraints { make in
            make.right.equalTo(img_hint_playing.snp.left).offset(0)
            make.centerY.equalToSuperview()
            make.width.equalTo(27)
            make.height.equalTo(27)
        }
 
        playBtn.addTarget(self, action: #selector(playingAction), for: .touchUpInside)
        addSubview(playBtn)
        playBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    func copyView()->VoiceHandleView{
        let copyView = VoiceHandleView()
        copyView.listenType = self.listenType
        copyView.playUrl = self.playUrl
        copyView.frame = self.frame
        return copyView
    }
 
    func resetView(){
        img_hint.isHidden = false
        btn_play.isHidden = false
        img_hint_playing.isHidden = true
        isPlayed = false
        jq_cornerRadius = 8
    }
 
    func playing(){
        img_hint.isHidden = true
        btn_play.isHidden = true
        isPlayed = true
        img_hint_playing.isHidden = false
    }
 
    func playAt(_ clouse:@escaping(Int)->Void){
        self.playAtClouse = clouse
    }
 
    @objc func playingAction(){
        if let url = playUrl{
            playAtClouse?(self.tag)
            VoicePlayer.share().playerAt(url: url)
            playing()
        }
    }
 
}