zoukankan      html  css  js  c++  java
  • iOS 指南针的制作 附带源码

    iOS  指南针的制作  附带源码




    代码下载地址:





    指南针的制作非常简单。
    直接看代码吧!
    ViewController.h代码如下:
    #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>
    @interface ViewController : UIViewController<CLLocationManagerDelegate>
    
    @property (retain, nonatomic) UIImageView *compassImageView;
    @property (retain, nonatomic) CLLocationManager *locationManager;
    @end

    ViewController.m代码如下:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIImageView* backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BackGroundPad.png"]];
        [self.view addSubview:backgroundImage];
        //创建指南针图片
        self.compassImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Compass_HD.png"]];
        
        self.compassImageView.center = CGPointMake(370, 500);
        [self.view addSubview:self.compassImageView];
        //初始化locationManager并设置代理类
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
    	if ([CLLocationManager headingAvailable]) {
            //设置精度
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            //设置滤波器不工作
            self.locationManager.headingFilter = kCLHeadingFilterNone;
            //开始更新
            [self.locationManager startUpdatingHeading];
        }
        else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"atention" message:@"compass not Available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
    }
    

    //调用locationManager成员方法
    - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    
    {
        //重置view的位置
        self.compassImageView.transform = CGAffineTransformIdentity;      
        CGAffineTransform transform = CGAffineTransformMakeRotation(-1 * M_PI*newHeading.magneticHeading/180.0);
        self.compassImageView.transform = transform;
    }
    

    这样就OK啦 ! 要在真机上测试哦!!!


  • 相关阅读:
    深入解析Hibernate核心接口
    Hibernate基本原理
    深入hibernate的三种状态
    Hibernate commit() 和flush() 的区别
    Hibernate中的merge使用详情解说
    Record is locked by another user
    Vue路由router-link的使用
    Vue-router的基本使用
    Vue把父组件的方法传递给子组件调用(评论列表例子)
    Vue中子组件调用父组件的方法
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3162859.html
Copyright © 2011-2022 走看看