ios - NSTimer does not call selector or repeat in time interval -


i newbie in objective-c , having trouble nstimer. while learning objective-c developing games, tried move sprite using left , right button of nstimer. but, seems selector not being called. searched stack overflow , found solutions, solutions i've seen, not working. here code-

#import "gameviewcontroller.h"  @interface gameviewcontroller () @property (strong) uiimage *playerimage; @property (strong) uiimageview *playerview; @property (strong) nstimer *movetimer; @property cgrect playerrect; @property int count; @end  @implementation gameviewcontroller  - (void)viewdidload {     [super viewdidload];     self.count=0;     self.playerimage = [uiimage imagenamed:@"ship.png"];     self.playerview = [[uiimageview alloc] initwithimage: self.                        playerimage];     self.playerrect = cgrectmake(50,400, 32,32);     self.playerview.frame = self.playerrect;     [self.view addsubview: self.playerview];  //   timer calls selector //    self.movetimer = [nstimer scheduledtimerwithtimeinterval:.03 //                                             target:self //                                           selector:@selector(moveplayerright:) //                                           userinfo:nil //                                            repeats:yes]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }   - (ibaction)moveleft:(id)sender {     [self releasetouch];     nslog(@"left move"); //this timer not call selector     self.movetimer = [nstimer scheduledtimerwithtimeinterval:.03                                                       target:self                                            selector:@selector(moveplayerleft:)                                                     userinfo:nil                                                      repeats:yes]; //    [[nsrunloop mainrunloop] addtimer:self.movetimer formode:nsdefaultrunloopmode]; //    [[nsrunloop mainrunloop] run];     //[self.movetimer fire]; }  - (ibaction)moveright:(id)sender {     [self releasetouch];     nslog(@"right move"); //    if(![nsthread ismainthread]) //        dispatch_async(dispatch_get_main_queue(), ^{ //this timer not call selector     self.movetimer = [nstimer scheduledtimerwithtimeinterval:.03                                                       target:self                                            selector:@selector(moveplayerright:)                                                     userinfo:nil                                                      repeats:yes];  }  - (ibaction)touchrelease:(id)sender {     [self releasetouch]; }  -(void) moveplayerright:(nstimer*)timer {     nslog(@"right move selector");     if(self.playerrect.origin.x <= 320){         self.playerrect = cgrectoffset(self.playerrect, 3, 0);         self.playerview.frame = self.playerrect;     } } -(void) moveplayerleft:(nstimer*)timer {     nslog(@"left move selector");     if(self.playerrect.origin.x >= 10){         self.playerrect = cgrectoffset(self.playerrect, -3, 0);         self.playerview.frame = self.playerrect;     } } -(void)releasetouch{     if(self.movetimer != nil){         [self.movetimer invalidate];         self.movetimer = nil;     } } @end 

let me explain i've tried.

  1. i tried scheduledtimerwithtimeinterval, shown in code. won't work.
  2. then tried timerwithtimeinterval nsrunloop. without nsrunloop [movetimer fire] fires nsrunloop repeats, not.
  3. then, in solution, found that, thread might creating problem; so, tried-

    if(![nsthread ismainthread]) dispatch_async(dispatch_get_main_queue(), ^{ 

    but, not case thread, debugger not step in "dispatch_async" line.

  4. to see nstimer works, ran in "viewdidload" method , works.

what doing wrong?

**solution: ** suggested @danil valeev , @aman.sood unchecked storyboard auto-layout, changed property 'nonatomic'; , selector being called timer :)

check moveplayerright: or moveplayerleft: called. , auto layout not using in view (i think reason). can disable auto layout in xib/storyboard file in first tab of utilities.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -