【发布时间】:2014-04-13 17:10:06
【问题描述】:
编辑:这个 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];
}];
}
我知道动画本身真的很乱,但目前我唯一关心的是摆脱旋转问题。
如果您需要更多代码来帮助我解决此问题,请告诉我,我会提供。
【问题讨论】:
标签: ios objective-c animation uicollectionview