【问题标题】:How to make UIButton Draggeable?如何使 UIButton 可拖动?
【发布时间】:2012-12-03 05:23:17
【问题描述】:

我只需要在子视图中拖放 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;
}

【问题讨论】:

  • 你能发一些代码吗??
  • @Angel create viewscramble global 意味着在 .h 文件中只定义这样你就可以在整个班级中使用它...

标签: iphone uiview drag-and-drop uibutton ios-simulator


【解决方案1】:

我刚刚从以下位置找到了您的问题的解决方案:- 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);
}

希望对你有帮助:)

【讨论】:

    【解决方案2】:
    - (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);
    }
    

    【讨论】:

      【解决方案3】:
      -(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
      }
      

      【讨论】:

      • @Angel create viewscramble global 意味着在 .h 文件中只定义这样你就可以在整个班级中使用它...
      • -[UIButton anyObject]: 无法识别的选择器发送到实例 0x758a930
      • viewscramble 仅在 .h 中创建。
      • 但这里不使用 UIButton 代替触摸对象配合...只需使用 touchmoved 方法的 touches 参数..
      【解决方案4】:
      - (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.
             }
          }
      }
      

      【讨论】:

        【解决方案5】:

        使用 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控制..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多