我是新手在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