根据 scrollview 使用自动布局水平滚动更改视图的高度和宽度
Change height and width of view according to scrollview scroll horizontally using auto layout
容器视图层次结构:-
- 容器视图
- 滚动视图
- 左视图
- 查看中心
- 右视图
- 浮动底部视图
我需要根据 scrollview
水平滚动更改 FloatingBottomView 的 height
和 width
约束。
初始出口:-
@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)")
我认为你需要像这样给按钮动画
为此,您无需做任何事情,只需向按钮添加动画即可,如下所示
在 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
}
}
容器视图层次结构:-
- 容器视图
- 滚动视图
- 左视图
- 查看中心
- 右视图
- 浮动底部视图
- 滚动视图
我需要根据 scrollview
水平滚动更改 FloatingBottomView 的 height
和 width
约束。
初始出口:-
@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)")
我认为你需要像这样给按钮动画
为此,您无需做任何事情,只需向按钮添加动画即可,如下所示 在 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
}
}