我有一個按鈕創建編程創建編程浮拖能按鈕沒有故事板(Xamarin)
InfoBtn = _utilProvider.FloatingBtn("My Button"); //Returns Floating UIButton
InfoBtn.Frame = new CGRect((ScreenWidth/2) - 22.5, ScreenHeight - 65, 55, 55);
View.Add(InfoBtn);
我想補充的阻力和編程能力的下降給它。問題是,由Xamarin迷上了事件不會讓我使用自定義事件處理程序,如UIEvent
以這種方式(從https://www.cocoanetics.com/2010/11/draggable-buttons-labels/的iOS例如直接轉換):
InfoBtn.TouchDragInside += (btn, uievent) =>
{
var button = (UIButton)btn;
var e = (UIEvent)uievent; //ERROR: Can not convert type 'System.EventArgs' to 'UIKit.UIEvent'
// get the touch
UITouch touch = (UITouch)e.TouchesForView(button).AnyObject;
// get delta
CGPoint previousLocation = touch.PreviousLocationInView(button);
CGPoint location = touch.LocationInView(button);
nfloat delta_x = location.X - previousLocation.X;
nfloat delta_y = location.Y - previousLocation.Y;
// move button
button.Center = new CGPoint(button.Center.X + delta_x, button.Center.Y + delta_y);
};
根據Xamarin的例子,因爲他們使用Using Touch in iOS
https://developer.xamarin.com/guides/ios/application_fundamentals/touch/ios_touch_walkthrough/故事板。
定製UIViewController
通過設置自定義類
創建
partial class TouchViewController : UIViewController
,並分配到ViewController怎麼可能customClass
進行編程設定?
我也嘗試添加UIGestureRecognizer
InfoBtn.AddGestureRecognizer(new MyGestureRecognizer());
partial class MyGestureRecognizer : UIGestureRecognizer
{
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
// Touch started
}
}
public override void TouchesCancelled(NSSet touches, UIEvent evt)
{
base.TouchesCancelled(touches, evt);
}
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
// move the shape
}
}
}
但它永遠只能進入TouchesBegan
方法。
基本上在網上找到每個教程後,我感到沮喪。
任何幫助,將不勝感激