移动UIButton

时间:2010-05-03 16:37:00

标签: iphone objective-c ipad

我试图在菜单中上下移动UIButton。 我用以下解决方案得到的问题是计时器不准确。有时按钮向上移动122px,有时仅为120px。我怎么解决这个问题?

    -(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 个答案:

答案 0 :(得分:1)

为什么不使用UIView动画块?

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

动画块(评论所在的位置)所做的更改将在commitAnimations后生成动画。

检查类mathods以获取更多选项:http://tinyurl.com/34y8j5o