Ios UIView上的深度页面转换,Swift
如何像这样创建深度页面变换动画(从上到下,从下到上)。我在谷歌上搜索过,但我不知道如何在iOS上实现 如下所示的示例输出: 我遵循了教程,得到了以下效果: 如果您不想阅读整个教程,以下是您必须做的: 1.创建Ios UIView上的深度页面转换,Swift,ios,objective-c,swift,Ios,Objective C,Swift,如何像这样创建深度页面变换动画(从上到下,从下到上)。我在谷歌上搜索过,但我不知道如何在iOS上实现 如下所示的示例输出: 我遵循了教程,得到了以下效果: 如果您不想阅读整个教程,以下是您必须做的: 1.创建TrasitionManager类 这是设置视图应如何变换的位置 let offScreenRight = CGAffineTransformConcat(CGAffineTransformMakeTranslation(container!.frame.width, 0), CGAffi
TrasitionManager
类
这是设置视图应如何变换的位置
let offScreenRight = CGAffineTransformConcat(CGAffineTransformMakeTranslation(container!.frame.width, 0), CGAffineTransformMakeScale(0.1, 0.1))
let offScreenLeft = CGAffineTransformMakeTranslation(-container!.frame.width, 0)
在这种情况下,一个视图将平移+缩放,而另一个视图仅平移
2.在ViewController
中实例化TransitionManager
3.设置转换委托
如果希望视图来自右侧,leftSide=false
transitionManager.leftSide = false
就这样
编辑:
为了达到这一效果:
以下是该项目的git:
使用对齐约束调整尺寸:
PanGestureRecognizer
下面是制作新swift文件并复制此代码的解决方案
import UIKit
class UltravisualLayout: UICollectionViewLayout {
private var contentWidth:CGFloat!
private var contentHeight:CGFloat!
private var yOffset:CGFloat = 0
var maxAlpha:CGFloat = 1
var minAlpha:CGFloat = 0
//DEFAULT VALUES
//var widthOffset:CGFloat = 35
// var heightOffset:CGFloat = 35
var widthOffset:CGFloat = 100
var heightOffset:CGFloat = 100
private var cache = [UICollectionViewLayoutAttributes]()
private var itemWidth:CGFloat{
return (collectionView?.bounds.width)!
}
private var itemHeight:CGFloat{
return (collectionView?.bounds.height)!
}
private var collectionViewHeight:CGFloat{
return (collectionView?.bounds.height)!
}
private var numberOfItems:Int{
return (collectionView?.numberOfItemsInSection(0))!
}
private var dragOffset:CGFloat{
return (collectionView?.bounds.height)!
}
private var currentItemIndex:Int{
return max(0, Int(collectionView!.contentOffset.y / collectionViewHeight))
}
var nextItemBecomeCurrentPercentage:CGFloat{
return (collectionView!.contentOffset.y / (collectionViewHeight)) - CGFloat(currentItemIndex)
}
override func prepareLayout() {
cache.removeAll(keepCapacity: false)
yOffset = 0
for item in 0 ..< numberOfItems{
let indexPath = NSIndexPath(forItem: item, inSection: 0)
let attribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attribute.zIndex = -indexPath.row
if (indexPath.item == currentItemIndex+1) && (indexPath.item < numberOfItems){
attribute.alpha = minAlpha + max((maxAlpha-minAlpha) * nextItemBecomeCurrentPercentage, 0)
let width = itemWidth - widthOffset + (widthOffset * nextItemBecomeCurrentPercentage)
let height = itemHeight - heightOffset + (heightOffset * nextItemBecomeCurrentPercentage)
let deltaWidth = width/itemWidth
let deltaHeight = height/itemHeight
attribute.frame = CGRectMake(0, yOffset, itemWidth, itemHeight)
attribute.transform = CGAffineTransformMakeScale(deltaWidth, deltaHeight)
attribute.center.y = (collectionView?.center.y)! + (collectionView?.contentOffset.y)!
attribute.center.x = (collectionView?.center.x)! + (collectionView?.contentOffset.x)!
yOffset += collectionViewHeight
}else{
attribute.frame = CGRectMake(0, yOffset, itemWidth, itemHeight)
attribute.center.y = (collectionView?.center.y)! + yOffset
attribute.center.x = (collectionView?.center.x)!
yOffset += collectionViewHeight
}
cache.append(attribute)
}
}
//Return the size of ContentView
override func collectionViewContentSize() -> CGSize {
contentWidth = (collectionView?.bounds.width)!
contentHeight = CGFloat(numberOfItems) * (collectionView?.bounds.height)!
return CGSizeMake(contentWidth, contentHeight)
}
//Return Attributes whose frame lies in the Visible Rect
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attribute in cache{
if CGRectIntersectsRect(attribute.frame, rect){
layoutAttributes.append(attribute)
}
}
return layoutAttributes
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
let itemIndex = round(proposedContentOffset.y / (dragOffset))
let yOffset = itemIndex * (collectionView?.bounds.height)!
return CGPoint(x: 0, y: yOffset)
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
// Logic that calculates the UICollectionViewLayoutAttributes of the item
// and returns the UICollectionViewLayoutAttributes
return UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
}
}
导入UIKit
类UltravisualLayout:UICollectionViewLayout{
私有变量contentWidth:CGFloat!
私有变量contentHeight:CGFloat!
私有变量yOffset:CGFloat=0
var maxapha:CGFloat=1
var minAlpha:CGFloat=0
//默认值
//变量宽度偏移量:CGFloat=35
//变量高度偏移:CGFloat=35
变量宽度偏移量:CGFloat=100
变量高度偏移:CGFloat=100
私有变量缓存=[UICollectionViewLayoutAttribute]()
私有var itemWidth:CGFloat{
返回(collectionView?.bounds.width)!
}
私有变量itemHeight:CGFloat{
返回(collectionView?.bounds.height)!
}
私有变量集合视图高度:CGFloat{
返回(collectionView?.bounds.height)!
}
私有变量numberOfItems:Int{
返回(collectionView?.numberOfItemsInSection(0))!
}
私有变量dragOffset:CGFloat{
返回(collectionView?.bounds.height)!
}
私有变量currentItemIndex:Int{
返回最大值(0,Int(collectionView!.contentOffset.y/collectionViewHeight))
}
var nextItemBecomeCurrentPercentage:CGFloat{
返回(collectionView!.contentOffset.y/(collectionViewHeight))-CGFloat(currentItemIndex)
}
重写函数prepareLayout(){
cache.removeAll(keepCapacity:false)
yOffset=0
对于0..CGSize{
contentWidth=(collectionView?.bounds.width)!
contentHeight=CGFloat(numberOfItems)*(collectionView?.bounds.height)!
返回CGSizeMake(contentWidth、contentHeight)
}
//返回帧位于可见矩形中的属性
重写func LayoutAttribute ForelementsInRect(rect:CGRect)->[UICollectionViewLayoutAttribute]{
变量LayoutAttribute=[UICollectionViewLayoutAttribute]()
对于缓存中的属性{
如果CGRectIntersectsRect(attribute.frame,rect){
layoutAttributes.append(属性)
}
}
返回布局属性
}
覆盖函数应为边界设置(新边界:CGRect)->Bool验证布局{
返回真值
}
覆盖func targetContentOffsetForProposedContentOffset(proposedContentOffset:CGPoint,使用CrollingVelocity:CGPoint)->CGPoint{
设itemIndex=round(proposedContentOffset.y/(dragOffset))
让yOffset=itemIndex*(collectionView?.bounds.height)!
返回点(x:0,y:y偏移)
}
重写func LayoutAttribute ForItemAtIndexPath(indexPath:NSIndexPath)->UICollectionViewLayoutAttribute{
//计算项的UICollectionViewLayoutAttribute的逻辑
//并返回UICollectionViewLayoutAttribute
返回UICollectionViewLayoutAttribute(forCellWithIndexPath:indexPath)
}
}
在Main.storyboard文件中,使用UICollectionView或UICollectionViewController
组件选择UICollectionView转到属性检查
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
transitionManager.leftSide = false
let toViewController = segue.destinationViewController
toViewController.transitioningDelegate = self.transitionManager
}
transitionManager.leftSide = false
import UIKit
class UltravisualLayout: UICollectionViewLayout {
private var contentWidth:CGFloat!
private var contentHeight:CGFloat!
private var yOffset:CGFloat = 0
var maxAlpha:CGFloat = 1
var minAlpha:CGFloat = 0
//DEFAULT VALUES
//var widthOffset:CGFloat = 35
// var heightOffset:CGFloat = 35
var widthOffset:CGFloat = 100
var heightOffset:CGFloat = 100
private var cache = [UICollectionViewLayoutAttributes]()
private var itemWidth:CGFloat{
return (collectionView?.bounds.width)!
}
private var itemHeight:CGFloat{
return (collectionView?.bounds.height)!
}
private var collectionViewHeight:CGFloat{
return (collectionView?.bounds.height)!
}
private var numberOfItems:Int{
return (collectionView?.numberOfItemsInSection(0))!
}
private var dragOffset:CGFloat{
return (collectionView?.bounds.height)!
}
private var currentItemIndex:Int{
return max(0, Int(collectionView!.contentOffset.y / collectionViewHeight))
}
var nextItemBecomeCurrentPercentage:CGFloat{
return (collectionView!.contentOffset.y / (collectionViewHeight)) - CGFloat(currentItemIndex)
}
override func prepareLayout() {
cache.removeAll(keepCapacity: false)
yOffset = 0
for item in 0 ..< numberOfItems{
let indexPath = NSIndexPath(forItem: item, inSection: 0)
let attribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attribute.zIndex = -indexPath.row
if (indexPath.item == currentItemIndex+1) && (indexPath.item < numberOfItems){
attribute.alpha = minAlpha + max((maxAlpha-minAlpha) * nextItemBecomeCurrentPercentage, 0)
let width = itemWidth - widthOffset + (widthOffset * nextItemBecomeCurrentPercentage)
let height = itemHeight - heightOffset + (heightOffset * nextItemBecomeCurrentPercentage)
let deltaWidth = width/itemWidth
let deltaHeight = height/itemHeight
attribute.frame = CGRectMake(0, yOffset, itemWidth, itemHeight)
attribute.transform = CGAffineTransformMakeScale(deltaWidth, deltaHeight)
attribute.center.y = (collectionView?.center.y)! + (collectionView?.contentOffset.y)!
attribute.center.x = (collectionView?.center.x)! + (collectionView?.contentOffset.x)!
yOffset += collectionViewHeight
}else{
attribute.frame = CGRectMake(0, yOffset, itemWidth, itemHeight)
attribute.center.y = (collectionView?.center.y)! + yOffset
attribute.center.x = (collectionView?.center.x)!
yOffset += collectionViewHeight
}
cache.append(attribute)
}
}
//Return the size of ContentView
override func collectionViewContentSize() -> CGSize {
contentWidth = (collectionView?.bounds.width)!
contentHeight = CGFloat(numberOfItems) * (collectionView?.bounds.height)!
return CGSizeMake(contentWidth, contentHeight)
}
//Return Attributes whose frame lies in the Visible Rect
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attribute in cache{
if CGRectIntersectsRect(attribute.frame, rect){
layoutAttributes.append(attribute)
}
}
return layoutAttributes
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
let itemIndex = round(proposedContentOffset.y / (dragOffset))
let yOffset = itemIndex * (collectionView?.bounds.height)!
return CGPoint(x: 0, y: yOffset)
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
// Logic that calculates the UICollectionViewLayoutAttributes of the item
// and returns the UICollectionViewLayoutAttributes
return UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
}
}