0
我有我有一个ImageView的UIScrollView。我试图实现的是:
- 当用户再次点击时放大图像以适合屏幕的全高,
- 自动启动第一个滚动到右侧的动画图像,
- 之后,开始滚动到图像中间的第二个动画,
- 最后再次开始动画滚动到图像的右侧。
我想获得漂浮在屏幕上的图像的印象。用户不能自己滚动图像。scrollView中imageView的多个动画
我已经完成了缩放的部分,但我有的问题是动画。我尝试过这样的事情,其中offset,middleOffset和endOffset是CGPoints:
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
setupGestureRecognizer()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
scrollView.panGestureRecognizer.isEnabled = false
scrollView.pinchGestureRecognizer?.isEnabled = false
}
fileprivate func updateMinZoomScaleForSize(size: CGSize) {
let widthScale = size.width/imageView.bounds.width
let heightScale = size.height/imageView.bounds.height
let minScale = min(widthScale, heightScale)
let maxHeightScale = view.frame.height/imageView.bounds.height
scrollView.maximumZoomScale = maxHeightScale
scrollView.minimumZoomScale = minScale
scrollView.zoomScale = minScale
}
fileprivate func updateConstraintsForSize(size: CGSize) {
let yOffset = max(0, (size.height - imageView.frame.height)/2)
imageViewTopConstraint.constant = yOffset
imageViewBottomConstraint.constant = yOffset
let xOffset = max(0, (size.width - imageView.frame.width)/2)
imageViewLeadingConstraint.constant = xOffset
imageViewTrailingConstraint.constant = xOffset
view.layoutIfNeeded()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateMinZoomScaleForSize(size: view.bounds.size)
}
func setupGestureRecognizer() {
let singleTap = UITapGestureRecognizer(target: self, action: #selector(self.handleSingleTap(recognizer:)))
singleTap.numberOfTapsRequired = 1
scrollView.addGestureRecognizer(singleTap)
}
func handleSingleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
UIView.animate(withDuration: 1, delay: 0.3, options: [], animations: {
scrollView.setContentOffset(offset, animated: false)
}, completion: { _ in
UIView.animate(withDuration: 1, delay: 0.3, options: [], animations: {
scrollView.setContentOffset(middleOffset, animated: false)
}, completion: { _ in
UIView.animate(withDuration: 1, delay: 0.3, options: [], animations: {
scrollView.setContentOffset(endOffset, animated: false)
}, completion: nil)
})
})
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
updateConstraintsForSize(size: view.bounds.size)
}
它不工作。在图像放大后加载第一个动画,但随后停止(不会崩溃)。看起来动画未完成,因为我再次点击时无法缩小图像。