A view loaded from the nib doesn't receive tou

2019-06-10 08:37发布

问题:

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