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
接下来,播放声音的代码如下:
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];
via Devdiv
***************************** 视频、音频调取 *********************
1.iOS视频播放代码(添加MediaPlayer.framework和#import)
-(void)playMovie:(NSString *)fileName{
//视频文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];
//视频URL
NSURL *url = [NSURL fileURLWithPath:path];
//视频播放对象
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
movie.controlStyle = MPMovieControlStyleFullscreen;
[movie.view setFrame:self.view.bounds];
movie.initialPlaybackTime = -1;
[self.view addSubview:movie.view];
// 注册一个播放结束的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:movie];
[movie play];
}
#pragma mark -------------------视频播放结束委托--------------------
-(void)myMovieFinishedCallback:(NSNotification*)notify
{
//视频播放对象
MPMoviePlayerController* theMovie = [notify object];
//销毁播放通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie.view removeFromSuperview];
// 释放视频对象
[theMovie release];
}
2.播放背景音乐,利用类进行播放《《《背景音乐播放 支持mp3格式 循环播放长音乐这种播放音乐的方式导入框架#import;#import{
AVAudioPlayer *myBackMusic;
}
//上边的步骤很重要,必须在h文件中实例化。不知道为什么,直接在M文件中实例化,会播不出来声音。
下边是M文件中的
-(void)viewDidLoad
{
NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"]; //创建音乐文件路径
NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
//创建播放器
myBackMusic = thePlayer; //赋值给自己定义的类变量
[musicURL release];
[thePlayer release];
[myBackMusic prepareToPlay];
[myBackMusic setVolume:1]; //设置音量大小
myBackMusic.numberOfLoops = -1;//设置音乐播放次数 -1为一直循环 ,将音频播放器的numberOfLoops属性设为负数使得播放无限循环
NSLog(@"%f seconds played so far", audioPlayer.currentTime); //查看播放的初始时间,也就是从多少秒开始播放
audioPlayer.currentTime = 10; // jump to the 10 second mark //设置播放开始的时间
[myBackMusic play]; //播放
[myBackMusic pause];
[myBackMusic stop];
}
3.iOS播放一段声音(添加AudioToolbox.framework和#import)
《《《主要用来播放一段声音,比如点击的声音,敲打
其次,在有播放声音方法的.m方法添加#import:
#import
接下来,播放声音的代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
网友评论