Swift Combat-QQ Online Music (first edition)

Source: Internet
Author: User

First, * Project preparation

1, QQ Music app interface Material: (I use the PP assistant, the QQ Music app Backup, extract the IPA file can get the picture material inside)

2, Watercress Radio Interface: "Http://douban.fm/j/mine/playlist?channel=1"

Second, * interface layout
Use Xcode to create a new single View application project and select the Swift language.

Import footage pictures and make the following layouts in the default Viewcontroller view.

There are not many interface controls, so a few key controls can be laid out.

Third, * background blur effect
The layout is ready to write the code, you will see a very beautiful effect after writing

First, you associate the control to the Viewcontroller

1   @IBOutlet weak var photoborderview:uiview! 2     @IBOutlet weak var progressslider:uislider! 3     @IBOutlet weak var backgroundimageview:uiimageview! 4     @IBOutlet weak var photo:uiimageview! 5     @IBOutlet weak var playbutton:uibutton! 6     @IBOutlet weak var titlelabel:uilabel! 7     @IBOutlet weak var artistlabel:uilabel! 8     @IBOutlet weak var playtimelabel:uilabel! 9     @IBOutlet weak var alltimelabel:uilabel!   

In the Viewdidload method, write the Blur effect.

 Overridefunc viewdidload () {super.viewdidload ()//Turn the image roundphoto.layer.cornerradius=self.photo.frame.size.width/2.0Photo.clipstobounds=truePhotoBorderView.layer.cornerRadius=self.photoborderview.frame.size.width/2.0Photoborderview.clipstobounds=true        //Blur effectLet blureffect=Uiblureffect (Style:UIBlurEffectStyle.Dark) let Blureview=Uivisualeffectview (effect:blureffect) blureview.frame=self.view.frame Backgroundimageview.addsubview (blureview)//Setting the slider iconProgressslider.setminimumtrackimage (UIImage (named:"Player_slider_playback_left.png"), ForState:UIControlState.Normal) Progressslider.setmaximumtrackimage (UIImage (named:"Player_slider_playback_right.png"), ForState:UIControlState.Normal) Progressslider.setthumbimage (UIImage (named:"Player_slider_playback_thumb.png"), ForState:UIControlState.Normal)}

Run it and see if it's pretty.

Four * Let the picture spin up

First write the following method, want to see the effect is in the Viewdidload () method plus rotationanimation () to call.

 //Picture Rotation Animationfunc rotationanimation () {Let rotation=cabasicanimation (keypath:"transform.rotation.z") Rotation.timingfunction=camediatimingfunction (name:kcamediatimingfunctionlinear) Rotation.tovalue=2*M_PI rotation.duration= -Rotation.repeatcount=HUGE rotation.autoreverses=falsephoto.layer.addAnimation (Rotation, Forkey:"rotationanimation")    }

Five * Popup Playlist page

Create a new Playlistview.swift and Playlistview.xib and associate the Xib class to the Playlistview

To lay out the Playlistview.xib



Pop-up method

 //Showfunc Showplaylistview () {uiapplication.sharedapplication (). Keywindow?. Addsubview (self)//animation enters from bottom upvar vbframe:cgrect =self.viewBackground.frame VBFRAME.ORIGIN.Y=vbframe.origin.y+vbFrame.size.height Self.viewBackground.frame=vbframe uiview.animatewithduration (0.15, animations: {(), Voidinchvar vbframe:cgrect=self.viewBackground.frame VBFRAME.ORIGIN.Y=vbframe.origin.y-vbFrame.size.height Self.viewBackground.frame=vbframe}); }

Close method (will close button, and Viewtouch Click event, Associate this method)

// Close     @IBAction func closeplaylistview (sender:anyobject) {        self.removefromsuperview ()    }

Popup I closed the method is written, the following is on the Viewcontroller page, call this view to bounce it out

@IBAction func showplaylist (Sender:uibutton) {        NSLog ("----Click---")        var playlist:playlistview=nsbundle.mainbundle (). loadnibnamed ("playlistview"   as playlistview        playlist.showplaylistview ()    }

Vi. Requesting network data

Request used to a wrapper class: Httpprotocol.swift

Import Uikitprotocol httpprotocol{func didrecieveresults (results:nsdictionary)}classhttpcontroller:nsobject{varDelegate: Httpprotocol?func Onsearch (url:string) {NSLog ("Request Address:%@", URL) var nsurl:nsurl=nsurl (string: URL)!var request:nsurlrequest=nsurlrequest (Url:nsurl) nsurlconnection.sendasynchronousrequest (Request, Queue:NSOperationQueue.mainQueue (), Completionhandler: {(Response:nsurlresponse!,data:nsdata!,error:nserror!) ->voidinch            if(Data! =Nil) {var jsonresult:nsdictionary=nsjsonserialization.jsonobjectwithdata (data, options:NSJSONReadingOptions.MutableContainers, Error:nil) asnsdictionary self.Delegate?. Didrecieveresults (Jsonresult)}}) }}

Create a new entity class Song.swift to hold the requested data

Import FoundationclassSong:nsobject {var adtype:nsnumber=0.0var aid:nsstring=""var album:nsstring=""var albumtitle:nsstring=""var artist:nsstring=""var company:nsstring=""var kbps:nsnumber=0.0var length:nsnumber=0.0var like:nsnumber=0.0var monitor_url:nsstring=""var picture:nsstring=""var public_time:nsstring=""var rating_avg:nsstring=""var sha256:nsstring=""var sid:nsstring=""var songlists_count:nsnumber=0.0var ssid:nsstring=""var subtype:nsstring=""var title:nsstring=""var url:nsstring=""}

Transform the requested data into an entity collection

  var ehttp:httpcontroller = Httpcontroller ()
// Request network Data        Ehttp. delegate=self        ehttp.onsearch ("http://douban.fm/j/mine/playlist?channel=1  ")// Mandarin
  //Request network Data Resultsfunc didrecieveresults (results:nsdictionary) {NSLog ("requested data:%@", results)if(results["Song"] !=Nil) {Let Resultdata:nsarray= results["Song"] asNsarray let List:nsmutablearray=Nsmutablearray () for(Var index:int=0; index<resultdata.count;index++) {var dic:nsdictionary= Resultdata[index] asnsdictionary var song:song=Song () song.setvaluesforkeyswithdictionary (DIC asnsdictionary) List.addobject (song)} Self.tabledata=list Self.setcurrentsong (list[0] asSong)} }

Seven, * Set the 1th data after the request to the song that will be currently advertised

Bind Current Song information

  //set the currently playing musicfunc Setcurrentsong (cursong:song) {Currentsong=Cursong photo.image=Getimagewithurl (currentsong.picture) backgroundimageview.image=photo.image Titlelabel.text=Currentsong.title Artistlabel.text=currentsong.artist playbutton.selected=falsePlaytimelabel.text="00:00"Self.progressSlider.value=0.0self.rotationanimation ()}

Eight, * play music

  self.audioPlayer.stop ()        Self.audioPlayer.contentURL=nsurl (string: Currentsong.url)        timer? . Invalidate ()        timer=nstimer.scheduledtimerwithtimeinterval (0.4"  UpdateTime"true)

@IBAction func Playbuttonclick (Sender:uibutton) {ifsender.selected {//Pause PlaybackSelf.audioPlayer.pause () Pauselayer (Photo.layer)}Else{            //Start/Resume playbackSelf.audioPlayer.play () Resumelayer (Photo.layer)} sender.selected= !sender.selected}//Update Play Timefunc updatetime () {Let C=Audioplayer.currentplaybacktime//Audioplayer.endplaybacktime        ifC>0.0{Let T=audioplayer.duration let P:cfloat=cfloat (c/t) progressslider.value=p; Let All:int=int (c)//total number of secondsLet M:int=all% -//secondsLet F:int=int (all/ -)//pointsvar time=nsstring (format:"%02d:%02d", f,m) Playtimelabel.text=Time }}

Nine, * when you click Play or pause, you need to animate the picture or stop the animation.

Use the following two methods

//Stop the animation above the layerfunc Pauselayer (layer:calayer) {var pausedtime:cftimeinterval=Layer.converttime (Cacurrentmediatime (), Fromlayer:nil) Layer.speed=0.0Layer.timeoffset=Pausedtime}//continue the animation above the layerfunc Resumelayer (layer:calayer) {var pausedtime:cftimeinterval=Layer.timeoffset layer.speed=1.0Layer.timeoffset=0.0Layer.begintime=0.0var timesincepause:cftimeinterval=layer.converttime (Cacurrentmediatime (), Fromlayer:nil)-pausedtime Layer.begintime=Timesincepause}

Final effect:

Source: http://download.csdn.net/detail/fangwulongtian/8565487

Original from: http://www.cnblogs.com/wuxian/p/4394094.html reproduced on the annotated source

Swift Combat-QQ Online Music (first edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.