根据滚动视图使用自动布局水平滚动更改视图的高度和宽度

时间:2017-07-27 05:26:22

标签: swift swift3 uiscrollview ios-autolayout

容器视图层次结构: -

  • 容器视图
    • 滚动型
      • 查看左侧
      • 查看中心
      • 查看右侧
    • FloatingBottomView

我需要根据height水平滚动来更改FloatingBottomView的widthscrollview约束。

初始出口: -

@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方法中获得一些比例。 从左到右快速滚动,反之亦然没有完全输出

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

1 个答案:

答案 0 :(得分:2)

我认为你需要像这样给出按钮动画 enter image description here

为此,你无需做任何事只需添加动画到按钮,如下所示 在viewDidLoad中调用setupButtonanimation

 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
               }
        }