Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
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.
 
Last edited:
No, I mean, what does it do, why is it needed?

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.
 
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
 
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
 
Last edited:
You would use UIView's class method animationWithDuration:animation:

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 ?
 
Last edited:
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!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.