1

Hello all,

I am using "Touch methods" first time so facing certain issue. In my app, there are 2 subviews in mainview.I am trying to implement drag and drop of uibutton from one view to another view. I'm trying lots of things but not able to do drag and drop.

please help me.

Thanks in advance.

My Code :


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
        for (UIButton *iView in self.view.subviews) {
            if ([iView isMemberOfClass:[UIButton class]])
            {
                if (touchPoint.x > iView.frame.origin.x &&
                    touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
                    touchPoint.y > iView.frame.origin.y &&
                    touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
                {
                    self.btnalpha = iView;
                    touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
                                                   touchPoint.y - iView.frame.origin.y);

                    homePosition = CGPointMake(iView.frame.origin.x,
                                                    iView.frame.origin.y);

                    [self.viewblue bringSubviewToFront:self.btnalpha];
                }
            }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];

    CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
                                           touchPoint.y - touchOffset.y,
                                           self.btnalpha.frame.size.width,
                                           self.btnalpha.frame.size.height);
    self.btnalpha.frame = newDragObjectFrame;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];

    if (touchPoint.x > self.vieworange.frame.origin.x &&
        touchPoint.x < self.vieworange.frame.origin.x + self.vieworange.frame.size.width &&
        touchPoint.y > self.vieworange.frame.origin.y &&
        touchPoint.y < self.vieworange.frame.origin.y + self.vieworange.frame.size.height )
    self.btnalpha.frame = CGRectMake(self.homePosition.x, self.homePosition.y, self.btnalpha.frame.size.width, self.btnalpha.frame.size.height);
}

- (void) drag
{
    str_scramble =@"IH";

    for (int i = 0; i < 2; i++)
    {
        self.btnalpha = [[UIButton alloc] init];
        [self.btnalpha setFrame:CGRectMake(i * 50 + 10, 5, 42, 42)];
        char letter = [str_scramble characterAtIndex:i];
        self.btnalpha.backgroundColor = [UIColor grayColor];
        self.btnalpha.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        [self.btnalpha setTitle:[NSString stringWithFormat:@"%c", letter] forState:UIControlStateNormal];
        [self.btnalpha addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
        [self.btnalpha addTarget:self action:@selector(touchesMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [viewblue addSubview:self.btnalpha];
        NSLog(@"string : --> %@", self.btnalpha.titleLabel.text);
    }
}
Abha
  • 1,032
  • 1
  • 13
  • 36

3 Answers3

0

you should use touchDrag methodes like touchDragEnter, touchDragExit, touchDragInside or touchDragOutside to drag a UIButton.

These all events are UIControlEvents

Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
0

I am using PanGestureRecognizer and it works great.

UIPanGestureRecognizer *drag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragButton:)];
            drag.delegate = self;
            drag.view.tag = position;
            [button addGestureRecognizer:drag];

- (void)dragButton:(UIPanGestureRecognizer *)gesture
{
    buttonPicked = [allObjects objectAtIndex:gesture.view.tag -1];
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        originalPoint = buttonPicked.center;
    }
    else
    {
        buttonPicked.center = [gesture locationInView:self.view];
    }
}
  • If you want some kind of special treatment for the end gesture (such as only to drop if in a certain location) - you can use the UIGestureRecognizedStateEnd.

  • if it's still not working - in the UIGestureRecognizerStateBegan, try to remove the button from the subview and add it to the main view, and in the UIGestureRecognizerStateEnd check which subview the button is in and add it instead of the main view. Should do the trick.

Good luck.

Naor Levi
  • 117
  • 7
0

According to apple Documentation on Event Handling Guide,
when you first touch on button, it becomes first responder.SO, touchesBegan:withEvent in your ViewController is not called.

Disable the userInteraction of UIbutton and now viewcontroller will be first responder and starts receiving touch events.
button.userInteractionEnabled=NO;

Eventhough you addGestures to ViewController Or any of those Views, they wont respond when your touches are on button.

santhu
  • 4,796
  • 1
  • 21
  • 29