name of UIView that was touched

前端 未结 3 1695
生来不讨喜
生来不讨喜 2021-01-07 06:39

How can I get the name of the [touch view] in the touchesbegan event. So if it were UIView *aaaaaview I would get aaaaaview as the return; Thank You, nonono

3条回答
  •  有刺的猬
    2021-01-07 07:00

    The touches parameter is a set of UITouch instances, which have a corresponding view property:

    The value of the property is the view object in which the touch originally occurred.

    UIViews don't have a specific name, but you can just check for their identity, i.e.:

    if([touch view] == aaaview) {}
    

    or use - (BOOL)isEqual:.

    You could also use the tag property instead to give them meaningful identities:

    enum MyViews {
        AaaView,
        // ...
    };
    
    aaaview.tag = AaaView;
    
    // ...
    if([touch view].tag == AaaView) {}
    

    If you really need names for some reason not mentioned in the question, you could subclass UIView and introduce a name property.

提交回复
热议问题