1

我只需要在子视图中拖放 uibutton。实际上我正在主视图中查看子视图。UIbutton 添加到子视图中。当我拖动按钮时,它会在两个视图上移动,但我只需要在子视图中拖动按钮。谁能帮助我。提前致谢。

- (void) drag
{
  //subview
  UIView *viewscramble = [[UIView alloc] initWithFrame:CGRectMake(0, 90, 320, 50)];
  [viewscramble setBackgroundColor:[UIColor redColor]];
  [self.view addSubview:viewscramble];

   //button
   UIButton  *btnscramble = [[UIButton alloc] initWithFrame:CGRectMake(5, 0, 50, 50)];
   btnscramble.titleLabel.text = @"A";
   [btnscramble setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
   [btnscramble addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
   [btnscramble setBackgroundImage:[UIImage imageNamed:@"box.png"] forState:UIControlStateNormal];

   //add button`     
   [viewscramble addSubview:btnscramble];
   [btnscramble release];
}

- (IBAction)imageMoved:(id)sender withEvent:(UIEvent *) event
{
  CGPoint point = [[[event allTouches]anyObject] locationInView:viewscramble];
  UIControl *control = sender;
  control.center = point;
}
4

5 回答 5

3

我刚刚从以下位置找到了您的问题的解决方案:- http://www.cocoanetics.com/2010/11/draggable-buttons-labels/

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // create a new button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Drag me!" forState:UIControlStateNormal];

    // add drag listener
    [button addTarget:self action:@selector(wasDragged:withEvent:) 
        forControlEvents:UIControlEventTouchDragInside];

    // center and size
    button.frame = CGRectMake((self.view.bounds.size.width - 100)/2.0,
            (self.view.bounds.size.height - 50)/2.0,
            100, 50);

    // add it, centered
    [self.view addSubview:button];
}


- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
    // get the touch
    UITouch *touch = [[event touchesForView:button] anyObject];

    // get delta
    CGPoint previousLocation = [touch previousLocationInView:button];
    CGPoint location = [touch locationInView:button];
    CGFloat delta_x = location.x - previousLocation.x;
    CGFloat delta_y = location.y - previousLocation.y;

    // move button
    button.center = CGPointMake(button.center.x + delta_x,
        button.center.y + delta_y);
}

希望它对你有帮助:)

于 2012-12-03T05:30:54.863 回答
0
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint pointMoved = [touch locationInView:"SUBVIEWNAME.view"];   
    button.frame = CGRectMake(pointMoved.x, pointMoved.y, 64, 64);
}
于 2012-12-03T05:27:30.800 回答
0
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{

    UITouch *tap = [touches anyObject];
    CGPoint pointToMove = [tap locationInView:yourSubView]; // just assign subview where you want to move the Button
}
于 2012-12-03T05:31:07.030 回答
0
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if([touch view] == YOUR_BUTTON_OBJECT){
       CGPoint point = [touch locationInView:YOUR_PARENT_VIEW];
       //YOUR_X_LIMIT is your subview end in x direction.
       //YOUR_Y_LIMIT is your subview end in y direction.
       if(point.x < YOUR_X_LIMIT && point.y < YOUR_Y_LIMIT){
          button.center = point;
       }else{
         // do nothing since touch is outside your limit.
       }
    }
}
于 2012-12-03T05:31:29.023 回答
0

使用 UIButton 的 UIControlEventTouchDragInside 和 UIControlEventTouchUpInside 事件,例如

// add drag listener
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
// add tap listener
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside];

您可以使用hbdraggablebutton控件..

于 2013-10-11T14:10:18.993 回答