在手游中用到传感器的很多,平时你也可能会用到iOS 加速器,这时就会用到传感器,通过一个demol简单的讲一下iOS传感器
创建一个UI View,通过代理方法时刻来更新加速度,时刻更新坐标
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
/* 协议给delegation使用,能够告诉label,现在x和y方向上实时更新的加速度 */
@protocol GravityBallViewDelegate
- (void)updateLabelWithX:(double)accelerometerX Y:(double)accelerometerY;
// 代理方法
@end
@interface GravityBallView : UIView
@property (nonatomic, strong) CMMotionManager *mManager;
// 代理人
@property (nonatomic, weak) id<GravityBallViewDelegate> delegate;
- (void)startUpdateAccelerometer;
- (void)stopUpdate;
@end
#import "GravityBallView.h"
@interface GravityBallView()
@property (readwrite) float velocity;
@end
// 坐标的变化过程
@implementation GravityBallView
@synthesize velocity;
@synthesize mManager = _mManager;
@synthesize delegate;
/* designate initializer */
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_mManager = [[CMMotionManager alloc] init];
/* 小球运动的速度(加速度的倍数),越大球运动越快,感觉上越灵敏 */
self.velocity = 200;
}
return self;
}
/* lazy init */
- (CMMotionManager *)mManager
{
if (!_mManager) {
_mManager = [[CMMotionManager alloc] init];
}
return _mManager;
}
- (void