编辑:这个SO答案似乎解释了我遇到下述问题的原因:UIViewController returns invalid frame?
我遇到了自定义动画的问题。当点击一个单元格时,我正在对一个viewController进行模态演示。但是当动画开始时,它会将我的单元格旋转90度(如下面的屏幕截图所示)。我最初虽然它会与方向有关,但我会拍摄细胞的快照,所以它不应该真正影响它。?
1 http://foffer.dk/rotation90.png
以下是代码:
启动viewController并呈现它(PhotosCollectionViewCont.m):
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//Fetch data to display
NSArray *photosArray = [self.dishes valueForKeyPath:@"dishes.content.image"];
NSArray *nameArray = [self.dishes valueForKeyPath:@"dishes.content.name"];
FOFDishDetailViewController *toVC = [[FOFDishDetailViewController alloc] init];
toVC.view.backgroundColor = [UIColor lightGrayColor];
[toVC.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://xx.yy.zz.qq:4000%@",[photosArray objectAtIndex: indexPath.row]]]];
toVC.nameLabel.text = [NSString stringWithFormat:@"%@", [nameArray objectAtIndex:indexPath.row]];
toVC.transitioningDelegate = self;
toVC.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:toVC animated:YES completion:nil];
[self collectionView:collectionView didDeselectItemAtIndexPath:indexPath];
}
动画,在同一个PhotosCollectionViewCont.m中调用:
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
NSLog(@"context class is %@", [transitionContext class]);
NSIndexPath *selected = self.collectionView.indexPathsForSelectedItems[0];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:selected];
UIView *container = transitionContext.containerView;
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *toView = toVC.view;
UIView *fromView = fromVC.view;
NSTimeInterval duration = [self transitionDuration:transitionContext];
CGRect beginFrame = [container convertRect:cell.bounds fromView:cell];
CGRect endFrame = [transitionContext initialFrameForViewController:fromVC];
//Remove comment for non-fullscreen
//endFrame = CGRectInset(endFrame, 40.0, 40.0);
fromVC.view.alpha = 1.0;
// toVC.view.alpha = 0.3;
CGRect mainScreen = [[UIScreen mainScreen] bounds];
UIView *intermediateView = [cell snapshotViewAfterScreenUpdates:NO];
intermediateView.frame = cell.frame;
[container addSubview:intermediateView];
// [container addSubview:move];
[UIView animateKeyframesWithDuration:duration
delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeCubic
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0
relativeDuration:0.5 animations:^{
intermediateView.frame = endFrame;
}];
[UIView addKeyframeWithRelativeStartTime:0.5
relativeDuration:0.5 animations:^{
fromVC.view.alpha = 0.0;
toVC.view.alpha = 1.0;
}];
} completion:^(BOOL finished) {
toVC.view.frame = mainScreen;
[container addSubview:toView];
[intermediateView removeFromSuperview];
fromVC.view.alpha = 1.0;
[transitionContext completeTransition:YES];
}];
}
我知道动画本身非常混乱,但此刻,我唯一关心的就是摆脱轮换问题。
如果您需要更多代码来帮助我解决此问题,请告知我们,我会提供。
答案 0 :(得分:0)
这类似于在横向中执行UIViewControllerTransitioning时遇到的问题。
无论出于何种原因,肖像中的坐标系统都与此过渡有关。当你在这里获得这个细胞时,我猜想会这样:
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:selected];
并在此处快照:
UIView *intermediateView = [cell snapshotViewAfterScreenUpdates:NO];
细胞是纵向的,而你显然是在风景中观察它。解决方案可能非常复杂,但我发现解决此问题的最简单方法是为UIViewController创建一个带有集合视图的协议。
在此协议中声明一个名为selectedCellSnapshotView
的只读属性。
/** A snapshot of the selected collection view cell. */
@property (nonatomic, strong, readonly) UIView *selectedCellSnapshotView;
显然,使集合视图采用此协议,并在getter中执行以下操作:
/**
* A snapshot of the selected collection view cell.
*
* @return A snapshot of the selected collection view cell.
*/
- (UIView)selectedCellSnapshotView
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.selectedIndexPath];
return [cell snapshotViewAfterScreenUpdates:NO];
}
然后将过渡代表移动到它自己的班级。
虽然这听起来很不合逻辑,但它对我有用,所以我希望它适合你。
判断整个答案的一个简单方法是,如果在纵向过渡时它的行为不同。你只展示了Landscape的截图,所以我不知道。
我真的希望这会有所帮助,如果没有,我道歉。