ios - CollectionView 延迟选择

标签 ios swift collectionview

在选择我的 CollectionView 单元格时,我遇到了第二次长时间延迟。这是我当前选择的 Collection View 代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let vc = PopUpCellViewController(nibName: "PopUpCellViewController", bundle: nil)
    self.navigationController?.pushViewController(vc, animated: true)
    print("called")
    let cell = collectionView.cellForItem(at: indexPath) as! AnnotatedPhotoCell
    sourceCell = cell
    vc.picture = resizeImage(image: cell.imageView.image!, targetSize: CGSize(width: (view.bounds.width - 45),height: 0))
    vc.comment = cell.commentLabel
    var image = UIImage(named: "back_button_thick")
    image = image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    self.navigationController?.navigationBar.backIndicatorImage = image
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = image
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
}

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size

    let widthRatio  = targetSize.width  / image.size.width

    // Figure out what our orientation is, and use that to form the rectangle
    var newSize: CGSize

    newSize = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

    // Actually do the resizing to the rect using the ImageContext stuff
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
}

我正在使用 UIViewControllerAnimatedTransitioning。我确信我的 UIViewControllerAnimatedTransitioning 不会出现延迟。我的 CollectionView Did 选择功能似乎有问题。如果将我的 didSelect 函数中的代码切换为打印语句,则不再有延迟。

最佳答案

我有两个解决方案:

1:可能不是:我猜测它会产生令人讨厌的延迟,因为您的应用程序需要一段时间才能运行代码来获取 nib 文件,并在执行过程中更改所有这些内容不应该。

2:可能:也许在真实设备上运行它会有所帮助。 Xcode 总是有错误,所以不用担心。 :)

关于ios - CollectionView 延迟选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841350/

相关文章:

ios - iOS 中的棕褐色调 PDF

ios - 如何在行的滑动操作配置中为 VoiceOver 添加辅助功能标签?

ios - 覆盖我的自定义 UIView 子类的初始化程序

ios - UITableViewCell 布局和约束何时最终确定?

arrays - fatal error : Array index out of range.关于collectionView

ember.js - 如何将模板与 Ember.CollectionView 一起使用

ios - iOS 中 UIScrollView 内内容的高度

ios - 将库添加到 IOS 应用程序会导致可怕的构建失败

ios - UICollectionView 始终在当前之前显示上一个选择

ios - 试图在 UICollectionViewCells 上同时加载不同的网站,但是当我滚动时它们被复制了