简体   繁体   English

iOS:UIgesture用于拖放

[英]IOS: UIgesture for drag an drop

In my app I should implement the drag and drop of a imageView; 在我的应用程序中,我应该实现imageView的拖放。 the problem is that my imageView is inside a scrollview; 问题是我的imageView在滚动视图内; it's my code 这是我的代码

- (void) viewDidLoad{
[super viewDidLoad];
UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dragGestureChanged:)];
[scrollViewAlfabeto addGestureRecognizer:downwardGesture];
for (UIGestureRecognizer *gestureRecognizer in myscrollView.gestureRecognizers)
{
    [gestureRecognizer requireGestureRecognizerToFail:downwardGesture];
}
}

- (void) dragGestureChanged:(UIPanGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:scrollViewAlfabeto];

if (gesture.state == UIGestureRecognizerStateBegan) 
{

    [imageViewToMove removeFromSuperview];
    [self.view addSubview:imageViewToMove];

    UIView *draggedView = [myscrollView hitTest:point withEvent:nil];
    if ([draggedView isKindOfClass:[UIImageView class]])
    {
        imageViewToMove = (UIImageView*)draggedView;
    }
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
    imageToMove.center = point;
}
else if (gesture.state == UIGestureRecognizerStateEnded     ||
         gesture.state == UIGestureRecognizerStateCancelled ||
         gesture.state == UIGestureRecognizerStateFailed)
{
    // Determine if dragged view is in an OK drop zone
    // If so, then do the drop action, if not, return it to original location

    NSLog(@"point.x final:%f", point.x);
    NSLog(@"point.y final:%f", point.y);

    if (CGRectContainsPoint(goal.frame, point)){
        imageToMove.frame = CGRectMake(167, 159, 100, 100);
    }

    else{

    [imageToMove removeFromSuperview];
    [myscrollView addSubview:imageToMove];
    [imageToMove setFrame:CGRectMake(12, 38, 100, 100)];
    imageToMove = nil;



    }
}
}

Then with my code I'm able to take an imageView from scrollView with longpress, drag it inside "self.view"; 然后用我的代码,我可以用longpress从scrollView中获取一个imageView,将其拖到“ self.view”中; also if I drop this imageView over another imageView in self.view it puts on it. 如果我将此imageView放在self.view中的另一个imageView上,它也会放在上面。 It work fine. 工作正常。 But I have two problems: 但是我有两个问题:

1- when I drag this imageView and I do 1-当我拖动此imageView并执行

[imageViewToMove removeFromSuperview];
[self.view addSubview:imageViewToMove];

the image view don't appear under my finger but in other position and I want that it remains under my finger 图像视图不会出现在手指下,而是处于其他位置,我希望它保留在手指下

2- This code work only the first time when I launch my viewcontroller, because If I don't drop the imageview over other imageview inside self.view, itr return inside scrollview, but If I want drag it a second time, it don't work. 2-此代码仅在我启动ViewController时才第一次起作用,因为如果我不将imageview放到self.view内的其他imageview上,则会在scrollview内返回,但是如果我想第二次将其拖动,则不会工作。

Can you help me? 你能帮助我吗?

I'm not advanced in objc, but... 我在objc方面不高,但是...

  1. Allow user ineraction [self.view userInteractionEnabled:YES]; 允许用户无动于衷[self.view userInteractionEnabled:YES]; self.view [self.view userInteractionEnabled:YES];
  2. Try [self.view setMultipleTouchEnabled:YES]; 尝试[self.view setMultipleTouchEnabled:YES];

Taken from the documentation of UIView in relation to userInteractionEnabled : 从UIView的的有关userInteractionEnabled文档措施:

Discussion 讨论区
When set to NO, user events—such as touch and keyboard—intended for the view are ignored and removed from the event queue. 设置为NO时,将忽略视图的用户事件(例如触摸和键盘)并将其从事件队列中删除。 When set to YES, events are delivered to the view normally. 设置为YES时,事件将正常传递到视图。 The default value of this property is YES. 此属性的默认值为YES。

During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property. 在动画过程中,无论该属性中的值如何,都将暂时禁用动画中涉及的所有视图的用户交互。 You can disable this behavior by specifying the UIViewAnimationOptionAllowUserInteraction option when configuring the animation. 您可以在配置动画时通过指定UIViewAnimationOptionAllowUserInteraction选项来禁用此行为。

Hope it works 希望能奏效

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

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