您的位置:首页 > 移动开发 > IOS开发

蓝懿iOS 技术内容和心得 12.19

2015-12-19 22:57 363 查看
    今天是练习日  我们还是复习一下 前面的知识:

GPS应用

#import "ViewController.h"

#import

@interface ViewController ()

@property (nonatomic, strong)CLLocationManager *locationManager;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.locationManager = [[CLLocationManager alloc]init];

    

    

    [self.locationManager requestWhenInUseAuthorization];

    

    

    

    self.locationManager.delegate = self;

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    //设置更新距离 距离越小 越费电

    self.locationManager.distanceFilter = 20;

    //开始获取自己位置

    [self.locationManager startUpdatingLocation];

    

    //开始获取当前陀螺仪的值

    [self.locationManager startUpdatingHeading];

   

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    

    [self.locationManager requestAlwaysAuthorization];

}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    

    NSLog(@"%@",locations);

    

}

#import

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong)CMMotionManager *manager;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

   第一种方式

    

    self.manager = [[CMMotionManager alloc]init];

    

    if (self.manager.isAccelerometerAvailable) {

        NSLog(@"加速计可用");

        //        每隔一段时间固定回调会block方法中

//           self.manager.accelerometerUpdateInterval = 1.0/30;

//        

//    [self.manager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {

//        

//                   NSLog(@"%f**%f**%f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);

//                }];

        

        

//        第二种方式

        //开始更新

        [self.manager startAccelerometerUpdates];

    }

    

    [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:selfselector:@selector(getInfo) userInfo:nil repeats:YES];

}

//摇一摇实现

//-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

//    NSLog(@"开始摇了");

//}

//-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

//    NSLog(@"摇完了");

//}

//-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{

//    NSLog(@"取消");

//}

- (void)getInfo{

    CMAccelerometerData *accelerometerData =self.manager.accelerometerData;

 }

mapVIew的用法:

1、先导入自带的MapKit​.frameWork;

​2、#import

3、​#import "ViewController.h"

#import

导入小红旗model​

#import "MyAnnotation.h"

@interface ViewController ()<<span se-mark="1">MapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

@implementation ViewController

- (IBAction)tapAction:(UITapGestureRecognizer *)sender {

    CGPoint p = [sender locationInView:self.view];  

地图中心位置为点击屏幕位置的点  

    CLLocationCoordinate2D coord = [self.mapView convertPoint:p toCoordinateFromView:self.mapView];

    坐标为经纬度

    NSLog(@"%f---%f",coord.longitude,coord.latitude);

闯建小气球

    MyAnnotation *ann = [[MyAnnotation alloc]init];

3个必有属性​,创建时要把只读去掉

    ann.coordinate = coord;

    ann.title = @"title";

    ann.subtitle = @"subTitle";

    [self.mapView addAnnotation:ann];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    //    北纬:39.90960456049752

//    东经:116.3972282409668

    

    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(39.90960456049752,116.3972282409668);

        [self.mapView setRegion:MKCoordinateRegionMake(coord, MKCoordinateSpanMake(.001,.001))];

    

}

#pragma mark MapViewDelegate

自定义​MKAnnotationView与自定义cell类似需要创建MKAnnotationView

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{

    static NSString *identifier = @"ann";

    MyAnnotationView *av = (MyAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    

    if (!av) {

        av = [[MyAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];

    }

    

    return av;

}

点击小红旗触发事件​

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

    NSLog(@"hehe");

}

改变地图位置​

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    CLLocationCoordinate2D centerCoord = mapView.centerCoordinate;    NSLog(@"%f--%f",centerCoord.longitude,centerCoord.latitude);

    MyAnnotation *ann = [[MyAnnotation alloc]init];

    ann.coordinate = centerCoord;

    ann.title = @"title";

    ann.subtitle = @"subTitle";

    [self.mapView addAnnotation:ann];

}

二、MyAnnotation同样要导入注意属性为必需而且要去掉只读属性

#import

@interface MyAnnotation : NSObject

@property (nonatomic) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy, nullable) NSString *title;

@property (nonatomic, copy, nullable) NSString *subtitle;

@end

三、自定义AnnotationView继承MKAnnotationView

​#import "MyAnnotationView.h"

@implementation MyAnnotationView

-(instancetype)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];​

    if (self) {       self.image = [UIImage imageNamed:@"001.png"];        //改变自身尺寸

        self.bounds = CGRectMake(0, 0, 50, 50);

    }

    returnself;

}

 
获取当前时间的秒数:拿到1970年到现在过的总秒数;

NSTimeInterval time = [[NSDate date] timeIntervalSince1970];

long long int date = (long long int)time;

NSLog(@”date\n%lld”, date); //1295322949

//把秒数转化成yyyy-MM-dd hh:mm:ss格式

NSDate *dd = [NSDate dateWithTimeIntervalSince1970:date];

NSLog(@”d:%@”,dd); //2011-01-18 03:55:49 +0000

//微博返回的时间格式​,字母为固定的,大小写不能错;

    NSDateFormatter *fmt = [[NSDateFormatteralloc] init];

//    EEE星期 MMM月 dd日 HH小时 mm分 ss秒 Z时区 yyyy年

    fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";

    //英文

  fmt.locale = [[NSLocalealloc] initWithLocaleIdentifier:@"en_US"];

       

//

 获得微博发布的具体时间 (倒推回去)

    NSDate *createDate = [fmt dateFromString:_created_at];

    //获取当前时间对象

    NSDate *nowDate = [NSDate date];

    long createTime = [createDate timeIntervalSince1970];

    long nowTime = [nowDate timeIntervalSince1970];

    long time = nowTime-createTime;

    if (time<<span se-mark="1">60) {

        return @"刚刚";

    }else if (time<<span se-mark="1">3600){

        return [NSString stringWithFormat:@"%ld分钟前",time/60];

    }else if (time<<span se-mark="1">3600*24){

        return [NSString stringWithFormat:@"%ld小时前",time/3600];

    }

//自己重新定义显示格式​

        fmt.

dateFormat

 = @"MM月dd日 HH:mm";

    return [fmt stringFromDate:createDate];

另外在分享一些常见 的错误:

1.[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key resultLabel.出错原因:sb控件和代码关联出错解决方案:找到相对应的页面的黄色圆点(ViewController)在此上面右键 删除 黄色叹号2.<__NSArrayM: 0x7c0320b0> was mutated while being enumerated.出错原因:forin循环时修改了数组 但继续循环了解决方案:
修改数组后 加break或return3.-[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 0]'出错原因:数组角标越界解决方案:角标的取值 要小于数组的count4.-[ViewController addZombX]: unrecognized selector sent to instance 0x79174e00'出错原因:在ViewController中找不到方法

解决方案:在某个类中没有实现这个方法  或者是对象类型错了

5.This application is modifying the autolayout engine from a background thread出错原因:子线程中修改了界面解决方案:回到主线程修改就可以了6.An

instance 0x7fb6dad221e0 of class Mouse was deallocated

while key value observers were still registered with it

原因:监听了一个已经被释放的对象

解决方案:在适当的位置删除监听

    学习ios  重要还是要理清楚思路  在做或者看老师代码的时候 自己多想想为什么  不要自己看着就抄       另外还是要推荐一下 蓝懿IOS这个培训机构  和刘国斌老师刘国斌老师还是很有名气的,听朋友说刘老师成立了蓝懿iOS,,老师讲课方式很独特,能够尽量让每个人都能弄明白,有的比较难懂的地方,如果有的地方还是不懂得话,老师会换个其它方法再讲解,这对于我们这些学习iOS的同学是非常好的,多种方式的讲解会理解得更全面,这个必须得给个赞,嘻嘻,还有就是这里的学习环境很好,很安静,可以很安心的学习,安静的环境是学习的基础,小班讲课,每个班20几个学生,学习氛围非常好,每天都学到9点多才离开教室,练习的时间很充裕,而且如果在练习的过程中有什么困难,随时可以向老师求助,不像其它机构,通过视频教学,有的甚至学完之后都看不到讲师本人,问点问题都不方便,这就是蓝懿与其它机构的区别,相信在刘国斌老师的细心指导下,每个蓝懿学员都能找到满意的工作,加油!

                                                                  写博客第七十天;

                                                                              QQ:565803433

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息