A view loaded from the nib doesn't receive touch events

倾然丶 夕夏残阳落幕 提交于 2019-12-08 13:01:56

问题


I am trying to load a a view from the nib and add it to the main view. Here is my code.

I loaded the nib in the initWithFrame of the @interface AddTaskView:UIView:

 - (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;
    }

After that I init the view and add it to the main view:

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

Everything works fine but the AddTaskView doesn't receive the touch event. I have three buttons and when I try to push them they don't receive the actions. I made the nib of type "AddTaskView" but without any luck. I also looked in the subviews and the "AddTaskView" is the top view.

I added this code in the AddTaskView to see if I get a touch and no response:

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

How do I get a chance to respond to touch events?


回答1:


I think you have to register a touch dispatcher and then try to get touch....following is the code for that

-(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];


来源:https://stackoverflow.com/questions/10616576/a-view-loaded-from-the-nib-doesnt-receive-touch-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!