PanGestureRecognizer 的有限区域

Posted

技术标签:

【中文标题】PanGestureRecognizer 的有限区域【英文标题】:Limited Area for PanGestureRecognizer 【发布时间】:2012-05-07 08:57:11 【问题描述】:

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

非常感谢你 ;)

【问题讨论】:

【参考方案1】:

您可以为 UIPanGestureRecognizer 实现委托方法。检查手势的位置是否在您感兴趣的范围内。对于 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;
    

【讨论】:

谢谢,“_bounds.Contains”非常有帮助;)

以上是关于PanGestureRecognizer 的有限区域的主要内容,如果未能解决你的问题,请参考以下文章

PanGestureRecognizer 在拖动过程中零星出现

使用 pangesturerecognizer 创建边界以阻止对象进入状态栏下方?

UIScrollView、PanGestureRecognizer 和 iOs 4

临界区的进度和有限等待是啥?

Swift - 带有英雄动画的 PanGestureRecognizer

如何通过 panGestureRecognizer 启用 collectionView 分页