iOS开发13:小技巧积累

iOS开发13:小技巧积累,第1张

概述1、获取全局的Delegate对象,这样我们可以调用这个对象里的方法和变量: [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable]; 2、获得程序的主Bundle: NSBundle *bundle = [NSBundle mainBundle]; Bundle可以理解成一种文件夹,其

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;}
总结

以上是内存溢出为你收集整理的iOS开发13:小技巧积累全部内容,希望文章能够帮你解决iOS开发13:小技巧积累所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1092046.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存