views:

96

answers:

1

I've tried to move a UIButton up and down in a menu. The problem I've got with the following solution is that the timer is not accurate. Sometimes the Button is moved up 122px, sometimes only 120px. How I can fix this?

    -(IBAction)marketTabClicked:(id)sender {

    if (marketTabExtended) {
        NSLog(@"marketTabExtended = YES");
        return;
    }
    else {
        if (iPhoneAppsExtended) {
            timer = [NSTimer scheduledTimerWithTimeInterval:0.005 target: self selector: @selector(animateItemApps) userInfo: nil repeats: YES];
        }
        else {
            if (homepageExtended) {
                timer = [NSTimer scheduledTimerWithTimeInterval:0.005 target: self selector: @selector(animateItemHomepage) userInfo: nil repeats: YES];
            }
            else {
                timer = [NSTimer scheduledTimerWithTimeInterval:0.005 target: self selector: @selector(animateItemMarketing) userInfo: nil repeats: YES];
            }

        }

    }

    [self performSelector:@selector(stopTimer) withObject:self afterDelay:0.605];
    iPhoneAppsExtended = NO;
    homepageExtended = NO;
    marketTabExtended = NO;
    marketTabExtended = YES;

}



-(void)animateItemApps {
    CGPoint movement;
    movement = CGPointMake(0, -1);
    homepage.center = CGPointMake(homepage.center.x, homepage.center.y + movement.y);
}

-(void)animateItemHomepage {
    CGPoint movement;
    movement = CGPointMake(0, 1);
    homepage.center = CGPointMake(homepage.center.x, homepage.center.y + movement.y);
    //marketTab.center = CGPointMake(marketTab.center.x, marketTab.center.y + movement.y);
}

-(void)animateItemMarketing {
    CGPoint movement;
    movement = CGPointMake(0, -1);
    //marketTab.center = CGPointMake(marketTab.center.x, marketTab.center.y + movement.y);
    homepage.center = CGPointMake(homepage.center.x, homepage.center.y + movement.y);
}

-(void)stopTimer {
    [timer invalidate];
}
+1  A: 

Why don't you use UIView animation blocks?

[UIView beginAnimations:nil context:nil];
//Change UIButton frame here
[UIView commitAnimations];

Changes made in the animation block (where the comment is), will be animated after commitAnimations.

Check the class mathods for more options: http://tinyurl.com/34y8j5o

Rengers
Thanks worked perfectly!
pbcoder