Resolved Sum bug in my code

Discussion in 'iOS Programming' started by DennisBlah, Jul 20, 2014.

  1. DennisBlah, Jul 20, 2014
    Last edited: Jul 21, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hey all,

    I made a sidebar that slides in and out.
    I done this with a timer.

    Code:
    -(void)moveViews {
        counter++;
        if(!inNavi) {
            if(slideOver)
                [mainView setFrame: CGRectMake(mainView.frame.origin.x - 5, mainView.frame.origin.y, mainView.frame.size.width, mainView.frame.size.height)];
            [naviView setFrame: CGRectMake(naviView.frame.origin.x - 5, naviView.frame.origin.y, naviView.frame.size.width, naviView.frame.size.height)];
        } else {
            if(slideOver)
                [mainView setFrame: CGRectMake(mainView.frame.origin.x + 5, mainView.frame.origin.y, mainView.frame.size.width, mainView.frame.size.height)];
            [naviView setFrame: CGRectMake(naviView.frame.origin.x + 5, naviView.frame.origin.y, naviView.frame.size.width, naviView.frame.size.height)];
        }
        if(counter == (int)(naviView.frame.size.width / 5)) {
            if(inNavi) {
                inNavi = NO;
                [naviView setFrame: CGRectMake(mainView.frame.size.width, naviView.frame.origin.y, naviView.frame.size.width, naviView.frame.size.height)];
                [touchView setFrame: CGRectMake(mainView.frame.size.width, touchView.frame.origin.y, touchView.frame.size.width, touchView.frame.size.height)];
            } else {
                inNavi = YES;
                [touchView setFrame: CGRectMake(0, touchView.frame.origin.y, touchView.frame.size.width, touchView.frame.size.height)];
            }
            
            [openNaviButtons invalidate];
            openNaviButtons = nil;
            isAnimating = NO;
            counter = 0;
        }
    }
    
    The touchview comes into the play when the sidebar is in view, and I'm detecting touches on there so i can move the sidebar out when I touched outside the sidebar.

    This works great, I love it! However, after moving to an other viewcontroller and going back to the main.
    The sidebar still works, it opens, closes, and then it won't open anymore because when I closed it after I came back to the view, it seems that my timer wont get invalidated. (I putted an log inside the if statement, when counter meets its end, and it keeps sending that log)

    This is how Im calling my timer.

    Code:
    -(IBAction)openButtons:(id)sender {
        if(!inNavi && !isAnimating) {
            isAnimating = YES;
            openNaviButtons = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(moveViews) userInfo:nil repeats: YES];
        }
    }
    
    -(IBAction)closeButtons:(id)sender {
        if(inNavi && !isAnimating) {
            isAnimating = YES;
            openNaviButtons = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(moveViews) userInfo:nil repeats: YES];
        }
    }
    
    setting isAnimating just makes the app be sure that the sidebar wont start bouncing for ever :)
    (for the people with a trigger finger ;-) )

    It works like a charm untill I moved to an other viewcontroller and came back.

    My viewDidAppear contains this:
    Code:
        [naviView setFrame: CGRectMake(mainView.frame.size.width, naviView.frame.origin.y, naviView.frame.size.width, naviView.frame.size.height)];
        isAnimating = NO;
        inNavi = NO;
        counter = 0;   //Forgot to mention this one.
    
    I really really can't see the fault. Anyone else does ?? I could really need a help here.

    -----

    Forgot to mention I'm using an swipe gesture on the naviView and a touchesBegan on the touchView.

    Huge improvement: UIView animationWithDuration:animation
    Fix: Properly delegate my gestures.
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    Sorry forgot to mention that counter = 0 in ViewDidAppear
     
  4. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #4
    No, I mean, what does it do, why is it needed?
     
  5. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    Because I can't make a NSTimer repeat 5 times ? Thats why I keep track of counter. and if counter is equal to the width of the naviView devided by 5. Then I know the naviView is in the correct place and the timer can stop.
     
  6. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #6
    Wait wait! Are you manually animating an UIView into place, bit by bit, by using a NSTimer?

    ps: yes you can make a NSTimer repeat 5 times
     
  7. DennisBlah, Jul 21, 2014
    Last edited: Jul 21, 2014

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #7
    Ehm yes Im doing that with the timer
    How wud i do it in a other way then?

    And I thought repeats cud only take a boolean
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    You would use UIView's class method animationWithDuration:animation:
     
  9. DennisBlah, Jul 21, 2014
    Last edited: Jul 21, 2014

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #9
    Did I mention that naviView is a UIView of 150 width?
    I want to move it from the right untill .. X == view.frame.size.width - 150.

    How can I do this with animationWithDuration:animation ?
     
  10. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #10
  11. DennisBlah, Jul 21, 2014
    Last edited: Jul 21, 2014

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #11
    Thank you
    Ive seen this comming by indeed
    Im now home from work so I will give this a show in a moment :) thanks again!

    This is actually pretty easy at it goes :) It's freakin smooth and no need for all the math and stuff!
    This is great!

    To come back on my problem :
    It was an delegate fault of the recognizers... I will keep this in mind for next time I'm posting a problem!

    Thanks all of you!
     

Share This Page