使用多个对象的手势识别器移动对象 [英] Move objects around, with gesture recognizer for multiple Objects

查看:69
本文介绍了使用多个对象的手势识别器移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个可以在Snapchat和Instagram上使用Stickers的应用程序。找到一种添加图像的技术完全可行,但是现在我希望如果在对象周围滑动对象会改变其位置(我也想使比例/旋转功能有效。)

I am trying to make an app where you can use Stickers like on Snapchat and Instagram. 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).

我的代码如下:

@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:


  • 将一个 UIPanGestureRecognizer 添加到上面有图像的父视图。

  • 实施 UIGestureRecognizerDelegate 跟踪用户的方法

  • 第一次触摸时,循环浏览所有图像并调用 image.frame.contains(touchPoint)。将触摸点下的所有图像添加到数组中。

  • 遍历触摸图像列表,并计算触摸点到图像中心的距离。选择中心距触摸点最近的图像。

  • 将选定的图像移动到视图堆栈的顶部。 [现在您已经选择了一个图像并将其显示。]

  • 接下来,当您收到平移事件时,请相应地更改所选图像的帧。

  • 用户释放屏幕后,请重置您可能拥有的所有状态变量,以便下次触摸时可以重新开始。

  • Add one UIPanGestureRecognizer to the parent view that has the images on it.
  • Implement UIGestureRecognizerDelegate methods to keep track of the user touching and releasing the screen.
  • On first touch, loop through all your images and call 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.

以上内容将为您提供一个很好的 pan 解决方案。您需要整理很多东西,但这并不是很难。

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.

正如我在评论中所说, scale 旋转非常棘手。我建议您先忘记这一点,然后首先实现应用程序的其他部分。

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.

这篇关于使用多个对象的手势识别器移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆