哎呀,新的一年过去了祝大家新年快乐
因为在之前的项目中使用观察者模式去解决了一些问题并且效果比较好,比如说键盘收起(这个用的是uiwindow中自带的通知类型),还有就是我年前的项目中一写具体的东西,需要app后台持续定位上传给服务器,并由服务器返回的字段判断是否结束定位,我讲返回的字段传给delegate的一个属性用于记录当前是否需要定位的状态,我选用了kvo进行检测appDelegate中我自定义的一个属性,一旦该属性的属性值变为1,则停止定位,可能这样比较麻烦但是个人认为思路比较清晰,哈哈。代码在最下面哦!
// // ViewController.m // strategyDemo // // Created by 樊星 on 16/2/8. // Copyright © 2016年 樊星. All rights reserved. // #import "ViewController.h" #import "strategyClass.h" @interface ViewController () { UITextField * text ; } @end @implementation ViewController - (void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(postBeginMyNotification:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(postBeginMyNotification:) name:UIKeyboardWillHideNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } - (void)viewDidLoad { [super viewDidLoad]; text = [[UITextField alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-50 , [UIScreen mainScreen].bounds.size.width,30 )]; [text setBackgroundColor:[UIColor redColor]]; [self.view addSubview:text]; } - (void)postBeginMyNotification:(NSNotification*)notification { NSValue* endrect = [notification.userInfo valueForKey:@"UIKeyboardFrameEndUserInfoKey"]; NSNumber* animationDuaring = [notification.userInfo valueForKey:@"UIKeyboardAnimationDurationUserInfoKey"]; //动画持续时间 CGFloat animationDuaringValue = [animationDuaring floatValue]; //键盘动画结束时键盘的frame CGRect endRext = [endrect CGRectValue]; [UIView animateWithDuration:animationDuaringValue animations:^{ self.view.frame = CGRectMake(0, endRext.origin.y-[UIScreen mainScreen].bounds.size.height, self.view.frame.size.width, self.view.frame.size.height); }]; } @end
//核心代码如下:- (void)viewDidLoad { [super viewDidLoad]; MyAnnotation = [[BMKPointAnnotation alloc]init]; if (!locationService) { locationService = [[BMKLocationService alloc]init]; locationService.delegate = self; } [self startGetSelfLocation]; [__APPDELEGATE addObserver:self forKeyPath:@"IsShare" options:NSKeyValueObservingOptionNew context:nil]; [self addMapView]; [self addShareBtn]; } //KVO返回检测Delegate中isShare的变化 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { shareBtn.selected = ([[change valueForKey:@"new"] isEqualToString:@"1"])? YES:NO; } -(void)dealloc { // 移除KVO [__APPDELEGATE removeObserver:self forKeyPath:@"IsShare"]; }
时间: 2024-11-05 03:20:05