【问题标题】:UIView appereance from bottom to top and vice versa(Core Animation)UIView 从下到上,反之亦然(核心动画)
【发布时间】:2017-07-08 16:32:37
【问题描述】:

我的目标是通过 Core Animation 理解和实现功能。
我认为这并不难,但不幸的是我不知道 swift/Obj C 并且很难理解原生示例。


可视化实现

那么我到底想要做什么(如图所示的几个步骤):
1.
2.
3.
4.

以及隐藏视图的相同步骤(反之亦然,从上到下)直到:

另外,我想让这个 UIView 更通用,我的意思是把这个 UIView 放在我的 StoryBoard 上,并对 AutoLayout 施加约束(以支持不同的设备屏幕) .

有什么想法吗?谢谢!

【问题讨论】:

  • 您想要实现的功能不需要 Core Animation。它可以通过简单的UIView animateWithDuration: API 来完成。我会强烈推荐阅读一些关于这个主题的教程并亲自尝试实现。想出一些代码,如果它有错误,你可以随时在这里要求调整。 :) raywenderlich.com/2454/…

标签: ios xamarin uiview xamarin.ios core-animation


【解决方案1】:

你可以像这个扩展一样使用

extension UIView{
    func animShow(){
        UIView.animate(withDuration: 2, delay: 0, options: [.curveEaseIn],
                       animations: {
                        self.center.y -= self.bounds.height
                        self.layoutIfNeeded()
        }, completion: nil)
        self.isHidden = false
    }
    func animHide(){
        UIView.animate(withDuration: 2, delay: 0, options: [.curveLinear],
                       animations: {
                        self.center.y += self.bounds.height
                        self.layoutIfNeeded()

        },  completion: {(_ completed: Bool) -> Void in
        self.isHidden = true
            })
    }
}

【讨论】:

    【解决方案2】:

    假设原始视图类似于:

    var view = new UIView(new CGRect(View.Frame.Left, View.Frame.Height - 200, View.Frame.Right, 0));
    view.BackgroundColor = UIColor.Clear;
    

    显示:

    UIView.Animate(2.0, 0.0,
        UIViewAnimationOptions.CurveLinear,
        () =>
            {
                view.BackgroundColor = UIColor.Blue;
                var height = 100;
                view.Frame = new CGRect(View.Frame.Left, view.Frame.Y - height , view.Superview.Frame.Right, height);
            },
        () =>
            {
                // anim done
            }                                  
    );
    

    隐藏:

    UIView.Animate(2.0, 0.0,
        UIViewAnimationOptions.CurveLinear,
        () =>
            {
                view.BackgroundColor = UIColor.Clear;
                var height = 100;
                view.Frame = new CGRect(View.Frame.Left, view.Frame.Y + height, view.Superview.Frame.Right, 0);
    
            },
        () =>
            {
                view.Hidden = true;
            }
    );
    

    【讨论】:

    • 谢谢!!!正是我需要的并且工作正常。但是为什么当我尝试使用我自己在情节提要中创建的 UIView 时,它不能正常工作,我的意思是它显示完整视图高度? i.stack.imgur.com/fuVhy.gif 请看这个 .gif 动画。另外,我尝试将参数“height”设置为“View.Frame.Height”,它需要真正的视图高度,但不起作用:(.
    • 另外,我不知道,但 AutoLayout 的约束似乎在 Xamarin.iOS 设计器(故事板)中不起作用。你有同样的行为吗?我想我会提出一个关于它的新问题......
    • @VetaLio 如果您通过情节提要/xib 使用约束,您将不想为Frame 高度设置动画,而是为约束高度设置动画。为高度约束添加一个出口并在动画动作中执行this.HeightCon.Constant = 100; 之类的操作。参考:出口创建:developer.xamarin.com/guides/ios/user_interface/controls/…
    • 我不知道,但我不能通过“@”给你写信。所以我试过了,但它没有奏效,所以明天我再试一次。顺便说一句,我会尽快将您的答案标记为解决方案,但需要解决我的问题:)。
    • @SushiHangover,请您在 Swift 4 上提供帮助
    【解决方案3】:

    看到我的观点相反,我直接在做改变,测试它是否适合你,

    显示逻辑

    //Add your view on storyBoard / programmatically bellow tab bar
    
    [self.view bringSubviewToFront:self.miniMenuView];
    
    CGRect rectformedicationTableViewcell;// = CGRectZero;
    
    rectformedicationTableViewcell = CGRectMake(0.0f, self.view.frame.size.hight, self.view.frame.size.width, 150.0f);
    
    self.miniMenuView.frame = rectformedicationTableViewcell;
    
    if([self.miniMenuView superview]) {
        self.miniMenuView.hidden = YES;
    }
    self.miniMenuView.hidden = NO;
    
    [UIView animateWithDuration:0.3f
                          delay:0.0f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                           [self.miniMenuView setFrame:CGRectMake(0.0f, self.view.frame.size.hight - 150.0f, self.view.frame.size.width, 150.0f)];
    
                     }
                     completion:nil];
    

    隐藏逻辑

    [self.view sendSubviewToBack:self.miniMenuView];
    
    [UIView animateWithDuration:0.3f
                          delay:0.0f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
    
                                                     [self.miniMenuView setFrame:CGRectMake(0.0f, self.view.frame.size.hight, self.view.frame.size.width, 150.0f)];
    
    
                     }
                     completion:^(BOOL completed){
                         if([self.miniMenuView superview]) {
                             self.miniMenuView.hidden = YES;
                         }
    
                     }];
    

    将此视为基本想法,根据您的要求进行更改祝你好运。

    【讨论】:

    • 我认为您也可以通过更改 ViewHeight 来做到这一点。 :)
    【解决方案4】:

    我也遇到过https://i.stack.imgur.com/fuVhy.gif评论https://stackoverflow.com/users/4793465/xtl上述解决方案的问题。

    我正在使用 web-view 底部的视图来显示和隐藏,就像 safari 移动浏览器一样。

    附上 UIView *viewV; UILabel *label;下面的示例代码

    和viewdidload

    -(void)viewDidLoad {
    [super viewDidLoad];
    WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
    webView.navigationDelegate = self;
    webView.UIDelegate = self;
    webView.scrollView.delegate = self;
    NSURL *nsurl=[NSURL URLWithString:@"https://www.google.com/"];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webView loadRequest:nsrequest];
    [self.view addSubview:webView];
    
    viewV = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    viewV.backgroundColor = [UIColor blueColor];
    [webView addSubview:viewV];}
    

    和滚动视图委托

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint velocity = [[scrollView panGestureRecognizer] velocityInView:scrollView.superview];
    if (velocity.y == 0) {
        return;
    }
    if (velocity.y < -1) {
        // Scrolling left
        NSLog(@"Top");
        if (viewV.frame.origin.y != self.view.frame.size.height - 50) {// if already hidden, don't need to hide again
            return;
        }
        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            viewV.backgroundColor = [UIColor clearColor];
            viewV.frame = CGRectMake(0, viewV.frame.origin.y + 50, self.view.frame.size.width, 0);
    
        } completion:^(BOOL finished) {
    
        }];
    } else if (velocity.y > 1) {
        // Scrolling Right
        NSLog(@"Bottom");
        if (viewV.frame.origin.y != self.view.frame.size.height) { // if already shown, no need to do show again
            return;
        }
        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            viewV.backgroundColor = [UIColor blueColor];
            viewV.frame = CGRectMake(0, viewV.frame.origin.y - 50, self.view.frame.size.width, 50);
    
        } completion:^(BOOL finished) {
        }];
    }}
    

    这对我有用。

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      相关资源
      最近更新 更多