Xcode版本4.5 & IOS6
1. 開啓一個iPad的專案
2. 設定iPad顯示的方向,將直行的點掉不要了。
3. 設定storyboard的預設方向為橫向
4. 加入所需要的附屬檔案,字型檔/音樂檔/文字檔/圖檔
5. 加入AV Framework
6. 加入字型檔設定,標楷體(BiauKai.ttf)
7. 對StoryBoard加入所需要的Label/Button/Slider
8. 將元件註冊到mainViewController.h,並加入所需變數的設定
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;
@property (weak, nonatomic) IBOutlet UIButton *forwardButton;
@property (weak, nonatomic) IBOutlet UIButton *backwardButton;
@property (weak, nonatomic) IBOutlet UISlider *timeSlider;
@property (weak, nonatomic) IBOutlet UISlider *soundSlider;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *soundLabel;
@property (nonatomic, retain) NSMutableArray *globalData;
@property (nonatomic, retain) NSMutableArray *lineData;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (retain) AVAudioPlayer* audioPlayer;
@property(nonatomic,retain)NSTimer *songTimer;
@end
9. 將所需的控制程式碼寫在mainViewController.m,IBAction要注意聯結好
#import "mainViewController.h"
@interface mainViewController ()
@end
#define Image_Height 600
#define Image_Width 1024
#define Screen_Width_OFFSET 200
#define Y_Start_Pos 0
#define X_Start_Pos 0
#define Image_ALPHA 0.3
#define ScrollView_OFFSET 2700
#define Show_Text_X_Pos 3600
#define Text_Line_Width 60
#define Text_Width 40
#define Text_Line_Y_Pos 40
#define Text_Line_Hieght 560
#define NO_Voice_Offset 720
#define Voice_Par 1.25
@implementation mainViewController
@synthesize globalData;
@synthesize lineData;
@synthesize scrollView;
@synthesize playButton;
@synthesize stopButton;
@synthesize forwardButton;
@synthesize backwardButton;
@synthesize soundSlider;
@synthesize timeSlider;
@synthesize timeLabel;
@synthesize soundLabel;
@synthesize audioPlayer;
@synthesize songTimer;
int clicked = 0;
NSInteger indexNumber = 0;
- (void)viewDidLoad
{
[self audioReady];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
globalData = [[NSMutableArray alloc] init];
lineData = [[NSMutableArray alloc] init];
UIFont *font = [UIFont fontWithName:@"BiauKai" size:24];
[playButton setFont:font];
[stopButton setFont:font];
[soundLabel setFont:font];
[timeLabel setFont:font];
[self.view setBackgroundColor:[UIColor grayColor]];
[self createScrollView];
[self readFile];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) createScrollView
{
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(X_Start_Pos, Y_Start_Pos, Image_Width, Image_Height)];
NSString *imageName = [NSString stringWithFormat:@"images.jpg"];
UIImage *image1 = [UIImage imageNamed:imageName];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image1];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image1];
UIImageView *imageView3 = [[UIImageView alloc] initWithImage:image1];
UIImageView *imageView4 = [[UIImageView alloc] initWithImage:image1];
[imageView1 setFrame:CGRectMake(X_Start_Pos, Y_Start_Pos, Image_Width, Image_Height)];// x, y, w, h
[imageView2 setFrame:CGRectMake(Image_Width, Y_Start_Pos, Image_Width, Image_Height)];// x, y, w, h
[imageView3 setFrame:CGRectMake((Image_Width*2), Y_Start_Pos, Image_Width, Image_Height)];// x, y, w, h
[imageView4 setFrame:CGRectMake((Image_Width*3), Y_Start_Pos, Image_Width, Image_Height)];// x, y, w, h
// 半透明
imageView1.alpha = Image_ALPHA;
imageView2.alpha = Image_ALPHA;
imageView3.alpha = Image_ALPHA;
imageView4.alpha = Image_ALPHA;
//UIFont *font = [UIFont systemFontOfSize:17.0];
[scrollView addSubview:imageView1];
[scrollView addSubview:imageView2];
[scrollView addSubview:imageView3];
[scrollView addSubview:imageView4];
[scrollView setContentSize:CGSizeMake((Image_Width*4), (Image_Height+Y_Start_Pos))]; // (x, y), 設定寬度為四倍,以延展視窗區寬度
[scrollView setScrollEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:YES]; // 啟動寬度的Scroll
CGPoint position = CGPointMake(ScrollView_OFFSET , Y_Start_Pos);
[scrollView setContentOffset:position animated:YES]; //移動到指定的位置
[self.view addSubview:scrollView];
}
- (void) readFile
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
if (filePath) {
NSString *contentOfFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[globalData addObject:contentOfFile];
}
[self rowLineData:10];
[self showRowLineData];
}
- (void) rowLineData:(NSInteger *) charNumInLine;
{
NSString *data = [globalData objectAtIndex:0];
NSInteger length = [data length];
//NSString *tempdata ;
NSInteger lineNum = (int)length/(int)charNumInLine;
int lastCharNum = (int)length%(int)charNumInLine;
for (int i=0; i<lineNum;i++) {
NSString *tempdata;
tempdata = [[data substringFromIndex: i*(int)charNumInLine]substringToIndex: charNumInLine];
[lineData addObject:tempdata];
}
NSNumber *tempNum;
tempNum = [NSNumber numberWithInt:lineNum];
if ([globalData count] >1 )
[globalData replaceObjectAtIndex:1 withObject:tempNum];
else
[globalData addObject:tempNum];
//
if (lastCharNum !=0) {
NSString *tempdata;
//取最後字元
tempdata = [[data substringFromIndex: lineNum*(int)charNumInLine]substringToIndex: lastCharNum];
[lineData addObject:tempdata];
lineNum ++;
tempNum = [NSNumber numberWithInt:lineNum];
[globalData replaceObjectAtIndex:1 withObject:tempNum];
}
}
- (void) showRowLineData
{
NSString *allData =@" ";
NSString *tempData = @" ";
id item;
int i =0;
//for (int i=0;i<10;i++)
for (item in lineData)
{
tempData = [lineData objectAtIndex:i];
allData= [allData stringByAppendingString: tempData];
int stringCount = [tempData length];
//char *tempChar;
NSString *output;
for (int j=0;j<stringCount;j++)
{
NSString * newString = [tempData substringWithRange:NSMakeRange(j, 1)];
if (j==0)
output = [newString stringByAppendingString:[NSString stringWithFormat:@"\n"]];
else {
newString = [newString stringByAppendingString:[NSString stringWithFormat:@"\n"]];
output = [output stringByAppendingString:newString];
}
}
[self drawLineAction:(Show_Text_X_Pos-Text_Line_Width*i) inString:output];
i++;
}
}
-(void) drawLineAction:(float)xPosition inString:(NSString *)lineString
{
NSNumber *tempNum;
// now in line number
tempNum = [NSNumber numberWithInt:0];
if ([globalData count] >2 )
[globalData replaceObjectAtIndex:1 withObject:tempNum];
else
[globalData addObject:tempNum];
UILabel *contentLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, Text_Line_Y_Pos , Text_Width, Text_Line_Hieght)];// x, y, w,h
UIFont *font = [UIFont fontWithName:@"BiauKai" size:32];
[contentLabel1 setFont:font];
contentLabel1.lineBreakMode = UILineBreakModeWordWrap; // 自動切換行
contentLabel1.numberOfLines = 0; // 設定行數為0
contentLabel1.text = lineString;
// 讓label的背景色為透明
contentLabel1.backgroundColor=[UIColor clearColor];
[scrollView addSubview:contentLabel1];
}
- (IBAction)playerPlay:(id)sender {
if (clicked == 0) {
clicked = 1;
[audioPlayer play];
[playButton setTitle:@"暫停" forState:UIControlStateNormal];
}
else {
clicked = 0;
[audioPlayer pause];
[playButton setTitle:@"播放" forState:UIControlStateNormal];
}
[self textScrollPosition];
}
- (IBAction)playerStop:(id)sender {
[audioPlayer stop];
audioPlayer.currentTime = 0;
clicked = 0;
[playButton setTitle:@"播放" forState:UIControlStateNormal];
}
- (IBAction)forwardAction:(id)sender {
NSTimeInterval time = audioPlayer.currentTime;
time += 5.0; // forward 5 secs
if (time > audioPlayer.duration)
[audioPlayer stop];
else
audioPlayer.currentTime = time;
[self textScrollPosition];
}
- (IBAction)backwardAction:(id)sender {
NSTimeInterval time = audioPlayer.currentTime;
time -=5;
if (time<=0)
audioPlayer.currentTime = 0.0;
else
audioPlayer.currentTime = time;
[self textScrollPosition];
}
- (IBAction)soundChange:(id)sender {
audioPlayer.volume = soundSlider.value;
}
- (IBAction)timeChange:(id)sender {
double timeValue = timeSlider.value;
audioPlayer.currentTime = (NSTimeInterval ) timeValue;
[self textScrollPosition];
}
- (void) showTime:(NSTimeInterval ) time
{
NSInteger *min;
NSInteger *sec;
min = (NSInteger ) time/60;
sec = (NSInteger ) time%60;
timeLabel.text = [NSString stringWithFormat:@"%2.2d:%2.2d",min, sec];
[self textScrollPosition];
}
-(void)timeLoader
{
NSTimeInterval time = audioPlayer.currentTime;
timeSlider.value = time;
if (audioPlayer.currentTime == 0)
[self showTime:audioPlayer.duration];
else
[self showTime:audioPlayer.currentTime];
[self textScrollPosition];
}
- (void) audioReady
{
NSString *myMusic = [[NSBundle mainBundle]pathForResource:@"大悲咒(齊豫)" ofType:@"mp3"];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:myMusic] error:NULL];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = soundSlider.value;
timeSlider.maximumValue = audioPlayer.duration;
songTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeLoader) userInfo:nil repeats:YES];
}
- (void) textScrollPosition
{
NSInteger total_time = audioPlayer.duration;
NSInteger numLines = [lineData count];
double douration = total_time/numLines; // 一行需幾秒
double width = Text_Line_Width;
double currentLine = numLines * audioPlayer.currentTime/total_time;
indexNumber = (NSInteger)currentLine * (Text_Line_Width*Voice_Par); // offset
if (indexNumber < NO_Voice_Offset) {
indexNumber = 0;
}
else {
indexNumber = indexNumber - NO_Voice_Offset;
}
[self scrollRangeToVisible:indexNumber];
}
- (void) scrollRangeToVisible:(NSInteger) offset
{
NSInteger offsetValue = (ScrollView_OFFSET - offset);
CGPoint position = CGPointMake(offsetValue, Y_Start_Pos);
[scrollView setContentOffset:position animated:YES]; //移動到指定的位置
}
@end
10. 結果如下圖,經文會配合聲音滑動










沒有留言:
張貼留言