UITapGesture does not work with dynamically created UIViews

I am doing dynamic UIViewsthrough code and trying to add UITapGestureRecogniseron them. But for some reason they do not respond. Here is the code:

-(void)createRandomBlock{
    UIView * block = [[UIView alloc] initWithFrame:CGRectMake([self getRandomPosition], 0-_heightOfBlock, _widthOfBlock, _heightOfBlock)];
    block.backgroundColor = [self randomColor];
    block.userInteractionEnabled=YES;

    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
    tapGesture.delegate = self;
    tapGesture.numberOfTouchesRequired = 1;
    tapGesture.numberOfTapsRequired=1;
    [block addGestureRecognizer:tapGesture];

    [self.view addSubview:block];

    [UIView animateWithDuration:_durationOfAnimation delay:0.0 options:options animations:^{
        [block setFrame:CGRectMake(block.frame.origin.x, ScreenHeight, block.bounds.size.width, block.bounds.size.height)];
    } completion:^(BOOL finished) {
        [block removeFromSuperview];
    }];
}

-(void)blockTapped:(UITapGestureRecognizer*)gesture{
    NSLog(@"I am being called?");
    UIView * block = (UIView*)[gesture view];
    [block removeFromSuperview];
}

Can anybody help?

thank

+4
source share
5 answers

I tried with the same code, it worked for me. Just a change, I did not set a delegate for tapGesture .

-(void)createRandomBlock{
    UIView * block = [[UIView alloc] initWithFrame:randomPosition];
    block.backgroundColor = [self randomColor];
    block.userInteractionEnabled=YES;

    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
    tapGesture.numberOfTapsRequired=1;
    [block addGestureRecognizer:tapGesture];

    [self.view addSubview:block];
}

-(void)blockTapped:(UITapGestureRecognizer*)gesture{
    NSLog(@"I am being called?");
    UIView * block = (UIView*)[gesture view];
    [block removeFromSuperview];
}

I think due to the addition of a delegate, it overrides the selector you selected and calls the delegate methods instead.

+1
source

. , , .

-(void)createRandomBlock{
    int x_coord = arc4random() % 200; //Random number from 0-320  // set random x position 
    int y_coord = arc4random() % 481; //Random number from 0-480  // set random y position

    UIView * block = [[UIView alloc] initWithFrame:CGRectMake(x_coord, y_coord, 100, 100)]; // for testing set width & height 100 * 100
    block.backgroundColor = [UIColor redColor]; // for fix color you can use your change color function
    block.userInteractionEnabled=YES;

    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
    tapGesture.delegate = self;
    tapGesture.numberOfTouchesRequired = 1;
    tapGesture.numberOfTapsRequired=1;
    [block addGestureRecognizer:tapGesture];

    [self.view addSubview:block];

[UIView animateKeyframesWithDuration:1.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        [block setFrame:CGRectMake(block.frame.origin.x, 0, block.bounds.size.width, block.bounds.size.height)];
    } completion:^(BOOL finished) {
//        [block removeFromSuperview];
    }];
}

-(void)blockTapped:(UITapGestureRecognizer*)gesture{
    NSLog(@"I am being called?");
    UIView * block = (UIView*)[gesture view];
    [block removeFromSuperview];
}
+1

, . -, createRandomBlock.

self.view.userInteractionEnabled = YES;

+1

, [self getRandomPosition] _widthOfBlock .. , .

 -(void)createRandomBlock{

UIView * block = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
block.backgroundColor = [UIColor redColor];
block.userInteractionEnabled=YES;

UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
tapGesture.delegate = self;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.numberOfTapsRequired=1;
[block addGestureRecognizer:tapGesture];

[self.view addSubview:block];
}

-(void)blockTapped:(UITapGestureRecognizer*)gesture{
NSLog(@"I am being called?");
UIView * block = (UIView*)[gesture view];
[block removeFromSuperview];
}

:

touchesBegan , ,

-(void)createRandomBlock{

UIView * block = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
block.backgroundColor = [UIColor redColor];
block.userInteractionEnabled=YES;
block.tag = 100;
self.view.userInteractionEnabled =YES;

UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
//  tapGesture.delegate = self;
// tapGesture.numberOfTouchesRequired = 1;
tapGesture.numberOfTapsRequired=1;


[block addGestureRecognizer:tapGesture];
[self.view addSubview:block];




 [UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    [block setFrame:CGRectMake(block.frame.origin.x, self.view.frame.size.height-200, block.bounds.size.width, block.bounds.size.height)];


} completion:^(BOOL finished) {
    [block removeFromSuperview];
}];


}

-(void)blockTapped:(UITapGestureRecognizer*)gesture{

NSLog(@"I am being called?");
UIView * block = (UIView*)[gesture view];
[block removeFromSuperview];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
UIView *tempView = [self.view viewWithTag:100];

if ([tempView.layer.presentationLayer hitTest:touchLocation]) {

    NSLog(@"call");
    [tempView removeFromSuperview];
}


}
+1

You delete the view in the completion block, which means that after the animation finishes, your view will be deleted. Remove the bottom line from the completion block.

[block removeFromSuperview];

You must not do this. Your presentation will be deleted, as Tap Gesture will work.

0
source

All Articles