我正在尝试缩放图像视图,因为我将scrollView滚动到屏幕顶部。这是我的代码:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.y
if (offset <= 0) {
let ratio: CGFloat = -offset*1.0 / UIScreen.main.bounds.height
self.coverImageView.transform = CGAffineTransform(scaleX: 1.0 + ratio, y: 1.0 + ratio)
}
}
这会在我向上滚动时缩放图像,但由于我也在向上滚动,我的视图会下降,并在图像展开时显示背后的白色背景。我该如何防止这种情况发生?
答案 0 :(得分:1)
听起来像滚动视图并不适合你想要做的事情。如何使用手势识别器呢?这些方面的东西:
override func viewDidLoad() {
super.viewDidLoad()
coverImageView.isUserInteractionEnabled = true
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPan))
coverImageView.addGestureRecognizer(panGestureRecognizer)
}
func didPan(panGestureRecognizer: UIPanGestureRecognizer) {
let translation = panGestureRecognizer.translation(in: coverImageView)
if translation.y > 0 {
let zoomRatio = (translation.y * 0.1) + 1.0
coverImageView.transform = CGAffineTransform(scaleX: zoomRatio, y: zoomRatio)
}
}
你必须四处游戏才能让它按照你想要的方式行事,但它应该足以让你开始。