下面是其它主要方法的介绍:
drawFrame方法主要作用是判断照相机是否拍照,然后加载机器人的跟踪配置信息,使得拍照后机器人的方位不变;
onAnimationEnd主要作用是当机器人的动画播放完后调用此方法播放后续动画;
接着就是覆盖UIResponds类里的Touch方法组了,包括touchsBegin()、touchsMoved()、touchsEnded();
首先来看touchsBegin(),它的作用就是在手指开始在屏幕上选择的的时候确定选择的3D模型是哪个;首先它会获得在屏幕上的触碰对象:
//获取触碰对象 UITouch *touch = [touches anyObject]; 然后对获取的触碰对象的坐标进行获取: //记录下它在屏幕上的坐标 CGPoint loc = [touch locationInView:glView]; 再测算当前屏幕的缩放因子: //记录下缩放因子 float scale = glView.contentScaleFactor;
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // record the initial states of the geometries with the gesture handler //记录开始手势前3D模型的状态 [m_gestureHandler touchesBegan:touches withEvent:event withView:glView]; // Here's how to pick a geometry //获取触碰对象 UITouch *touch = [touches anyObject]; //记录下它在屏幕上的坐标 CGPoint loc = [touch locationInView:glView]; // get the scale factor (will be 2 for retina screens) //记录下缩放因子 float scale = glView.contentScaleFactor; // ask sdk if the user picked an object(返回在loc坐标下的3D模型) // the 'true' flag tells sdk to actually use the vertices for a hit-test, instead of just the bounding box if (model == m_metaioMan) { // we have touched the metaio man // let's start an animation model->startAnimation( "shock_down" , false); } }
- (IBAction)onTVButtonClick:(id)sender { //这一步很关键,将sender转化为button类型 UIButton* button = (UIButton*)sender; // if the button is already selected, change its state to unselected, and hide the geometry //这里是将button设置成为双色跳变的模式,selected是button本身的属性 if ([button isSelected]) { button.selected = false; [self setVisibleTV:false]; } else { button.selected = true; [self setVisibleTV:true]; // reset the location of the geometry //重置3D模型的方位 CGRect screen = self.view.bounds; CGFloat width = screen.size.width * [UIScreen mainScreen].scale; CGFloat height = screen.size.height * [UIScreen mainScreen].scale; metaio::Vector3d translation = m_metaioSDK->get3DPositionFromScreenCoordinates(1, metaio::Vector2d(width/2, height/2)); m_tv->setTranslation(translation); m_screen->setTranslation(translation); m_screen->startMovieTexture(); // reset the scale of the geometry m_tv->setScale(metaio::Vector3d(50.0, 50.0, 50.0)); m_screen->setScale(metaio::Vector3d(50.0, 50.0, 50.0)); } }
下面就是一些简单的方法实现,
1、拍照方法
- (IBAction)onTakePicture:(id)sender { NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* filePath = [NSString stringWithFormat:@"%@/targetImage.jpg", dir]; m_metaioSDK->requestCameraImage([filePath UTF8String]); // m_metaioSDK->requestCameraImage(); }