Change height and width of view according to scrollview scroll horizontally using auto layout

孤人 提交于 2019-12-13 08:54:25

问题


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


来源:https://stackoverflow.com/questions/45342028/change-height-and-width-of-view-according-to-scrollview-scroll-horizontally-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!