我正在使用 UICollectionView,并且我通过子类化该类定义了自定义 UICollectionViewCell。该类的初始值设定项定义了两个 View ,如下所示:
[[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];
其中,flippedView 是“ProtelViewCellFlipped”xib 文件定义的 View ,selfView 是“ProtelViewCell”xib 文件中定义的 View 。 View 层次结构如下:the view hierarchy http://massimomarra.altervista.org/gerarchia.png
事情是,翻转的 View 通过 CATransform3D 旋转(就好像它是“selfView” View 的另一侧),然后隐藏。没关系,它有效。
selfView
View 上有一个 UIButton。在 View 的右下角。如果我按下该按钮,则会触发此方法:
- (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];
}
以便 self
View 随动画旋转。另外,在动画期间调用此方法:
- (void)flipLayers
{
[selfView setHidden:!selfView.hidden];
[flippedView setHidden:!selfView.hidden];
[selfView.layer setZPosition:flippedView.layer.zPosition];
[flippedView.layer setZPosition:(1-selfView.layer.zPosition)];
}
这样flippedView
变得可见(并且selfView
隐藏),并且他的图层的zPosition变得高于selfView
。
现在我看到了 flippedView
,并且它显示正确。它还有一个按钮,但要触发他的操作,我必须点击另一个位置:flipped view scheme: i have to tap where the button "was" before the rotation http://massimomarra.altervista.org/s03KRJAUk7C_nP3yTXb7TBQ.png
看起来旋转只是修改了 View 的图形,但没有修改内容。 你能帮我吗?
提前感谢大家!!
最佳答案
在改变方向之前将按钮转换回身份
像这样:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
oneButton.transform=CGAffineTransformIdentity;
}
我不知道为什么,但它似乎有效
关于objective-c - super View 旋转后 UIButton 未接收到触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13028734/