在滚动视图中垂直滚动时,图像视图移到一边

时间:2019-09-04 09:02:32

标签: ios swift uiscrollview uiimageview

我有一个具有UIView(featuredStoryView)的视图控制器,并且在该视图内部,添加了滚动视图(scrollView),在滚动视图内部,添加了ImageView(bookCover),一个最高的。这是所有这些元素的代码:

func setupFeaturedStoryView() {

    featuredStoryView.backgroundColor = .white
    featuredStoryView.layer.cornerRadius = 12

    // Let the user tap on the featured story image view
    featuredStoryView.isUserInteractionEnabled = true
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(featuredStoryViewTapped))
    featuredStoryView.addGestureRecognizer(tapGesture)
    view.addSubview(featuredStoryView)
    addFeaturedStoryViewConstraints()

}

func setupScrollView() {

    scrollView.backgroundColor = UIColor(red: 10/255, green: 10/255, blue: 20/255, alpha: 0.5)
    scrollView.layer.cornerRadius = 15
    featuredStoryView.addSubview(scrollView)

    // Add the constraints to the scroll view
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.topAnchor.constraint(equalTo: featuredStoryView.topAnchor).isActive = true
    scrollView.leadingAnchor.constraint(equalTo: featuredStoryView.leadingAnchor).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: featuredStoryView.trailingAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: featuredStoryView.bottomAnchor).isActive = true
}

func setupBookCover() {

    bookCover.backgroundColor = .yellow
    bookCover.layer.cornerRadius = 15
    bookCover.contentMode = .scaleAspectFill

    scrollView.addSubview(bookCover)
    addBookCoverConstraints()

}

// Add the constraints to the featured story view
func addFeaturedStoryViewConstraints() {

    featuredStoryView.translatesAutoresizingMaskIntoConstraints = false
    featuredStoryView.topAnchor.constraint(equalTo: view.centerYAnchor, constant: -130).isActive = true
    featuredStoryView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 100).isActive = true
    featuredStoryView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -100).isActive = true
    featuredStoryView.bottomAnchor.constraint(equalTo: view.centerYAnchor, constant: 130).isActive = true

}

// Add constraints to the book cover
func addBookCoverConstraints() {

    bookCover.translatesAutoresizingMaskIntoConstraints = false
    bookCover.topAnchor.constraint(equalTo: featuredStoryView.topAnchor).isActive = true
    bookCover.leadingAnchor.constraint(equalTo: featuredStoryView.leadingAnchor).isActive = true
    bookCover.trailingAnchor.constraint(equalTo: featuredStoryView.trailingAnchor).isActive = true
    bookCover.bottomAnchor.constraint(equalTo: featuredStoryView.bottomAnchor).isActive = true
}

点击功能强大的StoryView时,将运行以下代码段:

@objc func featuredStoryViewTapped() {

    scrollView.contentSize.height = 1500

    let animator = UIViewPropertyAnimator(duration: 0.6, dampingRatio: 0.8) {
        self.featuredStoryView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        self.scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        self.view.layoutIfNeeded()
        self.bookCover.frame = CGRect(x: self.featuredStoryView.frame.midX - (bookCoverWidth / 2), y: 90, width: bookCoverWidth, height: bookCoverHeight)
        self.view.layoutIfNeeded()
    }
    animator.startAnimation()

}

现在看看这个Gif:

View controller

这里的问题是,当我滚动时,黄色的imageView(bookCover)移到左上角,为什么会发生这种情况?它应该只是与滚动视图。有没有什么办法解决这一问题?希望我已经解释够了,感谢您的反馈! :) ps:很抱歉,如果我添加了不必要的代码。

1 个答案:

答案 0 :(得分:0)

在自动布局中,不能直接像self.featuredStoryView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)contentSize那样像scrollView.contentSize.height = 1500那样设置框架。因此,请尝试根据您的要求设置约束。