由于項(xiàng)目需求访娶,需要實(shí)現(xiàn)類似隨便走的AR功能地梨,先來張隨便走的截圖何荚,如下:
目前也有一些第三方的AR框架蛇券,所以不知道隨便走到底是基于第三方的還是自己做的缀壤,但接下來我們會(huì)用簡單的方式來實(shí)現(xiàn)AR功能。在這之前纠亚,你得先了解一些基本的常識塘慕,
1、真北真北指的是地理的北極
2蒂胞、磁北 磁北指的是磁場北極
3图呢、方位角是從某點(diǎn)的指北方向線起,依順時(shí)針方向到目標(biāo)方向線之間的水平夾角
我們知道在iOS系統(tǒng)框架中,有一個(gè)叫CoreLocation框架蛤织,專門是跟定位有關(guān)赴叹,通過這個(gè)框架,我們可以很方便的實(shí)現(xiàn)指南針的功能指蚜。我們可以看下CLLocationManager這個(gè)管理類乞巧,里面有經(jīng)緯度、設(shè)備的真實(shí)方向等信息姚炕。我們看下CLHeading這個(gè)類摊欠,如下:
通過這個(gè)類,我們可以獲取到設(shè)備跟磁北柱宦、真北的角度些椒,x,y,z方向上的磁力值,我們先簡單實(shí)現(xiàn)一個(gè)這樣的功能掸刊,先畫一個(gè)雷達(dá)圖免糕,在雷達(dá)圖上有一根線指向磁北,
1忧侧、我們先新建一個(gè)UIVew石窑,在drawRect:(CGRect)rec函數(shù)里畫一個(gè)黑色的圓圈,代碼如下:
[objc]view plaincopy
-?(void)drawRect:(CGRect)rect{
CGContextRef?context?=?UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context,?rect);
[[UIColorcolorWithRed:0green:0blue:0alpha:0.6f]set];
CGContextFillPath(context);
}
2蚓炬、我們在黑色的圓圈上面畫一圈圈的白色圓圈松逊,畫三次,代碼如下:
[objc]view plaincopy
NSArray*colors?=?@[
[UIColorcolorWithRed:240.0/255.0green:240.0/255.0blue:240.0/255.0alpha:0.8],
[UIColorcolorWithRed:240.0/255.0green:240.0/255.0blue:240.0/255.0alpha:0.8],
[UIColorcolorWithRed:240.0/255.0green:240.0/255.0blue:240.0/255.0alpha:0.8]
];
NSInteger?radius?=self.frame.size.width/2/3,?increment?=self.frame.size.width/2/3;
NSArray*angles?=?@[
@{@"start":@0,@"end":@360},
@{@"start":@0,@"end":@360},
@{@"start":@0,@"end":@360},
];
for(inti?=0;?i?<?colors.count;?i++)?{
[colors[i]setStroke];
UIBezierPath*path?=?[UIBezierPathbezierPathWithArcCenter:CGPointMake(self.frame.size.width/2,self.frame.size.height/2)
radius:radius
startAngle:DEGREES_TO_RADIANS([[angles[i]valueForKey:@"start"]integerValue])
endAngle:DEGREES_TO_RADIANS([[angles[i]valueForKey:@"end"]integerValue])
clockwise:YES];
path.lineWidth=1;
[pathstroke];
if(i==colors.count-2)
{
increment=self.frame.size.width/2/3;
}
radius?+=?increment;
}
效果如下圖:
3肯夏、然后我們畫一根線经宏,代碼如下:
[objc]view plaincopy
CGContextSetLineCap(context,?kCGLineCapRound);
CGContextSetLineWidth(context,1);//線寬
CGContextSetAllowsAntialiasing(context,true);
CGContextSetRGBStrokeColor(context,70.0/255.0,241.0/255.0,241.0/255.0,1.0);//線的顏色
CGContextBeginPath(context);
CGContextMoveToPoint(context,self.frame.size.width/2,0);
CGContextAddLineToPoint(context,self.frame.size.width/2,self.frame.size.height/2);
CGContextStrokePath(context);
4、我們初始化CLLocationManager這個(gè)類驯击,代碼如下:
[objc]view plaincopy
self.locationManager=?[[CLLocationManageralloc]init];
if([CLLocationManagerlocationServicesEnabled])
{
//?Configure?and?start?the?LocationManager?instance
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=?kCLLocationAccuracyBest;
self.locationManager.distanceFilter=100.0f;
//????????????[self.locationManager?startUpdatingLocation];
//????????????[self.locationManager?startUpdatingHeading];
}
5烁兰、在CLLocationManager回調(diào)函數(shù)里,
[objc]view plaincopy
-?(void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation
{
}
我們讓這個(gè)雷達(dá)圖跟著角度轉(zhuǎn)徊都,指向磁北或真北沪斟,代碼如下:
[objc]view plaincopy
floatdirection?=?newHeading.trueHeading;
floatheadingAngle?=?-(direction*M_PI/180);
_arcsView.transform=?CGAffineTransformMakeRotation(angle);
效果如下: