问题
Is it possible to drag an image from a collectionViewCell into a UIView?
I am creating a game where the user can pick a number of items to play with in the next VC. I'm representing each item as an image and the user can select each item by dragging the image to bottom of the screen. The image would then be reset to its original location.
If I have the UICollectionView functions in my code, with the image selected in a class of UIViewCollectionView all set up correctly... What do I need to include to move the image from top half (within CollectionView) to bottom half (within UIView).
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let Cell:BackpackCollectionVC = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! BackpackCollectionVC
Cell.backpackImage.image = UIImage(named: self.resultString[indexPath.row]["imageName"]!)!
return Cell
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
I was able to move an image in UIView using this code below. I can't figure out how to implement this into the UIViewCollectionView...
In ViewDidLoad:
let imageGesture = UIPanGestureRecognizer(target: self, action: #selector(self.imageInUIViewDragged(gestureRecognizer:)))
uiviewImage.isUserInteractionEnabled = true
uiviewImage.addGestureRecognizer(imageGesture)
func imageInUIViewDragged(gestureRecognizer: UIPanGestureRecognizer) {
let translation = gestureRecognizer.translation(in: view)
let uiviewImage = gestureRecognizer.view!
//self.view.bounds.width -
uiview.center = CGPoint(x: 52 + translation.x, y: 66 + translation.y)
if gestureRecognizer.state == UIGestureRecognizerState.ended {
if (uiviewImage.center.x > 100 && uiviewImage.center.x < self.view.bounds.width - 100) && (uiviewImage.center.y > self.view.bounds.height / 2 && uiviewImage.center.y < self.view.bounds.height - 100) { {
print("Item has been selected")
} else {
print("Not in backpack")
}
//self.view.bounds.width -
uiviewImage.center = CGPoint(x: 52, y: 66)
}
}
}
If this is not possible......
I was thinking about a StackView and have a horizontal scroll view for the 20 items? The only thing is if I hide certain items from the user, the widths are then adjusted to fit the StackView. It looks terrible and I don't know how to stop the widths from expanding.
Thanks for your help.
来源:https://stackoverflow.com/questions/40624345/drag-and-drop-image-from-uicollectionview-to-uiview