有没有一种方法可以在点击iPhone按钮时制作自定义动画?我想要像App Store按钮那样的东西-它显示价格,然后当你点击它时,它会改变颜色,文本变成buy now,然后当你再次点击它时,它就完成了购买。
UIViewAnimationTransition只包含几个不能提供全部功能的值。有没有可能用动画来做这样的事情:
- (IBAction) changeButtonDisplay:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:purchaseButton cache:NO]; //the built in UIViewAnimationTransition values don't provide enough flexibility
[purchaseButton setTitle:@"Buy Now" forState:UIControlStateNormal];
[purchaseButton setBackgroundColor:[UIColor greenColor]];
[purchaseButton addTarget:self action:@selector(purchaseItems:) forControlEvents:UIControlEventTouchUpInside];
[UIView commitAnimations];
}此代码工作并将显示正确的过渡,并允许第二次单击以执行适当的操作,但标题和颜色会立即更改,而不是在平滑的动画中。是否可以轻松地制作这样的动画,或者我是否必须创建一个UIButton子类并“手动”制作动画?如果是这样,我必须重写什么方法?
发布于 2009-06-22 23:08:06
不只是更改按钮的标题和目标等,而是有两个独立的按钮来执行每个操作,并且看起来不同。然后,当您按下按钮时,调用一个方法,该方法将当前按钮从其超级视图中移除,并将新按钮添加到视图中的位置。将其封装在某个UIView动画块中,您应该可以获得动画。
此外,要使此功能正常工作,您需要在按钮的superview上设置动画过渡。因此,如果有一个“容器视图”,然后将您的按钮添加为该视图的子视图,可能会很方便。
伪代码可能如下所示:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:buttonConteinerView cache:NO]; //the built in UIViewAnimationTransition values don't provide enough flexibility
[purchaseButton retain];
[purchaseButton removeFromSuperView];
[buttonContainerView addSubview:secondPurchaseButton];
[UIView commitAnimations];发布于 2009-06-23 01:05:05
一个标准的UIView会做你想做的事情。‘s视图的backgroundColor是一个可设置动画的属性。
我会通过对UIView进行子类化来创建自己的“按钮”,或者将UIButton子类化并向其添加子视图。(注意: UIButtons在2.2中是一个痛苦的问题,在3.0中不确定)
每个文本状态都需要一个UILabel:
UILabel *textForStateOne;
UILabel *textForStateTwo;您还可以更改父视图的背景色。
因此,您可以在新的按钮类中创建一个方法:
-(void)toggleState{
myState = !myState;
if(myState){
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[textForStateOne setAlpha:1.0]
[textForStateTwo setAlpha:0.0]
[self setBackgroundColor:[UIColor greenColor]];
[UIView commitAnimations];
} else{
//do the opposite here
}
}发布于 2013-08-05 18:13:20
UIView *btnView4=button;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:btnView4 cache:YES];
[UIView commitAnimations];``https://stackoverflow.com/questions/1029805
复制相似问题