Ios 如何使视图相对于scrollview动画滚动?
所以我有一个UIView,我想让它在滚动视图滚动动画的相反方向上设置一个平移动画。我该怎么做呢?当然,这应该取决于Ios 如何使视图相对于scrollview动画滚动?,ios,swift,animation,uiscrollview,Ios,Swift,Animation,Uiscrollview,所以我有一个UIView,我想让它在滚动视图滚动动画的相反方向上设置一个平移动画。我该怎么做呢?当然,这应该取决于scrollview.contentOffset我认为无论如何。因此,如果你以另一种方式滚动,它会回到原来的位置。因此,翻译取决于用户滚动的距离。我正在尝试使用以下代码。还请注意,我目前没有将UIView作为scrollview的子视图。它应该一直保持在屏幕上,但改变位置 func scrollViewDidScroll(_ scrollView: UIScrollView) { i
scrollview.contentOffset
我认为无论如何。因此,如果你以另一种方式滚动,它会回到原来的位置。因此,翻译取决于用户滚动的距离。我正在尝试使用以下代码。还请注意,我目前没有将UIView作为scrollview的子视图。它应该一直保持在屏幕上,但改变位置
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x >= scrollView.contentSize.width {
UIView.animate(withDuration: 2.0, animations: {
self.view.layoutIfNeeded()
})
}
一个简单的解决方案是:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let percentage = scrollView.contentOffset.x / scrollView.contentSize.width
self.box.frame.origin.x = UIScreen.main.bounds.width * percentage
}
只需计算已滚动的contentSize的百分比,并将X位置设置为屏幕宽度的相同百分比
它可以做一些改进,以确保视图在某些情况下不会超出屏幕的范围。因此,您可能需要在计算中考虑框的宽度作为增强
这是我用于测试的代码,背景图像是4K壁纸图像
class ViewController: UIViewController {
var box = UIView()
var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView = UIScrollView()
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
self.scrollView.delegate = self
self.view.addSubview(self.scrollView)
NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: scrollView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: scrollView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage(named: "background")
self.scrollView.addSubview(imageView)
NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: imageView, attribute: .left, relatedBy: .equal, toItem: scrollView, attribute: .left, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: imageView, attribute: .right, relatedBy: .equal, toItem: scrollView, attribute: .right, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: imageView, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
box.frame = CGRect(x: 0, y: UIScreen.main.bounds.height - 110, width: 100, height: 100)
box.backgroundColor = .black
self.view.addSubview(box)
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let percentage = scrollView.contentOffset.x / scrollView.contentSize.width
let xPosition = UIScreen.main.bounds.width * percentage
self.box.frame.origin.x = xPosition
}
}
如果它不是scrollview的子对象,它不是自动保持在原来的位置吗?是的。我没有问题,抱歉,如果我的措辞有误导性,那只是一个侧面,不可能改变情况,无论如何,试图让它以与卷轴相同的速度动画。我想我不明白你想做什么。您想让视图正好穿过屏幕吗?当它到达右边缘时,它应该做什么呢?所以我们的想法是它向右移动,但一次移动一点。我在滚动视图上启用了分页功能,因此它的行为与UIPageViewController非常相似,但对于每个页面,视图都会转换得更多。所以它总是停留在屏幕上,永远不会完全达到边缘。所以,假设您在滚动视图的第一页,然后向后滑动它将返回到它以前的位置。它总是朝着你滚动的相反方向移动。嘿,如果我想让视图停在某个点上,我该怎么做呢?现在我将x坐标设置为15,但是当我使用你的解决方案时,当我开始滚动时,它当然会将其重置为0。我尝试过使用self.box.frame.origin.x+=xPosition,但这并不能完全满足我的要求。这并不能完全满足我的要求,至少它会从最初设置的位置开始。但是,我希望当该页面的滚动完成时,它停止在另一个预定义的位置,比如x=40。