根据滚动视图使用自动布局水平滚动更改视图的高度和宽度 [英] Change height and width of view according to scrollview scroll horizontally using auto layout

查看:88
本文介绍了根据滚动视图使用自动布局水平滚动更改视图的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

容器视图层次结构:-

  • 容器视图
    • 滚动视图
      • 向左查看
      • 查看中心
      • 查看权限
      • Container View
        • Scrollview
          • view left
          • view center
          • view right

          我需要根据scrollview水平滚动来更改FloatingBottomView的heightwidth约束.

          I need to change height and width constrain of FloatingBottomView according to scrollview scroll horizontally.

          初始出口:-

          @IBOutlet weak var constantFloatingBottomViewWidth: NSLayoutConstraint! // 300
          @IBOutlet weak var constantFloatingBottomViewHeight: NSLayoutConstraint! // 70
          
          override func viewWillAppear(_ animated: Bool) {
                   self.scrollView.contentOffset.x = self.view.bounds.width
                   // view center in scrollview
          }
          
          func scrollViewDidScroll(_ scrollView: UIScrollView) {
               if scrollView.contentOffset.x < self.view.bounds.width {
                  // when scrollview move view center to view left, constantFloatingBottomViewWidth and height goes to down at some point  
               }
               else if scrollView.contentOffset.x > self.view.bounds.width {
                 // when scrollview move view left to view center, constantFloatingBottomViewWidth & height goes up to same point range while it down and back to original constantFloatingBottomViewWidth and height
               }
          }
          

          我尝试使用这种方法在scrollViewDidScroll方法中获得一定的比例. 从左向右快速滚动,反之亦然

          i have tried using this way to get some scale in scrollViewDidScroll method. scroll quick left to right or vice-versa did not get exactly out-put

          let scale =  abs(scrollView.contentOffset.x / self.view.bounds.width)
                      print("scale:=\(scale)")
          

          推荐答案

          我认为您需要提供这样的按钮动画

          I think you need to give button animation like this

          为此,您无需执行任何操作,只需将动画添加到按钮即可,如下所示 在viewDidLoad中调用setupButtonanimation

          For this you have nothing to do just add animation to button like follow call setupButtonanimation in viewDidLoad

           func setupButtonAnimation()
                  {
                      let animation = CABasicAnimation.init(keyPath: "transform.scale")
                      animation.fromValue = 1.0
                      animation.toValue = 0.45
                      animation.duration = 1.0
                      //Set the speed of the layer to 0 so it doesn't animate until we tell it to
                      self.btn1.layer.speed = 0.0;
                      self.btn1.layer.add(animation, forKey: "transform");
                  }
                  func scrollViewDidScroll(_ scrollView: UIScrollView)
                  {
                      let screenSize = self.view.bounds.size
                      if let btn =  self.btn1
                      {
                          var factor:CGFloat = 1.0
                          factor = scrollView.contentOffset.x / screenSize.width
                          if factor > 1
                          {
                                  factor = 2 - factor
                          }
                          print(factor)
                          //This will change the size
                          let timeOffset = CFTimeInterval(1-factor)
                          btn.layer.timeOffset = timeOffset
                         }
                  }
          

          这篇关于根据滚动视图使用自动布局水平滚动更改视图的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆