实现UIView的滑动效果

发布于 2024-10-26 22:28:04 字数 1250 浏览 1 评论 0原文

所有,

我正在拼命地编写一种方法,该方法将以一种“向上滑动”效果添加子视图的动画,其中视图从特定点滑到屏幕上。我可以让 UIImageView 工作,但我很难将它应用到 UIPickerView 上。所发生的情况是,UIPickerView 只移动和动画其高度的大约 1/4。 UIImageView 真正以 0 高度开始并向上滑动到屏幕上。

这是到目前为止我的代码:


+ (void) slideupView:(UIView*) view duration:(CGFloat) duration completion:(void (^)(BOOL 
                                     finished)) completion {
    CGRect endBounds = view.bounds;
    CGRect startBounds = endBounds;

    startBounds.size.height = 0;

    view.bounds = startBounds;
    view.contentMode = UIViewContentModeTop;
    view.clipsToBounds = YES;
    view.layer.anchorPoint = CGPointMake(0.5, 1.0); 

    CGPoint pos = view.layer.position;
    pos.y += (endBounds.size.height / 2);
    view.layer.position = pos;

    [UIView animateWithDuration:duration delay:0.0        
                 options:UIViewAnimationOptionAllowUserInteraction
         animations:^{
             view.bounds = endBounds;
         }
         completion:completion];
}

从我读过的内容来看,听起来鼓励的方法是使用边界矩形而不是框架,因为如果您修改了变换,框架方法将不起作用。

我还读到无法更改 UIPickerView 的高度,所以我想知道这是否是此问题的原因。诚然,我还没有在任何其他 UIView 子类上尝试过。

有什么想法吗?有更好的方法吗?我只想将视图动画显示到屏幕上,类似于呈现模态视图控制器的视图,只是我不希望它覆盖整个屏幕。我只想要向上滑动的效果,就像键盘出现在屏幕上一样。

谢谢! 贾斯汀

All,

I'm trying desperately to write a method that will animate adding a subview in a kind of "slide up" effect, where the view slides onto the screen from a particular point. I can get a UIImageView to work, but I'm having a heck of a time applying it to a UIPickerView. What happens is, the UIPickerView only moves and animates for about 1/4 of it's height. The UIImageView truly starts w/ a height of 0 and slides up onto the screen.

Here is my code so far:


+ (void) slideupView:(UIView*) view duration:(CGFloat) duration completion:(void (^)(BOOL 
                                     finished)) completion {
    CGRect endBounds = view.bounds;
    CGRect startBounds = endBounds;

    startBounds.size.height = 0;

    view.bounds = startBounds;
    view.contentMode = UIViewContentModeTop;
    view.clipsToBounds = YES;
    view.layer.anchorPoint = CGPointMake(0.5, 1.0); 

    CGPoint pos = view.layer.position;
    pos.y += (endBounds.size.height / 2);
    view.layer.position = pos;

    [UIView animateWithDuration:duration delay:0.0        
                 options:UIViewAnimationOptionAllowUserInteraction
         animations:^{
             view.bounds = endBounds;
         }
         completion:completion];
}

From the things I've read, it sounds like the encouraged approach is to use the bounds rectangle instead of the frame since the frame approach won't work if you've modified the transform.

I've ALSO read that one can't change the height of a UIPickerView, so I'm wondering if that's the reason for this problem. Admittedly, I haven't tried it on any other UIView subclass.

Any thoughts? Is there a better approach? I just want to animate a view onto the screen, similar to presenting a modal view controller's view, except that I don't want it to cover the entire screen. I just want a slide up effect, like when the keyboard appears on the screen.

Thanks!
Justin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

眼中杀气 2024-11-02 22:28:04

如果您只想像键盘出现一样向上滑动视图,那么您可以通过动画更改视图的 y 坐标来实现。

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f,460.0f,320.0f,260.0f)];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:[self view] cache:YES];
[view setFrame:CGRectMake(0.0f,200.0f,320.0f,260.0f)];
[UIView commitAnimations];

If you just want to slide up your view like as the keyboard comes up then you can do it by changing y coordinate of your view by animation.

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f,460.0f,320.0f,260.0f)];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:[self view] cache:YES];
[view setFrame:CGRectMake(0.0f,200.0f,320.0f,260.0f)];
[UIView commitAnimations];
情独悲 2024-11-02 22:28:04

“Bounds”指的是视图本身的内部坐标系,而“frame”和“center”指的是视图在其父坐标系中的位置。因此,要将视图滑动到屏幕上,您需要为框架或中心设置动画。

"Bounds" refers to the internal coordinate system of the view itself, whereas "frame" and "center" refer to the position of the view in its parent's coordinate system. So to slide a view onto the screen you'll want to animate either the frame or the center.

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