所以我有这个代码来移动UIImageView
如果它从底部触及3/4屏幕它会关闭视图控制器但是当我尝试设置下一个图像或前一个图像时,代码不会执行是否有任何线索?
func dismissImageView(panGesture: UIPanGestureRecognizer) {
if panGesture.state == .ended {
UIView.animate(withDuration: 0.25) {
self.imageView.frame = self.scrollView.frame
self.imageView.alpha = 1.0
}
} else {
if self.scrollView.zoomScale == 1.0 {
let scrollViewFrame = self.scrollView.frame
let translation = self.panGesture.translation(in: self.scrollView)
UIView.animate(withDuration: 0.25) {
self.imageView.frame.origin.x = translation.x
self.imageView.frame.origin.y = translation.y
if self.lastImageFramePosition != nil {
if translation.y > self.lastImageFramePosition {
self.imageView.alpha -= 0.01
} else {
self.imageView.alpha += 0.01
}
}
}
self.lastImageFramePosition = translation.y
if self.imageView.center.y >= ((scrollViewFrame.size.height * 3) / 4) {
self.dismissImageViewNormally()
} else if translation.x >= 150 {
self.nextImage() // if I print something out it prints but the next code doesn't execute
} else if translation.x <= -150 {
self.previousImage()
}
}
}
}
// nextImage
func nextImage() {
if selectedImage != (self.images.count - 1) {
self.goToImage(index: (selectedImage + 1))
}
}
// goToImage(index:)
func goToImage(index: Int) {
if FileManager.default.fileExists(atPath: images[index].path) {
if sharedFileManager.getFileType(url: images[index]) == kUTTypeImage {
do {
let imageData = try Data(contentsOf: images[index])
let image = UIImage(data: imageData)!
UIView.animate(withDuration: 0.25, animations: {
self.imageView.alpha = 0.0
self.imageView.image = image
self.imageView.alpha = 1.0
})
self.imageView.contentMode = .scaleAspectFit
self.selectedImage = index
self.navigationItem.title = self.images[selectedImage].deletingPathExtension().lastPathComponent
self.updateNavigationButtons()
} catch {}
}
}
}