将 UIButton 向下移动 10px 并打开新的 ViewController
Posted
技术标签:
【中文标题】将 UIButton 向下移动 10px 并打开新的 ViewController【英文标题】:Move UIButton 10px down and open new ViewController 【发布时间】:2011-12-21 10:00:06 【问题描述】:我是 ios 开发的新手,这是我关于 SO 的第一个问题。
我想制作“动画”。
当我点击 UIButton 时,我想缓慢(大约 0.5 秒)将其向下移动 10 像素,当我抬起手指时,我想打开新的 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];
【问题讨论】:
【参考方案1】:[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~
];
【讨论】:
感谢@IPaPa,但我的按钮是“自动动画”。当我运行我的应用程序时,它会自动移动 10 像素。我哪里错了? 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]; firstButton.transform = CGAffineTransformIdentity; [UIView animateWithDuration:0.5 动画:^(void) firstButton.transform = CGAffineTransformMakeTranslation(0,10); completion:^(BOOL finished) //显示你的视图控制器~ ]; 你把动画代码放在按钮TouchUpInside回调中了吗?在你做[UIView动画.....]之后,动画将立即运行【参考方案2】:[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);
];
【讨论】:
【参考方案3】:您可以在 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:completion 和包含非常强大的块的类似方法。更多关于方块动画的信息请参考苹果文档
【讨论】:
以上是关于将 UIButton 向下移动 10px 并打开新的 ViewController的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollview 在向下滚动并推送新的视图控制器后向上移动内容