swift 豆瓣音乐开发案例
搞了一天,总有把swift 豆瓣音乐开发完成。
功能:某一频道列表
所有频道列表,点击返回某一频道
工程结构图
代码如下:
ViewController.swift
importUIKitimportMediaPlayerimportQuartzCore//文启领航bjflexedu.com//qq:376610000classViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,HttpProtocol,ChannelProtocol{@IBOutletvarbtnPlay:UIImageView@IBOutletvartap:UITapGestureRecognizer=nil@IBOutletvarplayTim:UILabel@IBOutletvarprogressView:UIProgressView@IBOutletvariv:UIImageView@IBOutletvartv:UITableViewvartableData:NSArray=NSArray()varchannelData:NSArray=NSArray()varp_w_picpathCache=Dictionary<String,UIImage>()varaudioPlayer:MPMoviePlayerController=MPMoviePlayerController()varehttp:HttpController=HttpController()vartimer:NSTimer?@IBActionfunconTap(sender:UITapGestureRecognizer){println("tap")ifsender.view==btnPlay{btnPlay.hidden=trueaudioPlayer.play()btnPlay.removeGestureRecognizer(tap)iv.addGestureRecognizer(tap)}elseifsender.view==iv{btnPlay.hidden=falseaudioPlayer.pause()btnPlay.addGestureRecognizer(tap)iv.removeGestureRecognizer(tap)}}overridefuncviewDidLoad(){super.viewDidLoad()ehttp.delegate=selfehttp.onSearch("http://www.douban.com/j/app/radio/channels")ehttp.onSearch("http://douban.fm/j/mine/playlist?channel=0")progressView.progress=0.0iv.addGestureRecognizer(tap)}funconChangeChannel(channel_id:String){leturl:String="http://douban.fm/j/mine/playlist?\(channel_id)"ehttp.onSearch(url)}overridefuncprepareForSegue(segue:UIStoryboardSegue!,sender:AnyObject!){varchannelC:ChannelController=segue.destinationViewControllerasChannelControllerchannelC.channelData=self.channelDataprintln("d")channelC.delegate=self}overridefuncdidReceiveMemoryWarning(){super.didReceiveMemoryWarning()println("dd")}functableView(tableView:UITableView!,numberOfRowsInSectionsection:Int)->Int{returnself.tableData.count}functableView(tableView:UITableView!,cellForRowAtIndexPathindexPath:NSIndexPath!)->UITableViewCell!{varcell=UITableViewCell(style:UITableViewCellStyle.Subtitle,reuseIdentifier:"douban")letrowData:NSDictionary=self.tableData[indexPath.row]asNSDictionarycell.text=rowData["title"]asStringcell.detailTextLabel.text=rowData["artist"]asStringcell.p_w_picpath=UIImage(named:"detail.jpg")leturl=rowData["picture"]asStringletp_w_picpath=self.p_w_picpathCache[url]as?UIImageif!p_w_picpath?{letimgURL:NSURL=NSURL(string:url)letrequest:NSURLRequest=NSURLRequest(URL:imgURL)NSURLConnection.sendAsynchronousRequest(request,queue:NSOperationQueue.mainQueue(),completionHandler:{(response,data,error)->Voidinletimg=UIImage(data:data)cell.p_w_picpath=imgself.p_w_picpathCache[url]=img})}else{cell.p_w_picpath=p_w_picpath//self.p_w_picpathCache["url"]}returncell}funcdidRecieveResults(results:NSDictionary){println("dd")println(results);if(results["song"]){self.tableData=results["song"]asNSArrayself.tv.reloadData()letfirstDict:NSDictionary=self.tableData[0]asNSDictionaryletaudioURL:String=firstDict["url"]asStringonSetAudio(audioURL)letimgUrl:String=firstDict["picture"]asStringonSetImage(imgUrl)}elseif(results["channels"]){self.channelData=results["channels"]asNSArray}}funconSetAudio(url:String){timer?.invalidate()playTim.text="00:00"self.audioPlayer.stop()self.audioPlayer.contentURL=NSURL(string:url)self.audioPlayer.play()timer=NSTimer.scheduledTimerWithTimeInterval(0.4,target:self,selector:"onUpdate",userInfo:nil,repeats:true)btnPlay.removeGestureRecognizer(tap)iv.addGestureRecognizer(tap)btnPlay.hidden=true}funconUpdate(){println("==")letcurrentTimer=audioPlayer.currentPlaybackTimeifcurrentTimer>0.0{letdur=audioPlayer.durationletpecent:CFloat=CFloat(currentTimer/dur)progressView.setProgress(pecent,animated:false)letall:Int=Int(currentTimer)letm:Int=all%60letf:Int=Int(all/60)vartime:String=""//小时iff<10{time="0\(f):"}else{time="\(f):"}//分钟ifm<10{time+="0\(m)"}else{time+="\(m)"}playTim.text=time}}funconSetImage(url:String){letp_w_picpath=self.p_w_picpathCache[url]as?UIImageif!p_w_picpath?{letimgURL:NSURL=NSURL(string:url)letrequest:NSURLRequest=NSURLRequest(URL:imgURL)NSURLConnection.sendAsynchronousRequest(request,queue:NSOperationQueue.mainQueue(),completionHandler:{(response,data,error)->Voidinletimg=UIImage(data:data)self.iv.p_w_picpath=imgself.p_w_picpathCache[url]=img})}else{self.iv.p_w_picpath=p_w_picpath}}//选择其中之一functableView(tableView:UITableView!,didSelectRowAtIndexPathindexPath:NSIndexPath!){varrowData:NSDictionary=self.tableData[indexPath.row]asNSDictionaryleturl=rowData["url"]asStringonSetAudio(url)leturlImg=rowData["picture"]asStringonSetImage(urlImg)}//动画效果functableView(tableView:UITableView!,willDisplayCellcell:UITableViewCell!,forRowAtIndexPathindexPath:NSIndexPath!){cell.layer.transform=CATransform3DMakeScale(0.1,0.1,1)UIView.animateWithDuration(0.25,animations:{cell.layer.transform=CATransform3DMakeScale(1,1,1)})}}
ChannelProtocol.swift
importUIKitimportQuartzCore//文启领航bjflexedu.com//qq:376610000protocolChannelProtocol{funconChangeChannel(channel_id:String)}classChannelController:UIViewController,UITableViewDataSource,UITableViewDelegate{@IBOutletvartv:UITableViewvarchannelData:NSArray=NSArray()vardelegate:ChannelProtocol?overridefuncviewDidLoad(){super.viewDidLoad()}overridefuncdidReceiveMemoryWarning(){super.didReceiveMemoryWarning()}functableView(tableView:UITableView!,numberOfRowsInSectionsection:Int)->Int{returnchannelData.count}functableView(tableView:UITableView!,cellForRowAtIndexPathindexPath:NSIndexPath!)->UITableViewCell!{letcell=UITableViewCell(style:UITableViewCellStyle.Subtitle,reuseIdentifier:"channel")letrowData:NSDictionary=self.channelData[indexPath.row]asNSDictionarycell.text=rowData["name"]asStringcell.p_w_picpath=UIImage(named:"detail.jpg")//cell.detailTextLabel.text=rowData["channel_id"]asStringreturncell}//functableView(tableView:UITableView!,didSelectRowAtIndexPathindexPath:NSIndexPath!){varrowData:NSDictionary=self.channelData[indexPath.row]asNSDictionaryletchannel_id:AnyObject=rowData["channel_id"]asAnyObjectletchannel:String="channel=\(channel_id)"self.delegate?.onChangeChannel(channel)self.dismissViewControllerAnimated(true,completion:nil)}//动画效果functableView(tableView:UITableView!,willDisplayCellcell:UITableViewCell!,forRowAtIndexPathindexPath:NSIndexPath!){cell.layer.transform=CATransform3DMakeScale(0.1,0.1,1)UIView.animateWithDuration(0.25,animations:{cell.layer.transform=CATransform3DMakeScale(1,1,1)})}}
HttpProtocol.swift
importUIKit//文启领航bjflexedu.com//qq:376610000protocolHttpProtocol{funcdidRecieveResults(results:NSDictionary)}classHttpController:NSObject{vardelegate:HttpProtocol?funconSearch(url:String){varnsUrl:NSURL=NSURL(string:url)varrequest:NSURLRequest=NSURLRequest(URL:nsUrl)NSURLConnection.sendAsynchronousRequest(request,queue:NSOperationQueue.mainQueue(),completionHandler:{(response,data,error)->VoidinvarjsonResult:NSDictionary=NSJSONSerialization.JSONObjectWithData(data,options:NSJSONReadingOptions.MutableContainers,error:nil)asNSDictionaryself.delegate?.didRecieveResults(jsonResult)})}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。