使用iOS 7 API进行UICollectionView交互式布局转换 [英] UICollectionView interactive layout transition using iOS 7 APIs

查看:115
本文介绍了使用iOS 7 API进行UICollectionView交互式布局转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理新的iOS 7 API,允许交互式动画视图控制器转换,包括 UICollectionViewLayout 之间的转换。



我已经从WWDC 2013iOS-CollectionViewTransition中获取并修改了示例代码,可在此处找到: https://github.com/timarnold/UICollectionView-Transition-Demo



原始演示,当我找到它时,它不能处于工作状态,可以通过Apple Developer帐户访问,在这里: https://developer.apple.com/downloads/index.action?name=WWDC%202013



我的版本该应用程序提供了一个包含两个布局的集合视图,包括 UICollectionViewFlowLayout 具有不同属性的布局。



点击一个单元格Ť他首先将正确的动画布局到第二个,包括,重要的是,在新布局中滚动到的项目。起初我对新集合视图如何知道设置其内容偏移量以使适当的单元格可见感到困惑,但我了解到它是基于选择的属性来实现的。呈现集合视图。



使用 UICollectionViewTransitionLayout ,<$ c对第一个布局中的项目进行动画处理应该是动画的$ c> UIViewControllerAnimatedTransitioning ,以及 UIViewControllerInteractiveTransitioning ,也适用于新布局。这样可行,但是在新布局或转换布局中,收缩的单元格滚动到。



我试过设置在不同位置的压缩单元格上选择属性(试图模仿点击某个项目以推动新视图控制器时所描述的行为),但无济于事。



关于如何解决这个问题的任何想法?

解决方案

你可以在转换期间自己操纵 contentOffset ,这实际上比 UICollectionView的内置动画更精细。 / p>

例如,您可以像这样定义过渡布局,以在到和从偏移之间进行插值。你只需要根据你想要的结果自己计算到偏移量:

  @interface MyTransitionLayout:UICollectionViewTransitionLayout 
@property(非原子)CGPoint fromContentOffset;
@property(非原子)CGPoint toContentOffset;
@end

#importMyTransitionLayout.h
@implementation MyTransitionLayout

- (void)setTransitionProgress:(CGFloat)transitionProgress
{
super.transitionProgress = transitionProgress;
CGFloat f = 1 - transitionProgress;
CGFloat t = transitionProgress;
CGPoint offset = CGPointMake(f * self.fromContentOffset.x + t * self.toContentOffset.x,f * self.fromContentOffset.y + t * self.toContentOffset.y);
self.collectionView.contentOffset = offset;
}

@end

有一点需要注意的是当转换完成时, contentOffset 将被重置为from值,但您可以通过将其设置回<完成块中的to偏移量来否定它。 code> startInteractiveTransitionToCollectionViewLayout

  CGPoint toContentOffset = ...; 
[self.collectionViewController.collectionView startInteractiveTransitionToCollectionViewLayout:layout completion:^(BOOL completed,BOOL finish){
if(finish){
self.collectionView.contentOffset = toContentOffset;
}
}];

更新



我在新的GitHub库 TLLayoutTransitioning 中发布了此实现和一个工作示例。该示例是非交互式的,旨在通过 setCollectionViewLayout:animated:completion 来演示改进的动画,但它利用了交互式转换API以及上述技术。请查看 TLTransitionLayout 并尝试运行调整大小示例在示例工作区中。



或许 TLTransitionLayout 可以完成您的需要。



更新2



我在TLLayoutTransitioning库中添加了一个交互式示例。尝试在Examples工作区中运行Pinch示例 。这个将可见细胞作为一组捏住。我正在研究另一个压缩单个单元格的示例,以便在转换过程中单元格跟随您的手指,而其他单元格遵循默认的线性路径。



更新3



我最近添加了更多内容偏移位置选项:最小,中心,上,左,下和右。并且 transitionToCollectionViewLayout:现在支持30多个宽松功能,由Warren Moore的 AHEasing提供图书馆


I'm trying to get a handle on new iOS 7 APIs that allow for interactive, animated view controller transitions, including transitions between UICollectionViewLayouts.

I've taken and modified sample code from WWDC 2013, "iOS-CollectionViewTransition", which can be found here: https://github.com/timarnold/UICollectionView-Transition-Demo

The original demo, which was not in a working state when I found it, can be accessed with an Apple Developer account, here: https://developer.apple.com/downloads/index.action?name=WWDC%202013

My version of the app presents a collection view with two layouts, both UICollectionViewFlowLayout layouts with different properties.

Tapping on a cell in the first layout properly animates to the second, including, crucially, the tapped-on-item being scrolled to in the new layout. At first I was confused about how the new collection view knows to set its content offset so that the appropriate cell is visible, but I learned it does this based on the selected property of the presenting collection view.

Pinching on an item in the first layout should animate, using UICollectionViewTransitionLayout, UIViewControllerAnimatedTransitioning, and UIViewControllerInteractiveTransitioning, to the new layout as well. This works, but the pinched-at cell is not scrolled to in the new layout or the transition layout.

I've tried setting the selected property on the pinched-on cell at various locations (to try to mimic the behavior described when tapping on an item to push the new view controller), to no avail.

Any ideas about how to solve this problem?

解决方案

You can manipulate the contentOffset yourself during the transition, which actually gives you finer-grained control than UICollectionView's built-in animation.

For example, you can define your transition layout like this to interpolate between the "to" and "from" offsets. You just need to calculate the "to" offset yourself based on how you want things to end up:

@interface MyTransitionLayout : UICollectionViewTransitionLayout
@property (nonatomic) CGPoint fromContentOffset;
@property (nonatomic) CGPoint toContentOffset;
@end

#import "MyTransitionLayout.h"
@implementation MyTransitionLayout

- (void) setTransitionProgress:(CGFloat)transitionProgress
{
    super.transitionProgress = transitionProgress;
    CGFloat f = 1 - transitionProgress;
    CGFloat t = transitionProgress;
    CGPoint offset = CGPointMake(f * self.fromContentOffset.x + t * self.toContentOffset.x, f * self.fromContentOffset.y + t * self.toContentOffset.y);
    self.collectionView.contentOffset = offset;
}

@end

One thing to note is that the contentOffset will be reset to the "from" value when the transition completes, but you can negate that by setting it back to the "to" offset in the completion block of startInteractiveTransitionToCollectionViewLayout

CGPoint toContentOffset = ...;
[self.collectionViewController.collectionView startInteractiveTransitionToCollectionViewLayout:layout completion:^(BOOL completed, BOOL finish) {
    if (finish) {
        self.collectionView.contentOffset = toContentOffset;
    }
}];

UPDATE

I posted an implementation of this and a working example in a new GitHub library TLLayoutTransitioning. The example is non-interactive, intended to demonstrate improved animation over setCollectionViewLayout:animated:completion, but it utilizes the interactive transitioning APIs combined with the technique described above. Take a look at the TLTransitionLayout class and try running the "Resize" example in the Examples workspace.

Perhaps TLTransitionLayout can accomplish what you need.

UPDATE 2

I added an interactive example to the TLLayoutTransitioning library. Try running the "Pinch" example in the Examples workspace. This one pinches the visible cells as a group. I'm working on another example that pinches an individual cell such that the cell follows your fingers during the transition while the other cells follow the default linear path.

UPDATE 3

I've recently added more content offset placement options: Minimal, Center, Top, Left, Bottom and Right. And transitionToCollectionViewLayout: now supports 30+ easing functions courtesy of Warren Moore's AHEasing library.

这篇关于使用iOS 7 API进行UICollectionView交互式布局转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆