简体   繁体   中英

UIButton not receiving touch after superview rotate

I am working with a UICollectionView, and I have defined a custom UICollectionViewCell by subclassing that class. The initializer of the class defines two views, just like this:

    [[NSBundle mainBundle] loadNibNamed:@"ProtelViewCellFlipped" owner:self options:nil];

    CATransform3D transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
    [flippedView.layer setTransform:transform];
    [flippedView setHidden:YES];
    [self addSubview:flippedView];

    [[NSBundle mainBundle] loadNibNamed:@"ProtelViewCell" owner:self options:nil];
    [self addSubview:selfView];
    [selfView.layer setZPosition:1];

where flippedView is a view defined by the "ProtelViewCellFlipped" xib file, and selfView is the view defined in the "ProtelViewCell" xib file. The view hierarchy is like this: the view hierarchy http://massimomarra.altervista.org/gerarchia.png

The thing is that the flipped view is rotated (as if it was the other side of the "selfView" view) by a CATransform3D , and then it is hidden. And that's all right, it works.

The selfView view has a UIButton on it. In the bottom right corner of the view. If I press that button, this method gets triggered:

- (void)infoButtonPressed:(UIButton *)sender
{
    NSString *animationKey = (selfView.layer.zPosition > flippedView.layer.zPosition)? @"forward" : @"backwards";
    CGFloat animationDuration = 0.3;

    if (selfView.layer.zPosition > flippedView.layer.zPosition)
        [self.layer removeAllAnimations];

    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    [rotation setValue:animationKey forKey:@"id"];
    rotation.toValue = [NSNumber numberWithFloat:M_PI];
    rotation.duration = animationDuration;
    rotation.removedOnCompletion = NO;
    rotation.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:rotation forKey:animationKey];

    [self performSelector:@selector(flipLayers) withObject:nil afterDelay:animationDuration/2];
}

So that the self view is rotated with an animation. Also, during the animation gets called this method:

- (void)flipLayers
{
    [selfView setHidden:!selfView.hidden];
    [flippedView setHidden:!selfView.hidden];

    [selfView.layer setZPosition:flippedView.layer.zPosition];
    [flippedView.layer setZPosition:(1-selfView.layer.zPosition)];
}

so that the flippedView becomes visible (and selfView hides), and the zPosition of his layer becomes higher than selfView .

So now I have the flippedView visible, and it is displayed correctly. It has also a button, but to trigger his action I have to tap in another position: flipped view scheme: i have to tap where the button "was" before the rotation http://massimomarra.altervista.org/s03KRJAUk7C_nP3yTXb7TBQ.png

It seems like the rotation just modifies the graphics of the view, but not the content. Can you please help me?

Thanks to you all in advance!!

transform the button back to identity before change orientation

something like this:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{

    oneButton.transform=CGAffineTransformIdentity;
}

I don't know why but it seems working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM