1
votes

I'm animating several UIIimageView's that have a tap gesture recognizer attached to them. They respond when not animating, but when I do animate, there is no response (tried this with UIButton's as well). The weird thing is that I have the AllowUserInteraction and UIViewAnimationOptionAllowAnimatedContent options included in the animation and it still doesn't work. I also enable userInteraction on the iViews and double check to see the boolean value of the viewcontroller view and the imageview's view userInteractionEnabled to make sure it is on (it is and prints out 1,1) Any ideas?

This method creates the image view's:

-(void)addFloatingButtonHeadToView:(NSString*)userID{

/* Initialize and set properties */
UIImageView *head = [[UIImageView alloc]init];
[head setBackgroundColor:[UIColor clearColor]];
[head.layer setBorderColor:[[UIColor whiteColor]CGColor]];
[head.layer setBorderWidth:3.0f];
[head setUserInteractionEnabled:YES];
head.tag = _floatingButtonHeads.count;

/* Create tap gesture and assign to imageview head */
UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headPressed:)];
[headTap setDelegate:self];
[head addGestureRecognizer:headTap];


/* Add to array and subview, and initialize animation */
[_floatingButtonHeads addObject:head];
[self.view addSubview:head];
[self startFloating:head];

}

This method initiates the animations:

-(void)startFloating:(UIImageView*)head{

/* Print values of userInteraction enabled to make sure they are ON */
NSLog(@"%hhd %hhd",self.view.userInteractionEnabled,head.userInteractionEnabled);

/* Create random values for x axis,y axis, and animation duration */
NSUInteger randomX = arc4random_uniform(320) + 60;
NSUInteger randomY = 60 + arc4random() % ((int)self.view.frame.size.height - 60);
NSUInteger randomDuration = arc4random_uniform(4) + 2;

/* Set image view's frame according to values*/
[head setFrame:CGRectMake(randomX, randomY, 60, 60)];

/* Start Animation*/
[UIView animateWithDuration:randomDuration delay:0.0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent animations:^{
    [head setFrame:CGRectMake(350, head.frame.origin.y, 60, 60)];
}completion:^(BOOL finished) {
    if(finished){
        [self startFloating:head];
    }
}];

}
2

2 Answers

2
votes

No view will respond to touch events during animation - this is an intended behaviour as in most cases this would result in rather random touch locations and make for a poor user experience.

If You really need such behaviour I would suggest making a list of views to test hits, and attaching an UITapGestureRecognizer to their parrent view. Then You can use the

[view.layer.presentationLayer frame] 

property to get their frame during animation and test the touches for hits.

1
votes

The animation "view" you touch is the CALayer which can not receive touch and have no user interaction attribute, it just store image and make animation.

You can handle the touch in the background view which maybe the imageview's superview or super view's superview.... and test the touch position whether in the view.layer.presentationLayer's frame as @Przemysław Wrzesiński says.