【问题标题】:Xamarin iOS move UIButton with it click areaXamarin iOS 移动 UIButton 与它的点击区域
【发布时间】:2014-07-10 10:15:02
【问题描述】:

我在 View 中有一个 UIButton,并为此按钮使用了 UITapGestureRecognizer。
我在按钮移动时遇到问题:当我点击它时,我想更改按钮 Y 坐标(升高或降低按钮)。

点击按钮后我尝试这样做:

View.Frame = new RectangleF (0, topOffset, 320, 40); 

View.Frame = new RectangleF (0, 0, 320, 40 + _topOffset );  
_button.Frame = new RectangleF (80, _topOffset, 160, 40);

UI 移动良好,但单击区域保持在同一位置或向下拉伸。 我希望点击区域是有限的周边按钮

有什么想法吗? 谢谢。

【问题讨论】:

    标签: ios xamarin


    【解决方案1】:

    与其为 UIButton 使用 UITapGestureRecognizer,不如直接使用可用的按钮点击事件:

    myButton.TouchUpInside += HandleButtonTouchUpInside;
    
    void HandleButtonTouchUpInside (object sender, EventArgs e)
    {
    myButton.Frame = new RectangleF(myButton.Frame.X, myButton.Frame.Y + _topOffset, myButton.Frame.Width, myButton.Frame.Height);
    
    }
    

    如果你想为更改设置动画,只需像这样包装它:

    UIView.Animate (0.5f, delegate {
        // put the frame change in here
    });
    

    更好的解决方案是在按钮上使用 AutoLayout 约束,并在 HandleButtonTouchUpInside 方法中更新它们,并带有一些动画。

    记得在 ViewWillDisappear 中解开事件处理程序:

    myButton.TouchUpInside -= HandleButtonTouchUpInside;
    

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 2010-10-22
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多