我是新手在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];
}
感謝@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
firstButton.transform = CGAffineTransformIdentity; [UIView animateWithDuration:0.5動畫:^(void){ firstButton.transform = CGAffineTransformMakeTranslation(0,10); }完成:^(布爾完成){ //顯示您的視圖控制器〜 }]; – TCvetkovic 2011-12-21 11:17:03
是否將動畫代碼放在按鈕TouchUpInside回調中? [UIView animation .....]之後,動畫會立即運行 – IPaPa 2011-12-21 18:41:23