CustomPlayerView.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // CustomPlayerView.m
  3. // BuguLive
  4. //
  5. // Created by voidcat on 2024/5/23.
  6. // Copyright © 2024 xfg. All rights reserved.
  7. //
  8. #import "CustomPlayerView.h"
  9. @implementation CustomPlayerView
  10. - (instancetype)init {
  11. self = [super init];
  12. if (self) {
  13. [self setupViews];
  14. [self setupConstraints];
  15. [self setupRoundedCorners];
  16. [self setupShadow];
  17. }
  18. return self;
  19. }
  20. - (void)setupRoundedCorners {
  21. // UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
  22. // byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
  23. // cornerRadii:CGSizeMake(20.0, 20.0)];
  24. //
  25. // CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  26. // maskLayer.frame = self.bounds;
  27. // maskLayer.path = maskPath.CGPath;
  28. // self.layer.mask = maskLayer;
  29. }
  30. - (void)setupShadow {
  31. // CALayer *subLayer = [CALayer layer];
  32. // CGRect fixframe = CGRectMake(0, 0, SCREEN_WIDTH, 74);
  33. // subLayer.frame= fixframe;
  34. // subLayer.cornerRadius=10;
  35. // subLayer.backgroundColor=[UIColor whiteColor].CGColor;
  36. // subLayer.masksToBounds=NO;
  37. // subLayer.shadowColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor;
  38. // subLayer.shadowOffset = CGSizeMake(0,-1);;//shadowOffset阴影偏移,x向右偏移3,y向下偏移2,默认(0, -3),这个跟shadowRadius配合使用
  39. //// subLayer.shadowOpacity = 0.8;//阴影透明度,默认0
  40. // subLayer.shadowRadius = 4;//阴影半径,默认3
  41. // [self.layer insertSublayer:subLayer atIndex:0];
  42. self.layer.shadowOffset = CGSizeMake(0,-1);
  43. // subLayer.cornerRadius=10;
  44. self.layer.shadowOpacity = 1;
  45. self.layer.shadowRadius = 4;
  46. self.layer.shadowColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor;
  47. self.layer.cornerRadius = 10;
  48. }
  49. - (void)setupViews {
  50. self.backgroundColor = kWhiteColor;
  51. self.songTitleLabel = [[UILabel alloc] init];
  52. self.songTitleLabel.textColor = [UIColor colorWithHexString:@"#1A1A1A"];
  53. self.songTitleLabel.font = [UIFont systemFontOfSize:16];
  54. self.songTitleLabel.text = ASLocalizedString(@"未播放");
  55. [self addSubview:self.songTitleLabel];
  56. self.songSlider = [[UISlider alloc] init];
  57. [self addSubview:self.songSlider];
  58. //滑动条禁止拖动
  59. self.songSlider.enabled = NO;
  60. UIImage *img = [UIImage imageNamed:@"圆圈"];
  61. [self.songSlider setThumbImage: img forState:UIControlStateNormal];
  62. [self.songSlider setThumbImage: img forState:UIControlStateHighlighted];
  63. self.songTimeLabel = [[UILabel alloc] init];
  64. self.songTimeLabel.text = @"00:00";
  65. self.songTimeLabel.font = [UIFont systemFontOfSize:12];
  66. self.songTimeLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  67. [self addSubview:self.songTimeLabel];
  68. self.playPauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
  69. [self.playPauseButton setImage:[UIImage imageNamed:@"未播"] forState:UIControlStateNormal];
  70. [self.playPauseButton setImage:[UIImage imageNamed:@"播放大"] forState:UIControlStateSelected];
  71. [self.playPauseButton addTarget:self action:@selector(handlePlayEvent:) forControlEvents:UIControlEventTouchUpInside];
  72. // [self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
  73. [self addSubview:self.playPauseButton];
  74. }
  75. - (void)handlePlayEvent:(UIButton *)sender {
  76. sender.selected = !sender.selected;
  77. if(sender.selected)
  78. {
  79. if(self.play)
  80. {
  81. self.play(self.chosemusic);
  82. }
  83. }
  84. else
  85. {
  86. [[NSNotificationCenter defaultCenter] postNotificationName:@"stopMusic" object:nil];
  87. }
  88. }
  89. - (void)setupConstraints {
  90. [self.songTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.top.equalTo(self.mas_top).offset(20);
  92. make.left.equalTo(self).offset(20);
  93. }];
  94. [self.songSlider mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.left.equalTo(@20);
  96. make.right.equalTo(@-65);
  97. make.top.equalTo(self.songTitleLabel.mas_bottom).offset(7.5);
  98. }];
  99. [self.songTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  100. make.left.equalTo(self.mas_left).offset(20);
  101. make.top.equalTo(self.songSlider.mas_bottom).offset(6.5);
  102. }];
  103. [self.playPauseButton mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.left.equalTo(self.songSlider.mas_right).offset(20);
  105. make.width.and.height.equalTo(@25);
  106. make.centerY.equalTo(self.songSlider.mas_centerY);
  107. }];
  108. }
  109. @end