Is there a way to make a custom animation when clicking on an iPhone button? I would like something like the App Store button - it shows the price, and then when you click on it, it changes color and the text changes to buy now, then when you click it again, it completes the purchase.
有没有办法在点击iPhone按钮时制作自定义动画?我想要像App Store按钮一样 - 它显示价格,然后当你点击它时,它会改变颜色,文本变为现在购买,然后当你再次点击它时,它完成了购买。
The UIViewAnimationTransition only contains a few values which don't provide the full functionality. Is it possible to do something like this but with the animation:
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];
}
This code works and will display the proper transition and allow for the second click to do the proper action, but the title and color changes instantly, not in a smooth animation. Is it possible to do such an animation easily, of do I have to create a UIButton subclass and do the animation "manually"? If so, what method would I have to override?
此代码有效并将显示正确的转换,并允许第二次单击执行正确的操作,但标题和颜色会立即更改,而不是平滑动画。是否可以轻松地制作这样的动画,我是否必须创建一个UIButton子类并“手动”动画?如果是这样,我必须覆盖哪种方法?
3 个解决方案
#1
Rather than just changing the title and target, etc of the button, have two separate buttons that do each action and look different. Then when you press the button, call a method that removes the current button from its super view and adds the new button to the view in its place. Wrap that up in some UIView animation block and you should get your animation.
而不是仅仅更改按钮的标题和目标等,有两个单独的按钮,每个操作和看起来不同。然后,当您按下按钮时,调用一个方法,从超级视图中删除当前按钮,并将新按钮添加到视图中。在一些UIView动画块中包装它,你应该得到你的动画。
Also, for this to work, you'll need to set the animation transition on the superview of the button. So it might be handy to have a 'container view' as it may be and then add you buttons as subviews of that.
此外,为此,您需要在按钮的超级视图上设置动画过渡。所以有一个'容器视图'可能会很方便,然后添加按钮作为其子视图。
Pseudo code might look something like so:
伪代码可能看起来像这样:
[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];
#2
A Standard UIView will do what you want. UIView's backgroundColor is an animatable property.
标准UIView将做你想要的。 UIView的backgroundColor是一个可动画的属性。
I would make own "button" by subclassing UIView OR alternatively subclass a UIButton and add subviews to it. (note: UIButtons were a pain about this in 2.2, not sure in 3.0)
我会通过子类化UIView来创建自己的“按钮”,或者将子类化为UIButton并向其添加子视图。 (注意:UIButton在2.2中对此很痛苦,在3.0中不确定)
You need a UILabel for each text state:
每个文本状态都需要一个UILabel:
UILabel *textForStateOne;
UILabel *textForStateTwo;
And you can change the background color of the parent view.
您可以更改父视图的背景颜色。
So then you make a method in your new button class:
那么你在新的按钮类中创建一个方法:
-(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
}
}
#3
UIView *btnView4=button;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:btnView4 cache:YES];
[UIView commitAnimations];``
#1
Rather than just changing the title and target, etc of the button, have two separate buttons that do each action and look different. Then when you press the button, call a method that removes the current button from its super view and adds the new button to the view in its place. Wrap that up in some UIView animation block and you should get your animation.
而不是仅仅更改按钮的标题和目标等,有两个单独的按钮,每个操作和看起来不同。然后,当您按下按钮时,调用一个方法,从超级视图中删除当前按钮,并将新按钮添加到视图中。在一些UIView动画块中包装它,你应该得到你的动画。
Also, for this to work, you'll need to set the animation transition on the superview of the button. So it might be handy to have a 'container view' as it may be and then add you buttons as subviews of that.
此外,为此,您需要在按钮的超级视图上设置动画过渡。所以有一个'容器视图'可能会很方便,然后添加按钮作为其子视图。
Pseudo code might look something like so:
伪代码可能看起来像这样:
[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];
#2
A Standard UIView will do what you want. UIView's backgroundColor is an animatable property.
标准UIView将做你想要的。 UIView的backgroundColor是一个可动画的属性。
I would make own "button" by subclassing UIView OR alternatively subclass a UIButton and add subviews to it. (note: UIButtons were a pain about this in 2.2, not sure in 3.0)
我会通过子类化UIView来创建自己的“按钮”,或者将子类化为UIButton并向其添加子视图。 (注意:UIButton在2.2中对此很痛苦,在3.0中不确定)
You need a UILabel for each text state:
每个文本状态都需要一个UILabel:
UILabel *textForStateOne;
UILabel *textForStateTwo;
And you can change the background color of the parent view.
您可以更改父视图的背景颜色。
So then you make a method in your new button class:
那么你在新的按钮类中创建一个方法:
-(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
}
}
#3
UIView *btnView4=button;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:btnView4 cache:YES];
[UIView commitAnimations];``