0
当我切换到以编程方式创建UIView而不是使用xib时,我的手势识别器停止工作。 问题:我如何以编程方式实现仅使用自定义视图控制器进行手势识别,而没有自定义UIView,或者这不可能?可能使用自定义UIViewController实现手势识别,但没有自定义UIView类?
我知道有人认为这是不好的做法,但那不是我的问题。我宁愿不必将与手势相关的函数移动到UIView子类,因为那样我就会为视图设置一种委托协议,以告诉视图控制器关于该手势,这在我看起来很迂回时很高兴在视图控制器中拥有这一切。接口生成器和xib必须完成一些我可以编程的方式 - 它是什么?
这是我的代码,以防它是相关的。手势识别器与xib一起运行良好。
@interface CustomViewController() <UITextFieldDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate, UITableViewDataSource, UITableViewDelegate, UIViewControllerRestoration, UIScrollViewDelegate>
-(id)init
{
self = [super init];
if(self){
....
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
UIView *sv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
sv.backgroundColor = [UIColor redColor];
self.view = sv;
...
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUp:)];
upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
upSwipe.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:upSwipe];
}
谢谢你的任何建议。
谢谢。那就是诀窍。 – 2014-11-22 14:43:25