Drag image from UICollectionView to UIView?

Can I drag an image from the ViewCell collection into a UIView?

I am creating a game in which the user can select several items to play in the next VC. I present each element as an image, and the user can select each element by dragging the image at the bottom of the screen. Then the image will be reset to its original location.

If I have UICollectionView functions in my code, then the image selected in the UIViewCollectionView class is configured correctly ... What do I need to enable to move the image from the upper half (inside CollectionView) to the lower half (in 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 managed to move the image to a UIView using this code below. I cannot figure out how to implement this in a 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 StackView and had a horizontal scroll view for 20 items? The only thing I can hide certain elements from the user is, then the width will be adjusted to fit the StackView. It looks awful and I don't know how to stop the width expansion.

Thank you for your help.

+5
source share

Source: https://habr.com/ru/post/1259801/


All Articles