【问题标题】:how to move the UIButton如何移动 UIButton
【发布时间】:2011-07-14 13:26:45
【问题描述】:

我创建了一个UIButton *actionbtn,它的默认图像是1.png,高光图像是2.png,我选择这个按钮,并将它移动到屏幕的任何地方。我的代码是

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self];
    actionbtn.frame = CGRectMake(touchMoved.x-40, touchMoved.y-40, 80, 80);
}

如果我点击按钮并移动它,它不能移动,但如果我触摸屏幕,并在屏幕上移动,按钮可以工作......

【问题讨论】:

    标签: ios uibutton touch


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      当您继续触摸屏幕时,按钮会移动,因为您实现了-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;。如果您在简单地触摸屏幕任意位置时尝试移动按钮,请将该代码放入-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

      【讨论】:

      • 我的意思是我只能移动按钮,不能移动其他按钮
      【解决方案3】:

      在 touchesMoved 函数中添加 if 子句。

      UITouch *touch = [touches anyObject];    
      if( touch.view == button){
           [button.center = [touch locationInView:self.view];
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-28
        • 2015-05-09
        • 2012-01-13
        • 2011-05-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多