0

Can we use blocks with Gesture Recognizers? It doesn't appear so. For example, this does not work:

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget: self
     action:^(id sender) {
     }
];

Am I missing something, or are blocks just not supported by the UIGestureRecognizer class?

Axeva
  • 4,697
  • 6
  • 40
  • 64

1 Answers1

11

However, this should:

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:[^{
    // do stuff
} copy] action:@selector(invoke)];

You shouldn't probably do this, however, since it's a private method.

  • I think it's not recommended to follow this pattern since invoke is a private method. – as-cii Jan 23 '13 at 16:52
  • 3
    @AS-CII True, nevertheless, it should work. :) OP asked "Can we" and not "Should we". –  Jan 23 '13 at 16:53
  • eheh fair enough. Anyway I would write that "although it is possible you shouldn't" :) – as-cii Jan 23 '13 at 16:55
  • 1
    @AS-CII Added for completeness. –  Jan 23 '13 at 16:55
  • I think question is what works for right now vs. what will break horribly in a future version of iOS causing you to frantically hunt down every instance of the bad pattern to get a fix for all your users. – Jeffery Thomas Jan 23 '13 at 16:58
  • @JefferyThomas "You shouldn't probably do this" means one should not probably do this. –  Jan 23 '13 at 17:01
  • So, the real answer is "Just don't". Noted. – Axeva Jan 23 '13 at 22:01