一、獲取全局的Delegate對象,這樣咱們能夠調用這個對象裏的方法和變量: app
- [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable];
二、得到程序的主Bundle: 框架
- NSBundle *bundle = [NSBundle mainBundle];
Bundle能夠理解成一種文件夾,其內容遵循特定的框架。工具
Main Bundle一種主要用途是使用程序中的資源文件,如圖片、聲音、plst文件等。字體
- NSURL *plistURL = [bundle URLForResource:@"plistFile" withExtension:@"plist"];
上面的代碼得到plistFile.plist文件的路徑。url
三、在程序中播放聲音: spa
首先在程序添加AudioToolbox:orm
其次,在有播放聲音方法的.m方法添加#import:server
- #import<AudioToolbox/AudioToolbox.h>
接下來,播放聲音的代碼以下: 對象
- NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"];
- SystemSoundID soundID;
- AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
- AudioServicesPlaySystemSound (soundID);
四、設置和獲取類中屬性值: blog
- [self setValue: 變量值 forKey: 變量名];
- [self valueForKey: 變量名];
五、讓某一方法在將來某段時間以後執行:
- [self performSelector:@selector(方法名) withObject:nil afterDelay:延遲時間(s)];
六、得到設備版本號:
- float version = [[[UIDevice currentDevice] systemVersion] floatValue];
七、捕捉程序關閉或者進入後臺事件:
- UIApplication *app = [UIApplication sharedApplication];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
applicationWillResignActive:這個方法中添加想要的操做
八、查看設備支持的字體:
- for (NSString *family in [UIFont familyNames]) {
- NSLog(@"%@", family);
- for (NSString *font in [UIFont fontNamesForFamilyName:family]) {
- NSLog(@"\t%@", font);
- }
- }
九、爲UIImageView添加單擊事件:
- imageView.userInteractionEnabled = YES;
- UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
- [imageView addGestureRecognizer:singleTap];
十、添加多語言支持:
好比Image Picker這樣的組件,它上面的按鈕的文字是隨着設備語言環境的改變而改變的,可是要先在工程添加語言:
十一、使程序支持iTunes這樣的設備,好比可使用PC端的工具往程序的Documents中拖放文件:
十二、頁面切換效果設置:
- controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
- [self presentModalViewController:controller animated:YES];
可供使用的效果:
- UIModalTransitionStyleCoverVertical //新視圖從下向上出現
- UIModalTransitionStyleFlipHorizontal //以設備的長軸爲中心翻轉出現
- UIModalTransitionStyleCrossDissolve //漸漸顯示
- UIModalTransitionStylePartialCurl //原視圖向上捲起
恢復以前的頁面:
- [self dismissModalViewControllerAnimated:YES];
1三、獲取截屏
- - (UIImage *)getScreenShot {
- UIGraphicsBeginImageContext(self.view.bounds.size);
- [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }