简体   繁体   English

使用多个对象的手势识别器来移动对象

[英]Move objects around, with gesture recognizer for multiple Objects

I am trying to make an app where you can use Stickers like on Snapchat and Instagram. 我正在尝试开发一个可以在Snapchat和Instagram上使用Stickers的应用程序。 It fully worked to find a technique, that adds the images, but now I want that if you swipe the object around the object changes its position (I also want to make the scale / rotate function). 找到一种添加图像的技术完全可行,但是现在我希望如果在对象周围滑动对象会更改其位置(我也想创建缩放/旋转功能)。

My code looks like this: 我的代码如下所示:

@objc func StickerLaden() {
   for i in 0 ..< alleSticker.count {
        let imageView = UIImageView(image: alleSticker[i])

        imageView.frame = CGRect(x: StickerXScale[i], y:StickerYScale[i], width: StickerScale[i], height: StickerScale[i])

        ImageViewsSticker.append(imageView)
        ImageView.addSubview(imageView)
        imageView.isUserInteractionEnabled = true

        let aSelector : Selector = "SlideFunc"
        let slideGesture = UISwipeGestureRecognizer(target: self, action: aSelector)

        imageView.addGestureRecognizer(slideGesture)
    }
}

func SlideFunc(fromPoint:CGPoint, toPoint: CGPoint) {
}

Here are the high-level steps you need to take: 这是您需要采取的高级步骤:

  • Add one UIPanGestureRecognizer to the parent view that has the images on it. 将一个UIPanGestureRecognizer添加到上面有图像的父视图。
  • Implement UIGestureRecognizerDelegate methods to keep track of the user touching and releasing the screen. 实现UIGestureRecognizerDelegate方法以跟踪用户触摸和释放屏幕的情况。
  • On first touch, loop through all your images and call image.frame.contains(touchPoint) . 第一次触摸时,循环浏览所有图像并调用image.frame.contains(touchPoint) Add all images that are under the touch point to an array. 将触摸点下的所有图像添加到数组中。
  • Loop through the list of touched images and calculate the distance of the touch point to the center of the image. 循环浏览触摸的图像列表,并计算触摸点到图像中心的距离。 Chose the image whose center is closest to the touched point. 选择中心最靠近触摸点的图像。
  • Move the chosen image to the top of the view stack. 将选定的图像移到视图堆栈的顶部。 [You now have selected an image and made it visible.] [您现在选择了一个图像并将其显示。]
  • Next, when you receive pan events, change the frame of the chosen image accordingly. 接下来,当您收到平移事件时,请相应地更改所选图像的帧。
  • Once the user releases the screen, reset any state variables you may have, so that you can start again when the next touch is done. 用户释放屏幕后,请重置您可能拥有的所有状态变量,以便在下次触摸时可以重新开始。

The above will give you a nicely working pan solution. 以上将为您提供一个很好的平底锅解决方案。 It's a good amount of things you need to sort out, but it's not very difficult. 您需要整理很多东西,但这并不是很难。

As I said in my comment, scale and rotate are very tricky. 正如我在评论中所说, 缩放旋转非常棘手。 I advise you to forget that for a bit and first implement other parts of your app. 我建议您先忘记这一点,然后首先实现应用程序的其他部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM