添加 UITapGestureRecognizer 时 UIImageView 崩溃

Posted

技术标签:

【中文标题】添加 UITapGestureRecognizer 时 UIImageView 崩溃【英文标题】:UIImageView crashes when adding UITapGestureRecognizer 【发布时间】:2012-10-02 18:36:30 【问题描述】:
@interface OOTRotaryWheel : UIControl


@property (weak) id <OOTRotaryProtocol> delegate;
@property (nonatomic, strong) UIView *container;
@property int numberOfSections;
@property CGAffineTransform startTransform;
@property (nonatomic, strong) NSMutableArray *sectors;
@property int currentSector;
@property (nonatomic, retain) UIImageView *mask;

- (id) initWithFrame:(CGRect)frame andDelegate:(id)del withSections:(int)sectionsNumber;
- (void)rotate;
- (void) buildSectorsEven;
- (void) buildSectorsOdd;

@end

在我的 .m 文件中

我的初始化方法

mask = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 58, 58)];
mask.image =[UIImage imageNamed:@"centerButton.png"];
mask.center = self.center;
mask.center = CGPointMake(mask.center.x, mask.center.y+3);
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressedDown:)];
[mask addGestureRecognizer:singleTap];
[mask setMultipleTouchEnabled:YES];
[mask setUserInteractionEnabled:YES];



-(IBAction)buttonPressedDown

    //Connect this IBAction to touchDown
    mask.alpha = 0.5f;
    NSLog(@"tapping the center of the earth");

当我点击图片时,它会因这条消息而崩溃

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[OOTRotaryWheel buttonPressedDown:]: unrecognized selector sent to instance 0x71724e0'
*** First throw call stack:

我以编程方式绘制了 UI,因此 Interface Builder 中没有 UI 元素。我的 UI 是一个故事板,但它是空的。

【问题讨论】:

【参考方案1】:

您在 buttonPressedDown: 的实现中忘记了冒号。要么去掉 singleTap 定义的 action 参数中的冒号,要么将冒号和参数添加到你的实现中。

【讨论】:

如何注册 touchUpEvent?当 UIImage 不再被触摸时,我想更改 alpha 这就是 singleTap 所做的——“点击”是先触地然后再触电,所以在触电之前不会调用 buttonPressedDown。

以上是关于添加 UITapGestureRecognizer 时 UIImageView 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

将 UITapGestureRecognizer 添加到 UIControl

如何将参数添加到通用 UITapGestureRecognizer?

尝试添加 UITapGestureRecognizer 但不工作

UITableView添加UITapGestureRecognizer与didSelectRowAtIndexPath冲突解决方法

如何知道 UITapGestureRecognizer 是不是已添加到子视图

添加 UITapGestureRecognizer 时 UIImageView 崩溃