使用动画从窗口底部加载视图
我想使用动画从窗口底部加载视图,我知道这可以通过presentmodalViewController 完成,但根据我的应用程序的要求,它无效,因为我只想加载窗口一半的视图。所以我使用了动画,下面看看我做了什么。
-(void) displayPicker
{
UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[done setFrame:CGRectMake(197, 199, 103, 37)];
[done setBackgroundImage:[UIImage imageNamed:@"button_0.png"] forState:UIControlStateNormal];
[done setTitle:@"Done" forState:UIControlStateNormal];
[done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[done addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
[pickerDisplayView addSubview:datePicker];
[pickerDisplayView addSubview:done];
//animating here
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[self.view addSubview:pickerDisplayView];
[UIView commitAnimations];
}
名为 pickerDisplayView 的视图有两个名为 Picker 和 Button 的组件,但问题是 Picker 无法正常工作,并且视图 (pickerDisplayView) 都没有以平滑的方式加载。
@Matt:我已按照您的指示进行操作,并按照您的建议进行操作,这就是我所做的,
-(void) animationIn
{
CGPoint p = {x:0,y:185};
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[pickerDisplayView setCenter:p];
[UIView commitAnimations];
}
但问题是视图位于窗口顶部,而不是精确的 y 轴 185。我从 IB 获取了这些坐标。请帮助我,先生
i wanted to use animations to load a view from the bottom of the window, i know this can be done via presentmodalViewController but in as per the requirement of my app its not valid as i only want to load the view at half of the window. So i used Animations and heres a look at what i did
-(void) displayPicker
{
UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[done setFrame:CGRectMake(197, 199, 103, 37)];
[done setBackgroundImage:[UIImage imageNamed:@"button_0.png"] forState:UIControlStateNormal];
[done setTitle:@"Done" forState:UIControlStateNormal];
[done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[done addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
[pickerDisplayView addSubview:datePicker];
[pickerDisplayView addSubview:done];
//animating here
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[self.view addSubview:pickerDisplayView];
[UIView commitAnimations];
}
The view called pickerDisplayView has two components called Picker and Button, but the problem is that Picker is not working correctly and neither the view (pickerDisplayView) is loading in a smooth way.
@Matt: I have followed ur instructions and did as u advised here's what i did
-(void) animationIn
{
CGPoint p = {x:0,y:185};
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[pickerDisplayView setCenter:p];
[UIView commitAnimations];
}
But the prob is that the view is going at the top of the window and is not coming at exact y axis of 185. I took these coordinates from IB. Please help me sir
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将视图添加到屏幕外,然后将其动画化。
值
offscreenRect
是一个 CGRect,表示您希望视图开始的大小和位置。onscreenPosition
是您希望动画完成的位置的 CGPoint。对视图中心进行动画处理就足够了,因为您不想在动画处理时更改视图的大小。另外,我将动画放置在一个单独的方法中,该方法使用
-performSelector:withObject:afterDelay
进行调用,以确保动画有火。有时,如果您在添加要制作动画的视图的同一运行循环中启动动画,您将看不到动画运行。You will have to add the view offscreen and then animate it in.
The value
offscreenRect
is a CGRect for the size and position where you want the view to start.onscreenPosition
is a CGPoint for the position where you want the animation to finish. Animating the view's center is adequate since you don't want to change the size of the view while animating.Also, I placed the animation in a separate method that gets called with
-performSelector:withObject:afterDelay
to ensure that the animation with fire. Sometimes you won't see the animation run if you start it in the same run loop where you added the view you're animating.您需要在
beginAnimations:
之前进行init
和addSubview
。当您init
pickerDisplayView
定位它时,使其顶部与超级视图的底部内联。然后动画将向上滑动pickerDisplayView
。理想情况下,您应该在 nib 或loadView
方法中创建视图。 (我强烈建议使用笔尖而不是“loadView”)。在 loadView 中执行此操作(或移动到笔尖):
这是动画:
另外,我建议不要在框架中使用“幻数”。相反,您可以从 superviews 框架派生
pickerDisplayView
框架。如果您确实使用“魔术”数字,请务必以某种方式解释它们(即将它们分配给适当命名的变量或注释)。You need to
init
andaddSubview
beforebeginAnimations:
. When youinit
pickerDisplayView
position it so that the top of it is inline with the bottom of the superview. The animation will then slide uppickerDisplayView
. Ideally you should create the view in a nib or in theloadView
method. (I strongly recommend using nibs over 'loadView').Do this in loadView (or move to a nib):
This is the animation:
Also I'd advice against using 'magic numbers' in the frame. Instead you can derive the
pickerDisplayView
frame from the superviews frame. If you do use 'magic' numbers be sure explain them in some way (ie assigning them to a appropriately named variable or a comment).