#import "ViewController.h"
#import<CoreMotion/CoreMotion.h> //陀螺儀
@interface ViewController ()
@property (nonatomic, strong) CMMotionManager *motionManager;//陀螺儀
@property (nonatomic, strong) UIImageView *circleView;
@property (nonatomic, strong) UIImageView *anotherView;
@end
@implementation ViewController
{
? ? UIImageView*tempImageView;
}
- (void)dealloc
{
? ? _motionManager = nil;
}
-(CMMotionManager*)motionManager {
? ? if (_motionManager == nil) {
? ? ? ? _motionManager= [[CMMotionManageralloc]init];
? ? }
? ? return _motionManager;
}
- (void)stopUpdate//陀螺儀暫停
{
? ? if ([self.motionManager isAccelerometerActive] == YES)
? ? {
? ? ? ? [self.motionManager stopAccelerometerUpdates];
? ? }
}
- (void)startUpdateAccelerometerResult{//陀螺儀開(kāi)始
? ? if ([self.motionManager isAccelerometerAvailable] == YES) {
? ? ? ? [self.motionManager setAccelerometerUpdateInterval:0.06];
? ? ? ? [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
?? ? ? ? {
?? ? ? ? ? ? doublex = accelerometerData.acceleration.x;
?? ? ? ? ? ? doubley = accelerometerData.acceleration.y;
?? ? ? ? ? ? intDown =0, Portrait =0, Right =0, Left =0;
?? ? ? ? ? ? if(fabs(y) >=fabs(x))
?? ? ? ? ? ? {
?? ? ? ? ? ? ? ? if(y >=1){
?? ? ? ? ? ? ? ? ? ? //Down
?? ? ? ? ? ? ? ? ? ? Down--;
?? ? ? ? ? ? ? ? ? ? NSLog(@"Down");
?? ? ? ? ? ? ? ? }
?? ? ? ? ? ? ? ? if(y<= -1){
?? ? ? ? ? ? ? ? ? ? //Portrait
?? ? ? ? ? ? ? ? ? ? Portrait++;
?? ? ? ? ? ? ? ? ? ? NSLog(@"Portrait");
?? ? ? ? ? ? ? ? }
?? ? ? ? ? ? }
?? ? ? ? ? ? else
?? ? ? ? ? ? {
?? ? ? ? ? ? ? ? if(x >=1){
?? ? ? ? ? ? ? ? ? ? //Right
?? ? ? ? ? ? ? ? ? ? Right++;
?? ? ? ? ? ? ? ? ? ? NSLog(@"Right");
?? ? ? ? ? ? ? ? }
?? ? ? ? ? ? ? ? if(x<= -1){
?? ? ? ? ? ? ? ? ? ? //Left
?? ? ? ? ? ? ? ? ? ? Left--;
?? ? ? ? ? ? ? ? ? ? NSLog(@"Left");
?? ? ? ? ? ? ? ? }
?? ? ? ? ? ? }
?? ? ? ? ? ? [UIView animateWithDuration:0.1f animations:^{
?? ? ? ? ? ? ? ? CGRectrect =self->_circleView.frame;
?? ? ? ? ? ? ? ? floatxx = rect.origin.x+Left*50+Right*50<0?0: rect.origin.x+Left*50+Right*50;
?? ? ? ? ? ? ? ? floatyy = rect.origin.y+Down*50+Portrait*50<64?64:rect.origin.y+Down*50+Portrait*50;
?? ? ? ? ? ? ? ? self->_circleView.frame = CGRectMake(xx > (self.view.frame.size.width-100) ? (self.view.frame.size.width-100) : xx, yy > (self.view.frame.size.height-100) ? (self.view.frame.size.height-100) : yy, rect.size.width, rect.size.height);
?? ? ? ? ? ? ? ? CGRectrectF =self->_anotherView.frame;
?? ? ? ? ? ? ? ? floatmm = rectF.origin.x-Left*50-Right*50<0?0: rectF.origin.x-Left*50-Right*50;
?? ? ? ? ? ? ? ? floatnn = rectF.origin.y-Down*50-Portrait*50<64?64:rectF.origin.y-Down*50-Portrait*50;
?? ? ? ? ? ? ? ? ? self->_anotherView.frame=CGRectMake(mm > (self.view.frame.size.width-100) ? (self.view.frame.size.width-100) : mm, nn > (self.view.frame.size.height-100) ? (self.view.frame.size.height-100) : nn, rectF.size.width, rectF.size.height);
?? ? ? ? ? ? }];
?? ? ? ? }];
? ? }
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.title? =@"小創(chuàng)測(cè)試";
? ? [self startUpdateAccelerometerResult];
? ? [self.viewaddSubview:self.circleView];
? ? [self.view addSubview:self.anotherView];
? ? //
? ? [self.view setUserInteractionEnabled:YES];
? ? [self.view setMultipleTouchEnabled:YES];
}
#pragma touch
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event
{
? ? //獲取任意一個(gè)touch對(duì)象
? ? UITouch* pTouch = [touchesanyObject];
? ? //獲取對(duì)象所在的坐標(biāo)
? ? CGPointpoint = [pTouchlocationInView:self.view];
? ? //以字符的形式輸出觸摸點(diǎn)
? ? NSLog(@"觸摸點(diǎn)的坐標(biāo):%@",NSStringFromCGPoint(point));
? ? [UIView animateWithDuration:0.1f animations:^{
? ? ? ? CGRectrect =self->_circleView.frame;
? ? ? ? floatxx = point.x<0?0:point.x;
? ? ? ? floatyy = point.y<64?64: point.y;
? ? ? ? self->tempImageView.frame = CGRectMake(xx > (self.view.frame.size.width-100) ? (self.view.frame.size.width-100) : xx, yy > (self.view.frame.size.height-100) ? (self.view.frame.size.height-100) : yy, rect.size.width, rect.size.height);
? ? }];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(nullableUIEvent*)event{
? ? //獲取任意一個(gè)touch對(duì)象
? ? UITouch* pTouch = [touchesanyObject];
? ? //獲取對(duì)象所在的坐標(biāo)
? ? CGPointpoint = [pTouchlocationInView:self.view];
? ? //以字符的形式輸出觸摸點(diǎn)
? ? NSLog(@"開(kāi)始的坐標(biāo):%@",NSStringFromCGPoint(point));
? ? //獲取觸摸的次數(shù)
? ? //NSUInteger tapCount = [pTouch tapCount];
? ? if (tempImageView == nil) {
? ? ? ? floatdistance1;
? ? ? ? //下面就是高中的數(shù)學(xué)悼做,不詳細(xì)解釋了
? ? ? ? CGFloatxDist = (point.x-_circleView.frame.origin.x);
? ? ? ? CGFloatyDist = (point.y-_circleView.frame.origin.y);
? ? ? ? distance1 =sqrt((xDist * xDist) + (yDist * yDist));
? ? ? ? floatdistance2;
? ? ? ? //下面就是高中的數(shù)學(xué)惠爽,不詳細(xì)解釋了
? ? ? ? CGFloatxDist2 = (point.x-_anotherView.frame.origin.x);
? ? ? ? CGFloatyDist2 = (point.y-_anotherView.frame.origin.y);
? ? ? ? distance2 =sqrt((xDist2 * xDist2) + (yDist2 * yDist2));
? ? ? ? if(distance1 >= distance2) {
? ? ? ? ? ? tempImageView = _anotherView;
? ? ? ? }else{
?? ? ? ? ? tempImageView = _circleView;
? ? ? ? }
? ? }
? ? [UIView animateWithDuration:0.1f animations:^{
? ? ? ? CGRectrect =self->_circleView.frame;
? ? ? ? floatxx = point.x<0?0:point.x;
? ? ? ? floatyy = point.y<64?64: point.y;
? ? ? ? self->tempImageView.frame = CGRectMake(xx > (self.view.frame.size.width-100) ? (self.view.frame.size.width-100) : xx, yy > (self.view.frame.size.height-100) ? (self.view.frame.size.height-100) : yy, rect.size.width, rect.size.height);
? ? }];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(nullableUIEvent*)even{
? ? //獲取任意一個(gè)touch對(duì)象
? ? UITouch* pTouch = [touchesanyObject];
? ? //獲取對(duì)象所在的坐標(biāo)
? ? CGPointpoint = [pTouchlocationInView:self.view];
? ? //以字符的形式輸出觸摸點(diǎn)
? ? NSLog(@"結(jié)束的坐標(biāo):%@",NSStringFromCGPoint(point));
? ? [UIView animateWithDuration:0.1f animations:^{
? ? ? ? CGRectrect =self->_circleView.frame;
? ? ? ? floatxx = point.x<0?0:point.x;
? ? ? ? floatyy = point.y<64?64: point.y;
? ? ? ? self->tempImageView.frame = CGRectMake(xx > (self.view.frame.size.width-100) ? (self.view.frame.size.width-100) : xx, yy > (self.view.frame.size.height-100) ? (self.view.frame.size.height-100) : yy, rect.size.width, rect.size.height);
? ? }];
}
-(UIImageView*)circleView{
? ? if(_circleView==nil) {
? ? ? ? _circleView= [[UIImageViewalloc]init];
? ? ? ? _circleView.backgroundColor = [UIColor orangeColor];
? ? ? ? _circleView.frame = CGRectMake(self.view.frame.size.width/2-50, self.view.frame.size.height/2-100, 100, 100);
? ? ? ? _circleView.layer.cornerRadius = 50;
? ? ? ? _circleView.layer.masksToBounds = YES;
? ? ? ? _circleView.userInteractionEnabled = YES;
? ? ? ? UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction1:)];
? ? ? ? [_circleView addGestureRecognizer:tap];
? ? }
? ? return _circleView;
}
-(UIImageView*)anotherView{
? ? if(_anotherView==nil) {
? ? ? ? _anotherView= [[UIImageViewalloc]init];
? ? ? ? _anotherView.backgroundColor = [UIColor purpleColor];
? ? ? ? _anotherView.frame = CGRectMake(self.view.frame.size.width/2-50, self.view.frame.size.height/2+100, 100, 100);
? ? ? ? _anotherView.layer.cornerRadius = 50;
? ? ? ? _anotherView.layer.masksToBounds = YES;
? ? ? ? _anotherView.userInteractionEnabled = YES;
? ? ? ? UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction2:)];
? ? ? ? [_anotherView addGestureRecognizer:tap];
? ? }
? ? return _anotherView;
}
-(void)tapAction1:(UITapGestureRecognizer *)tap{
? ? tempImageView= (UIImageView*)tap.view;
}
-(void)tapAction2:(UITapGestureRecognizer *)tap{
? ? tempImageView= (UIImageView*)tap.view;
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
@end