IOS 开发技巧
2014-04-18 19:40
267 查看
1、获取全局的Delegate对象,这样我们可以调用这个对象里的方法和变量:
[(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable];
2、获得程序的主Bundle:
NSBundle *bundle = [NSBundle mainBundle];
Bundle可以理解成一种文件夹,其内容遵循特定的框架。
Main Bundle一种主要用途是使用程序中的资源文件,如图片、声音、plst文件等。
NSURL *plistURL = [bundle URLForResource:@"plistFile" withExtension:@"plist"];
上面的代码获得plistFile.plist文件的路径。
3、在程序中播放声音:
首先在程序添加AudioToolbox:
其次,在有播放声音方法的.m方法添加#import:
#import<AudioToolbox/AudioToolbox.h>
接下来,播放声音的代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
4、设置和获取类中属性值:
[self setValue: 变量值 forKey: 变量名];
[self valueForKey: 变量名];
5、让某一方法在未来某段时间之后执行:
[self performSelector:@selector(方法名) withObject:nil afterDelay:延迟时间(s)];
6、获得设备版本号:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
7、捕捉程序关闭或者进入后台事件:
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
applicationWillResignActive:这个方法中添加想要的操作
8、查看设备支持的字体:
for (NSString *family in [UIFont familyNames]) {
NSLog(@"%@", family);
for (NSString *font in [UIFont fontNamesForFamilyName:family]) {
NSLog(@"\t%@", font);
}
}
9、为UIImageView添加单击事件:
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
[imageView addGestureRecognizer:singleTap];
10、添加多语言支持:
比如Image Picker这样的组件,它上面的按钮的文字是随着设备语言环境的改变而改变的,但是要先在工程添加语言:
11、使程序支持iTunes这样的设备,比如可以使用PC端的工具往程序的Documents中拖放文件:
12、页面切换效果设置:
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
可供使用的效果:
UIModalTransitionStyleCoverVertical //新视图从下向上出现
UIModalTransitionStyleFlipHorizontal //以设备的长轴为中心翻转出现
UIModalTransitionStyleCrossDissolve //渐渐显示
UIModalTransitionStylePartialCurl //原视图向上卷起
恢复之前的页面:
[self dismissModalViewControllerAnimated:YES];
13、获取截屏
- (UIImage *)getScreenShot {
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
[(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable];
2、获得程序的主Bundle:
NSBundle *bundle = [NSBundle mainBundle];
Bundle可以理解成一种文件夹,其内容遵循特定的框架。
Main Bundle一种主要用途是使用程序中的资源文件,如图片、声音、plst文件等。
NSURL *plistURL = [bundle URLForResource:@"plistFile" withExtension:@"plist"];
上面的代码获得plistFile.plist文件的路径。
3、在程序中播放声音:
首先在程序添加AudioToolbox:
其次,在有播放声音方法的.m方法添加#import:
#import<AudioToolbox/AudioToolbox.h>
接下来,播放声音的代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
4、设置和获取类中属性值:
[self setValue: 变量值 forKey: 变量名];
[self valueForKey: 变量名];
5、让某一方法在未来某段时间之后执行:
[self performSelector:@selector(方法名) withObject:nil afterDelay:延迟时间(s)];
6、获得设备版本号:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
7、捕捉程序关闭或者进入后台事件:
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
applicationWillResignActive:这个方法中添加想要的操作
8、查看设备支持的字体:
for (NSString *family in [UIFont familyNames]) {
NSLog(@"%@", family);
for (NSString *font in [UIFont fontNamesForFamilyName:family]) {
NSLog(@"\t%@", font);
}
}
9、为UIImageView添加单击事件:
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
[imageView addGestureRecognizer:singleTap];
10、添加多语言支持:
比如Image Picker这样的组件,它上面的按钮的文字是随着设备语言环境的改变而改变的,但是要先在工程添加语言:
11、使程序支持iTunes这样的设备,比如可以使用PC端的工具往程序的Documents中拖放文件:
12、页面切换效果设置:
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
可供使用的效果:
UIModalTransitionStyleCoverVertical //新视图从下向上出现
UIModalTransitionStyleFlipHorizontal //以设备的长轴为中心翻转出现
UIModalTransitionStyleCrossDissolve //渐渐显示
UIModalTransitionStylePartialCurl //原视图向上卷起
恢复之前的页面:
[self dismissModalViewControllerAnimated:YES];
13、获取截屏
- (UIImage *)getScreenShot {
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
相关文章推荐
- 6种有效的iOS团队开发技巧
- iOS快速开发技巧:7技巧加快你的开发
- iOS开发技巧之:图片压缩成指定的大小
- iOS 开发小技巧
- iOS开发常见的奇葩技巧,
- iOS开发断点调试高级技巧
- ios开发技巧(一)
- iOS开发技巧 - 使用UISlider来调整值的范围
- ios开发常用技巧
- ios 开发日记 22-iOS 保持界面流畅的技巧
- iOS开发UI篇—IOS开发中Xcode的一些使用技巧
- 不可不知的:iOS开发的22个诡异技巧
- IOS程序设相关计开发技巧
- iOS开发之 -- Xcode 调试技巧 常用命令和断点
- ios开发之技巧总结
- iOS开发技巧--使用CocoaPods创建、删除第三方框架
- iOS开发动态计算cell高度或UILabel多行文本计算高度、宽度的技巧
- 黑马程序员——IOS基础——OC面向对象四开发技巧
- iOS开发实用技巧——如何调用第三方APP