iOS 陀螺儀與UITouch 簡(jiǎn)單運(yùn)用

#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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末嫉拐,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子命贴,更是在濱河造成了極大的恐慌,老刑警劉巖秆吵,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件样傍,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡镜遣,警方通過(guò)查閱死者的電腦和手機(jī)己肮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)悲关,“玉大人谎僻,你說(shuō)我怎么就攤上這事≡⑷瑁” “怎么了艘绍?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)秫筏。 經(jīng)常有香客問(wèn)我诱鞠,道長(zhǎng)挎挖,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任般甲,我火速辦了婚禮肋乍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘敷存。我一直安慰自己墓造,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布锚烦。 她就那樣靜靜地躺著觅闽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪涮俄。 梳的紋絲不亂的頭發(fā)上蛉拙,一...
    開(kāi)封第一講書(shū)人閱讀 51,578評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音彻亲,去河邊找鬼孕锄。 笑死,一個(gè)胖子當(dāng)著我的面吹牛苞尝,可吹牛的內(nèi)容都是我干的畸肆。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼宙址,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼轴脐!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起抡砂,我...
    開(kāi)封第一講書(shū)人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤大咱,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后注益,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體碴巾,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年丑搔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了厦瓢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡低匙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出碳锈,到底是詐尸還是另有隱情顽冶,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布售碳,位于F島的核電站强重,受9級(jí)特大地震影響绞呈,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜间景,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一佃声、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧倘要,春花似錦圾亏、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至泽西,卻和暖如春曹铃,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背捧杉。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工陕见, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人味抖。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓评甜,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親非竿。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蜕着,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1、禁止手機(jī)睡眠 [UIApplication sharedApplication].idleTimerDisab...
    小小夕舞閱讀 1,454評(píng)論 1 1
  • UIView+SGFrame.h #import @interface UIView (SGFrame) @pro...
    超越時(shí)空的思念_8a22閱讀 1,811評(píng)論 1 1
  • 今天是平凡的一天红柱。 早上承匣,有一個(gè)關(guān)于超導(dǎo)歷史的講座,去參加了锤悄。自從實(shí)習(xí)后韧骗,我很少有機(jī)會(huì)去聽(tīng)相關(guān)的報(bào)告。今天聽(tīng)了后零聚,...
    方塊V閱讀 177評(píng)論 0 0
  • 各位游戲迷們隶症,在游戲界里闖了這么多年了政模,玩過(guò)多少游戲,打怪升級(jí)其實(shí)并不是游戲的真正樂(lè)趣蚂会,和游戲開(kāi)發(fā)者和設(shè)計(jì)獅斗智...
    源音塘閱讀 526評(píng)論 0 0
  • 放棄了許多年手寫(xiě)日記的習(xí)慣淋样,已經(jīng)有十多年了。 重啟手寫(xiě)日記胁住,內(nèi)心的不安全感依然存在趁猴,寫(xiě)過(guò)多年的日記都被人看過(guò)刊咳,是一...
    好菇?jīng)霭」饷⑷f(wàn)丈閱讀 266評(píng)論 0 0