使用动画从窗口底部加载视图

发布于 2024-10-01 14:30:12 字数 1592 浏览 4 评论 0原文

我想使用动画从窗口底部加载视图,我知道这可以通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2024-10-08 14:30:13

您必须将视图添加到屏幕外,然后将其动画化。

// In your viewDidLoad or wherever you initialize the view.
pickerDisplayView = [[UIView alloc]initWithFrame:offscreenRect];

[self.view addSubview:pickerDisplayView];
[self performSelector:@selector(animateIn) withObject:nil afterDelay:0.25];

// Declare animateIn
- (void)animateIn
{
    UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [pickerDisplayView setCenter:onscreenPosition];
    [UIView commitAnimations];
}

offscreenRect 是一个 CGRect,表示您希望视图开始的大小和位置。 onscreenPosition 是您希望动画完成的位置的 CGPoint。对视图中心进行动画处理就足够了,因为您不想在动画处理时更改视图的大小。

另外,我将动画放置在一个单独的方法中,该方法使用 -performSelector:withObject:afterDelay 进行调用,以确保动画有火。有时,如果您在添加要制作动画的视图的同一运行循环中启动动画,您将看不到动画运行。

You will have to add the view offscreen and then animate it in.

// In your viewDidLoad or wherever you initialize the view.
pickerDisplayView = [[UIView alloc]initWithFrame:offscreenRect];

[self.view addSubview:pickerDisplayView];
[self performSelector:@selector(animateIn) withObject:nil afterDelay:0.25];

// Declare animateIn
- (void)animateIn
{
    UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [pickerDisplayView setCenter:onscreenPosition];
    [UIView commitAnimations];
}

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.

巴黎盛开的樱花 2024-10-08 14:30:13

您需要在 beginAnimations: 之前进行 initaddSubview。当您 init pickerDisplayView 定位它时,使其顶部与超级视图的底部内联。然后动画将向上滑动pickerDisplayView。理想情况下,您应该在 nib 或 loadView 方法中创建视图。 (我强烈建议使用笔尖而不是“loadView”)。

在 loadView 中执行此操作(或移动到笔尖):

CGRect offScreenFrame = ?????; //Figure this out from self.view
self.pickerDisplayView = [[UIView alloc]initWithFrame:offScreenFrame];
[self.view addSubview:self.pickerDisplayView];

这是动画:

[UIView beginAnimations:nil context:NULL];
self.pickerDisplayView.frame = CGRectMake(0, 185, 320, 275);
//[UIView setAnimationDuration:0.5]; //this line is probably not needed
[UIView commitAnimations];

另外,我建议不要在框架中使用“幻数”。相反,您可以从 superviews 框架派生 pickerDisplayView 框架。如果您确实使用“魔术”数字,请务必以某种方式解释它们(即将它们分配给适当命名的变量或注释)。

You need to init and addSubview before beginAnimations:. When you init pickerDisplayView position it so that the top of it is inline with the bottom of the superview. The animation will then slide up pickerDisplayView. Ideally you should create the view in a nib or in the loadView method. (I strongly recommend using nibs over 'loadView').

Do this in loadView (or move to a nib):

CGRect offScreenFrame = ?????; //Figure this out from self.view
self.pickerDisplayView = [[UIView alloc]initWithFrame:offScreenFrame];
[self.view addSubview:self.pickerDisplayView];

This is the animation:

[UIView beginAnimations:nil context:NULL];
self.pickerDisplayView.frame = CGRectMake(0, 185, 320, 275);
//[UIView setAnimationDuration:0.5]; //this line is probably not needed
[UIView commitAnimations];

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文