iOS Development--qq Music practice, lyrics display, lyrics scrolling, lyrics color change

Source: Internet
Author: User

The lyrics show--first lyrics are on the ScrollView, the size of the ScrollView is two screen width

  • ScrollView Scrolling to modify the transparency of the Code
  • Customize the view that displays lyrics, inherit from Uiscrollview, and provide an attribute of the lyrics file name to the outside world

    /** the name of the lyrics file * /

    @property(nonatomic,copy) nsstring *lrcfilename;

    Rewrite the setter, parse the lyrics, get the model collection through the Lyrics tool class

  • implementation of the lyrics tool class
    1 #import "ChaosLrcTool.h"2 #import "ChaosLrc.h"3 4 @implementationChaoslrctool5+ (Nsarray *) Lrctoolwithlrcname: (NSString *) Lrcname6 {7     //1. Get the file path8NSString *lrcpath =[[NSBundle Mainbundle] Pathforresource:lrcname Oftype:nil];9     //2. Loading file contentsTenNSString *lrcstring =[NSString Stringwithcontentsoffile:lrcpath encoding:nsutf8stringencoding Error:nil]; One     //3. Cutting Strings ANsarray *lrclines = [lrcstring componentsseparatedbystring:@"\ n"]; -     //4. Iterate through the collection and transform it into a lyrics model -Nsmutablearray *arr =[Nsmutablearray array]; the      for(NSString *lrclinestringinchlrclines) { -         /* - [ti: Simple Love] - [ar: Jay Chou] + [Al: Fant West] -          */ +         //5. Skip a specified line A         if([lrclinestring Hasprefix:@"[Ti"] || [Lrclinestring Hasprefix:@"[Ar"] || [Lrclinestring Hasprefix:@"[Al"] || ! [Lrclinestring Hasprefix:@"["]) { at             Continue; -         } -CHAOSLRC *LRC =[CHAOSLRC lrcwithlrcline:lrclinestring]; - [arr ADDOBJECT:LRC]; -     } -     returnarr; in } - @end

  • After the lyrics parsing, the data source method of TableView (lyrics with TableView) is implemented according to the lyrics model collection.

Two. Scrolling of lyrics

  • The scrolling of the lyrics is determined by the time of each sentence, and the custom lyrics of the view require the outside world to constantly provide the time for the song to play, to judge and scroll to display the corresponding lyrics. So the custom lyrics view needs to provide a time attribute to the outside world, rewrite the time attribute to realize the lyrics scrolling
    1 #pragmaMark-Scroll the lyrics2 //rewrite the setter of time3- (void) Setcurrenttime: (nstimeinterval) CurrentTime4 {5_currenttime =currenttime;6     //traverse the lyrics to find the lyrics model that should be displayed at the corresponding time7      for(inti =0; i < Self.lrcList.count; i++) {8         //the current lyrics9CHAOSLRC *LRC =Self.lrclist[i];TenNsinteger Next = i +1; One         //the next line of lyrics ACHAOSLRC *NEXTLRC; -         if(Next <self.lrcList.count) { -NEXTLRC =Self.lrclist[next]; the         } -         if(Self.currentlrcindex! = i && currenttime >= lrc.time && currenttime <nextlrc.time) { -              -Nsindexpath *indexpath = [Nsindexpath indexpathforrow:i insection:0]; +Nsindexpath *previousindexpath = [Nsindexpath indexPathForRow:self.currentLrcIndex insection:0]; -             //Record Current line +Self.currentlrcindex =i; A             //satisfying conditions, TableView scrolling at [Self.lrcview scrolltorowatindexpath:indexpath atscrollposition:uitableviewscrollpositiontop Animated:YES] ; -             //refreshes the previous row, font restore - [Self.lrcview Reloadrowsatindexpaths:@[previousindexpath] withrowanimation:uitableviewrowanimationnone];
           -             //refreshes the current line and the font becomes larger - [Self.lrcview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone]; -         } in         //The progress of the label of the current lyrics -         if(Self.currentlrcindex = =i) { to              +             //gets the current cell -Nsindexpath *currentindexpath = [Nsindexpath indexpathforrow:i insection:0]; theChaoslrccell *currentcell =[Self.lrcview Cellforrowatindexpath:currentindexpath]; *CGFloat totaltime = nextlrc.time-lrc.time;//Current Lyrics Total time $CGFloat progresstime = currenttime-lrc.time;//How long has the lyrics gonePanax NotoginsengCurrentCell.lrcLabel.progress = progresstime/TotalTime; -              the             //label of the home page +Self.mainLabel.text =Lrc.text; ASelf.mainLabel.progress =currentCell.lrcLabel.progress; the         } +     } -}

  • The outside of the lyrics of the view to provide time is not every second to provide once so simple, the outside world needs a more awesome timer
    • Add a timer when playing
    • The time attribute assigned to the view provided by the lyrics in the method of the timer

Three. Color change of lyrics--painted (the Label,label of the display lyrics in the cell requires the outside world to provide a progress value, which is drawn internally according to the progress value)

  • Customizing the label Implementation
    1 #import "ChaosLabel.h"2 3 @implementationChaoslabel4 5- (void) Setprogress: (cgfloat) Progress6 {7_progress =progress;8     //Redraw9 [self setneedsdisplay];Ten } One  A- (void) DrawRect: (cgrect) rect { -      - [Super Drawrect:rect]; the      -     //1. Get the area you want to draw -CGRect FillRect = CGRectMake (0,0, Self.bounds.size.width *self.progress, self.bounds.size.height); -     //2. Select a color +[[Uicolor Yellowcolor]Set]; -     //3. Add area to start drawing + //Uirectfill (fillRect); A Uirectfillusingblendmode (FillRect, Kcgblendmodesourcein); at } -  - @end

  • Calculation of external progress values

iOS Development--qq Music practice, lyrics display, lyrics scrolling, lyrics color change

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.