iOS开发:小技巧积累


4人收藏此文章, 我要收藏发表于2个月前 , 已有 391次阅读 共 0个评论

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];

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值