【问题标题】:A view loaded from the nib doesn't receive touch events从 nib 加载的视图不接收触摸事件
【发布时间】:2012-05-16 10:22:26
【问题描述】:

我正在尝试从笔尖加载视图并将其添加到主视图。这是我的代码。

我在@interface AddTaskView:UIViewinitWithFrame中加载了笔尖:

 - (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"init with frame");
        // Initialization code

        NSArray *addTaskArrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"AddTaskView"
                                                                     owner:self
                                                                   options:nil];
        self=[addTaskArrayOfViews objectAtIndex:0];
    }
    return self;
    }

之后我初始化视图并将其添加到主视图中:

 self.addTaskView=[[AddTaskView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
    [self.view addSubview:self.addTaskView];

一切正常,但 AddTaskView 没有收到触摸事件。我有三个按钮,当我尝试按下它们时,它们不会收到动作。 我制作了“AddTaskView”类型的笔尖,但没有任何运气。我还查看了子视图,“AddTaskView”是顶视图。

我在 AddTaskView 中添加了这段代码,看看我是否得到了触摸而没有响应:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"touch");
    }

我如何有机会响应触摸事件?

【问题讨论】:

    标签: iphone ios nib loadnibnamed


    【解决方案1】:

    我认为您必须注册一个触摸调度程序,然后尝试获取触摸....以下是该代码

    -(void)registerWithTouchDispatcher{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0swallowsTouches:YES];}
    
    -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    return YES;}
    
    -(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
    touchLocation = [touch  locationInView:[touch view]];
    NSLog(@"Touch is working");}
    

    【讨论】:

      【解决方案2】:
      [UIView beginAnimations:@"MoveAndStrech" context:nil];
      
      [UIView setAnimationDuration:1];
      
      [UIView setAnimationBeginsFromCurrentState:YES];
      
      [UIView commitAnimations];
      

      【讨论】:

      • 您还回答其他问题吗?这如何回答这个问题?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 2020-07-02
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多