【代碼筆記】看圖聽故事,代碼筆記看圖

來源:互聯網
上載者:User

【代碼筆記】看圖聽故事,代碼筆記看圖

一,。

二,工程圖。

三,代碼。

RootViewController.h

#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@interface RootViewController : UIViewController<AVAudioPlayerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{    UIImageView * backImageView;    AVAudioPlayer *_audioPlayer;    NSMutableArray *musicArray;    NSMutableArray *titleArray;        int songIndex;    UIButton * leftButton;    UIButton * rightButton;        UILabel *titleLabel;    UISlider *Slider;    UISlider *volumeSlider;    NSTimer * processTimer;    NSTimer * timer1;    }@end

 

RootViewController.m

#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];        self.title = @"故事";    //初始化資料    [self initData];    //初始化背景圖    [self initBackgroundView];    }#pragma -mark -functions//初始化資料-(void)initData{    musicArray=[[NSMutableArray alloc]initWithObjects:@"7",@"11",@"9",@"17",@"5",@"6",@"1",@"8",@"15",@"10",@"2",@"12",@"13",@"14",@"4",@"16",@"3",@"18", nil];    titleArray=[[NSMutableArray alloc]initWithObjects:@"三頭公牛和獅子",@"小紅帽",@"天女散花",@"朋友再見",@"女媧造人",@"天神的啞水",@"小青蛙聽故事",@"淘淘的願望",@"講禮貌",@"醜小鴨",@"老鼠,小鳥和香腸",@"兩頭驢子",@"驢子和主人",@"四個朋友",@"獎品",@"沒法通過",@"五顆豌豆",@"彼得*潘", nil];    }//初始化背景圖-(void)initBackgroundView{        self.navigationController.navigationBar.tintColor =[UIColor orangeColor];    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:69.0/255 green:161.0/255 blue:241.0/255 alpha:1];            //背景    backImageView= [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 460)];    backImageView.image= [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[musicArray objectAtIndex:songIndex]]];    [self.view addSubview:backImageView];            //播放    UIButton* button= [UIButton buttonWithType:UIButtonTypeCustom];    button.frame=CGRectMake(130, 260, 60, 50);    button.tag=100;    [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];    [button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];    [self.view addSubview:button];        //上一首    leftButton= [UIButton buttonWithType:UIButtonTypeCustom];    leftButton.frame=CGRectMake(50, 260, 60, 50);    [leftButton addTarget:self action:@selector(prier) forControlEvents:UIControlEventTouchUpInside];    [leftButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];    [self.view addSubview:leftButton];        //下一首    rightButton= [UIButton buttonWithType:UIButtonTypeCustom];    rightButton.frame=CGRectMake(210, 260, 60, 50);    [rightButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];    [rightButton setImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal];    [self.view addSubview:rightButton];        //音量按鈕    UIButton *volumebutton= [UIButton buttonWithType:UIButtonTypeCustom];    volumebutton.frame=CGRectMake(10, 330, 40, 40);    [volumebutton setImage:[UIImage imageNamed:@"labalan.png"] forState:UIControlStateNormal];    [volumebutton addTarget:self action:@selector(showVolume) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:volumebutton];        //背景    UIButton *backButton= [UIButton buttonWithType:UIButtonTypeCustom];    backButton.frame=CGRectMake(270, 330, 40, 40);    [backButton setImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];    [backButton addTarget:self action:@selector(setBackground) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:backButton];            //音樂logo圖片    UIImageView* musicImageView= [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"musicLogo.png"]];    musicImageView.frame= CGRectMake(80, 40, 160, 140);    [self.view addSubview:musicImageView];            //標題    titleLabel= [[UILabel alloc] initWithFrame:CGRectMake(0, 195, 320, 30)];    titleLabel.font= [UIFont systemFontOfSize:25];    titleLabel.textAlignment= NSTextAlignmentCenter;    titleLabel.textColor= [UIColor blueColor];    titleLabel.numberOfLines=0;    titleLabel.backgroundColor= [UIColor clearColor];    titleLabel.text= [titleArray objectAtIndex:0];    [self.view addSubview:titleLabel];            //進度控制    Slider= [[UISlider alloc] initWithFrame:CGRectMake(50, 230, 220, 5)];    Slider.maximumValue=100;    Slider.minimumValue=0;    Slider.value=0;    Slider.continuous=NO;    [Slider addTarget:self action:@selector(processSet:) forControlEvents:UIControlEventValueChanged];    [Slider addTarget:self action:@selector(processTimerStop) forControlEvents:UIControlEventTouchDown];    [self.view addSubview:Slider];        //音量控制    volumeSlider= [[UISlider alloc] initWithFrame:CGRectMake(-85, 280, 220, 5)];    volumeSlider.maximumValue=1;    volumeSlider.minimumValue=0;    volumeSlider.value= 0.5;    volumeSlider.hidden=YES;    [volumeSlider addTarget:self action:@selector(volumeSet:) forControlEvents:UIControlEventValueChanged];    volumeSlider.transform= CGAffineTransformMakeRotation(-90* M_PI/180);    [self.view addSubview:volumeSlider];         processTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(process) userInfo:nil repeats:YES];        [self loadMusic:[musicArray objectAtIndex:0] type:@"mp3"];            UILongPressGestureRecognizer* rightLongPress= [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(rightLongPress:)];    [rightButton addGestureRecognizer:rightLongPress];        UILongPressGestureRecognizer* leftLongPress= [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(leftLongPress:)];    [leftButton addGestureRecognizer:leftLongPress]; }//進度控制-(void)processSet:(UISlider*)slider{    processTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(process) userInfo:nil repeats:YES];    _audioPlayer.currentTime=slider.value/100*_audioPlayer.duration;    if(_audioPlayer.playing==YES)        [_audioPlayer playAtTime:_audioPlayer.currentTime];}-(void)processTimerStop{    [processTimer invalidate];}//背景-(void)setBackground{    UIImagePickerController* picker= [[UIImagePickerController alloc] init];    picker.sourceType= UIImagePickerControllerSourceTypeSavedPhotosAlbum;    picker.delegate=self;    [self presentViewController:picker animated:NO completion:nil];}#pragma -mark -doClickActions//右側按鈕手勢-(void)rightLongPress:(UILongPressGestureRecognizer*)longPress{    if (longPress.state != UIGestureRecognizerStateBegan)        return;        if(_audioPlayer.playing)    {        if(_audioPlayer.currentTime>_audioPlayer.duration-5)            _audioPlayer.currentTime=_audioPlayer.currentTime;        else            _audioPlayer.currentTime+=5;        [_audioPlayer playAtTime:_audioPlayer.currentTime];            }}//左側按鈕手勢-(void)leftLongPress:(UILongPressGestureRecognizer*)longPress{    if (longPress.state != UIGestureRecognizerStateBegan)        return;    if(_audioPlayer.playing)    {        if(_audioPlayer.currentTime<5)            _audioPlayer.currentTime=0;        else            _audioPlayer.currentTime-=5;                [_audioPlayer playAtTime:_audioPlayer.currentTime];                printf("left\n");    }}//雪花函數-(void)snow{    int startX= random()%320;    int endX= random()%320;    int width= random()%25;    CGFloat time= (random()%100)/10+5;    CGFloat alp= (random()%9)/10.0+0.1;        UIImage* image= [UIImage imageNamed:@"snow.png"];    UIImageView* imageView = [[UIImageView alloc] initWithImage:image];    imageView.frame= CGRectMake(startX,-1*width,width,width);    imageView.alpha=alp;    [self.view addSubview:imageView];        [UIView beginAnimations:nil context:(__bridge void *)(imageView)];    [UIView setAnimationDuration:time];    if(endX>50&&endX<270)    {        imageView.frame= CGRectMake(endX, 270-width/2, width, width);            }    else if((endX>10&&endX<50)||(endX>270&&endX<310))        imageView.frame= CGRectMake(endX, 400-width/2, width, width);    else        imageView.frame= CGRectMake(endX, 480, width, width);    [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];    [UIView setAnimationDelegate:self];    [UIView commitAnimations];}-(void)onAnimationComplete:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context{    UIImageView* snowView=(__bridge UIImageView *)(context);    [snowView removeFromSuperview];    }//封裝系統載入函數-(void)loadMusic:(NSString*)name type:(NSString*)type{    NSString* path= [[NSBundle mainBundle] pathForResource: name ofType:type];    NSURL* url = [NSURL fileURLWithPath:path];        _audioPlayer= [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];    _audioPlayer.delegate=self;    _audioPlayer.volume= 0.5;    [_audioPlayer prepareToPlay];    }//音量設定-(void)volumeSet:(UISlider*)slider{    _audioPlayer.volume= slider.value;}-(void)showVolume{    volumeSlider.hidden=NO;    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(hideVolume) userInfo:nil repeats:NO];}-(void)hideVolume{    volumeSlider.hidden=YES;}//歌曲進度-(void)process{    Slider.value= 100*_audioPlayer.currentTime/_audioPlayer.duration;}#pragma -mark -doClickActions//播放-(void)play:(UIButton*)button{    if(_audioPlayer.playing)    {        [button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];        [_audioPlayer pause];        [timer1 invalidate];    }    else    {        [button setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];        timer1=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(snow) userInfo:nil repeats:YES];        [_audioPlayer play];    }    }//上一首-(void)prier{    BOOL playFlag;    if(_audioPlayer.playing)    {        playFlag=YES;        [_audioPlayer stop];    }    else    {        playFlag=NO;    }    songIndex--;    if(songIndex<0)        songIndex= musicArray.count-1        ;    [self loadMusic:[musicArray objectAtIndex:songIndex] type:@"mp3"];        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[musicArray objectAtIndex:songIndex]]];        [self setBackground:image];    titleLabel.text= [titleArray objectAtIndex:songIndex];    if(playFlag==YES)    {        [_audioPlayer play];    }    }//下一首-(void)next{    BOOL playFlag;    if(_audioPlayer.playing)    {        playFlag=YES;        [_audioPlayer stop];    }    else{        playFlag=NO;    }    songIndex++;    if(songIndex==musicArray.count)    {        songIndex= 0;    }        [self loadMusic:[musicArray objectAtIndex:songIndex] type:@"mp3"];        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[musicArray objectAtIndex:songIndex]]];        [self setBackground:image];        titleLabel.text= [titleArray objectAtIndex:songIndex];    if(playFlag==YES)    {        [_audioPlayer play];            }}#pragma -mark -AVAudioPlayerDelegate//播放完成自動切換-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{    songIndex++;    if(songIndex==musicArray.count)        songIndex= 0;    [self loadMusic:[musicArray objectAtIndex:songIndex] type:@"mp3"];    titleLabel.text= [titleArray objectAtIndex:songIndex];    [_audioPlayer play];    }#pragma -mark -UIImagePickerControllerDelegate-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{    [self dismissViewControllerAnimated:YES completion:nil];}-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{    backImageView.image = image;    [self dismissViewControllerAnimated:YES completion:nil];}-(void)setBackground:(UIImage *)image{    backImageView.image = image;    }@end

 

 

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.