title: Drag UIButton from one view to another by using touch method
body: <p>Hello all,</p>
  
  <p>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.</p>
  
  <p>please help me.</p>
  
  <p>Thanks  in advance.</p>
  
  <pre><code>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 &gt; iView.frame.origin.x &amp;&amp;
                      touchPoint.x &lt; iView.frame.origin.x + iView.frame.size.width &amp;&amp;
                      touchPoint.y &gt; iView.frame.origin.y &amp;&amp;
                      touchPoint.y &lt; 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 &gt; self.vieworange.frame.origin.x &amp;&amp;
          touchPoint.x &lt; self.vieworange.frame.origin.x + self.vieworange.frame.size.width &amp;&amp;
          touchPoint.y &gt; self.vieworange.frame.origin.y &amp;&amp;
          touchPoint.y &lt; 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 &lt; 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 : --&gt; %@", self.btnalpha.titleLabel.text);
      }
  }
  </code></pre>