2017-10-20 80 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總是有錯誤,所以不要擔心。 :)