CollectionViewCell与Pinch Gesture无法正常工作

时间:2015-07-05 13:02:41

标签: ios swift gesture pinch

即时使用连接到CVCell的CollectionView和Pinch Gesture。 我在单元格中有一个图像,而Pinch Gesture在图像中放大和缩小。我的问题是Pinch Gesture仅适用于偶数行,不适用于奇数行。我的意思是第一个细胞我不能放大图片,我可以放大第二个细胞中的图片,而不能放入第三个细胞,可以放到第四个细胞,它就像这样继续。我无法找到合乎逻辑的解释。所以也许你可以看到我想念的东西。谢谢你的建议。我的代码在这里:

`

@IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) {
    if let view = recognizer.view {
        view.transform = CGAffineTransformScale(view.transform,
            recognizer.scale, recognizer.scale)
        recognizer.scale = 1
    }
}

@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
    super.viewDidLoad()
    var b = UIBarButtonItem(barButtonSystemItem: .Bookmarks, target: self, action: "barButton")
    self.navigationItem.rightBarButtonItem = b
    var ItemIndex = NSIndexPath(forItem: catchNumber, inSection: 0)
    self.collectionView?.scrollToItemAtIndexPath(ItemIndex, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func barButton(){
    let alert = UIAlertController(title: "Select an option", message: " ", preferredStyle: UIAlertControllerStyle.ActionSheet)
    let shareButton = UIAlertAction(title: "Share", style: UIAlertActionStyle.Default) { (alert) -> Void in
        let content = FBSDKSharePhotoContent()
        let photo : FBSDKSharePhoto = FBSDKSharePhoto()
        photo.image = self.image
        photo.userGenerated = true
        content.photos = [photo]
        FBSDKMessageDialog.showWithContent(content, delegate:nil)
    }
    let saveButton = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default) { (alert) -> Void in
        UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil)
    }
    let deleteButton = UIAlertAction(title: "Delete", style: UIAlertActionStyle.Default) { (alert) -> Void in
        var tmp = self.catchPictures[self.catchNumber].componentsSeparatedByString("_")
        self.idDelete = tmp[1]
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/\(self.idDelete)", parameters: nil , HTTPMethod: "DELETE")
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
            if (error == nil){
                self.navigationController?.popViewControllerAnimated(true)
            }else{
                let alertController = UIAlertController(title: "Delete Error", message:
                    "An error occured while deleting photo", preferredStyle: UIAlertControllerStyle.Alert)
                alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))

                self.presentViewController(alertController, animated: true, completion: nil)
            }

        })
    }
    let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alert) -> Void in
        println("Cancel Pressed")
    }
    alert.addAction(shareButton)
    alert.addAction(saveButton)
    alert.addAction(deleteButton)
    alert.addAction(cancelButton)
    self.presentViewController(alert, animated: true, completion: nil)
}

func collectionView(collectionView: UICollectionView, numberOfSectionInCollectionView section: Int) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return catchPictures.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath) as! UICollectionViewCell
    let a:UIImageView = cell.contentView.viewWithTag(1) as! UIImageView
    dispatch_async(dispatch_get_main_queue(),{
        var urlString: NSString = self.catchPictures[indexPath.row] as NSString
        var imgURL: NSURL = NSURL(string: urlString as String)!
        var request: NSURLRequest = NSURLRequest(URL: imgURL)
        var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)!
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
            if !(error != nil) {
                var image = UIImage(data: data)
                a.image = image
                self.image = a.image
            }})

        self.flowLayout.minimumInteritemSpacing = 0.0
        self.flowLayout.minimumLineSpacing = 0.0
        self.flowLayout.itemSize = CGSizeMake(self.view.frame.width, 350)
        self.flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        self.collectionView.setCollectionViewLayout(self.flowLayout, animated: true)
    })
    self.catchNumber = indexPath.row
    return cell
}

} `

1 个答案:

答案 0 :(得分:0)

不,没有没有自定义的CVCell,我的错误是我写了CVCell作为CollectionViewCell的简短含义,在我没有使用任何自定义集合视图单元的代码中看到;)抱歉误解