因为一些原因,用了整整一周的时间来重构项目,因为期末考试的原因,决定暑假继续这个项目,在复习之前最后总结一下这一段开发过程中遇到的一些问题。

定位问题


参考博客:后台定位上传的代码实践|里脊串的开发随笔

需求是每隔一定时间向服务器上传一次地理位置,而不管用户或系统是否杀死了APP。这里参考了里脊串博客里的写法,使用BackgroundMode中的Location updates即可实现。但是与这篇博客中的APP的需求不同,并不需要考虑速度距离等因素,只要考虑时间即可,因此可以通过判断两次location中的时间戳间隔即可。

1
2
3
4
5
#define kUpdateTimeInteval 60.0 //上传间隔
@interface LocationManager()
@property (nonatomic, strong) CLLocation * lastLocation; //最新一次的地理位置
@property (nonatomic) double timeInteval; //上一次上传地理位置时的时间戳
@end

然后在locationManager的委托方法中加入如下代码:

1
2
3
4
5
6
7
8
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
_lastLocation = locations[0];
if (_lastLocation.timestamp.timeIntervalSince1970 - _timeInteval > kUpdateTimeInteval) {
NSLog(@"update location");
[self updateLocation]; //上传地理位置
_timeInteval = _lastLocation.timestamp.timeIntervalSince1970;
}
}

需要注意的是,在进入后台模式或者是APP被杀的情况下,如果设备的位置没有发生变化,那么locationManager的 didUpdateLocations方法将不会被调用,因此尽管间隔的时间已经超过了设定的时间,地理位置也不会被上传。

不显示动画


由于APP中存在用户注册和登录的需求,因此需要加入验证码认证,两次验证码之间应有一分钟的间隔,此时,UI应该每隔1s刷新一次剩余时间以提醒用户,因此我使用了一个每隔1s执行一次的NSTimer去刷新UI,却发现每次刷新的时候按钮上的Title都会有渐入渐出的动画效果。后来发现这是UIButtonTypeRoundedRect的自带动画效果,为了不让这个渐变动画干扰了UI的刷新,加入如下代码:

1
2
3
4
[UIView performWithoutAnimation:^{
//在这里设置UIButton的属性如Text等
[_button.layer layoutIfNeeded];
}];

自定义UIBarButtonItem


参考了StackOverFlow

需求是自定义一个同时带图片和文字的UIBarButtonItem,查阅文档和搜索后发现UIBarButtonItem有以下的方法:

- (instancetype)initWithCustomView:(UIView *)customView;

因此我们可以先实例化一个UIButton,给这个UIButton添加Image并且设置Title,再用这个UIButton去实例化一个UIBarButtonItem即可,代码如下:

1
2
3
4
5
6
7
8
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"返回" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:17.0];
[button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 0); //使用这个属性控制UIImageView和UILabel之间的距离
[button sizeToFit]; //设定button的frame
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithCustomView:button];

但是这样又发现一个新的问题,我们无法控制UIBarButtonItem的边距。官方文档UINavigationItem中对于leftBarButtonItems和rightBarButtonItems这两个属性中有如下描述:

leftBarButtonItems are placed in the navigation bar left to right with the first item in the list at the left outside edge and left aligned.

rightBarButtonItems are placed right to left with the first item in the list at the right outside edge and right aligned.

因此我们可以再实例化一个UIBarButtonItem并设置它的width,完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
UIBarButtonItem * negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -16.0; //可根据实际需要调整
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"返回" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:17.0];
[button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 0);
[button sizeToFit];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithCustomView:button];
self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftbutton];

去除UINavigationBar的阴影


我采用的是如下的方法:

[nav.navigationBar setShadowImage:[[UIImage alloc]init]];