//
// ViewController.m
// QQMusic-02
//
// Created by imac on 15/8/12.
// Copyright (c) 2015年 刘All rights reserved.
//
//#define name @"name"
#import "ViewController.h"
@interface ViewController ()
//@property(nonatomic,weak)UIImageView *imageView;
@property(nonatomic,strong)NSArray *imageData;
@property(nonatomic,assign)NSInteger index;
@property (nonatomic,strong)UIImageView *imageView;
@property (strong,nonatomic) UILabel *textLable;
@property (strong,nonatomic) UILabel *textLable2;
@property (strong,nonatomic) UILabel *textLable3;
@property (strong,nonatomic) UILabel *textLable4;
@property(nonatomic,assign)NSInteger time1;
@property(nonatomic,assign)NSInteger time2;
@property(nonatomic,assign)NSInteger i;
@property(nonatomic,assign)NSInteger anim;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 除使话数据
_time1 = 0;
_time2 = 0;
_index = 0;
_i = 0;
_anim = 0;
// 初始化页面
_imageView = [[UIImageViewalloc] init];
_imageView.frame =CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height);
[self.viewaddSubview:_imageView];
//加载控件
[selfinitView];
// 加载图片资源
[selfimageData];
// 换图片方法
[selfdata];
}
-(void)initView{
// 点击页面隐藏上下栏
UIButton *button = [[UIButtonalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.height,self.view.frame.size.height)];
[self.viewaddSubview:button];
[button addTarget:selfaction:@selector(hide)forControlEvents:UIControlEventTouchUpInside];
// 创建控件
UIView *topView = [[UIViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,80)];
// 设置图片
topView.backgroundColor = [UIColorcolorWithWhite:0.502alpha:1.000];
// 设置透明度
topView.alpha =0.6;
topView.tag =1;
// 添加到self.view 上
[self.viewaddSubview:topView];
//-----lable,创建歌手名和歌曲名并初始化
_textLable = [[UILabelalloc]init];
_textLable.frame =CGRectMake(160,20, 100, 30);
_textLable2 = [[UILabelalloc]init];
_textLable2.frame =CGRectMake(160,53, 100, 30);
// 添加歌曲名歌手名lable到topview
[topViewaddSubview:_textLable];
[topViewaddSubview:_textLable2];
//love Button,创建控件
UIButton *loveBtn = [[UIButtonalloc]initWithFrame:CGRectMake(320 ,40, 35, 35)];
// 设置图片
[loveBtn setImage:[UIImageimageNamed:@"playing_btn_love"]forState:UIControlStateNormal];
[loveBtn setImage:[UIImageimageNamed:@"playing_btn_in_myfavor_h"]forState:UIControlStateHighlighted];
[loveBtn setImage:[UIImageimageNamed:@"playing_btn_in_myfavor"]forState:UIControlStateSelected];
// 添加控件到topview上
[topViewaddSubview:loveBtn];
// 添加事件
[loveBtn addTarget:selfaction:@selector(loveAction:)forControlEvents:UIControlEventTouchUpInside];
//---back,创建控件,返回按钮
UIButton *backBtn = [[UIButtonalloc] initWithFrame:CGRectMake(20,40, 35, 35)];
// 设置图片
[backBtn setImage:[UIImageimageNamed:@"top_back_white"]forState:UIControlStateNormal];
// 添加到topView 上
[topViewaddSubview:backBtn];
//----bottom,创建控件
UIView *bottomView = [[UIViewalloc] initWithFrame:CGRectMake(0,self.view.frame.size.height -80, self.view.frame.size.width,80)];
// 设置颜色
bottomView.backgroundColor = [UIColorcolorWithWhite:0.502alpha:1.000];
// 设置透明度
bottomView.alpha =0.6;
bottomView.tag =2;
// 添加到self。view上
[self.viewaddSubview:bottomView];
//下方lable,创建时间lable。并格式化
_textLable3 = [[UILabelalloc]init];
_textLable3.frame =CGRectMake(10,5, 120, 30);
_textLable3.text =@"00:00";
_textLable4 = [[UILabelalloc]init];
_textLable4.frame =CGRectMake(330,5, 100, 30);
_textLable4.text =@"04:20";
[bottomViewaddSubview:_textLable3];
[bottomViewaddSubview:_textLable4];
//---UISlider,创建控件
UISlider *slider = [[UISlideralloc] initWithFrame:CGRectMake(5,self.view.frame.size.height -90, self.view.frame.size.width -5, 20)];
// 设置样式
[slider setThumbImage:[UIImageimageNamed:@"playing_slider_thumb"]forState:UIControlStateNormal];
// 设置颜色
slider.minimumTrackTintColor = [UIColorcolorWithRed:0.000green:0.502blue:0.251alpha:1.000];
// 设置slider值
slider.value =0.0;
slider.minimumValue =0.0;
slider.maximumValue =260.0;
slider.tag =100;
// 添加到self.view上
[self.viewaddSubview:slider];
// 添加slider监听事件
[slider addTarget:selfaction:@selector(sliderAction)forControlEvents:UIControlEventValueChanged];
//----pause,创建控件
UIButton *pause = [[UIButtonalloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 30,10 , 60, 60)];
// 设置图片
[pause setImage:[UIImageimageNamed:@"playing_btn_play_n"]forState:UIControlStateNormal];
[pause setImage:[UIImageimageNamed:@"playing_btn_play_h"]forState:UIControlStateHighlighted];
[pause setImage:[UIImageimageNamed:@"playing_btn_pause_n"]forState:UIControlStateSelected];
[pause setImage:[UIImageimageNamed:@"playing_btn_pause_h"]forState:UIControlStateHighlighted];
// 添加到bottomView 上
[bottomViewaddSubview:pause];
// 设置点击事件
[pause addTarget:selfaction:@selector(pasueAction:)forControlEvents:UIControlEventTouchUpInside];
//---------nextButton,创建控件
UIButton *nextButton = [[UIButtonalloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 + 40,25, 30, 30)];
// 添加图片
[nextButton setImage:[UIImageimageNamed:@"playing_btn_next_n"]forState:UIControlStateNormal];
[nextButton setImage:[UIImageimageNamed:@"playing_btn_next_h"]forState:UIControlStateHighlighted];
// 设置tag
nextButton.tag =30;
// 添加下一曲按钮到bottomView 上
[bottomViewaddSubview:nextButton];
// 设置点击事件
[nextButton addTarget:selfaction:@selector(nextAction)forControlEvents:UIControlEventTouchUpInside];
//-------backButton,创建上一曲按钮
UIButton *backButton = [[UIButtonalloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 70,25, 30, 30)];
// 设置图片
[backButton setImage:[UIImageimageNamed:@"playing_btn_pre_n"]forState:UIControlStateNormal];
[backButton setImage:[UIImageimageNamed:@"playing_btn_pre_h"]forState:UIControlStateHighlighted];
// 设置tag值,tag是控件的唯一标识
backBtn.tag =20;
// 添加按钮到bottomView 上
[bottomViewaddSubview:backButton];
// 设置点击事件
[backButton addTarget:selfaction:@selector(backAction)forControlEvents:UIControlEventTouchUpInside];
}
//love喜爱实现方法
-(void)loveAction:(UIButton *)loveButton{
loveButton.selected = !loveButton.selected;
}
-(void)pasueAction:(UIButton *)pasueBtn{
// 切换暂停按钮和
pasueBtn.selected = !pasueBtn.selected;
// 能否调用定时器条件判断,避免重复调用
if (_i == 0) {
[NSTimerscheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerAction:)
userInfo:nil
repeats:YES];
}
// 判断按钮点击次数
//控制定时器职能在播放过程中调用一次
_i += 1;
}
-(void)timerAction:(NSTimer *)timer{
// 控制暂停
if (_i >= 2) {
[timerinvalidate];
_i = 0;
}
UISlider *mySlider = (UISlider *)[self.viewviewWithTag:100];
//根据定时器,设置mySlider.value 递增
mySlider.value +=1;
// 停止定时器条件
if (mySlider.value ==260) {
[timerinvalidate];
_i = 0;
}
_time1 = mySlider.value;
//判断是否够一分钟,够的话分位加1,秒位置为0
if (_time1 >=60) {
_time1 = 0;
_time2 ++;
}
//格式化显示到lable上
_textLable3.text = [NSStringstringWithFormat:@"%02ld:%02ld",_time2,_time1];
}
-(void)sliderAction{
UISlider *mySlider = (UISlider *)[self.viewviewWithTag:100];
if (mySlider.value <= mySlider.maximumValue) {
_time1 = mySlider.value -_time2 * 60;
_time2 = mySlider.value /60;
//判断是否够一分钟,够的话分位加1,秒位置为0
if (_time1 >=60) {
_time1 = 0;
}
//格式化显示到lable上
_textLable3.text = [NSStringstringWithFormat:@"%02ld:%02ld",_time2,_time1];
}
}
//延时加载
-(NSArray *)imageData{
if (_imageData ==nil) {
// 获取文件全路径
NSBundle *bundle = [NSBundlemainBundle];
NSString *path = [bundle pathForResource:@"image" ofType:@"plist"];
// 通过路径找到资源
_imageData = [NSArrayarrayWithContentsOfFile:path];
}
return_imageData;
}
-(void)data{
//读出数组中元素
NSDictionary *dic =_imageData[_index];
// 取出字典中对应的value
_textLable.text = [NSStringstringWithFormat:@"%@",dic[@"geming"]];
_textLable2.text = [NSStringstringWithFormat:@"%@",dic[@"geshou"]];
//设置图片
_imageView.image = [UIImageimageNamed:dic[@"name"]];
}
//下一曲
-(void)nextAction{
// 通过tag获取进度条,点击事件发生时进度条value置为0
UISlider *mySlider = (UISlider *)[self.viewviewWithTag:100];
mySlider.value =0;
//点击下一曲时:时间置为00:00
_time1 = 0;
_time2 = 0;
//判断是否是最后一张,如果是点击后进入第一张显示
if (_index ==3) {
_index = 0;
[selfdata];
}else{//否则正常执行下一曲
_index ++;
[selfdata];
}
}
//上一曲
-(void)backAction{
// 通过tag获取进度条,点击事件发生时进度条value置为0
UISlider *mySlider = (UISlider *)[self.viewviewWithTag:100];
mySlider.value =0;
//点击下一曲时:时间置为00:00
_time1 = 0;
_time2 = 0;
//判断是否是第一张,如果是点击后进入最后一张显示
if (_index ==0) {
_index = 3;
[selfdata];
}else{//否则正常执行上一曲
_index --;
[selfdata];
}
}
//动画
-(void)hide{
if (_anim ==0) {
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDelay:1];
[self.viewviewWithTag:1].transform =CGAffineTransformMakeTranslation(0, -80);
[self.viewviewWithTag:2].transform =CGAffineTransformMakeTranslation(0, +80);
[UIViewcommitAnimations];
_anim = 1;
}else{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDelay:1];
[self.viewviewWithTag:1].transform =CGAffineTransformMakeTranslation(0,0);
[self.viewviewWithTag:2].transform =CGAffineTransformMakeTranslation(0,0);
[UIViewcommitAnimations];
_anim = 0;
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end