Change height and width of view according to scrol

2019-08-28 04:18发布

问题:

Container View hierarchy:-

  • Container View
    • Scrollview
      • view left
      • view center
      • view right
    • FloatingBottomView

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

Initial Outlet:-

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

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

回答1:

I think you need to give button animation like this

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