2016-10-04 28 views
0

我试图用initWithFrame但我不能够显示UIView,这种代码解决了很多问题,因为我看到这里,但不是在我的情况:包括tapgesture的UIview自定义类可以吗?

 
- (id)initWithFrame:(CGRect)frame myParams:(NSArray*)params; 

.m文件

 

    - (id)initWithFrame:(CGRect)frame myParams:(NSArray*)params{ 
     self = [super initWithFrame:frame]; 
     if(self){ 
     UIView *view = [UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; 
     //Trying to send an uiview with gesture include 
     view.userInteractionEnabled = YES; 
     UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc] 
     initWithTarget:self action:@selector(test)]; 
     [view addGestureRecognizer:tap];  
     [self addSubview:view]; 
     } 
     return self; 
    } 

这里是我如何添加视图在UIViewController: 也许在这里我做错了吧?

 


    Custom *view = [Custom alloc] initWithFrame:self.view.bounds myParams:array]; 
     [self.view addSubview:view]; 

所以,我的问题是,它可以添加UITapGesture在这个类中产生的UIView,因为它不点火:

.h文件中

 

    + (UIView *)viewParams:(NSArray*)params; 

.M文件

 

    + (UIView *)viewWithParams:(NSArray*)params{ 
     UIView *view = [UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; 

     NSLog(@"read params %@", params); 

     //Trying to send an uiview with gesture include 
     view.userInteractionEnabled = YES; 
     UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc] 
     initWithTarget:self action:@selector(test)]; 
     [view addGestureRecognizer:tap]; 

     return view; 
    } 
    - (void)test{ 
     NSLog('it works...'); 
    } 

更新:实例方法正在工作。但作为类方法我不能使它工作。有人知道可能吗?

+1

你可以尝试给self.view.userInteractionEnabled = YES;在视图控制器? – Arun

+0

这并没有工作既不尝试作为类方法。 – usosystem

回答

0

第1部分

如果您正在使用XIB您customView,那么你需要使用awakeFromNib方法。但是,如果您尝试通过代码创建,则只需使用initWithFrame:方法并在初始化程序中添加您的手势识别器。

第二部分

烧成双击手势识别您要添加识别目标即你customView。所以事件会在你的customViewClass类中触发,然后才能在控制器中获取这些事件,以便使用协议和委托。并将customView的代理设置为您要在控制器中使用的任何控制器。

0

View将烧制的initWithCoder正好与编码器初始化使用这个代码后加...

dispatch_after(0.1, dispatch_get_main_queue(), ^(void) 
      { 
       UIView *view = [UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; 
      //Trying to send an uiview with gesture include 
      view.userInteractionEnabled = YES; 
      UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc] 
      initWithTarget:self action:@selector(test)]; 
      [view addGestureRecognizer:tap];  
      [self addSubview:view]; 
      }); 
} 
     return self; 
    } 

最小时间间隔0.1最大时间interaval设置,按您的数据负载要求

相关问题