2011-12-21 64 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強大。有關塊動畫的更多信息,請參考蘋果文檔