主要的UIView包含两个子视图 - UIView_1
和UIView_2
在UIView_2
中,有一个按钮可显示或隐藏UIView_1
例如,当用户触摸按钮以显示UIView_1
时, UIView_1
将向下滑动,UIView_2
将向下推进,并进行转换。
我对动画知之甚少。有人可以给我看一些示例代码供参考吗?
我应该使用CGAffineTransformMakeTranslation吗?
感谢。
答案 0 :(得分:20)
你不需要那么复杂的东西。只需更改视图的帧大小。
NSTimeInterval animationDuration = /* determine length of animation */;
CGRect newFrameSize = /* determine what the frame size should be */;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
theViewToChange.frame = newFrameSize;
[UIView commitAnimations];
答案 1 :(得分:7)
简单隐藏/显示淡入/淡出效果 `
/ 显示 /
sliderView.hidden = NO;
sliderView.alpha = 0.1;
[UIView animateWithDuration:0.25 animations:^{
sliderView.alpha = 1.0f;
} completion:^(BOOL finished) {
// do some
}];
/ 隐藏 /
[UIView animateWithDuration:0.25 animations:^{
sliderView.frame = CGRectMake(130, 30, 0, 0);
[sliderView setAlpha:0.1f];
} completion:^(BOOL finished) {
sliderView.hidden = YES;
}];
`
答案 2 :(得分:3)
这取决于您对UIView_2
的要求。
将UIView_1
放在Interface Builder的UIView_2
下面。
尺寸UIView_2
占据UINavigationBar
下方的所有空间。
使用以下代码调整(uiview2_resized_rect
)UIView_2
的框架大小,或翻译/移动UIView_2
的框架(使用uiview2_translated_rect
) :
CGRect uiview1_original_rect = UIView_1.frame;
CGRect uiview2_original_rect = UIView_2.frame;
CGRect uiview2_translated_rect = CGRectMake(uiview2_original_rect.origin.x,
uiview2_original_rect.origin.y+uiview1_original_rect.size.height,
uiview2_original_rect.size.width,
uiview2_original_rect.size.height);
CGRect uiview2_resized_rect = CGRectMake(uiview2_original_rect.origin.x,
uiview2_original_rect.origin.y+uiview1_original_rect.size.height,
uiview2_original_rect.size.width,
uiview2_original_rect.size.height-uiview1_original_rect.size.height);
[UIView animateWithDuration:0.300 delay:0.0
options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState
animations:^{
//uncomment this and comment out the other if you want to move UIView_2 down to show UIView_1
//UIView_2.frame = uiview2_translated_rect;
UIView_2.frame = uiview2_resized_rect;
} completion:^(BOOL finished) {
}];