metaio是德国的一家增强现实公司,它开发的增强现实sdk具有诸多优点,最近在做一个类似的项目,选用了它的SDK,根据下载下来的SDK包中的教程7(Tutorial7)
我将在接下来的几章中详细介绍其实现原理,和一些基本的IOS开发知识;废话不多说,开始metaio增强现实之旅
具体流程:
1、首先在AppDelegate上修改didFinishLaunchingWithOptions:方法。增强现实
此方法当然是在启动时完成的一些操作;然后隐藏状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
由于该应用为自适应的,所以载入程序时需要判断一下设备是ipad或iphone
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
当然判断完后要载入相应的视图控制对象,这里通过initwithNibName方法将viewController用IB创建的视图控制对象(如ViewController_iPhone
)初始化,ViewController_iPhone如下图
可以看到它是包含了一个UIWebView的;这个稍后就介绍;
2、编写ViewController类,新建类就省去了,增强现实
在ViewController.h中
@interface ViewController : UIViewController <UIWebViewDelegate>
@property (unsafe_unretained, nonatomic) IBOutlet UIWebView *tutorialsView;
由于在nib文件中有UIWebView视图,所以需要遵守UIWebViewDelegate委托协议实现相关功能,在.h文件中声明一个UIWebView类型插座,用于和nib文件对象连接到一起,连接过程就不说了;
在ViewController.mm中
- (void)viewWillAppear:(BOOL)animated {
//NSBundle通过mainBundle方法获取应用程序更路径,在tutorialContent_crossplatform/Menu目录下查找index.html,
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"tutorialContent_crossplatform/Menu"];
NSURL *url = [NSURL fileURLWithPath:htmlPath];
//根据url创建并返回一个request对象
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//发起一个异步请求连接到制定url
[tutorialsView loadRequest:requestObj];
}
viewwillAppear:方法是在视图将要显示的时候调用,这个时候应该是载入相应的html页面,具体内容代码里写的很详细,此段代码载入了index.html,(注意,方法pathForResource:ofType:inDirectory方法中inDirectory的路径一定要在
实现
代码相对简单,就不细说了; 注释我都写的很详细了,就不再讲解了;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
委托方法,该方法主要是让用户根据用户点击连接的request来load视图页面,其中一段方法如下
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//获取request的路径
NSURL* theURL = [request mainDocumentURL];
//转换成string
NSString* absoluteString = [theURL absoluteString];
//取出=号后面的数字
tutorialId = [[absoluteString componentsSeparatedByString:@"="] lastObject];
//如果有前缀为metaiosdkexample://
if ([[absoluteString lowercaseString] hasPrefix:@"metaiosdkexample://"])
{
if( tutorialId )
{
xibFile = [NSString stringWithFormat:@"Tutorial%@",tutorialId];
if ([tutorialId isEqualToString:@"1"])
{
//根据NibName初始化对应的视图控制对象
Tutorial1ViewController* tutorialViewController = [[Tutorial1ViewController alloc] initWithNibName:xibFile bundle:nil];
//设置视图控制对象的模态显示变换效果为渐变
tutorialViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//设置以模态视图控制对象显示tutorialViewController
[self presentModalViewController:tutorialViewController animated:YES];
//[tutorialViewController release];
}
当然,最后在视图加载完了后,也就是viewDidLoad方法中,由于遵守了UIWebViewDelegate协议的ViewController类实现了委托方法webView,所以设置UIWebView的delegate属性为ViewController自己,这样的话就可以调用自己覆盖的webView方法了;代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tutorialsView.delegate = self;
}
自动转屏代码实现如下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//只有当iphone时朝下不自动转屏
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
代码相对简单,就不细说了;
总结:至此,我们可以实现从:AppDelegate中初始化ViewController-->ViewController中ViewwillAppear显示index.html主页-->针对UIWebView的链接点击的处理通过webview方法来实现-->跳转显示指定的nib文件,增强现实
下一章将介绍具体AR效果的实现!