ios-仅当我在箭头上方滑动下一张图片时,屏幕滑动才起作用
发布时间:2022-06-17 21:33:36 224
相关标签: # android
点击照片后,我有一个photoview在前台打开照片。照片有一个左右箭头,分别用作下一张和上一张照片。当我使用它们时,这个箭头可以正常工作。当我转到最左边的照片时,左箭头会隐藏起来,当我转到最右边(最后一张)的照片时,右箭头也会隐藏起来。但是当我在照片上使用滑动时,此处理不起作用。箭头仍然存在并且没有隐藏。
所以我做了一个滑动功能,它会像上面一样隐藏箭头。刷卡功能有效,但仅在我刷过箭头而不是照片本身的情况下。当我在箭头上滑动时,照片自然不会移动到下一张......有人可以解释一下我做错了什么吗?
更新可以在照片上滑动,但不调用 gesSwipe()
这是我的 PhotoViewcontroller.swift
import Foundation
import UIKit
struct PhotoParam {
let urls: [URL]
}
class PhotoViewController: ModalViewController, UIGestureRecognizerDelegate {
// MARK: properties
@IBOutlet private weak var imageCollection: UICollectionView!
@IBOutlet weak var arrowLeftButton: UIButton!
@IBOutlet weak var arrowRightButton: UIButton!
var urls: [URL] = [] {
didSet {
imageCollection?.reloadData()
}
}
// MARK: init
override func initPresenter() {
}
override func setParam(param: Any) {
guard let param = param as? PhotoParam else {
return
}
self.urls = param.urls
}
@IBAction func gesSwipe(_ sender: UISwipeGestureRecognizer) {
let screenW = UIScreen.main.bounds.width
let offset: CGPoint = imageCollection.contentOffset
let page = CGFloat(Int(offset.x / screenW) - 1)
switch sender.direction {
case .left:
print("swipe right")
print("%%%", page)
print("%%%", CGFloat(urls.count - 2))
if page >= CGFloat(urls.count - 2) {
self.arrowRightButton.isHidden = true
} else {
self.arrowRightButton.isHidden = false
}
case .right:
print("swipe left")
if page < 0 {
self.arrowLeftButton.isHidden = true
} else {
self.arrowLeftButton.isHidden = false
}
default:
break
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.arrowLeftButton.isHidden = true
self.imageCollection.delegate = self
self.imageCollection.dataSource = self
self.imageCollection.isPagingEnabled = true
self.imageCollection.reloadData()
let leftSwipeGesture = UISwipeGestureRecognizer(
target: self,
action: #selector(PhotoViewController.gesSwipe(_:))
)
leftSwipeGesture.direction = .left
self.view.addGestureRecognizer(leftSwipeGesture)
let rightSwipeGesture = UISwipeGestureRecognizer(
target: self,
action: #selector(PhotoViewController.gesSwipe(_:))
)
rightSwipeGesture.direction = .right
self.view.addGestureRecognizer(rightSwipeGesture)
}
// MARK: methods
@IBAction func arrowLeftTapped(_ sender: Any) {
let screenW = UIScreen.main.bounds.width
let offset: CGPoint = imageCollection.contentOffset
let page = CGFloat(Int(offset.x / screenW) - 1)
self.arrowRightButton.isHidden = false
if page <= 0 {
self.arrowLeftButton.isHidden = true
} else {
self.arrowLeftButton.isHidden = false
}
if page >= 0 {
imageCollection.setContentOffset(CGPoint(x: screenW * page, y: 0), animated: true)
}
}
@IBAction func arrowRightTapped(_ sender: Any) {
let screenW = UIScreen.main.bounds.width
let offset: CGPoint = imageCollection.contentOffset
let page = CGFloat(Int(offset.x / screenW) + 1)
self.arrowLeftButton.isHidden = false
if page >= CGFloat(urls.count - 1) {
self.arrowRightButton.isHidden = true
} else {
self.arrowRightButton.isHidden = false
}
if page < CGFloat(urls.count) {
imageCollection.setContentOffset(CGPoint(x: screenW * page, y: 0), animated: true)
}
}
}
extension PhotoViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// section数
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
// 列数
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.urls.count
}
// セル生成
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let photoCell: PhotoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
if indexPath.row < self.urls.count {
photoCell.setImageURL(url: self.urls[indexPath.row])
}
return photoCell
}
// Screenサイズに応じたセルサイズを返す
// UICollectionViewDelegateFlowLayoutの設定が必要
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
}
}
extension PhotoViewController: PhotoCellDelegate {
func isNotFound(url: URL) {
if let index = urls.firstIndex(of: url) {
self.urls.remove(at: index)
self.imageCollection.reloadData()
}
}
}
更新这对我有用
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {
let pageWidth = self.imageCollection.bounds.width
let targetXContentOffset = Float(targetContentOffset.pointee.x)
let contentWidth = Float(self.imageCollection!.contentSize.width)
if targetXContentOffset == 0.0 {
self.arrowLeftButton.isHidden = true
}
else {
self.arrowLeftButton.isHidden = false
}
if contentWidth == targetXContentOffset + Float(pageWidth) {
self.arrowRightButton.isHidden = true
}
else {
self.arrowRightButton.isHidden = false
}
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报