在滾動檢視上新增子檢視(UIScrollView)
阿新 • • 發佈:2019-01-04
在我做一個學習APP的時候,遇到讓我很頭疼的問題,頭疼的原因是因為我自己太菜了吧!
APP的大部分功能都是在一個滾動檢視上完成的,幾乎所有頁面都加在滾動檢視上的,因為每一頁都是一道題目,而且題目非常多,每個年級每一單元這樣來分類的。
題目型別也比較多,我不可能一道題都設計一個頁面吧,這樣肯定是不可能的,所以我就每個型別的題目設計了一個介面。
後來問題來了,我該怎麼從json檔案中取出每道題然後一道一道,一中型別一種型別地載入頁面呢?
因為每種型別的題目有多項,而且是一種型別完了之後才會有另一型別的題目。
後來糾結鬱悶思考之後解決了,部分程式碼如下((⊙o⊙)…,還是貼一個類算了吧,嘟嘟嘟嘟嘟...):
#import "ContentController.h" #import "Layout1Controller.h" #import "Layout2Controller.h" #import "Layout41Controller.h" #import "Layout4Controller.h" #define viewW self.view.frame.size.width #define viewH self.view.frame.size.height #import "TitleCatalogue.h" #import "Layout21Controller.h" #import "Layout26Controller.h" #import "Layout52Controller.h" #import "Layout15Controller.h" #import "Layout51Controller.h" #import "Layout56Controller.h" #import "Layout6aController.h" #import "Layout5Controller.h" #import "Layout54Controller.h" #import "Layout55Controller.h" #import "Layout25Controller.h" #import "Layout42ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ContentController ()<UIScrollViewDelegate,AVAudioPlayerDelegate> { NSDictionary *jsonDic; NSInteger tipNum;//提示圖片的每一頁的數字 NSInteger _currentPage;//第幾頁 TitleCatalogue *_title; NSMutableDictionary *_dicMP3;//儲存所有的MP3 //儲存每種題目型別 NSMutableArray *arrayNo1; NSMutableArray *arrayNo2; NSMutableArray *arrayNo3; NSMutableArray *arrayNo4; NSMutableArray *arrayNo5; NSMutableArray *arrayNo6; NSMutableArray *arrayNo7; NSMutableArray *arrayNo8; NSMutableArray *arrayNo9; NSMutableArray *arrayNo10; NSMutableArray *arrayNo11; NSMutableArray *arrayNo12; NSMutableArray *arrayNo13; NSMutableArray *arrayNo14; NSMutableArray *arrayNo15; NSMutableArray *arrayNo21; NSMutableArray *arrayNo41; NSMutableArray *arrayNo42; NSMutableArray *arrayNo26; NSMutableArray *arrayNo52; NSMutableArray *arrayNo51; NSMutableArray *arrayNo54; NSMutableArray *arrayNo55; NSMutableArray *arrayNo56; NSMutableArray *arrayNo6a; NSMutableArray *arrayNo6b; NSMutableArray *arrayNo6c; NSMutableArray *arrayNo6d; NSMutableArray *arrayNo6e; NSMutableArray *arrayNo6f; NSMutableArray *arrayNo6g; NSMutableArray *arrayNo6h; NSMutableArray *arrayNo6i; NSMutableArray *arrayNo6j; NSMutableArray *arrayNo25; //儲存選擇的答案 NSMutableDictionary *dicAnswer; NSMutableArray *arrayAnswer; //儲存正確的答案 NSMutableDictionary *dicRealAnswer; NSMutableArray *arrayRealAnswer; } //建立播放器 @property (nonatomic,strong)AVAudioPlayer *player; //模型物件 //@property (nonatomic,strong)TitleCatalogue *title; @end @implementation ContentController /* @synthesize title=_title; - (TitleCatalogue *)title { if(!_title) { _title=[[TitleCatalogue alloc]init]; } return _title; } */ - (void)viewDidLoad { [super viewDidLoad]; [self loadFrame]; [self loadDate]; } - (void)loadDate { _currentPage=0; _dicMP3=[[NSMutableDictionary alloc]initWithCapacity:0]; arrayNo1=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo2=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo21=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo41=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo42=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo4=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo5=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo8=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo26=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo52=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo15=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo51=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo56=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6a=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6b=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6c=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6d=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6e=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6f=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6g=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6h=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6i=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6j=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo54=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo55=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo3=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo7=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo9=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo10=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo11=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo12=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo13=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo14=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo6=[[NSMutableArray alloc]initWithCapacity:0]; arrayNo25=[[NSMutableArray alloc]initWithCapacity:0]; dicAnswer=[[NSMutableDictionary alloc]initWithCapacity:0]; dicRealAnswer=[[NSMutableDictionary alloc]initWithCapacity:0]; arrayAnswer=[[NSMutableArray alloc]initWithCapacity:0]; arrayRealAnswer=[[NSMutableArray alloc]initWithCapacity:0]; jsonDic=[[NSDictionary alloc]init]; //得到每種型別的數量,得到每道題的答案,儲存MP3 for (int i=0; i<self.jsonArray.count; i++) { jsonDic=[self.jsonArray objectAtIndex:i]; NSString *strMp3=[jsonDic objectForKey:@"mp3"]; //把所有的MP3都儲存在字典中 if (![strMp3 isEqualToString:@""]) { NSInteger num=(NSInteger)i; NSNumber *number=[NSNumber numberWithInteger:num]; [_dicMP3 setObject:strMp3 forKey:number]; } //把所有答案取出來,儲存在字典中(setObject:答案;forKey:題目) NSString *strAnswer=[jsonDic objectForKey:@"realanswer"]; NSInteger answerNum=(NSInteger)i; NSNumber *answerNumber=[NSNumber numberWithInteger:answerNum]; [dicRealAnswer setObject:strAnswer forKey:answerNumber]; //把所有答案取出來,儲存在陣列中 [arrayRealAnswer addObject:[jsonDic objectForKey:@"realanswer"]]; if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"1"]) { [arrayNo1 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"2"]) { [arrayNo2 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"3"]) { [arrayNo3 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"21"]) { [arrayNo21 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"41"]) { [arrayNo41 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"42"]) { [arrayNo42 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"4"]) { [arrayNo4 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"5"]) { [arrayNo5 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6"]) { [arrayNo6 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"7"]) { [arrayNo7 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"8"]) { [arrayNo8 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"9"]) { [arrayNo9 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"10"]) { [arrayNo10 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"11"]) { [arrayNo11 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"12"]) { [arrayNo12 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"13"]) { [arrayNo13 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"14"]) { [arrayNo14 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6a"]) { [arrayNo6a addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6b"]) { [arrayNo6b addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6c"]) { [arrayNo6c addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6d"]) { [arrayNo6d addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6e"]) { [arrayNo6e addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6f"]) { [arrayNo6f addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6g"]) { [arrayNo6g addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6h"]) { [arrayNo6h addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6i"]) { [arrayNo6i addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"6j"]) { [arrayNo6j addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"55"]) { [arrayNo55 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"54"]) { [arrayNo54 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"56"]) { [arrayNo56 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"51"]) { [arrayNo51 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"15"]) { [arrayNo15 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"52"]) { [arrayNo52 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"26"]) { [arrayNo26 addObject:[jsonDic objectForKey:@"no"]]; } if ([[jsonDic objectForKey:@"train_type"] isEqualToString:@"25"]) { [arrayNo25 addObject:[jsonDic objectForKey:@"no"]]; } } //最後那個題的序號,就是序號的最大值,用作題目提示的總題號 NSInteger tipSum1 = 0; for( NSString *tmp in arrayNo1){ NSInteger num = [tmp integerValue]; if(num > tipSum1){ tipSum1 = num; } } NSInteger tipSum2 = 0; for( NSString *tmp in arrayNo2){ NSInteger num = [tmp integerValue]; if(num > tipSum2){ tipSum2 = num; } } NSInteger tipSum3 = 0; for( NSString *tmp in arrayNo3){ NSInteger num = [tmp integerValue]; if(num > tipSum3){ tipSum3 = num; } } NSInteger tipSum21 = 0; for( NSString *tmp in arrayNo21){ NSInteger num = [tmp integerValue]; if(num > tipSum21){ tipSum21 = num; } } NSInteger tipSum41 = 0; for( NSString *tmp in arrayNo41){ NSInteger num = [tmp integerValue]; if(num > tipSum41){ tipSum41 = num; } } NSInteger tipSum42 = 0; for( NSString *tmp in arrayNo42){ NSInteger num = [tmp integerValue]; if(num > tipSum42){ tipSum42 = num; } } NSInteger tipSum4 = 0; for( NSString *tmp in arrayNo4){ NSInteger num = [tmp integerValue]; if(num > tipSum4){ tipSum4 = num; } } NSInteger tipSum5 = 0; for( NSString *tmp in arrayNo5){ NSInteger num = [tmp integerValue]; if(num > tipSum5){ tipSum5 = num; } } NSInteger tipSum6 = 0; for( NSString *tmp in arrayNo6){ NSInteger num = [tmp integerValue]; if(num > tipSum6){ tipSum6 = num; } } NSInteger tipSum7 = 0; for( NSString *tmp in arrayNo7){ NSInteger num = [tmp integerValue]; if(num > tipSum7){ tipSum7 = num; } } NSInteger tipSum8 = 0; for( NSString *tmp in arrayNo8){ NSInteger num = [tmp integerValue]; if(num > tipSum8){ tipSum8 = num; } } NSInteger tipSum9 = 0; for( NSString *tmp in arrayNo9){ NSInteger num = [tmp integerValue]; if(num > tipSum9){ tipSum9 = num; } } NSInteger tipSum10 = 0; for( NSString *tmp in arrayNo10){ NSInteger num = [tmp integerValue]; if(num > tipSum10){ tipSum10 = num; } } NSInteger tipSum11 = 0; for( NSString *tmp in arrayNo11){ NSInteger num = [tmp integerValue]; if(num > tipSum11){ tipSum11 = num; } } NSInteger tipSum12 = 0; for( NSString *tmp in arrayNo12){ NSInteger num = [tmp integerValue]; if(num > tipSum12){ tipSum12 = num; } } NSInteger tipSum13 = 0; for( NSString *tmp in arrayNo13){ NSInteger num = [tmp integerValue]; if(num > tipSum13){ tipSum13 = num; } } NSInteger tipSum14 = 0; for( NSString *tmp in arrayNo14){ NSInteger num = [tmp integerValue]; if(num > tipSum14){ tipSum14 = num; } } NSInteger tipSum6a = 0; for( NSString *tmp in arrayNo6a){ NSInteger num = [tmp integerValue]; if(num > tipSum6a){ tipSum6a = num; } } NSInteger tipSum6b = 0; for( NSString *tmp in arrayNo6b){ NSInteger num = [tmp integerValue]; if(num > tipSum6b){ tipSum6b = num; } } NSInteger tipSum6c = 0; for( NSString *tmp in arrayNo6c){ NSInteger num = [tmp integerValue]; if(num > tipSum6c){ tipSum6c = num; } } NSInteger tipSum6d = 0; for( NSString *tmp in arrayNo6d){ NSInteger num = [tmp integerValue]; if(num > tipSum6d){ tipSum6d = num; } } NSInteger tipSum6e = 0; for( NSString *tmp in arrayNo6e){ NSInteger num = [tmp integerValue]; if(num > tipSum6e){ tipSum6e = num; } } NSInteger tipSum6f = 0; for( NSString *tmp in arrayNo6f){ NSInteger num = [tmp integerValue]; if(num > tipSum6f){ tipSum6f = num; } } NSInteger tipSum6g = 0; for( NSString *tmp in arrayNo6g){ NSInteger num = [tmp integerValue]; if(num > tipSum6g){ tipSum6g = num; } } NSInteger tipSum6h = 0; for( NSString *tmp in arrayNo6h){ NSInteger num = [tmp integerValue]; if(num > tipSum6h){ tipSum6h = num; } } NSInteger tipSum6i = 0; for( NSString *tmp in arrayNo6i){ NSInteger num = [tmp integerValue]; if(num > tipSum6i){ tipSum6i = num; } } NSInteger tipSum6j = 0; for( NSString *tmp in arrayNo6j){ NSInteger num = [tmp integerValue]; if(num > tipSum6j){ tipSum6j = num; } } NSInteger tipSum55 = 0; for( NSString *tmp in arrayNo55){ NSInteger num = [tmp integerValue]; if(num > tipSum55){ tipSum55 = num; } } NSInteger tipSum54 = 0; for( NSString *tmp in arrayNo54){ NSInteger num = [tmp integerValue]; if(num > tipSum54){ tipSum54 = num; } } NSInteger tipSum56 = 0; for( NSString *tmp in arrayNo56){ NSInteger num = [tmp integerValue]; if(num > tipSum56){ tipSum56 = num; } } NSInteger tipSum51 = 0; for( NSString *tmp in arrayNo51){ NSInteger num = [tmp integerValue]; if(num > tipSum51){ tipSum51 = num; } } NSInteger tipSum15 = 0; for( NSString *tmp in arrayNo15){ NSInteger num = [tmp integerValue]; if(num > tipSum15){ tipSum15 = num; } } NSInteger tipSum52 = 0; for( NSString *tmp in arrayNo52){ NSInteger num = [tmp integerValue]; if(num > tipSum52){ tipSum52 = num; } } NSInteger tipSum26 = 0; for( NSString *tmp in arrayNo26){ NSInteger num = [tmp integerValue]; if(num > tipSum26){ tipSum26 = num; } } NSInteger tipSum25 = 0; for( NSString *tmp in arrayNo25){ NSInteger num = [tmp integerValue]; if(num > tipSum25){ tipSum25 = num; } } // NSInteger int1=[[arrayNo1 valueForKeyPath:@"@max.intValue"] integerValue]; //取出每一題及其內容 for (int i=0; i<self.jsonArray.count; i++) { jsonDic=[self.jsonArray objectAtIndex:i]; _title.train_type=[jsonDic objectForKey:@"train_type"]; _title.mp3=[jsonDic objectForKey:@"mp3"]; _title.question=[jsonDic objectForKey:@"question"]; _title.answera=[jsonDic objectForKey:@"answera"]; _title.answerb=[jsonDic objectForKey:@"answerb"]; _title.answerc=[jsonDic objectForKey:@"answerc"]; _title.answerd=[jsonDic objectForKey:@"answerd"]; _title.realanswer=[jsonDic objectForKey:@"realanswer"]; _title.no=[jsonDic objectForKey:@"no"]; tipNum=[_title.no integerValue]; //根據檔案上的題目依次顯示 if ([_title.train_type isEqualToString:@"1"])//2個圖片的選擇題 { Layout1Controller *lay1=[[Layout1Controller alloc]init]; lay1.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay1.lblTitle.text=_title.question; lay1.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum1,tipNum]]; [lay1.btnAnswer1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@s.png",_title.answera]] forState:UIControlStateSelected]; [lay1.btnAnswer2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",_title.answerb]] forState:UIControlStateNormal]; [lay1.btnAnswer1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",_title.answera]] forState:UIControlStateNormal]; [lay1.btnAnswer2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@s.png",_title.answerb]] forState:UIControlStateSelected]; [_myScrollView addSubview:lay1.view]; //新增子控制器 [self addChildViewController:lay1]; } else if ([_title.train_type isEqualToString:@"2"])//2個單詞選擇題 { Layout2Controller *lay2=[[Layout2Controller alloc]init]; lay2.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay2.lblTitlt.text=_title.question; lay2.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum2,tipNum]]; lay2.lblAnswer1.text=_title.answera; lay2.lblAnswer2.text=_title.answerb; [lay2.btnAnswer setImage:[UIImage imageNamed:@"photo3_1_0s.png"] forState:UIControlStateSelected]; [lay2.btnAnswer2 setImage:[UIImage imageNamed:@"grid_view1.png"] forState:UIControlStateNormal]; [lay2.btnAnswer setImage:[UIImage imageNamed:@"grid_view1.png"] forState:UIControlStateNormal]; [lay2.btnAnswer2 setImage:[UIImage imageNamed:@"photo3_1_0s.png"] forState:UIControlStateSelected]; //判斷答案 [_myScrollView addSubview:lay2.view]; [self addChildViewController:lay2]; } else if ([_title.train_type isEqualToString:@"3"])//3個圖片選擇題,Layout21Controller { Layout21Controller *lay3=[[Layout21Controller alloc]init]; lay3.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay3.lblTitle.text=_title.question; lay3.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum3,tipNum]]; lay3.lblAnswer1.hidden=YES; lay3.lblAnswer2.hidden=YES; lay3.lblAnswer3.hidden=YES; [lay3.btnAnswer1 setImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay3.btnAnswer2 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay3.btnAnswer3 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay3.btnAnswer1 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay3.btnAnswer2 setImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay3.btnAnswer3 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay3.btnAnswer1 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay3.btnAnswer3 setImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay3.btnAnswer2 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [_myScrollView addSubview:lay3.view]; [self addChildViewController:lay3]; } else if ([_title.train_type isEqualToString:@"4"])//3個單詞選擇題,Layout21Controller { Layout21Controller *lay4=[[Layout21Controller alloc]init]; lay4.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay4.lblTitle.text=_title.question; lay4.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum4,tipNum]]; lay4.lblAnswer1.text=_title.answera; lay4.lblAnswer2.text=_title.answerb; lay4.lblAnswer3.text=_title.answerc; // NSLog(@"answer3:%@\nlblAnswer3:%@",_title.answerc,lay4.lblAnswer3.text); [lay4.btnAnswer1 setImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay4.btnAnswer2 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay4.btnAnswer3 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay4.btnAnswer1 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay4.btnAnswer2 setImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay4.btnAnswer3 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay4.btnAnswer1 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay4.btnAnswer3 setImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay4.btnAnswer2 setImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [_myScrollView addSubview:lay4.view]; [self addChildViewController:lay4]; } else if([_title.train_type isEqualToString:@"5"])//1個填空 { Layout5Controller *lay5=[[Layout5Controller alloc]init]; lay5.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay5.lblTitle.text=_title.question; lay5.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum5,tipNum]]; lay5.lblAnswer1.text=_title.answera; //屬性傳值 lay5.strQuestion=_title.answera; [self.myScrollView addSubview:lay5.view]; [self addChildViewController:lay5]; } else if([_title.train_type isEqualToString:@"15"])//看圖填1個空 { Layout15Controller *lay15=[[Layout15Controller alloc]init]; lay15.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay15.lblTitle.text=_title.question; lay15.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum15,tipNum]]; lay15.lblAnswer1.text=_title.answera; lay15.imgAnswer2.image=[UIImage imageNamed:_title.answerc]; //屬性傳值 lay15.strQuestion=_title.answera; [self.myScrollView addSubview:lay15.view]; [self addChildViewController:lay15]; } else if([_title.train_type isEqualToString:@"16"])//看圖填2個空 { Layout15Controller *lay16=[[Layout15Controller alloc]init]; lay16.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay16.lblTitle.text=_title.question; lay16.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum15,tipNum]]; lay16.lblAnswer1.text=_title.answera; lay16.imgAnswer2.image=[UIImage imageNamed:_title.answerc]; //屬性傳值 lay16.strQuestion=_title.answera; [self.myScrollView addSubview:lay16.view]; [self addChildViewController:lay16]; } else if([_title.train_type isEqualToString:@"21"])//圖片的排序、編號,Layout4Controller { Layout4Controller *lay21=[[Layout4Controller alloc]init]; lay21.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay21.lblTitle.text=_title.question; lay21.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum21,tipNum]]; lay21.imgAnswer1.image=[UIImage imageNamed:_title.answera]; lay21.imgAnswer2.image=[UIImage imageNamed:_title.answerb]; if([_title.answerc isEqualToString:@"no2"])//判斷是幾張圖片 { lay21.btnFive.hidden=YES; lay21.btnSix.hidden=YES; } if([_title.answerc isEqualToString:@"no"]) { lay21.btnSix.hidden=YES; } if([_title.answerc isEqualToString:@"no3"]) { lay21.btnSix.hidden=YES; lay21.btnFive.hidden=YES; lay21.btnFour.hidden=YES; lay21.imgAnswer2.hidden=YES; } [_myScrollView addSubview:lay21.view]; [self addChildViewController:lay21]; } else if([_title.train_type isEqualToString:@"41"])//根據給出的字母,選出(題目上的)正確答案 { Layout41Controller *lay41=[[Layout41Controller alloc]init]; lay41.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay41.lblTitle.text=_title.question; lay41.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum41,tipNum]]; lay41.lblWorld.text=_title.answera; //屬性傳值 lay41.strSelect=_title.answerb; [self.myScrollView addSubview:lay41.view]; [self addChildViewController:lay41]; } else if([_title.train_type isEqualToString:@"42"])//根據給出的圖片以及聽MP3,選出(題目上的)正確答案 { Layout42ViewController *lay42=[[Layout42ViewController alloc]init]; lay42.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay42.lblTitle.text=_title.question; lay42.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum42,tipNum]]; lay42.imgAnswer1.image=[UIImage imageNamed:_title.answera]; //根據給出的字母的長度(個數)動態建立按鈕選項 //屬性傳值 lay42.strSelect=_title.answerb; [self.myScrollView addSubview:lay42.view]; [self addChildViewController:lay42]; } else if ([_title.train_type isEqualToString:@"26"])//畫圖 { Layout26Controller *lay26=[[Layout26Controller alloc]init]; lay26.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay26.lblTitle.text=_title.question; lay26.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum26,tipNum]]; //………………建立畫板,畫圖 [self.myScrollView addSubview:lay26.view]; [self addChildViewController:lay26]; } else if([_title.train_type isEqualToString:@"52"])//看圖選 { Layout52Controller *lay52=[[Layout52Controller alloc]init]; lay52.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay52.lblTitle.text=_title.question; lay52.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum52,tipNum]]; lay52.imgAnswer1.image=[UIImage imageNamed:_title.answera]; lay52.lblAnswer2.text=_title.answerb; lay52.lblAnswer3.text=_title.answerc; [lay52.btnAnswer2 setImage:[UIImage imageNamed:@"grid_view1.png"] forState:UIControlStateNormal]; [lay52.btnAnswer3 setImage:[UIImage imageNamed:@"photo3_1_0s.png"] forState:UIControlStateSelected]; [lay52.btnAnswer2 setImage:[UIImage imageNamed:@"photo3_1_0s.png"] forState:UIControlStateSelected]; [lay52.btnAnswer3 setImage:[UIImage imageNamed:@"grid_view1.png"] forState:UIControlStateNormal]; [self.myScrollView addSubview:lay52.view]; [self addChildViewController:lay52]; } else if([_title.train_type isEqualToString:@"51"])//看單詞選(2選項) { Layout51Controller *lay51=[[Layout51Controller alloc]init]; lay51.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay51.lblTitle.text=_title.question; lay51.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum51,tipNum]]; lay51.lblAnswer.text=_title.answera; [lay51.btnAnswer2 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay51.btnAnswer2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay51.btnAnswer3 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay51.btnAnswer3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay51.btnAnswer2 setTitle:_title.answerb forState:UIControlStateNormal]; [lay51.btnAnswer3 setTitle:_title.answerc forState:UIControlStateNormal]; [lay51.btnAnswer2 setTitle:_title.answerb forState:UIControlStateSelected]; [lay51.btnAnswer3 setTitle:_title.answerc forState:UIControlStateSelected]; [lay51.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"grid_view1.png"] forState:UIControlStateNormal]; [lay51.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"photo3_1_0s.png"] forState:UIControlStateSelected]; [lay51.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"photo3_1_0s.png"] forState:UIControlStateSelected]; [lay51.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"grid_view1.png"] forState:UIControlStateNormal]; [self.myScrollView addSubview:lay51.view]; [self addChildViewController:lay51]; } else if([_title.train_type isEqualToString:@"56"])//四個選項選擇題 { Layout56Controller *lay56=[[Layout56Controller alloc]init]; lay56.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay56.lblTitle.text=_title.question; lay56.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum56,tipNum]]; [lay56.btnAnswer1 setTitle:_title.answera forState:UIControlStateNormal]; [lay56.btnAnswer2 setTitle:_title.answerb forState:UIControlStateNormal]; [lay56.btnAnswer3 setTitle:_title.answerc forState:UIControlStateNormal]; [lay56.btnAnswer4 setTitle:_title.answerd forState:UIControlStateNormal]; [lay56.btnAnswer1 setTitle:_title.answera forState:UIControlStateSelected]; [lay56.btnAnswer2 setTitle:_title.answerb forState:UIControlStateSelected]; [lay56.btnAnswer3 setTitle:_title.answerc forState:UIControlStateSelected]; [lay56.btnAnswer4 setTitle:_title.answerd forState:UIControlStateSelected]; [lay56.btnAnswer2 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay56.btnAnswer2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay56.btnAnswer3 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay56.btnAnswer3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay56.btnAnswer1 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay56.btnAnswer1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay56.btnAnswer4 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay56.btnAnswer4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay56.btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay56.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer4 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay56.btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer4 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay56.btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer4 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer4 setBackgroundImage:[UIImage imageNamed:@"select3_4ds.png"] forState:UIControlStateSelected]; [lay56.btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [lay56.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"select3_4d.png"] forState:UIControlStateNormal]; [self.myScrollView addSubview:lay56.view]; [self addChildViewController:lay56]; } else if([_title.train_type isEqualToString:@"6a"])//填空1(聽錄音,在橫線上填入所缺數字) { Layout6aController *lay6a=[[Layout6aController alloc]init]; lay6a.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay6a.lblTitle.text=_title.question; lay6a.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum6a,tipNum]]; lay6a.lblAnswer.text=_title.answera; //這個待完善^^^^^^^^^^^^^^^^^^ [self.myScrollView addSubview:lay6a.view]; [self addChildViewController:lay6a]; } else if([_title.train_type isEqualToString:@"54"])//看單詞選(3選項) { Layout54Controller *lay54=[[Layout54Controller alloc]init]; lay54.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay54.lblTitle.text=_title.question; lay54.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum54,tipNum]]; lay54.lblAnswer1.text=_title.answera; [lay54.btnAnswer2 setTitle:_title.answerb forState:UIControlStateNormal]; [lay54.btnAnswer3 setTitle:_title.answerc forState:UIControlStateNormal]; [lay54.btnAnswer4 setTitle:_title.answerd forState:UIControlStateNormal]; [lay54.btnAnswer2 setTitle:_title.answerb forState:UIControlStateSelected]; [lay54.btnAnswer3 setTitle:_title.answerc forState:UIControlStateSelected]; [lay54.btnAnswer4 setTitle:_title.answerd forState:UIControlStateSelected]; [lay54.btnAnswer2 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay54.btnAnswer2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay54.btnAnswer4 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay54.btnAnswer4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay54.btnAnswer3 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [lay54.btnAnswer3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lay54.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"select3_4bs.png"] forState:UIControlStateSelected]; [lay54.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"select3_4b.png"] forState:UIControlStateNormal]; [lay54.btnAnswer4 setBackgroundImage:[UIImage imageNamed:@"select3_4b.png"] forState:UIControlStateNormal]; [lay54.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"select3_4bs.png"] forState:UIControlStateSelected]; [lay54.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"select3_4b.png"] forState:UIControlStateNormal]; [lay54.btnAnswer4 setBackgroundImage:[UIImage imageNamed:@"select3_4b.png"] forState:UIControlStateNormal]; [lay54.btnAnswer4 setBackgroundImage:[UIImage imageNamed:@"select3_4bs.png"] forState:UIControlStateSelected]; [lay54.btnAnswer3 setBackgroundImage:[UIImage imageNamed:@"select3_4b.png"] forState:UIControlStateNormal]; [lay54.btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"select3_4b.png"] forState:UIControlStateNormal]; [self.myScrollView addSubview:lay54.view]; [self addChildViewController:lay54]; } else if([_title.train_type isEqualToString:@"55"])//選圖和填空 { Layout55Controller *lay55=[[Layout55Controller alloc]init]; lay55.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay55.lblTitle.text=_title.question; lay55.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum55,tipNum]]; [lay55.btnAnswer1 setTitle:_title.answera forState:UIControlStateNormal]; [lay55.btnAnswer2 setTitle:_title.answerb forState:UIControlStateNormal]; [lay55.btnAnswer1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@s.png",_title.answera]] forState:UIControlStateSelected]; [lay55.btnAnswer2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",_title.answerb]] forState:UIControlStateNormal]; [lay55.btnAnswer1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",_title.answera]] forState:UIControlStateNormal]; [lay55.btnAnswer2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@s.png",_title.answerb]] forState:UIControlStateSelected]; //^^^^^^^^下面判斷空格和選項是否正確 [self.myScrollView addSubview:lay55.view]; [self addChildViewController:lay55]; } else if([_title.train_type isEqualToString:@"25"])//排序8個 { Layout25Controller *lay25=[[Layout25Controller alloc]init]; lay25.view.frame=CGRectMake(viewW*i, 0, viewW, viewH); lay25.lblTitle.text=_title.question; lay25.imgTip.image=[UIImage imageNamed:[NSString stringWithFormat:@"select2_%ic%i.png",tipSum25,tipNum]]; lay25.imgAnswer1.image=[UIImage imageNamed:_title.answera]; lay25.imgAnswer2.image=[UIImage imageNamed:_title.answerb]; lay25.imgAnswer3.image=[UIImage imageNamed:_title.answerc]; [self.myScrollView addSubview:lay25.view]; [self addChildViewController:lay25]; } //播放第一頁的MP3 [self playMp3]; } } /* //獲取字串的寬度 -(float) widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height { CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(CGFLOAT_MAX, height) lineBreakMode:NSLineBreakByWordWrapping];//此處的換行型別(lineBreakMode)可根據自己的實際情況進行設定 return sizeToFit.width; } //獲得字串的高度 -(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width { CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByCharWrapping];//此處的換行型別(lineBreakMode)可根據自己的實際情況進行設定 return sizeToFit.height; } */ //初始化介面 - (void)loadFrame{ _title=[[TitleCatalogue alloc]init]; //是否滑動到最頂端 _myScrollView.scrollsToTop = NO; _myScrollView.delegate = self; _myScrollView.contentSize = CGSizeMake(viewW*self.jsonArray.count, 0);//設定內容大小 _myScrollView.bounces = NO;//是否反彈 _myScrollView.pagingEnabled = YES;//是否分頁 _myScrollView.scrollEnabled = YES;// 是否滾動 //設定取消觸控 self.myScrollView.canCancelContentTouches = NO; //設定滾動條型別 // self.myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; //是否自動裁切超出部分 self.myScrollView.clipsToBounds = YES; //設定是否可以縮放 self.myScrollView.scrollEnabled = YES; //設定是否可以進行畫面切換 self.myScrollView.pagingEnabled = YES; //設定在拖拽的時候是否鎖定其在水平或者垂直的方向 self.myScrollView.directionalLockEnabled = NO; //隱藏滾動條設定(水平、跟垂直方向) self.myScrollView.alwaysBounceHorizontal = NO; self.myScrollView.alwaysBounceVertical = NO; self.myScrollView.showsHorizontalScrollIndicator = NO; self.myScrollView.showsVerticalScrollIndicator = NO; [self bottomViewSet]; } //滾動檢視底部上一題和下一題按鈕的變化 - (void)bottomViewSet { //如果是第一題,那麼這個按鈕就不可用 if (_currentPage==0) { [_btnLast setImage:[UIImage imageNamed:@"select2_7b9.png"] forState:UIControlStateNormal]; _btnLast.enabled=NO; } else { [_btnLast setImage:[UIImage imageNamed:@"select2_7b.png"] forState:UIControlStateNormal]; _btnLast.enabled=YES; } //如果是最後一題,那麼下一題的按鈕隱藏不可用,提交按鈕出現 if (_currentPage==self.jsonArray.count-1) { _btnNext.enabled=NO; _btnSubmit.enabled=YES; _btnNext.hidden=YES; _btnSubmit.hidden=NO; } else { _btnNext.enabled=YES; _btnSubmit.enabled=NO; _btnNext.hidden=NO; _btnSubmit.hidden=YES; } } //第一頁播放MP3 - (void)playMp3 { if (_currentPage==0) { NSNumber *num=[NSNumber numberWithInteger:0]; NSString *strMp3=[_dicMP3 objectForKey:num]; //如果沒有MP3 if ([strMp3 rangeOfString:@""].location !=NSNotFound) { // 按鈕禁用 self.btnListen.enabled=NO; } else { //按鈕啟用,播放MP3 self.btnListen.enabled=YES; NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:strMp3 ofType:@"mp3"]]; _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; _player.delegate=self; [_player prepareToPlay]; [_player play]; } } } //每頁播放自己的MP3 - (void)playerMp3 { //如果沒有MP3,那麼這個按鈕就不可用,否則播放MP3 NSNumber *num=[NSNumber numberWithInteger:_currentPage]; NSString *strMp3=[_dicMP3 objectForKey:num]; //播放MP3 NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:strMp3 ofType:@"mp3"]]; //建立播放器 _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; _player.delegate=self; if ([strMp3 rangeOfString:@""].location !=NSNotFound) {//找得到MP3的地址 //禁用按鈕 self.btnListen.enabled=NO; //停止上一個播放 [_player stop]; } else{ self.btnListen.enabled=YES; //緩衝 [self.player prepareToPlay]; //播放 [self.player play]; } } //滾動檢視減速完成,滾動將停止時,呼叫該方法。一次有效滑動,只執行一次。顯示當前頁面的索引 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ _currentPage = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width; [self playerMp3]; [self bottomViewSet]; } //回到目錄頁面 - (IBAction)clickHome:(id)sender { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"是否要回到目錄頁?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self.navigationController popViewControllerAnimated:YES]; }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; } //上一題 - (IBAction)clickLast:(id)sender { //如果是第一題,那個這個按鈕就不可用 if (_currentPage==0) { [_btnLast setImage:[UIImage imageNamed:@"select2_7b9.png"] forState:UIControlStateNormal]; _btnLast.enabled=NO; } else { _currentPage--; [_btnLast setImage:[UIImage imageNamed:@"select2_7b.png"] forState:UIControlStateNormal]; _btnLast.enabled=YES; } //檢視同步滾到到相應的題目 CGFloat with=_currentPage*viewW; [_myScrollView setContentOffset:CGPointMake(with, 0) animated:YES]; if (_btnNext.hidden==YES&&_btnNext.enabled==NO) { _btnNext.hidden=NO; _btnNext.enabled=YES; _btnSubmit.enabled=NO; _btnSubmit.hidden=YES; } //如果沒有MP3,那麼這個按鈕就不可用,否則播放MP3 [self playerMp3]; } //播放當前題目的MP3 - (IBAction)clickListen:(id)sender { //如果沒有MP3,那麼這個按鈕就不可用 //播放MP3 [self playerMp3]; } //下一題 - (IBAction)clickNext:(id)sender { //如果是最後一題,那麼這個按鈕變為提交按鈕 if (_currentPage==self.jsonArray.count-1) { _btnNext.enabled=NO; _btnNext.hidden=YES; _btnSubmit.enabled=YES; _btnSubmit.hidden=NO; } else { _currentPage++; _btnNext.enabled=YES; _btnNext.hidden=NO; _btnSubmit.enabled=NO; _btnSubmit.hidden=YES; } //檢視同步滾到到相應的題目 CGFloat with=_currentPage*viewW; [_myScrollView setContentOffset:CGPointMake(with, 0) animated:YES]; if (_btnLast.enabled==NO) { _btnLast.enabled=YES; [_btnLast setImage:[UIImage imageNamed:@"select2_7b.png"] forState:UIControlStateNormal]; } [self playerMp3]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (IBAction)clickSubmit:(id)sender { NSLog(@"提交!"); } @end
這段程式碼呢,也是不完全的,因為功能基本上我都還沒完成!
嘿嘿!不過解決我說的問題還是可以滴!