我是新来的编码。我正在制作一个应用程序,当点击一个按钮时,我需要使视图显示出来,并且视图应该显示为来自按钮本身。再次点击按钮时,视图应该返回到按钮(动画)。单击按钮时如何通过动画显示视图?
我有像翻转,卷曲的动画,但我不知道如何做到这一点。
我是新来的编码。我正在制作一个应用程序,当点击一个按钮时,我需要使视图显示出来,并且视图应该显示为来自按钮本身。再次点击按钮时,视图应该返回到按钮(动画)。单击按钮时如何通过动画显示视图?
我有像翻转,卷曲的动画,但我不知道如何做到这一点。
//这里animationButton是按钮的名称 //这里aView是一个视图
aView.view.center = animationButton.center;
现在,如图所示将视图缩小到一个小的比例,以便当它围起来时,它会显示为好像它是来自按钮本身。
CGAffineTransform trans = CGAffineTransformScale(aView.view.transform, 0.01, 0.01);
aView.view.transform = trans; // do it instantly, no animation
[self.view addSubview:aView.view];
// now return the view to normal dimension, animating this transformation
//现在用动画的帮助下,通过动画缩放视图一定很大程度上
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationCurveEaseInOut
animations:^{
aView.view.transform = CGAffineTransformScale(aView.view.transform, 70.0, 70.0);
}
completion:nil];
这是一个简单的例子。设置showView:
作为按钮的动作。
- (IBAction)showView:(UIButton *)sender {
// Create a view with the size and position of the tapped button
UIView *view = [[UIView alloc] initWithFrame:sender.frame];
// Set a color on the view
view.backgroundColor = [UIColor redColor];
// Add the new view to the main view. The new view will be displayed over any other views
[self.view addSubview:view];
// Animate the change of the new view's frame. We use the bounds of the main view.
[UIView animateWithDuration:3.6 animations:^{
view.frame = self.view.bounds;
}];
}
完整的解决方案:
首先产生用于视图和按钮的属性。你如何初始化这些取决于你。
@property (strong, nonatomic) UIButton *button;
@property (strong, nonatomic) UIView *aView;
...
@synthesize button = _button;
@synthesize aView = _aView;
然后,创建动画两帧之间的图,并且如果要求将在动画结束除去从它的父视图的方法。
- (void)animateView:(UIView *)view
fromRect:(CGRect)from
toRect:(CGRect)to
inParentView:(UIView *)parent
removeWhenDone:(BOOL)remove
{
if (!remove) {
[parent addSubview:view];
}
view.frame = from;
[UIView animateWithDuration:3.6 animations:^{
view.frame = to;
} completion:^(BOOL finished) {
if (remove) {
[view removeFromSuperview];
}
}];
}
然后创建一个布尔属性,指示是否显示的视图,并实现该属性的定制设定器。
@property (assign, nonatomic) BOOL viewShown;
...
@synthesize viewShown = _viewShown;
- (void)setViewShown:(BOOL)viewShown
{
_viewShown = viewShown;
if (_viewShown) {
// Insert your own toRect
[self animateView:self.aView fromRect:self.button.frame toRect:CGRectMake(0, 0, 100, 100) inParentView:self.view removeWhenDone:NO];
} else {
[self animateView:self.aView fromRect:self.aView.frame toRect:self.button.frame inParentView:self.view removeWhenDone:YES];
}
}
最后,在按钮的动作中,您翻转了viewShown
属性。
- (IBAction)showView:(UIButton *)sender {
self.viewShown = !self.viewShown;
}
Thanks..it是有益的......请看到我的编辑的问题.. –
请参阅我编辑的答案:) –