實作一個ScrollView的砍圖機制,此機制是使用tag來完成的。
本例的測試在於所增加的小圖,可以隨時移除所需的部分。
Xcode版本4.5 & IOS6
1. 開啓一個iPad的專案
2. 加入兩個Button
3. 加入所需的圖檔
4. 在objectTagViewController.m加入控制碼
#import "objectTagViewController.h"
@interface objectTagViewController ()
@end
@implementation objectTagViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *imageName = [NSString stringWithFormat:@"TaiwanMap.jpg"];
UIImage *image1 = [UIImage imageNamed:imageName];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image1];
UIImage *image2 = [UIImage imageNamed:imageName];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
[imageView1 setFrame:CGRectMake(0, 0, 512, 600)];// x, y, w, h
[imageView2 setFrame:CGRectMake(512, 0, 512, 600)];// x, y, w, h
[self.view addSubview:imageView1];
[self.view addSubview:imageView2];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)deleteImage:(id)sender {
[self removeSubViewByTag:12];
[self removeSubViewByTag:15];
}
- (IBAction)addImage:(id)sender {
NSString *imageName1 = [NSString stringWithFormat:@"Green Apple.gif"];
NSString *imageName2 = [NSString stringWithFormat:@"Orange.gif"];
NSString *imageName3 = [NSString stringWithFormat:@"Pear.gif"];
NSString *imageName4 = [NSString stringWithFormat:@"Red Apple.gif"];
NSString *imageName5 = [NSString stringWithFormat:@"Strawberry.gif"];
UIImage *image1 = [UIImage imageNamed:imageName1];
UIImage *image2 = [UIImage imageNamed:imageName2];
UIImage *image3 = [UIImage imageNamed:imageName3];
UIImage *image4 = [UIImage imageNamed:imageName4];
UIImage *image5 = [UIImage imageNamed:imageName5];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image1];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
UIImageView *imageView3 = [[UIImageView alloc] initWithImage:image3];
UIImageView *imageView4 = [[UIImageView alloc] initWithImage:image4];
UIImageView *imageView5 = [[UIImageView alloc] initWithImage:image5];
[imageView1 setFrame:CGRectMake(50, 50, 72, 72)];// x, y, w, h
[imageView2 setFrame:CGRectMake(100, 100, 72, 72)];// x, y, w, h
[imageView3 setFrame:CGRectMake(150, 150, 72, 72)];// x, y, w, h
[imageView4 setFrame:CGRectMake(250, 250, 72, 72)];// x, y, w, h
[imageView5 setFrame:CGRectMake(300, 300, 72, 72)];// x, y, w, h
imageView1.tag = 11;
imageView2.tag = 12;
imageView3.tag = 13;
imageView4.tag = 14;
imageView5.tag = 15;
[self.view addSubview:imageView1];
[self.view addSubview:imageView2];
[self.view addSubview:imageView3];
[self.view addSubview:imageView4];
[self.view addSubview:imageView5];
}
- (void) removeSubViewByTag:(NSInteger *)tagNum
{
for(UIView *subview in [self.view subviews]) {
//if([subview isKindOfClass:[UILabel class]]) {
if (tagNum == subview.tag) {
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
}
@end
5. 執行結果 ,原始背景圖,後來加入五個水果,然後再移除中間的兩個。
2012年9月27日 星期四
2012年9月26日 星期三
UIToolBar的基本使用(二)
延續前一篇的的功能,加上隱藏toolBar/NavigationBar的功能,但可以使用Touch來回復顯示,
並加上計數器於五秒後自動再隱藏。
1. 在StoryBoard加入Tap Gesture的元件
2. 在mainViewController.h加入Timer變數及Tap的IBOutlet
#import <UIKit/UIKit.h>
@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIToolbar *myToolBar;
@property (weak, nonatomic) IBOutlet UINavigationBar *myNavBar;
@property (strong, nonatomic) IBOutlet UITapGestureRecognizer *myTap;
@property (nonatomic) NSTimer *timer;
@property (nonatomic) int nowTime;
@property (nonatomic) int lastTime;
@end
3. 在mainViewController.m加入所需的控制碼,Timer及Tap的IBAction
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize myNavBar, myToolBar;
@synthesize timer;
@synthesize nowTime;
@synthesize lastTime;
#define CountDownTime 500 // 5 sec
#define TimeUnin 1 // 1 sec
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myNavBar.hidden = YES;
myToolBar.hidden = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)myTapAction:(id)sender {
myNavBar.hidden = NO;
myToolBar.hidden = NO;
lastTime = CountDownTime;
nowTime = lastTime;
[self initTimer];
}
- (void) initTimer
{
nowTime = lastTime;
timer = [NSTimer scheduledTimerWithTimeInterval:TimeUnin target:self selector:@selector(showCount) userInfo:nil repeats:YES];
}
- (void)showCount {
if (nowTime > 0)
{
nowTime-- ;
}
else {
[timer invalidate];
timer = nil;
myNavBar.hidden = YES;
myToolBar.hidden = YES;
}
}
@end
並加上計數器於五秒後自動再隱藏。
1. 在StoryBoard加入Tap Gesture的元件
2. 在mainViewController.h加入Timer變數及Tap的IBOutlet
#import <UIKit/UIKit.h>
@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIToolbar *myToolBar;
@property (weak, nonatomic) IBOutlet UINavigationBar *myNavBar;
@property (strong, nonatomic) IBOutlet UITapGestureRecognizer *myTap;
@property (nonatomic) NSTimer *timer;
@property (nonatomic) int nowTime;
@property (nonatomic) int lastTime;
@end
3. 在mainViewController.m加入所需的控制碼,Timer及Tap的IBAction
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize myNavBar, myToolBar;
@synthesize timer;
@synthesize nowTime;
@synthesize lastTime;
#define CountDownTime 500 // 5 sec
#define TimeUnin 1 // 1 sec
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myNavBar.hidden = YES;
myToolBar.hidden = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)myTapAction:(id)sender {
myNavBar.hidden = NO;
myToolBar.hidden = NO;
lastTime = CountDownTime;
nowTime = lastTime;
[self initTimer];
}
- (void) initTimer
{
nowTime = lastTime;
timer = [NSTimer scheduledTimerWithTimeInterval:TimeUnin target:self selector:@selector(showCount) userInfo:nil repeats:YES];
}
- (void)showCount {
if (nowTime > 0)
{
nowTime-- ;
}
else {
[timer invalidate];
timer = nil;
myNavBar.hidden = YES;
myToolBar.hidden = YES;
}
}
@end
NSTimer基本使用
一個Timer倒數計時,有暫停及重置功能,供Timer基本功能測試
Xcode版本4.5 & IOS6
1. 開一個新專案
2. 基本檔案列表
3. StoryBoard Layout
4. timerViewController.h基本變數及元件設定
#import <UIKit/UIKit.h>
@interface timerViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@property (weak, nonatomic) IBOutlet UIButton *countDownButton;
@property (weak, nonatomic) IBOutlet UIButton *resetButton;
@property (nonatomic) NSTimer *timer;
@property (nonatomic) int min;
@property (nonatomic) int sec;
@property (nonatomic) int minSec;
@property (nonatomic) int nowTime;
@property (nonatomic) int lastTime;
@end
5. timerViewController.m主程式碼設計
#import "timerViewController.h"
@interface timerViewController ()
@end
#define CountDownTime 10000
#define TimeUnin 0.01 // 0.01 sec
@implementation timerViewController
@synthesize timerLabel;
@synthesize countDownButton;
@synthesize timer;
@synthesize min;
@synthesize sec;
@synthesize minSec;
@synthesize nowTime;
@synthesize lastTime;
BOOL isRunning = NO;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lastTime = CountDownTime;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)countDown:(id)sender {
if (isRunning == NO) {
isRunning = YES;
[self init_timer];
[countDownButton setTitle:@"暫停" forState:UIControlStateNormal];
}
else {
isRunning = NO;
lastTime = nowTime;
[timer invalidate];
timer = nil;
[countDownButton setTitle:@"計數" forState:UIControlStateNormal];
}
}
- (void) showNowTime
{
min = nowTime / 6000;
sec = (nowTime % 6000) /100 ;
minSec = nowTime %100;
timerLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", min, sec, minSec];
}
- (void) init_timer
{
nowTime = lastTime;
timer = [NSTimer scheduledTimerWithTimeInterval:TimeUnin target:self selector:@selector(GameTimer_Count) userInfo:nil repeats:YES];
}
- (void)GameTimer_Count {
if (nowTime > 0)
{
nowTime-- ;
[self showNowTime];
}
else
nowTime = CountDownTime;
}
- (IBAction)reset:(id)sender {
lastTime = CountDownTime;
[countDownButton setTitle:@"倒數計數" forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
isRunning = NO;
nowTime = lastTime;
[self showNowTime];
}
6. 結果圖,分別表示,起始/啟動/暫停狀態
Xcode版本4.5 & IOS6
1. 開一個新專案
2. 基本檔案列表
3. StoryBoard Layout
4. timerViewController.h基本變數及元件設定
#import <UIKit/UIKit.h>
@interface timerViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@property (weak, nonatomic) IBOutlet UIButton *countDownButton;
@property (weak, nonatomic) IBOutlet UIButton *resetButton;
@property (nonatomic) NSTimer *timer;
@property (nonatomic) int min;
@property (nonatomic) int sec;
@property (nonatomic) int minSec;
@property (nonatomic) int nowTime;
@property (nonatomic) int lastTime;
@end
5. timerViewController.m主程式碼設計
#import "timerViewController.h"
@interface timerViewController ()
@end
#define CountDownTime 10000
#define TimeUnin 0.01 // 0.01 sec
@implementation timerViewController
@synthesize timerLabel;
@synthesize countDownButton;
@synthesize timer;
@synthesize min;
@synthesize sec;
@synthesize minSec;
@synthesize nowTime;
@synthesize lastTime;
BOOL isRunning = NO;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lastTime = CountDownTime;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)countDown:(id)sender {
if (isRunning == NO) {
isRunning = YES;
[self init_timer];
[countDownButton setTitle:@"暫停" forState:UIControlStateNormal];
}
else {
isRunning = NO;
lastTime = nowTime;
[timer invalidate];
timer = nil;
[countDownButton setTitle:@"計數" forState:UIControlStateNormal];
}
}
- (void) showNowTime
{
min = nowTime / 6000;
sec = (nowTime % 6000) /100 ;
minSec = nowTime %100;
timerLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", min, sec, minSec];
}
- (void) init_timer
{
nowTime = lastTime;
timer = [NSTimer scheduledTimerWithTimeInterval:TimeUnin target:self selector:@selector(GameTimer_Count) userInfo:nil repeats:YES];
}
- (void)GameTimer_Count {
if (nowTime > 0)
{
nowTime-- ;
[self showNowTime];
}
else
nowTime = CountDownTime;
}
- (IBAction)reset:(id)sender {
lastTime = CountDownTime;
[countDownButton setTitle:@"倒數計數" forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
isRunning = NO;
nowTime = lastTime;
[self showNowTime];
}
6. 結果圖,分別表示,起始/啟動/暫停狀態
2012年9月25日 星期二
UIToolBar的基本使用(ㄧ)
一個最基本的ToolBar的使用,並聯合一個TableView來做Menu的顯示
Xcode版本4.5 & IOS6
1. 開一個基本的專案
2. 加入一個新的class 檔給TableView使用
3. 設定View為Landscape,並加入一個Toolbar
4. 加入一個NavigationBar
5. 對NavigationBar加入一個BarItem,並將Item命名為Menu
6. 對ToolBar加入一個BarItem,並將Item命名為ToolMenu
7. 加入一個UITableView Controller
8.對兩個所增加的BarItem,各拉一個Segue到TableView Controller,並設定為popover
9. 將TableView Controller設定給tableViewController.h /m檔案
10. 對tableViewController.h,加入Table變數
#import <UIKit/UIKit.h>
@interface tableViewController : UITableViewController
@property (retain) NSArray *myData;
@end
11.對tableViewController.m,加入所需程式碼
#import "tableViewController.h"
@interface tableViewController ()
@end
@implementation tableViewController
@synthesize myData;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
myData = [[NSArray alloc] initWithObjects:@"First",@"Second",@"Third",@"Fourth",@"Fifth", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
//return 0;
return [myData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = (NSString *)[myData objectAtIndex:indexPath.row];
return cell;
}
11. 執行結果,因為兩個BarItem都指向同一個TableView,因此結果都顯示相同的TableView,但是所指向的啓動位置,會不一樣。
2012年9月24日 星期一
UIScrollView進階使用(二)
一個音樂ebook的實作範例,文字會配合音樂來做移動,一個最基本的iPad App設計
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. 結果如下圖,經文會配合聲音滑動
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. 結果如下圖,經文會配合聲音滑動
解析度參考表 for IOS App
參考網址: http://www.kdzone.net/xcode-ios5-1-new-ipad-ipad3-%E8%A7%A3%E6%9E%90%E5%BA%A6%E5%8F%83%E8%80%83%E8%A1%A8/
| Device/Screen | File Name (PNG) | Icon Size (pixels) |
| iPhone and iPod | ||
| Application Icon for iPhone (retina display) | Icon@2x.png | 114 x 114 |
| Application Icon icon for iPhone | Icon.png | 57 x 57 |
| Settings/Spotlight icon for iPhone (retina display) | Icon-Small@2x.png | 58 x 58 |
| Settings/Spotlight icon for iPhone | Icon-Small.png | 29 x 29 |
| Launch image Portrait (retina display) | Default@2x.png | 640 x 960 |
| Launch image Portrait | Default.png | 320 x 480 |
| iPad | ||
| Application Icon for the new iPad (retina display) | Icon-72@2x.png | 144 x 144 |
| Application Icon for the iPad | Icon-72.png | 72 x 72 |
| Settings/Spotlight icon for iPad | Icon-Small-50@2x.png | 100 x 100 |
| Settings/Spotlight icon for iPad | Icon-Small-50.png | 50 x 50 |
| Launch image Portrait (retina display) | Default-Portrait@2x.png | 1536 x 2008 |
| Launch image Portrait | Default-Portrait.png | 768 x 1004 |
| Launch image Landscape (retina display) | Default-Landscape@2x.png | 2048 x 1496 |
| Launch image Landscape | Default-Landscape.png | 1024 x 748 |
| iTunes App Store | ||
| App icon for the App Store (retina display) | iTunesArtwork@2x.png | 1024 x 1024 |
| App icon for the App Store | iTunesArtwork.png | 512 x 512 |
訂閱:
文章 (Atom)


































