向下移动UIButton 10px并打开新的ViewController

时间:2011-12-21 10:00:07

标签: iphone objective-c ios uibutton

我是iOS开发的新手,这是我关于SO的第一个问题。

我想创建“动画”。

当我点击UIButton时,我想慢慢地(约0.5秒)向下移动10px,当我举起手指时,我想打开新的viewController。

我做了某事,但我不知道如何制作“动画”。

    UIImage *normalImage = [UIImage imageNamed:@"FirstImage"];        
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
    [firstButton setImage:normalImage forState:UIControlStateNormal];
    [firstButton addTarget:self action:@selector(showSecondViewController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:firstButton];


- (void)showSecondViewController; {
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    secondViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:secondViewController animated:YES];
    [progressViewController release]; 
}

3 个答案:

答案 0 :(得分:3)

[UIView animateWithDuration:(NSTimeInterval)duration animations:^(void) {
    //put your animation result here
    //then it will do animation for you
} completion:^(BOOL finished){
    //do what you need to do after animation complete
}];

例如:

UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//add firstButton to some view~
//....

firstButton.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.5 animations:^(void) {
    firstButton.transform = CGAffineTransformMakeTranslation(0,10);
} completion:^(BOOL finished){
    show your view controller~
}];

答案 1 :(得分:1)

[UIView animateWithDuration:0.5 animations: ^ {
  button.frame = CGRectMake:(button.frame.origin.x, button.frame.origin.y + 10.0f, button.frame.size.width, button.frame.size.height);
}];

答案 2 :(得分:0)

您可以在beginAnimations和commitAnimations块之间进行转换(重新定位),缩放和旋转,您可以在其中指定AnimationRepeatCount,AnimationCurve等。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:2];
..........................//etc.

firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
[UIView commitAnimations];

但在iOS4及以后版本中,鼓励动画由

执行

animateWithDuration:delay:options:animations:compeletion和类似的方法,它们可以激活非常强大的块。有关块动画的更多信息,请参阅Apple文档