初做項目,兩次涉及到了360度全景展示(也可以是720的旋轉),查找了很多資料,基本都是用PanoramaGL這個庫實現的,下面我就這兩次用這個庫出現的問題以及應項目需求在庫裏做的改動簡單的總結一下。技術就是前人栽樹,後人乘涼,我老在大樹下乘涼心裏有點過意不去,時不時的也種點小樹苗。
在code4app裏面用到的那個庫其實只是基礎,不完善,靈活度和可操作性都不夠,所以建議大家捨棄這個庫,直接在csdn裏面下載。下載地址:http://download.csdn.net/detail/pingyan88/5697173
PanoramaGL用法:將庫下載下來導入項目中,並添加相關的依賴框架(框架缺一不可),但並不是單純的導入框架就可以了,我自己在第一次導入的時候做了很多修改,那錯誤就像個無底洞,但後來幾次就輕鬆多了,主要是得對照源代碼在
Build Settings 裏進行配置。(由於要修改的地方太多,這裏就不一一列舉出來了。)
因爲,這個庫是12年更新的,所以沒有使用ARC,如果,你引用了它的庫,你會發現ARC錯誤,即使你全部加上-fno-objc-arc,你也會發現**變量的錯誤,網上有改正的方法,但是有的地方,改了還是報錯,而且許多地方都要更改(**,-fno-objc-arc),所以,我選擇建立工程後,把工程修改爲不適用ARC,對自己新建的文件修改,添加-f-objc-arc,使用ARC。這樣即使報錯,雙擊fix一下就編譯通過了。
多說無益,直接上代碼。
- (void)viewDidLoad
{
[super viewDidLoad];
plView = (PLView *)self.view;
plView.delegate = self;
plView.PLdelegate = self;
panorama = nil;
cubicPanorama= [PLCubicPanorama panorama];
//下面就是核心代碼,用6張JPG的圖片來實現。相當於拼成了一個正方體。
[cubicPanorama setTexture:[PLTexture textureWithImage:[PLImage imageWithPath:[[NSBundle mainBundle] pathForResource:@"quito1_f" ofType:@"jpg"]]] face:PLCubeFaceOrientationFront];
[cubicPanorama setTexture:[PLTexture textureWithImage:[PLImage imageWithPath:[[NSBundle mainBundle] pathForResource:@"quito1_b" ofType:@"jpg"]]] face:PLCubeFaceOrientationBack];
[cubicPanorama setTexture:[PLTexture textureWithImage:[PLImage imageWithPath:[[NSBundle mainBundle] pathForResource:@"quito1_l" ofType:@"jpg"]]] face:PLCubeFaceOrientationLeft];
[cubicPanorama setTexture:[PLTexture textureWithImage:[PLImage imageWithPath:[[NSBundle mainBundle] pathForResource:@"quito1_r" ofType:@"jpg"]]] face:PLCubeFaceOrientationRight];
[cubicPanorama setTexture:[PLTexture textureWithImage:[PLImage imageWithPath:[[NSBundle mainBundle] pathForResource:@"quito1_u" ofType:@"jpg"]]] face:PLCubeFaceOrientationUp];
[cubicPanorama setTexture:[PLTexture textureWithImage:[PLImage imageWithPath:[[NSBundle mainBundle] pathForResource:@"quito1_d" ofType:@"jpg"]]] face:PLCubeFaceOrientationDown];
panorama = cubicPanorama;
[plView setPanorama:panorama];
//改變初始化的位置(有些項目需要點擊全景進去是指定的某一個角度)
[panorama.currentCamera lookAtWithPitch:0.0f yaw:40.0f];(具體角度根據需要調整)
currentCamera = panorama.currentCamera;
//添加熱點(這個類可以實現在全景裏面添加按鈕,並且按鈕跟隨場景的轉動而轉動。有的需求是,當轉動到某一個頁面時,隨之出現一個按鈕,後面會說到)
PLTexture *hotspotTexture = [PLTexture textureWithImage:[PLImage imageWithPath:[[NSBundle mainBundle] pathForResource:@"360_f_arrow@2x" ofType:@"png"]]];
PLHotspot *hotspot = [PLHotspot hotspotWithId:1 texture:hotspotTexture atv:-20.0f ath:-40.0f width:0.08f height:0.08f];(角度是相對場景而調整,可自設置)
//width height 熱點大小 atv上正數下負數 ath 位置右邊正數 左邊負數
[panorama addHotspot:hotspot];
當旋轉到某一個頁面的時候出現一個按鈕(yaw是橫向,pitch是縱向)
if ((camera.yaw <90.f && camera.yaw >=0 && !isshow)) //|| (camera.pitch <10 && camera.pitch >5.f))
{
self.curtainLabel = [[UIButton alloc]initWithFrame:CGRectMake(290, 0, 30, 30)];
self.curtainLabel.backgroundColor = [UIColor blueColor];
self.curtainLabel.layer.cornerRadius = 5.0f;
self.curtainLabel.hidden = YES;
self.curtainLabel.layer.shadowColor = [UIColor blackColor].CGColor;
self.curtainLabel.layer.shadowOffset = CGSizeMake(2, 2);
//添加觸發到視頻也的按鈕
[self.curtainLabel addTarget:self action:@selector(curtainBtn) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.curtainLabel];
}
- (void)view:(UIView<PLIView> *)pView didClickHotspot:(PLHotspot *)hotspot screenPoint:(CGPoint)point scene3DPoint:(PLPosition)position
{
NSLog(@"hhhhhh");
}
前面有說到,該庫支持360和720旋轉。具體修改旋轉角度是在Plconstants類裏面
我項目中所涉及的技術也就這些吧。如果還有別的功能,大家就自己在研究研究吧。