#import <UIKit/UIKit.h>
@interface PlayView : UIView
@property(nonatomic,strong)UIButton *playBtn;
@property(nonatomic,strong)UILabel *currentTime;
@property(nonatomic,strong)UILabel *duration;
@property(nonatomic,strong)UISlider *slider;
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@end
#import "PlayView.h"
@interface PlayView ()<UIScrollViewDelegate>
{
UIPageControl *pageControl;
UIScrollView *sv;
}
@end
@implementation PlayView
//更改名称,默认_playBtn
//当左边和右边一样的时候,右边可以省略
@synthesize playBtn = playBtn,currentTime,duration,slider,label1,label2;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self uiconfig];
}
return self;
}
- (void)uiconfig
{
//初始化滚动视图
sv = [[UIScrollView alloc] init];
//屏幕宽度
float width = [UIScreen mainScreen].bounds.size.width;
//设置位置
sv.frame = CGRectMake(0, 0, self.frame.size.width, 440);
//设置滚动视图的内容视图的宽度和高度
//self.view.frame.origin = 横坐标和纵坐标
// self.view.frame.size = 宽度和高度
sv.contentSize = CGSizeMake(width * 3, sv.frame.size.height);
//设置默认显示图片,内容视图偏移
[sv setContentOffset:CGPointMake(width, 0)];
//添加到父视图上
[self addSubview:sv];
//中间的imageview
//初始化
UIImageView *iv = [[UIImageView alloc] init];
//设置位置
iv.frame = CGRectMake(width, 0, width, sv.frame.size.height);
//放图片
iv.image = [UIImage imageNamed:@"IMG_0025"];
//添加到sv
[sv addSubview:iv];
//左边的表格
UITableView *tableLeft = [[UITableView alloc] init];
tableLeft.frame = CGRectMake(0, 0, width, sv.frame.size.height);
[sv addSubview:tableLeft];
//右边的表格
UITableView *tableRight = [[UITableView alloc] init];
tableRight.frame = CGRectMake(2 * width, 0, width, sv.frame.size.height);
[sv addSubview:tableRight];
//设置滚动视图整页滑动
sv.pagingEnabled = YES;
//横向隐藏滚动条
sv.showsHorizontalScrollIndicator = NO;
//竖值方向隐藏
sv.showsVerticalScrollIndicator = NO;
//设置代理
sv.delegate = self;
//设置label
label1= [[UILabel alloc] initWithFrame:CGRectMake(10, 450, 150, 20)];
label1.text = @"姓名";
label1.textColor = [UIColor whiteColor];
label1.font = [UIFont systemFontOfSize:15];
[self addSubview:label1];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 470, 150, 20)];
label2.text = @"歌曲名";
label2.textColor = [UIColor lightGrayColor];
label2.font = [UIFont systemFontOfSize:12];
[self addSubview:label2];
//设置UIPageControl
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(width/2 - 50, 480, 60, 20)];
//有几个点点
pageControl.numberOfPages = 3;
//设置默认显示哪个点
pageControl.currentPage = 1;
//设置点击事件,⌚️,注意用UIControlEventValueChanged而不是UIControlEventTouchUpInside
[pageControl addTarget:self action:@selector(click) forControlEvents:UIControlEventValueChanged];
//所有点点的颜色
// pageControl.pageIndicatorTintColor = [UIColor redColor];
//当前点点点的颜色
// pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
[self addSubview:pageControl];
//下载和点赞按钮
UIButton *topLelt = [UIButton buttonWithType:UIButtonTypeCustom];
topLelt.frame = CGRectMake(width - 90, 450, 40, 40);
[topLelt setBackgroundImage:[UIImage imageNamed:@"cell_download_all_h"] forState:UIControlStateNormal];
[self addSubview:topLelt];
UIButton *topRight = [UIButton buttonWithType:UIButtonTypeCustom];
topRight.frame = CGRectMake(width - 60, 450, 40, 40);
[topRight setBackgroundImage:[UIImage imageNamed:@"cell_like_in_my_music_pressed"] forState:UIControlStateNormal];
[self addSubview:topRight];
//滑动条
slider = [[UISlider alloc] init];
slider.tintColor = [UIColor redColor];
slider.frame = CGRectMake(10, 480, width - 20, 80);
//设置左边球的图片
// [slider setThumbImage:[UIImage imageNamed:@"cell_like_in_my_music_pressed"] forState:UIControlStateNormal];
[self addSubview:slider];
//播放按钮
playBtn= [UIButton buttonWithType:UIButtonTypeCustom];
playBtn.frame = CGRectMake(width / 2 - 35, 530, 70, 70);
[playBtn setImage:[UIImage imageNamed:@"forme_btn_play"] forState:UIControlStateNormal];
[playBtn setImage:[UIImage imageNamed:@"forme_btn_stop"] forState:UIControlStateSelected];
[self addSubview:playBtn];
//当前播放时间
currentTime = [[UILabel alloc] initWithFrame:CGRectMake(10, 540, 40, 15)];
currentTime.font = [UIFont systemFontOfSize:10];
currentTime.textColor = [UIColor whiteColor];
currentTime.text = @"00:00";
[self addSubview:currentTime];
//音乐持续时间
duration = [[UILabel alloc] initWithFrame:CGRectMake(width - 40, 540, 80, 15)];
duration.font = [UIFont systemFontOfSize:10];
duration.textColor = [UIColor whiteColor];
duration.text = @"00:00";
[self addSubview:duration];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int pageNumber = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
pageControl.currentPage = pageNumber;
}
- (void)click
{
float width = [UIScreen mainScreen].bounds.size.width;
NSInteger pageNuber= pageControl.currentPage;
[sv setContentOffset:CGPointMake(width * pageNuber, 0) animated:YES];
}
@end
转载于:https://my.oschina.net/suyongchen/blog/648280