小Q项目开始了,每天抽出来写的时间不多,但是我会尽可能的多写一些,尽量吧代码都封装一下,为有需要的朋友拿过去用,如发现什么BUG,欢迎大家及时反馈
明天就是周六了,也是平安夜,提前祝大家平安夜快乐,周六日我会不定期直播写代码,开播前,我会到QQ群里通知,欢迎大家来围观(斗鱼直播群 145447833)
今天写的代码不多,加了一个定位,用得百度地图,还有一个Label数字递增递减的效果(我不知道怎么形容,先给大家上个Gif吧,gif效果不是很好,但是在手机上还是蛮漂亮的)
00000.gif
定位的代码实际很简单,注册百度地图 APPKEY,对应项目的 Bundel id,详见百度地图API吧, 百度地图开发文档
分析:我们使用的时百度的定位功能,要展示给用户一个位置信息,但不是一个经纬度,给用户经纬度用户也不知道这是哪里啊,是吧,所以我们通过百度定位拿到经纬度后,要通过地理位置反编码,所以,我们需要百度的地理功能,以及反编码功能,代码我简单的封装了一下,如下:
导入文件:
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
声明:
@property (nonatomic,strong) BMKMapManager *manager;
@property (nonatomic, strong)BMKLocationService *locService;
@property (nonatomic, strong)BMKGeoCodeSearch *geocodesearch;
签的代理协议
<BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
代码实现
+ (GD_BaiduMapLocationObject *)locationObject;
{
static GD_BaiduMapLocationObject *locationObject = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
locationObject = [[GD_BaiduMapLocationObject alloc]init];
});
return locationObject;
}
- (instancetype)init
{
self = [super init];
if (self) {
self.manager = [[BMKMapManager alloc]init];
BOOL ret = [self.manager start:BAIDU_KEY generalDelegate:nil];
if (!ret) {
NSLog(@"开启失败!!");
}
}
return self;
}
#pragma mark 百度定位
- (void)installBasiduMapKit
{
//初始化百度定位
self.locService = [[BMKLocationService alloc]init];
self.locService.delegate = self;
[self.locService startUserLocationService];
self.geocodesearch = [[BMKGeoCodeSearch alloc]init];
self.geocodesearch.delegate = self;
[UserDefaults setObject:@"北京" forKey:@"KCityName"];
}
#pragma mark----BMKLocationServiceDelegate
/**
*用户位置更新后,会调用此函数
*@param userLocation 新的用户位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
pt = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
reverseGeocodeSearchOption.reverseGeoPoint = pt;
BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
if(flag){
//反geo检索发送成功
}else{
//反geo检索发送失败
[UserDefaults setObject:@"北京" forKey:@"KCityName"];
}
}
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
if (error == BMK_SEARCH_NO_ERROR) {
// 因为我只需要城市名字,城市名字后面都有一个市,根据需求把市切断
NSString *CityName = [result.addressDetail.city componentsSeparatedByString:@"市"][0];
[UserDefaults setObject:CityName forKey:
@"KCityName"];
//定位成功后拿到城市 停止定位
[_locService stopUserLocationService];
}else{
[UserDefaults setObject:@"北京" forKey:@"KCityName"];
}
}
- (void)dealloc {
if (_geocodesearch) {
_geocodesearch = nil;
}
if (_locService) {
_locService = nil;
}
}
好了,完成,在需要定位的地方调用,代码如下:
#pragma mark -- 初始化百度地图并开始定位
[[GD_BaiduMapLocationObject locationObject] installBasiduMapKit];
这时候重点来了,也是比较容易忽略的地方
自iOS8起,系统定位功能进行了升级,SDK为了实现最新的适配,自v2.5.0起也做了相应的修改,开发者在使用定位功能之前,需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription): NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述 NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述
加这个切记,不加的话拿不到位置并打包发到 iTunes的时候会显示版本无效(四天前的文章有讲到过 iOS APP版本构建版本无效)
百度定位结束
这个效果大家在上面的gif中看到了,可能gif是个帧动画,感觉这个很不流畅,像一个一个数字蹦一样,就在我写文章的时候,还有个哥们说,你这很卡啊,计算label的长度,和数组一起变化,其实这个不用担心的,label的宽度是使用的Masonry,比较省心,数字的渐变是使用的POP动画实现代码如下:
GDScrollLabel.h文件
//
// GDScrollLabel.h
// GD_XiaoQ
//
// Created by GuangdongQi on 2016/12/23.
// Copyright © 2016年 GuangdongQi. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface GDScrollLabel : NSObject
+ (GDScrollLabel *)shaerScrollLabel;
/*
* property: label 要显示数字的Label
* property: fromValue 数字开始值
* property: toValue 数字终点值
* property: duration 从开始时间到结束
*/
- (void)installScrollLabel:(UILabel *)label withFromValue:(CGFloat)fromValue withToValue:(CGFloat)toValue withDuration:(CFTimeInterval)duration;
@end
GDScrollLabel.m文件
//
// GDScrollLabel.m
// GD_XiaoQ
//
// Created by GuangdongQi on 2016/12/23.
// Copyright © 2016年 GuangdongQi. All rights reserved.
//
#import "GDScrollLabel.h"
@interface GDScrollLabel ()
@property (nonatomic , strong)UILabel *label;
@property (nonatomic , strong)POPBasicAnimation * textProperty;
@end
@implementation GDScrollLabel
+ (GDScrollLabel *)shaerScrollLabel;
{
static GDScrollLabel *scrollLabel = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
scrollLabel = [[GDScrollLabel alloc]init];
});
return scrollLabel;
}
- (void)installScrollLabel:(UILabel *)label withFromValue:(CGFloat)fromValue withToValue:(CGFloat)toValue withDuration:(CFTimeInterval)duration
{
self.label = label;
self.textProperty = [POPBasicAnimation animationWithPropertyNamed:@"progress"];
self.textProperty.property = [self animationProperty:label];
self.textProperty.fromValue = @(fromValue);
self.textProperty.toValue = @(toValue);
self.textProperty.duration = duration;
self.textProperty.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
self.textProperty.delegate = self;
[self pop_addAnimation:self.textProperty forKey:nil];
}
- (POPMutableAnimatableProperty *)animationProperty:(UILabel *)label {
return [POPMutableAnimatableProperty
propertyWithName:@"moneyNum"
initializer:^(POPMutableAnimatableProperty *prop) {
prop.writeBlock = ^(id obj, const CGFloat values[]) {
NSNumber *number = @(values[0]);
float num = [number floatValue];
label.text = [NSString stringWithFormat:@"%.2f",num];
};
}];
}
- (void)pop_animationDidStop:(POPAnimation *)anim finished:(BOOL)finished{
if (finished) {
//实现缩放动画,和tabbar是一个代码
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1, 1)];
scaleAnimation.velocity = [NSValue valueWithCGPoint:CGPointMake(10, 10)];
scaleAnimation.springBounciness = 60.f;
//不要加代理,如果有代理是个死循环,一直在动
// scaleAnimation.delegate = self;
[self.label pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}
}
@end
好了,结束,调用方法如下:
[[GDScrollLabel shaerScrollLabel]installScrollLabel:self.label withFromValue:0 withToValue:10000 withDuration:2];
在手机上运行完美,感兴趣的同学可以复制一下看看,几个参数写的很明白了,看一下就会用了。
细心的同学观察到我的导航栏是透明的
Paste_Image.png
其实这里的代码很简单,使用KVC方法
#pragma mark -- 设置导航栏全部透明
- (void)navigationBackgroundColor
{
[self.navigationController.navigationBar setValue:@0 forKeyPath:@"backgroundView.alpha"];
}
这里没做过多的改动,准备之后把这里改成随着滑动渐变的效果,可能下篇,或者更晚的一片能提到
好今天先到这