mono touch中touch触发对象判断

来源:互联网 发布:数据的统计分析 编辑:程序博客网 时间:2024/05/19 21:58

先看一段代码:

        public override void TouchesBegan (NSSet touches, UIEvent evt)
        {
            base.TouchesBegan (touches, evt);
            
            UITouch touch = touches.AnyObject as UITouch;
            
            if (touch != null) {
                PointF pt = touch.LocationInView (this);
                
                if (_path.ContainsPoint (pt, true)) {
                    Title = "You touched the square";
                } else {
                    Title = "You didn't touch the square";
                }
            }
        }

粗体红色部分是我们的待触发对象(派生至NSOjbect对象),如果当前触发的点(point)是在我们的目标对象内部就表明接触对象。除此之外还定义了几个虚函数用来处理

触碰事件:

public class UIResponder :NSObject
        {
            //...
            public virtual void TouchesBegan (NSSet touches, UIEvent evt);
            public virtual void TouchesMoved (NSSet touches, UIEvent evt);
            public virtual void TouchesEnded (NSSet touches, UIEvent evt);
            public virtual void TouchesCancelled (NSSet touches, UIEvent evt);
            //...
        }



public override void TouchesBegan (NSSet touches, UIEvent evt){base.TouchesBegan (touches, evt);UITouch touch = touches.AnyObject as UITouch;if (touch != null) {if (_img.Frame.Contains (touch.LocationInView (View))) {Tools.Alert ("yes");}}}


原创粉丝点击