點擊照片后,我有一個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<CGPoint>) {
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
}
}
uj5u.com熱心網友回復:
或者,您可以使用以下代碼來實作相同的功能。
讓我們在你的類中全域宣告一個變數
// By default this will be first page
var currentPage: Int = 0
然后您可以向您的班級確認以下內容UIScrollViewDelegate,因為UICollectionViewDelegate 是從UIScrollViewDelegate
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageWidth = self.collectionView.bounds.width
let targetXContentOffset = Float(targetContentOffset.pointee.x)
let contentWidth = Float(self.collectionView!.contentSize.width)
var newPage = Float(currentPage)
if velocity.x == 0 {
newPage = floor( (targetXContentOffset - Float(pageWidth) / 2) / Float(pageWidth)) 1.0
} else {
newPage = Float(velocity.x > 0 ? currentPage 1 : currentPage - 1)
if newPage < 0 {
newPage = 0
}
if (newPage > contentWidth / Float(pageWidth)) {
newPage = ceil(contentWidth / Float(pageWidth)) - 1.0
}
}
let point = CGPoint (x: CGFloat(newPage * Float(pageWidth)), y: targetContentOffset.pointee.y)
targetContentOffset.pointee = point
currentPage = Int(newPage)
}
而且您的按鈕操作也可以像這樣簡化
@IBAction func leftArrowTapped(_ sender: UIButton) {
// Validate for previous page presence
guard currentPage > 0 else {
return
}
currentPage -= 1
let indexPath = IndexPath(item: currentPage, section: 0)
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
@IBAction func rightArrowTapped(_ sender: UIButton) {
// Validate for next page presence
guard currentPage < (urls.count-1) else {
return
}
currentPage = 1
let indexPath = IndexPath(item: currentPage, section: 0)
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491680.html
上一篇:swiftUI:更新視圖
