iOS拖动UIImageView

发布于 2024-11-17 08:02:00 字数 108 浏览 4 评论 0原文

好吧,所以我一直想知道如何实际拾取图像视图并拖动它。我正在计划当我拖动图像视图时,当用户将其放置到正确的位置时,它会锁定在那里。我真的不知道该怎么做,这已经困扰我一段时间了。

非常感谢!

Ok, so I have always wondered how do I actually pick up an image view and drag it. I was planning when I drag an image view and when user places it to correct location it locks there. I really don't have idea how to do this and it has bothered me for sometime.

Thanks so much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

这样的小城市 2024-11-24 08:02:00

或者,如果您不想子类化视图并自己处理触摸,则可以使用 UIPanGestureRecognizer

在任何类(例如视图控制器)中创建一个平移识别器并将其添加到您的视图中:

UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                 action:@selector(handlePanGesture:)];
[myDraggedView addGestureRecognizer:panRecognizer];

然后简单地说:

- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGRect frame = myDraggedView.frame;
    frame.origin = [gestureRecognizer locationInView:myDraggedView.superview];
    myDraggedView.frame = frame;
}

如果您像我一样喜欢 Interface Builder,您也可以将平移手势识别器拖到图像视图上,然后连接handlePanGesture: 的识别器应声明为 IBAction 而不是 void

Or you can use a UIPanGestureRecognizer if you don't want to subclass the view and handle touches yourself.

Create a pan recognizer in any class (e.g. view controller) and add it to your view:

UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                 action:@selector(handlePanGesture:)];
[myDraggedView addGestureRecognizer:panRecognizer];

And then simply:

- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGRect frame = myDraggedView.frame;
    frame.origin = [gestureRecognizer locationInView:myDraggedView.superview];
    myDraggedView.frame = frame;
}

If you like Interface Builder as much as me you can also just drag a pan gesture recognizer over your image view, and then connect the recognizer to the handlePanGesture: that should be declared as an IBAction instead of void.

多像笑话 2024-11-24 08:02:00

如果您喜欢阅读,请查看 UIResponder 类参考,特别是 touchesBegantouchesMoved

If you're one for reading, then check out the UIResponder Class Reference, and, in particular, touchesBegan and touchesMoved.

泪是无色的血 2024-11-24 08:02:00

我已经使用过这个,但是用户可以自由地将 imageview 拖动到他们想要的位置。这是我的 swift 自定义类

class DraggableImageView: UIImageView {
      var beganPoint: CGPoint? = nil
      var originCenter: CGPoint? = nil
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
           if let position = touches.first?.location(in: superview){
                beganPoint = position
                originCenter = center
           }
      }
      override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
      }
      override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
           if let position = touches.first?.location(in: superview){
                let newPosition = CGPoint(x: position.x - (beganPoint?.x)!, y: position.y - (beganPoint?.y)!)
                center = CGPoint(x: (originCenter?.x)! + newPosition.x, y: (originCenter?.y)! + newPosition.y)
           }
      }
}

只需将这个自定义类与您的 UIImageView 一起使用

I already worked with this, but the user can drag imageview freely with position that they want. and this is my custom class with swift

class DraggableImageView: UIImageView {
      var beganPoint: CGPoint? = nil
      var originCenter: CGPoint? = nil
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
           if let position = touches.first?.location(in: superview){
                beganPoint = position
                originCenter = center
           }
      }
      override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
      }
      override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
           if let position = touches.first?.location(in: superview){
                let newPosition = CGPoint(x: position.x - (beganPoint?.x)!, y: position.y - (beganPoint?.y)!)
                center = CGPoint(x: (originCenter?.x)! + newPosition.x, y: (originCenter?.y)! + newPosition.y)
           }
      }
}

Just user this custom class with your UIImageView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文