UIGestureRecognizer 应该在 ViewController 的什么地方设置? [英] Where in ViewController should UIGestureRecognizer be set up?

查看:24
本文介绍了UIGestureRecognizer 应该在 ViewController 的什么地方设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 ViewController 的视图设置为 MyView

If ViewController's view is set to MyView

并且 ViewController 维护一个指向它的视图的指针(通过故事板设置)

And ViewController maintains a pointer to it's view (set up via storyboards)

@property (weak, nonatomic) IBOutlet MyView *v;

ViewController 的什么地方,应该发生以下情况吗?

Where in ViewController, should the following take place?

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] 
             initWithTarget:v action:@selector(pan:)];
[v addGestureRecognizer: panGesture];

推荐答案

viewDidLoad 是进行这种初始化的好地方,因为此时视图层次结构已经加载到内存中,无论它是从故事板或笔尖加载的,或者以编程方式创建的.

viewDidLoad is a good place for this kind of initialization, since the view hierarchy will have been loaded into memory at that point, whether it was loaded from a storyboard or nib, or created programmatically.

这篇关于UIGestureRecognizer 应该在 ViewController 的什么地方设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆