1,擷取翻轉事件,并開啟翻轉:
隻要在viewcontroller的類中加入
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
//翻轉後要執行的代碼
return YES;
}
2,-(void)viewWillAppear:(BOOL)animated,- (void)viewDidLoad 的差別。
viewwillappear是每次視圖控制器的視圖出現前執行的代碼。而viewdidload是每次視圖控制器載入是執行的代碼。
比如說:當a視圖控制器的視圖第一次出現是兩個都要執行,但當a被push後有pop回來時,隻有viewwillappear執行。
3,如何讓視圖始終跟着手指移動,并有反彈事件
xsum=photopositon.origin.x+photopositon.size.width/2-touchstart.x;
ysum=photopositon.origin.y+photopositon.size.height/2-touchstart.y;
currentview.center=CGPointMake(xsum+p.x, ysum+p.y);
if (pow(currentview.center.x-160,2.0)>pow(photopositon.size.width/2,2.0)||
pow(currentview.center.y-240,2.0)>pow(photopositon.size.height/2, 2.0))
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
currentview.center=CGPointMake(160, 240);
[UIView commitAnimations];
}
就是讓currentview的視圖中心始終與手指保持一定的方位。
4,控制導覽列和toolbar
[self.navigationController ...]
[self.navigationController.toolbar ...]
比如說讓他們都消失:
[self.navigationController setToolbarItems:NULL animated:YES];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackTranslucent];
[self.navigationController setToolbarHidden:NO animated:YES];
5,自定義控件事件:
addTarget:self action:@selector(....) forControlEvents:....
比如擷取某個按鈕的觸摸事件;
[new addTarget:self action:@selector(onChooseItem:) forControlEvents:UIControlEventTouchUpInside]
則當按鈕按下時,- (void)onChooseItem:(id) sender 就會被調用。
sender傳的就是被按下按鈕的指針。
6.擷取檔案的路徑,即擷取documents的路徑
//擷取檔案路徑
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath=[path objectAtIndex:0];
NSLog(@"%@",documentsPath);
補充:
1. NSSearchPathForDirectoriesInDomains和NSHomeDirectory
iPhone和symbian 3rd一樣,會為每一個應用程式生成一個私有目錄,這個目錄位于/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,并随即生成一個數字字母串作為目錄名,在每一次應用程式啟動時,這個字母數字串都是不同于上一次。
通常使用Documents目錄進行資料持久化的儲存,而這個Documents目錄可以通過 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES) 得到,代碼如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *path = [documentsDirectory stringByAppendingPathComponent:@"aa.plist"];
NSLog(@"path: %@",path);
列印結果如下:
path: /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7/Documents
而這個目錄還可以通過 NSHomeDirectory()來得到,代碼如下:
NSString *destPath = NSHomeDirectory();
NSLog(@"path: %@",destPath);
//destPath = [destPath stringByAppendingPathComponent: @"Documents"];
//NSString *xmlpath = [destPath stringByAppendingPathComponent: @"menu/menu.xml"];
列印結果如下:
path: /Users/apple/Library/Application Support/iPhone Simulator/4.2/Applications/6F4BC466-C5D6-440C-BAAC-BE20FA468C61
看看兩者列印出來的結果,我們可以看出這兩種方法的不同。
2. 浏覽document下所有圖檔資源
#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
NSArray *fileList = [[[NSFileManager defaultManager] directoryContentsAtPath:DOCUMENTS_FOLDER]
pathsMatchingExtensions:[NSArray arrayWithObject:@"png"]] ;
3. 得到圖檔中的某一部分:
UIImage *image = [UIImage imageNamed:filename];
CGImageRef imageRef = image.CGImage;
CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);
CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];
7.在documents的路徑下建立檔案。
首先要擷取documents的路徑,如上第6條。
其次就是下面的語句了:
NSString *writePath=[NSString stringWithFormat:@"%@/%@.txt",documentsPath,@"aaa"];
NSData *data = [@"" dataUsingEncoding:NSUTF8StringEncoding];//新檔案的初始資料,設為空
[[NSFileManager defaultManager] createFileAtPath:writePath contents:data attributes:nil];//建立檔案的指令在這裡
這樣就可以在documents檔案夾下建立一個aaa.txt的檔案了。哈哈哈哈哈。
或者是用下面的語句,
NSString *writePath=[NSString stringWithFormat:@"%@/%@.txt",documentsDirectory,@"bbb"];
NSError *error;
[@"fasdfasdasddaa" writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
這樣就可以在documents檔案夾下建立一個bbb.txt的檔案了。并且,檔案中的有"fasdfasdasddaa"字元。
總結起來就是,先給要存儲的東西取一個名字,然後,找到它的路徑。然後以這個名字建立。建立的時候可以添加内容。
當然,圖檔也可以批量的生成,假如說讓你把一張圖檔複制500遍,并且給他按照(1-500)重命名。
你可以用上面的方法,用一個循環批量的生成500張圖檔。
UIImage *image = [UIImage imageNamed:@"240*360.png"];
NSData *data = UIImagePNGRepresentation(image);
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableArray *muArray;
NSString *filePath = nil;
for (int j = 1; j<=500; j++) {
filePath = [[NSString alloc] initWithFormat:@"%@/%d.png",documentPath,j];
[data writeToFile:filePath atomically:YES];
NSString *newPath = [[NSString alloc] initWithFormat:@"%@",documentPath];
muArray = [[NSMutableArray alloc] initWithContentsOfFile:newPath];
[muArray addObject:filePath];
[filePath release];
}
7.iphone開發中随機數的産生。
// Get random number between 0 and 99
int x = arc4random() % 81;
// Get random number between 500 and 999
int y = ((arc4random()%501)+500);
NSLog(@"0--99之間的随機數%d",x);
NSLog(@"500--999之間的随機數%d",y);
8.好看的文字處理
以tableView中cell的textLabel為例子:
cell.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
//設定文字的字型
cell.textLabel.font = [UIFont fontWithName:@"American Typewriter" size:100.0f];
//設定文字的顔色
cell.textLabel.textColor = [UIColor orangeColor];
//設定文字的背景顔色
cell.textLabel.shadowColor = [UIColor whiteColor];
//設定文字的顯示位置
cell.textLabel.textAlignment = UITextAlignmentCenter;
原文連結:http://www.cnblogs.com/iphone520/archive/2011/11/28/2225160.html