gpt4 book ai didi

xamarin.ios - PanGestureRecognizer 的有限区域

转载 作者:行者123 更新时间:2023-12-04 18:21:54 25 4
gpt4 key购买 nike

是否可以为 PanGestureRecognizer 设置边框,使其只能在有限区域/ View 中平移图像?

非常感谢 ;)

最佳答案

您可以实现 UIPanGestureRecognizer 的委托(delegate)方法。检查手势的位置是否在您感兴趣的范围内。对于 should* 方法,您可以返回 false 以取消手势。手势开始后,您可以通过将 State 属性设置为 Cancelled 来取消它。

public class GestureView: UIView
{
RectangleF _bounds;

public GestureView (RectangleF rect) : base (rect)
{
this.BackgroundColor = UIColor.Brown;

UIPanGestureRecognizer pan = new UIPanGestureRecognizer (this, new Selector ("panViewWithGestureRecognizer:"));
this.AddGestureRecognizer (pan);
pan.WeakDelegate = this;
_bounds = new RectangleF (0,0,200, 100);
}

[Export("panViewWithGestureRecognizer:")]
void PanGestureMoveAround (UIPanGestureRecognizer p)
{
if (_bounds.Contains (p.LocationInView (this)))
{
Console.WriteLine ("PanGestureMoveAround true");
return;
}
Console.WriteLine ("PanGestureMoveAround false");
p.State = UIGestureRecognizerState.Cancelled;
return;
}

[Export ("gestureRecognizerShouldBegin:")]
bool ShouldBegin (UIGestureRecognizer recognizer)
{
if (_bounds.Contains (recognizer.LocationInView (recognizer.View)))
{
Console.WriteLine ("ShouldBegin true");
return true;
}
Console.WriteLine ("ShouldBegin false");
return false;
}

[Export ("gestureRecognizer:shouldReceiveTouch:")]
public bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch)
{
if (_bounds.Contains (touch.LocationInView (recognizer.View)))
{
Console.WriteLine ("ShouldReceiveTouch true");
return true;
}
Console.WriteLine ("ShouldReceiveTouch false");
return false;
}
}

关于xamarin.ios - PanGestureRecognizer 的有限区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com