#import "ViewController.h"
#import
@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{//陀螺仪开始
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 =@"小创测试";
[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
{
//获取任意一个touch对象
UITouch* pTouch = [touchesanyObject];
//获取对象所在的坐标
CGPointpoint = [pTouchlocationInView:self.view];
//以字符的形式输出触摸点
NSLog(@"触摸点的坐标:%@",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{
//获取任意一个touch对象
UITouch* pTouch = [touchesanyObject];
//获取对象所在的坐标
CGPointpoint = [pTouchlocationInView:self.view];
//以字符的形式输出触摸点
NSLog(@"开始的坐标:%@",NSStringFromCGPoint(point));
//获取触摸的次数
//NSUInteger tapCount = [pTouch tapCount];
if (tempImageView == nil) {
floatdistance1;
//下面就是高中的数学,不详细解释了
CGFloatxDist = (point.x-_circleView.frame.origin.x);
CGFloatyDist = (point.y-_circleView.frame.origin.y);
distance1 =sqrt((xDist * xDist) + (yDist * yDist));
floatdistance2;
//下面就是高中的数学,不详细解释了
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{
//获取任意一个touch对象
UITouch* pTouch = [touchesanyObject];
//获取对象所在的坐标
CGPointpoint = [pTouchlocationInView:self.view];
//以字符的形式输出触摸点
NSLog(@"结束的坐标:%@",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