前言
最近在做個房地產(chǎn)的項目街州,需要有全景的效果晴音。在網(wǎng)上google了一番,發(fā)現(xiàn)了PanoramaGL這個庫氯窍,但是好久沒更新了饲常。嘗試著拖到工程中用了下,更改了一些PLConstants.h的最大圖片尺寸的參數(shù)狼讨,便可以跑起來了贝淤。
但是
不幸的是,設(shè)置scrollEnable=YES后政供,滑動一下播聪,便會一直不停的旋轉(zhuǎn)。因為滑一下布隔,在PLViewBase中离陶,就會啟動個timer/CADisplaylink進(jìn)行刷新,不斷調(diào)用drawViewInternally执泰。后來想了個粗暴的辦法枕磁,就是在drawViewInternally里面,延遲0.4s將其stop术吝。但是這樣一來计济,停的有點突然,不夠平滑排苍。╮(╯▽╰)╭沦寂,又不知道怎么去改源碼,所以尋找另外的解決方法淘衙。
新發(fā)現(xiàn)
還是google传藏,發(fā)現(xiàn)有pano2vr可以將全景圖片轉(zhuǎn)換成html,并且切換的很平順彤守。然后在app里面毯侦,弄個webview就夠了。要吐槽一下的是具垫,它是個付費軟件侈离,沒找到mac的破解版,而mac的試用版筝蚕,會打水印卦碾,沒法用铺坞。后來還是切到了win版,下了個破解的洲胖。
簡單實用
用起來還是蠻方便的济榨,將圖片拖進(jìn)去,支持一張全景圖绿映,或者6張立體的擒滑。然后設(shè)置參數(shù),一般都不需要怎么設(shè)置绘梦,我只是將html設(shè)置了全屏橘忱。
還可設(shè)置hotspot,并且可以編輯皮膚卸奉,使用自定義的圖片钝诚。總體還是比較方便的榄棵。視頻1 凝颇,視頻2
qq有一篇講實現(xiàn)全景星球的文章,里面有比較詳細(xì)的對各種全景工具的分析對比疹鳄,可戳這里拧略。
接入到app
其實在這一步,還是有點坎坷的瘪弓。主要是圖片路徑惹的禍垫蛆。因為是以黃色文件夾加進(jìn)去的,所以所有的資源都在mainbundle下面腺怯,沒有層級了袱饭,而它自動生成的xml里面的image路徑,是images/xx.jpg的呛占。所以需要手動修改路徑虑乖,將images/去掉,直接引用xx.jpg晾虑。
還有一點要注意疹味,就是加載本地html時,baseURL的設(shè)定帜篇。需要設(shè)置為[[NSBundle mainBundle] bundleURL]糙捺。如果設(shè)置為nil,會出現(xiàn)提示笙隙。因為它找不js文件和img继找。
This content requires HTML5/CSS3, WebGL, or Adobe Flash Player Version 9 or higher.
NSString *path = [[NSBundle mainBundle] pathForResource:@"A5_cube" ofType:@"html"];
NSString *data = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[_webView loadHTMLString:data baseURL:[[NSBundle mainBundle] bundleURL]];
最后,如果還遇到j(luò)s找不到或沒有執(zhí)行的情況逃沿,請檢查build phase-->compile source里有沒有它的身影婴渡。如有,將其拖至copy bundle resource中凯亮。
不過我用xcode 7.3直接拖進(jìn)去边臼,是正常的。
呼哈哈假消,坑柠并,終于填滿了。
6.25更新
=======
Demo在此
7.25更新
前段時間請教過一個前輩富拗,關(guān)于自動停止滑動的問題臼予。解決方案是設(shè)置一個摩擦因子(0-1),在滑動是不斷的乘以該因子啃沪,以達(dá)到減速的目的粘拾。簡單的貼下代碼吧。
在PLViewBase.m里面创千,drawViewInternally
-(void)drawViewInternally
{
if(scene && !isValidForFov && !isSensorialRotationRunning)
{
PLCamera *camera = scene.currentCamera;
[camera rotateWithVelocity:self.velocity];
if (touchStatus==PLTouchEventTypeEnded) {
//在PLCamera自定義一個轉(zhuǎn)動方法缰雇,手指離開觸摸屏的時候按畫面按最后產(chǎn)生的速度及方向轉(zhuǎn)動
[camera rotateWithVelocity:self.velocity];
}
else {
[camera rotateWithStartPoint:startPoint endPoint:endPoint];
}
if(delegate && [delegate respondsToSelector:@selector(view:didRotateCamera:rotation:)])
[delegate view:self didRotateCamera:camera rotation:[camera getAbsoluteRotation]];
}
if(renderer)
[renderer render];
//利用摩擦因子減速
if (touchStatus==PLTouchEventTypeEnded) {
//摩擦因子
float friction = 0.9;
if (fabs(self.velocity.x)>1||fabs(self.velocity.y)>1) {
self.velocity=CGPointMake(self.velocity.x*friction, self.velocity.y*friction);
}
else
{
[self stopAnimationInternally];
}
}
}
然后self.velocity需要自己計算,在move的時候根據(jù)touch移動的距離除以時間追驴。
- (CGPoint)calculateVelocity:(NSSet *)touches {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGPoint prevLocation = [touch previousLocationInView:self];
CGFloat xDistance = location.x - prevLocation.x;
CGFloat yDistance = location.y - prevLocation.y;
// self.curTimestamp械哟,self.previousTimestamp自己定義
NSTimeInterval time = self.curTimestamp - self.previousTimestamp;
if (time != 0) {
CGFloat xSpeed = xDistance/time;
CGFloat ySpeed = yDistance/time;
return CGPointMake(xSpeed, ySpeed);
}
return CGPointZero;
}
最后在PLObject中添加rotateWithVelocity的方法
-(void)rotateWithVelocity:(CGPoint)velocity
{
float sensitivity = 900;
self.pitch += velocity.y/ sensitivity;
self.yaw += -velocity.x/sensitivity;
}