【问题标题】:UIGesture returns UIImageView and not custom viewUIGesture 返回 UIImageView 而不是自定义视图
【发布时间】:2013-02-23 01:54:34
【问题描述】:

我在我的根视图中添加了一个自定义视图并添加了一个 UITapGesture。在调用 UITapGesture 代码的“sender.view”时,它不再由我的自定义 UIView 表示,而是“sender.view”的类型为 UIImageView。

因此,当 iOS 需要 UIView 时,向我的自定义视图发送消息真的很困难 - 我得到的警告是:“不兼容的指针类型将 UIView 发送到参数 CardView(我的自定义视图)。

我认为这与响应链有关;文档说 - “当 iOS 识别出一个事件时,它会将事件传递给看起来与处理该事件最相关的初始对象,例如发生触摸的视图。” - 关于 iOS 中的事件

这里的关键字是“似乎”——我如何告诉 iOS 我的自定义视图应该处理手势?

问题:如何让 sender.view 直接引用我的自定义视图?

下面是一些代码:

//Creates the Card's view
CardView *cardView = [[CardView alloc]initWithFrame:CGRectMake(0, 0, 67,99)];
//For Single Tap
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapGesture:)];
[cardView addGestureRecognizer:tapGesture];
[self.view addSubview:cardView];

【问题讨论】:

  • 尝试将发件人投射到 CardView,我认为它应该可以工作
  • Hossam - 感谢您的回复。我不知道如何投射它以及在哪里投射它。我说 CardView 是 UIImage 类型吗?代码在哪里进行转换?舒克兰
  • in the handleSingleTapGesture: 会给你 (TapRecognizer *)sender。然后投:CardView *card = (CardView *)sender.view
  • 它对你有用吗?如果是这样,我会将其添加为答案,您会将其标记为正确答案,以帮助任何人提出相同的问题

标签: ios objective-c uitapgesturerecognizer responder-chain


【解决方案1】:

handleSingleTapGesture: 中会给你(UITapGestureTecongnizer *)sender 所以像这样投射:

CardView *card = (CardView *)sender.view;
//do you stuff with card 

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多