CoreMotion
2014-07-09 19:01
253 查看
#import "TRViewController.h"
#import <CoreMotion/CoreMotion.h>
#import <AVFoundation/AVFoundation.h>
@interface
TRViewController ()<AVAudioPlayerDelegate>
@property (nonatomic,
strong)AVAudioPlayer *player;
@property(nonatomic,strong)CMMotionManager* manager;
@property (weak,
nonatomic) IBOutlet
UIButton *myBall;
@property (nonatomic,assign)CGRect oldFrame;
@property (weak,
nonatomic) IBOutlet
UILabel *tip;
@property (weak,
nonatomic) IBOutlet
UILabel *label;
@property (nonatomic,assign)BOOL bRet;
@property (strong,
nonatomic) IBOutletCollection(UIView)
NSArray *iv;
@end
@implementation TRViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.manager = [[CMMotionManager
alloc]init];
[self.manager
setAccelerometerUpdateInterval:1.0/30];
[self.manager
startAccelerometerUpdates];
self.oldFrame =
self.myBall.frame;
self.bRet =
YES;
[NSTimer
scheduledTimerWithTimeInterval:1.0/30
target:self
selector:
4000
@selector(getInfo)
userInfo:nil
repeats:YES];
}
-(void)getInfo
{
CMAcceleration acc =
self.manager.accelerometerData.acceleration;
//NSLog(@"x = %f y = %f z = %f", acc.x, acc.y, acc.z);
self.myBall.center =
CGPointMake(self.myBall.center.x + acc.x,
self.myBall.center.y-acc.y);
if (self.myBall.center.x <
0 || self.myBall.center.x >
320 || self.myBall.center.y <
0)
{
[UIView
animateWithDuration:2
animations:^{
self.myBall.frame =
self.oldFrame;
}];
}
if (CGRectIntersectsRect(self.label.frame,
self.myBall.frame) &&
self.bRet ==
YES)
{
self.bRet =
NO;
self.tip.text =
@"请靠边行驶";
[UIView
animateWithDuration:5
animations:^{
self.tip.alpha =
0;
}completion:^(BOOL finisd)
{
self.tip.text =
@"";
self.tip.alpha =
1;
}];
}
for (UIView* iv
in self.iv)
{
if (CGRectIntersectsRect(self.myBall.frame, iv.frame))
{
if (2 ==iv.tag)
{
UIAlertView* alertView = [[UIAlertView
alloc]initWithTitle:@"恭喜"
message:@"你赢了" delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertView
show];
}
else if (1 == iv.tag)
{
UIImage* image = [UIImage
imageNamed:@"IMG_0993.JPG"];
UIImageView* imageView = [[UIImageView
alloc]initWithImage:image];
imageView.frame =
self.view.frame;
[self.view
addSubview:imageView];
NSString *path=[[NSBundle
mainBundle] pathForResource:@"ghost"
ofType:@"mp3"];
NSURL *url = [NSURL
fileURLWithPath:path];
self.player = [[AVAudioPlayer
alloc]initWithContentsOfURL:url
error:nil];
self.player.delegate =
self;
[self.player
play];
[UIView
animateWithDuration:5.0
animations:^{
imageView.alpha =
0;
}completion:^(BOOL finished){
[imageView
removeFromSuperview];
}];
}
[UIView
animateWithDuration:2
animations:^{
self.myBall.frame =
self.oldFrame;
}];
NSLog(@"xxxxxxxxxxxxxxxx");
}
}
}
#import <CoreMotion/CoreMotion.h>
#import <AVFoundation/AVFoundation.h>
@interface
TRViewController ()<AVAudioPlayerDelegate>
@property (nonatomic,
strong)AVAudioPlayer *player;
@property(nonatomic,strong)CMMotionManager* manager;
@property (weak,
nonatomic) IBOutlet
UIButton *myBall;
@property (nonatomic,assign)CGRect oldFrame;
@property (weak,
nonatomic) IBOutlet
UILabel *tip;
@property (weak,
nonatomic) IBOutlet
UILabel *label;
@property (nonatomic,assign)BOOL bRet;
@property (strong,
nonatomic) IBOutletCollection(UIView)
NSArray *iv;
@end
@implementation TRViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.manager = [[CMMotionManager
alloc]init];
[self.manager
setAccelerometerUpdateInterval:1.0/30];
[self.manager
startAccelerometerUpdates];
self.oldFrame =
self.myBall.frame;
self.bRet =
YES;
[NSTimer
scheduledTimerWithTimeInterval:1.0/30
target:self
selector:
4000
@selector(getInfo)
userInfo:nil
repeats:YES];
}
-(void)getInfo
{
CMAcceleration acc =
self.manager.accelerometerData.acceleration;
//NSLog(@"x = %f y = %f z = %f", acc.x, acc.y, acc.z);
self.myBall.center =
CGPointMake(self.myBall.center.x + acc.x,
self.myBall.center.y-acc.y);
if (self.myBall.center.x <
0 || self.myBall.center.x >
320 || self.myBall.center.y <
0)
{
[UIView
animateWithDuration:2
animations:^{
self.myBall.frame =
self.oldFrame;
}];
}
if (CGRectIntersectsRect(self.label.frame,
self.myBall.frame) &&
self.bRet ==
YES)
{
self.bRet =
NO;
self.tip.text =
@"请靠边行驶";
[UIView
animateWithDuration:5
animations:^{
self.tip.alpha =
0;
}completion:^(BOOL finisd)
{
self.tip.text =
@"";
self.tip.alpha =
1;
}];
}
for (UIView* iv
in self.iv)
{
if (CGRectIntersectsRect(self.myBall.frame, iv.frame))
{
if (2 ==iv.tag)
{
UIAlertView* alertView = [[UIAlertView
alloc]initWithTitle:@"恭喜"
message:@"你赢了" delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertView
show];
}
else if (1 == iv.tag)
{
UIImage* image = [UIImage
imageNamed:@"IMG_0993.JPG"];
UIImageView* imageView = [[UIImageView
alloc]initWithImage:image];
imageView.frame =
self.view.frame;
[self.view
addSubview:imageView];
NSString *path=[[NSBundle
mainBundle] pathForResource:@"ghost"
ofType:@"mp3"];
NSURL *url = [NSURL
fileURLWithPath:path];
self.player = [[AVAudioPlayer
alloc]initWithContentsOfURL:url
error:nil];
self.player.delegate =
self;
[self.player
play];
[UIView
animateWithDuration:5.0
animations:^{
imageView.alpha =
0;
}completion:^(BOOL finished){
[imageView
removeFromSuperview];
}];
}
[UIView
animateWithDuration:2
animations:^{
self.myBall.frame =
self.oldFrame;
}];
NSLog(@"xxxxxxxxxxxxxxxx");
}
}
}
相关文章推荐
- CoreMotion框架-iOS设备的核心运动
- 加速传感器(CoreMotion) swift
- CoreMotion可以测到的各种值
- iOS CoreMotion实现设备运动加速度计陀螺仪
- iOS使用CoreMotion实现摇一摇功能
- iOS使用CoreMotion实现摇一摇功能
- CoreMotion初探及问题总结
- iOS使用CoreMotion
- CoreMotion框架-iOS设备的核心运动
- iOS开发之加速计(二)CoreMotion
- CoreMotion框架-iOS设备的核心运动
- iOS使用CoreMotion实现摇一摇功能
- 传感器- 加速计 - CoreMotion
- iOS之 CoreMotion 框架
- Swift用CoreMotion得到屏幕当前方向的方法
- CoreMotion(简介)
- 19.CoreMotion(陀螺仪)
- CoreMotion--iOS中的陀螺仪用法
- CoreMotion的实际使用,主要获得加速度或者手机朝向
- CoreMotion可以测到的各种值