iOS开发之小技巧积累

移动开发 iOS
没有奇迹,只有努力。我们成长在如此无奈而又相似的人生中,或许我们该真的活一次,即便是失败,至少这种成就感和真实感是很难得的。通过日积月累的技术成长,总有一天你也是个大咔。

1、获取全局的Delegate对象,这样我们可以调用这个对象里的方法和变量:

  1. [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable]; 

2、获得程序的主Bundle:

  1. NSBundle *bundle = [NSBundle mainBundle]; 

Bundle可以理解成一种文件夹,其内容遵循特定的框架。

Main Bundle一种主要用途是使用程序中的资源文件,如图片、声音、plst文件等。

  1. NSURL *plistURL = [bundle URLForResource:@"plistFile" withExtension:@"plist"]; 

上面的代码获得plistFile.plist文件的路径。

3、在程序中播放声音:

首先在程序添加AudioToolbox:

其次,在有播放声音方法的.m方法添加#import:

  1. #import<AudioToolbox/AudioToolbox.h> 

接下来,播放声音的代码如下:

  1. NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"];  
  2. SystemSoundID soundID;  
  3. AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);  
  4. AudioServicesPlaySystemSound (soundID);  

4、设置和获取类中属性值:

  1. [self setValue: 变量值 forKey: 变量名]; 
  2. [self valueForKey: 变量名]; 

5、让某一方法在未来某段时间之后执行:

  1. [self performSelector:@selector(方法名) withObject:nil afterDelay:延迟时间(s)];  

6、获得设备版本号:

  1. float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 

7、捕捉程序关闭或者进入后台事件:

  1. UIApplication *app = [UIApplication sharedApplication]; 
  2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; 

applicationWillResignActive:这个方法中添加想要的操作

8、查看设备支持的字体:

  1. for (NSString *family in [UIFont familyNames]) { 
  2.     NSLog(@"%@", family); 
  3.     for (NSString *font in [UIFont fontNamesForFamilyName:family]) { 
  4.         NSLog(@"\t%@", font); 
  5.     } 

9、为UIImageView添加单击事件:

  1. imageView.userInteractionEnabled = YES; 
  2. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)]; 
  3. [imageView addGestureRecognizer:singleTap]; 

10、添加多语言支持:
比如Image Picker这样的组件,它上面的按钮的文字是随着设备语言环境的改变而改变的,但是要先在工程添加语言:

11、使程序支持iTunes这样的设备,比如可以使用PC端的工具往程序的Documents中拖放文件:

12、页面切换效果设置:

  1. controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
  2. [self presentModalViewController:controller animated:YES]; 

可供使用的效果:

  1. UIModalTransitionStyleCoverVertical  //新视图从下向上出现 
  2. UIModalTransitionStyleFlipHorizontal //以设备的长轴为中心翻转出现 
  3. UIModalTransitionStyleCrossDissolve  //渐渐显示 
  4. UIModalTransitionStylePartialCurl    //原视图向上卷起 

恢复之前的页面:

  1. [self dismissModalViewControllerAnimated:YES]; 

13、获取截屏

  1. - (UIImage *)getScreenShot { 
  2.     UIGraphicsBeginImageContext(self.view.bounds.size); 
  3.     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
  4.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
  5.     UIGraphicsEndImageContext(); 
  6.     return image; 
责任编辑:闫佳明 来源: oschina
相关推荐

2015-03-23 09:44:55

iOS开发技巧

2014-08-04 17:46:15

NavBarTarBar

2013-07-29 04:46:48

iOS开发iOS开发学习iOS小知识

2011-02-21 17:15:14

SilverlightNEY

2013-06-20 11:04:46

iOS技巧NotificatioBadgeView

2012-08-16 10:35:50

Windows Pho

2015-02-04 10:32:57

Objective-CSwift

2015-08-10 09:50:21

ios图片文本

2015-07-20 09:16:42

iOSWatchKit开发

2023-05-15 08:18:21

CSS技巧代码

2014-07-23 13:17:53

iOSUITextField

2014-07-21 14:49:35

iOSUILabel

2015-08-27 11:16:14

ios开发技巧

2011-04-02 08:39:27

Visual Stud

2011-03-23 16:24:44

LAMPMySQL

2015-07-27 09:36:09

storyboard

2016-12-28 13:19:08

Android开发坑和小技巧

2023-01-03 09:00:52

React前端

2011-08-08 17:05:02

XCode UserScript 脚本

2011-08-02 11:07:42

iOS开发 UIWebView
点赞
收藏

51CTO技术栈公众号