2017-10-20 41 views
0

选择我的collectionview单元格时,我收到第二长延迟。这里是我目前的代码收集视图确实选择:使用CollectionView进行延迟选择

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选择功能似乎有问题。如果在打印语句的didSelect函数中切换我的代码,则不存在延时。

+0

您是否在下一个VC中使用了一些IBInspectable,或者在下一个VC的ViewDidLoad中加载了一些笨重的数据。如果您提供的IBInspecatble的UIView的一些其他元素比视图将加载抱怨不可满足的限制。而选择适当的一个将需要几秒钟的时间。 – Amit

+0

将此行'self.navigationController?.pushViewController(vc,animated:true)'移动到方法'didSelectItemAt'的末尾。在推送之前,您应该设置目标对象的所有成员 – Lamar

回答

0

我有两个解决方案:

1:可能不是:我猜测,它给一个恼人的延迟,因为它采取了一段时间你的应用程序来运行代码来获取笔尖文件,并改变所有这些事情,而不应该。

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