2011-12-21 63 views
4

我是新手在iOS开发,这是我的第一个问题。移动UIButton 10px并打开新的ViewController

我想创建“动画”。

当我点击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
[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~ 
}]; 
+0

感谢@IPaPa但我的按钮“自动动画”。当我运行我的应用程序时,它会自动移动10px。我错在哪里? 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]; [self.view addSubview:firstButton]; – TCvetkovic 2011-12-21 11:12:45

+0

firstButton.transform = CGAffineTransformIdentity; [UIView animateWithDuration:0.5动画:^(void){ firstButton.transform = CGAffineTransformMakeTranslation(0,10); }完成:^(布尔完成){ //显示您的视图控制器〜 }]; – TCvetkovic 2011-12-21 11:17:03

+0

是否将动画代码放在按钮TouchUpInside回调中? [UIView animation .....]之后,动画会立即运行 – IPaPa 2011-12-21 18:41:23

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); 
}]; 
0

你可以把beginAnimations和commitAnimations块之间的转换(复位),缩放和rotatations在那里你可以指定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和类似的方法,invlove块是ve强大。有关块动画的更多信息,请参考苹果文档