- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当用户点击应用程序中的标签时,我正在尝试检测 Xamarin 中的按下和释放事件,以实现长按手势。
我已经从另一个 StackOverflow 问题创建了一个 LongPressBehavior 类,但标签没有附加按下和释放事件。
我可以使用按钮,但我仍然需要格式化文本 (FormattedString),其中包含用户可以单击以导航到网站的 URL 链接。
C#
public class LongPressBehavior : BehaviorBase<ExtendedLabel>
{
private readonly object _syncObject = new object();
private const int Duration = 1000;
//timer to track long press
private Timer _timer;
//the timeout value for long press
private readonly int _duration;
//whether the button was released after press
private volatile bool _isReleased;
/// <summary>
/// Occurs when the associated button is long pressed.
/// </summary>
public event EventHandler LongPressed;
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command),
typeof(ICommand), typeof(LongPressBehavior), default(ICommand));
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(LongPressBehavior));
/// <summary>
/// Gets or sets the command parameter.
/// </summary>
public object CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
/// <summary>
/// Gets or sets the command.
/// </summary>
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
protected override void OnAttachedTo(ExtendedLabel button)
{
base.OnAttachedTo(button);
this.BindingContext = button.BindingContext;
button.Clicked += Button_Pressed;
//button.Released += Button_Released;
}
protected override void OnDetachingFrom(ExtendedLabel button)
{
base.OnDetachingFrom(button);
this.BindingContext = null;
button.Clicked -= Button_Pressed;
//button.Released -= Button_Released;
}
/// <summary>
/// DeInitializes and disposes the timer.
/// </summary>
private void DeInitializeTimer()
{
lock (_syncObject)
{
if (_timer == null)
{
return;
}
_timer.Change(Timeout.Infinite, Timeout.Infinite);
_timer.Dispose();
_timer = null;
Debug.WriteLine("Timer disposed...");
}
}
/// <summary>
/// Initializes the timer.
/// </summary>
private void InitializeTimer()
{
lock (_syncObject)
{
_timer = new Timer(Timer_Elapsed, null, _duration, Timeout.Infinite);
}
}
private void Button_Pressed(object sender, EventArgs e)
{
Debug.WriteLine("Pressed");
_isReleased = false;
InitializeTimer();
}
private void Button_Released(object sender, EventArgs e)
{
Debug.WriteLine("Released");
_isReleased = true;
DeInitializeTimer();
}
protected virtual void OnLongPressed()
{
var handler = LongPressed;
handler?.Invoke(this, EventArgs.Empty);
if (Command != null && Command.CanExecute(CommandParameter))
{
Command.Execute(CommandParameter);
}
}
public LongPressBehavior()
{
_isReleased = true;
_duration = Duration;
}
public LongPressBehavior(int duration) : this()
{
_duration = duration;
}
private void Timer_Elapsed(object state)
{
DeInitializeTimer();
if (_isReleased)
{
return;
}
Device.BeginInvokeOnMainThread(OnLongPressed);
}
}
public class ExtendedLabel : Label
{
private event EventHandler Pressed;
private event EventHandler Released;
public string Name
{
get; set;
}
public void DoClick()
{
Pressed.Invoke(this, null);
}
public event EventHandler Clicked
{
add
{
lock (this)
{
Pressed += value;
TapGestureRecognizer g = new TapGestureRecognizer();
g.Tapped += (s, e) => Pressed?.Invoke(s, e);
GestureRecognizers.Add(g);
}
}
remove
{
lock (this)
{
Pressed -= value;
GestureRecognizers.Clear();
}
}
}
public event EventHandler UnClicked
{
add
{
lock (this)
{
Released += value;
TapGestureRecognizer g = new TapGestureRecognizer();
g.Tapped += (s, e) => Released?.Invoke(s, e);
GestureRecognizers.Add(g);
}
}
remove
{
lock (this)
{
Released -= value;
GestureRecognizers.Clear();
}
}
}
}
XAML
<controls:ExtendedLabel HorizontalOptions="Fill" HorizontalTextAlignment="End" TextColor="White" FormattedText="{Binding OutFormattedBody}">
<controls:ExtendedLabel.Behaviors>
<behaviors:LongPressBehavior LongPressed="OnClick"/>
</controls:ExtendedLabel.Behaviors>
</controls:ExtendedLabel>
我希望能够在标签上点击并按住大约一秒钟以触发 LongPress 事件,但仍然能够单击可能位于 FormattedString 中的链接。
最佳答案
您可以尝试在各个平台上实现效果。首先,在 Forms 项目中定义效果类:
public class PressedEffect : RoutingEffect
{
public PressedEffect() : base("MyApp.PressedEffect")
{
}
public static readonly BindableProperty LongPressedCommandProperty = BindableProperty.CreateAttached("LongPressedCommand", typeof(ICommand), typeof(PressedEffect), (object)null);
public static ICommand GetLongPressedCommand(BindableObject view)
{
return (ICommand)view.GetValue(LongPressedCommandProperty);
}
public static void SetLongPressedCommand(BindableObject view, ICommand value)
{
view.SetValue(LongPressedCommandProperty, value);
}
public static readonly BindableProperty LongParameterProperty = BindableProperty.CreateAttached("LongParameter", typeof(object), typeof(PressedEffect), (object)null);
public static object GetLongParameter(BindableObject view)
{
return view.GetValue(LongParameterProperty);
}
public static void SetLongParameter(BindableObject view, object value)
{
view.SetValue(LongParameterProperty, value);
}
public static readonly BindableProperty LongRelesedCommandProperty = BindableProperty.CreateAttached("LongRelesedCommand", typeof(ICommand), typeof(PressedEffect), (object)null);
public static ICommand GetLongRelesedCommand(BindableObject view)
{
return (ICommand)view.GetValue(LongRelesedCommandProperty);
}
public static void SetLongRelesedCommand(BindableObject view, ICommand value)
{
view.SetValue(LongRelesedCommandProperty, value);
}
}
Android 实现:
[assembly: ResolutionGroupName("MyApp")]
[assembly: ExportEffect(typeof(AndroidPressedEffect), "PressedEffect")]
namespace LongPressedDemo.Droid
{
public class AndroidPressedEffect : PlatformEffect
{
private bool _attached;
public AndroidPressedEffect()
{
}
protected override void OnAttached()
{
if (!_attached)
{
if (Control != null)
{
Control.LongClickable = true;
Control.Touch += Control_Touch;
}
else
{
Container.LongClickable = true;
Container.Touch += Control_Touch;
}
_attached = true;
}
}
private void Control_Touch(object sender, Android.Views.View.TouchEventArgs e)
{
if (e.Event.Action == MotionEventActions.Down)
{
var command = PressedEffect.GetLongPressedCommand(Element);
command?.Execute(PressedEffect.GetLongParameter(Element));
}
else if (e.Event.Action == MotionEventActions.Up)
{
var command = PressedEffect.GetLongRelesedCommand(Element);
command?.Execute(PressedEffect.GetLongParameter(Element));
}
}
protected override void OnDetached()
{
if (_attached)
{
if (Control != null)
{
Control.LongClickable = true;
Control.Touch -= Control_Touch;
}
else
{
Container.LongClickable = true;
Container.Touch -= Control_Touch;
}
_attached = false;
}
}
}
}
iOS 实现:
[assembly: ResolutionGroupName("MyApp")]
[assembly: ExportEffect(typeof(iOSPressedEffect), "PressedEffect")]
namespace LongPressedDemo.iOS
{
public class iOSPressedEffect : PlatformEffect
{
private bool _attached;
private readonly UILongPressGestureRecognizer _longPressRecognizer;
public iOSPressedEffect()
{
_longPressRecognizer = new UILongPressGestureRecognizer(HandleLongClick);
}
protected override void OnAttached()
{
if (!_attached)
{
if (Control != null)
{
Control.AddGestureRecognizer(_longPressRecognizer);
Control.UserInteractionEnabled = true;
}
else
{
Container.AddGestureRecognizer(_longPressRecognizer);
}
_attached = true;
}
}
private void HandleLongClick(UILongPressGestureRecognizer recognizer)
{
if (recognizer.State == UIGestureRecognizerState.Began)
{
var command = PressedEffect.GetLongPressedCommand(Element);
command?.Execute(PressedEffect.GetLongParameter(Element));
}
else if (recognizer.State == UIGestureRecognizerState.Ended)
{
var command = PressedEffect.GetLongRelesedCommand(Element);
command?.Execute(PressedEffect.GetLongParameter(Element));
}
}
protected override void OnDetached()
{
if (_attached)
{
if (Control != null)
{
Control.RemoveGestureRecognizer(_longPressRecognizer);
}
else
{
Container.RemoveGestureRecognizer(_longPressRecognizer);
}
_attached = false;
}
}
}
}
最后,消耗了这个效果:
<Label Text="Welcome to Xamarin.Forms!"
local:PressedEffect.LongPressedCommand="{Binding PressedCommand}"
local:PressedEffect.LongRelesedCommand="{Binding ReleasedCommand}">
<Label.Effects>
<local:PressedEffect/>
</Label.Effects>
</Label>
关于c# - Xamarin 中是否有针对标签上的 GestureRecognizer 的新闻和发布 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56797488/
我正在尝试在 iPhone 的 Objective C 中长按一个按钮。 这是我为 GestureRecognizer 编写的代码: UILongPressGestureRecognizer
视网膜设备每侧的像素数是非视网膜设备的两倍。 使用 UIPanGestureRecognizer 时,我可以通过调用 - (CGPoint)velocityInView:(UIView *)view
我的 Storyboard中有此设置。 在我的第一个 ViewController 场景中,我有一个来自 MapBox 的 MapView。我在那里放置了一个文本字段(AddressTextField
我想长按表格 View 单元格,但我收到错误: UIGestureRecognizer.Type' does not have a member named 'state' 这是代码 overrid
我正在尝试使用 gestureRecognizer 使我的单元格可滑动: - (UITableViewCell *)tableView:(UITableView *)aTableView cellFo
我将 gestureRecognizer 与 UIImageView 一起使用。我有两个 UIImageView。我需要在两个 UIImageView 中上传图片。为了打开 UIImagePicker
我在理解 UIGestureRecognizers 时遇到了问题。我现在的目标是拥有一组 GestureRecognizers 来执行不同的任务,例如: override func viewDidLo
通过 UIImageView 的子类初始化后,我有以下代码行: self.userInteractionEnabled = true self.addGestureRecognizer(UITapGe
我有一个带有自定义按钮的 Collection View 。并且有一个可以更改图像及其背景颜色的功能。我的问题:如果我单击它,它适用于每个按钮,我想创建一个可以更改我的 SELECTED 按钮的颜色和
我目前正在使用 cocos2d 构建游戏,但遇到以下问题:我有一个 MenuScene,用户可以在其中开始游戏。当他这样做时,gestureRecognizer 将通过以下方式使用级别进行初始化: C
我在包含多个手势识别器的 View 中添加了一个 subview ,但是当引入此 View 时,当用户在这个新添加的 View 中点击时,手势将不会触发。然而,我要介绍的是一个UIBezierPath
我有以下问题:在我的 iphone 应用程序中,我有一个 UIView 包含 0 到多个 subview 。这些 subview 可能会溢出父 View 并因此被隐藏,它们都是 UIButtons。我
@IBAction func BetPop(sender: UITapGestureRecognizer) { var BetPopVC = self.storyboard?.instanti
我有 2 个 GesturerRecognizer,一个是自定义 3DTouch,另一个是单个 UITapGestureRegognizer。我想做的是,如果你点击一次它执行一个 segue,如果你去
我尝试识别用户在 View 上的点击。我用下面的代码试了一下: override func viewDidLoad() { super.viewDidLoad() let tap =
我正在尝试在我的应用程序上同时检测向上和向下滑动。我有一个 SKScene,我在 func didMoveToView: view 中添加了手势识别器我已设置委托(delegate):UIGestur
我一直在使用 this文章(和其他一些文章)尝试在我的应用程序中实现手势识别,它确实有效。但是,我想做的是检测多个手势;例如,滑动和触摸。我似乎无法确定 MouseUp 事件是由手势结束引起的,还是由
我正在尝试找到一种方法,当用户在过滤我的 tableview 的内容后点击屏幕上的其他地方时,立即隐藏键盘。 但不知何故,当我添加 UITapGestureRecognizer 时,我的 TableV
我尝试结合许多教程来尝试让手势识别器在 mapView 上收到手势后在 tableView 上启动动画(一旦 map 像旧的 uber 应用程序一样移动就会缩小表格)。动画功能,但是一旦动画完成,我收
我有一个 Web View (在 View 内)和一个大部分时间隐藏的工具栏。 这是 ipad 杂志很常见的行为:点击页面会隐藏和显示工具栏,但工具栏默认隐藏。 我正在使用 shouldRecogni
我是一名优秀的程序员,十分优秀!