Ios 是否可以在Swift中创建带有动画页面控件的滚动视图?
设计师希望通过滑动手势获得以下动画 正如可以看到的,用户可以刷卡,看看每张卡都有什么。同时,用户可以在屏幕右侧看到下面的卡和左侧的最后一张卡。此外,当用户移动滚动条时,卡片会改变其大小 我已经使用过页面控件视图,但我不知道是否可以使用页面控件(这实际上是本文的问题) 此外,我已经尝试使用collectionView,但当我滑动(实际上是水平滚动)时,滚动有一个令人不舒服的惯性,而且,我不知道如何制作动画 在一个滚动页面控件实现,但现在我只是想知道,如果像gif提供的动画和动画是可能的 如果答案是肯定的,如果你能给我一些建议,我会非常感激Ios 是否可以在Swift中创建带有动画页面控件的滚动视图?,ios,swift,Ios,Swift,设计师希望通过滑动手势获得以下动画 正如可以看到的,用户可以刷卡,看看每张卡都有什么。同时,用户可以在屏幕右侧看到下面的卡和左侧的最后一张卡。此外,当用户移动滚动条时,卡片会改变其大小 我已经使用过页面控件视图,但我不知道是否可以使用页面控件(这实际上是本文的问题) 此外,我已经尝试使用collectionView,但当我滑动(实际上是水平滚动)时,滚动有一个令人不舒服的惯性,而且,我不知道如何制作动画 在一个滚动页面控件实现,但现在我只是想知道,如果像gif提供的动画和动画是可能的 如果答案
提前感谢。根据Denislava Shentova的评论,我找到了一个很好的库来解决这个问题 对于未来的所有人和他们的工作时间,我只是从中提取代码并删除一些我不需要的代码 以下是显示以下结果的简单viewController的代码: 请注意,此代码中还有一些其他类别,但暂时可以在ViewController.swift文件中运行整个代码。测试后,请将它们拆分为不同的文件 为了运行此代码,您需要以下模块。制作一个名为UPCarouselFlowLayout.swift的文件并粘贴所有代码:
import UIKit
public enum UPCarouselFlowLayoutSpacingMode {
case fixed(spacing: CGFloat)
case overlap(visibleOffset: CGFloat)
}
open class UPCarouselFlowLayout: UICollectionViewFlowLayout {
fileprivate struct LayoutState {
var size: CGSize
var direction: UICollectionViewScrollDirection
func isEqual(_ otherState: LayoutState) -> Bool {
return self.size.equalTo(otherState.size) && self.direction == otherState.direction
}
}
@IBInspectable open var sideItemScale: CGFloat = 0.6
@IBInspectable open var sideItemAlpha: CGFloat = 0.6
open var spacingMode = UPCarouselFlowLayoutSpacingMode.fixed(spacing: 40)
fileprivate var state = LayoutState(size: CGSize.zero, direction: .horizontal)
override open func prepare() {
super.prepare()
let currentState = LayoutState(size: self.collectionView!.bounds.size, direction: self.scrollDirection)
if !self.state.isEqual(currentState) {
self.setupCollectionView()
self.updateLayout()
self.state = currentState
}
}
fileprivate func setupCollectionView() {
guard let collectionView = self.collectionView else { return }
if collectionView.decelerationRate != UIScrollViewDecelerationRateFast {
collectionView.decelerationRate = UIScrollViewDecelerationRateFast
}
}
fileprivate func updateLayout() {
guard let collectionView = self.collectionView else { return }
let collectionSize = collectionView.bounds.size
let isHorizontal = (self.scrollDirection == .horizontal)
let yInset = (collectionSize.height - self.itemSize.height) / 2
let xInset = (collectionSize.width - self.itemSize.width) / 2
self.sectionInset = UIEdgeInsetsMake(yInset, xInset, yInset, xInset)
let side = isHorizontal ? self.itemSize.width : self.itemSize.height
let scaledItemOffset = (side - side*self.sideItemScale) / 2
switch self.spacingMode {
case .fixed(let spacing):
self.minimumLineSpacing = spacing - scaledItemOffset
case .overlap(let visibleOffset):
let fullSizeSideItemOverlap = visibleOffset + scaledItemOffset
let inset = isHorizontal ? xInset : yInset
self.minimumLineSpacing = inset - fullSizeSideItemOverlap
}
}
override open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let superAttributes = super.layoutAttributesForElements(in: rect),
let attributes = NSArray(array: superAttributes, copyItems: true) as? [UICollectionViewLayoutAttributes]
else { return nil }
return attributes.map({ self.transformLayoutAttributes($0) })
}
fileprivate func transformLayoutAttributes(_ attributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
guard let collectionView = self.collectionView else { return attributes }
let isHorizontal = (self.scrollDirection == .horizontal)
let collectionCenter = isHorizontal ? collectionView.frame.size.width/2 : collectionView.frame.size.height/2
let offset = isHorizontal ? collectionView.contentOffset.x : collectionView.contentOffset.y
let normalizedCenter = (isHorizontal ? attributes.center.x : attributes.center.y) - offset
let maxDistance = (isHorizontal ? self.itemSize.width : self.itemSize.height) + self.minimumLineSpacing
let distance = min(abs(collectionCenter - normalizedCenter), maxDistance)
let ratio = (maxDistance - distance)/maxDistance
let alpha = ratio * (1 - self.sideItemAlpha) + self.sideItemAlpha
let scale = ratio * (1 - self.sideItemScale) + self.sideItemScale
attributes.alpha = alpha
attributes.transform3D = CATransform3DScale(CATransform3DIdentity, scale, scale, 1)
attributes.zIndex = Int(alpha * 10)
return attributes
}
override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView , !collectionView.isPagingEnabled,
let layoutAttributes = self.layoutAttributesForElements(in: collectionView.bounds)
else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset) }
let isHorizontal = (self.scrollDirection == .horizontal)
let midSide = (isHorizontal ? collectionView.bounds.size.width : collectionView.bounds.size.height) / 2
let proposedContentOffsetCenterOrigin = (isHorizontal ? proposedContentOffset.x : proposedContentOffset.y) + midSide
var targetContentOffset: CGPoint
if isHorizontal {
let closest = layoutAttributes.sorted { abs($0.center.x - proposedContentOffsetCenterOrigin) < abs($1.center.x - proposedContentOffsetCenterOrigin) }.first ?? UICollectionViewLayoutAttributes()
targetContentOffset = CGPoint(x: floor(closest.center.x - midSide), y: proposedContentOffset.y)
}
else {
let closest = layoutAttributes.sorted { abs($0.center.y - proposedContentOffsetCenterOrigin) < abs($1.center.y - proposedContentOffsetCenterOrigin) }.first ?? UICollectionViewLayoutAttributes()
targetContentOffset = CGPoint(x: proposedContentOffset.x, y: floor(closest.center.y - midSide))
}
return targetContentOffset
}
}
导入UIKit
公共枚举UpcarouselFlowLayoutPacingMode{
固定大小写(间距:CGFloat)
案例重叠(visibleOffset:CGFloat)
}
开放类UPCarouselFlowLayout:UICollectionViewFlowLayout{
fileprivate结构布局状态{
变量大小:CGSize
变量方向:UICollectionViewScrollDirection
func isEqual(uOtherState:LayoutState)->Bool{
返回self.size.equalTo(otherState.size)和&self.direction==otherState.direction
}
}
@IBInspectable open var sideItemScale:CGFloat=0.6
@IBInspectable open var sideItemAlpha:CGFloat=0.6
open var spacingMode=UPCAROUSELFLOWLAYOUTPACINGMODE.固定(间距:40)
fileprivate var state=LayoutState(大小:CGSize.zero,方向:。水平)
覆盖打开的func prepare(){
超级准备
让currentState=LayoutState(大小:self.collectionView!.bounds.size,方向:self.scrollDirection)
if!self.state.isEqual(当前状态){
self.setupCollectionView()
self.updateLayout()
self.state=当前状态
}
}
fileprivate func setupCollectionView(){
guard let collectionView=self.collectionView else{return}
如果collectionView.decelerationRate!=UIScrollViewDecelerationRateFast{
collectionView.decelerationRate=UIScrollViewDecelerationRateFast
}
}
fileprivate func updateLayout(){
guard let collectionView=self.collectionView else{return}
让collectionSize=collectionView.bounds.size
设isHorizontal=(self.scrollDirection==.horizontal)
让yInset=(collectionSize.height-self.itemSize.height)/2
让xInset=(collectionSize.width-self.itemSize.width)/2
self.sectionInset=UIEdgeInsetsMake(yInset,xInset,yInset,xInset)
let side=isHorizontal?self.itemSize.width:self.itemSize.height
设scaledItemOffset=(side-side*self.sideItemScale)/2
开关自间距模式{
案例。固定(让间距):
self.minimumLineSpacing=间距-scaledItemOffset
案例重叠(让visibleOffset):
设fullSizeSideItemOverlap=visibleOffset+ScaleEdItemOffset
让插图=水平?xInset:yInset
self.minimumlinespating=插入-全尺寸尺寸最小重叠
}
}
覆盖打开的函数应验证布局(forBoundsChange newBounds:CGRect)->Bool{
返回真值
}
是否覆盖打开的func layoutAttributesForElements(在rect:CGRect中)->[UICollectionViewLayoutAttributes]{
guard let superAttributes=super.layoutAttributesForElements(in:rect),
将attributes=NSArray(数组:SuperAttribute,copyItems:true)设为?[UICollectionViewLayoutAttribute]
else{return nil}
返回attributes.map({self.transformLayoutAttribute($0)})
}
fileprivate func TransformLayoutAttribute(u属性:UICollectionViewLayoutAttribute)->UICollectionViewLayoutAttribute{
guard let collectionView=self.collectionView else{return attributes}
设isHorizontal=(self.scrollDirection==.horizontal)
让collectionCenter=isHorizontal?collectionView.frame.size.width/2:collectionView.frame.size.height/2
让offset=isHorizontal?collectionView.contentOffset.x:collectionView.contentOffset.y
设normalizedCenter=(isHorizontal?attributes.center.x:attributes.center.y)-偏移量
设maxDistance=(isHorizontal?self.itemSize.width:self.itemSize.height)+self.minimumlinespace
让距离=最小值(abs(采集中心-标准化中心),最大距离)
let比率=(最大距离-距离)/最大距离
设alpha=比率*(1-self.sideItemAlpha)+self.sideItemAlpha
设比例=比率*(1-self.sideItemScale)+self.sideItemScale
attributes.alpha=alpha
attributes.transform3D=CATTransferrM3DScale(CATTransferrM3DidEntity,scale,scale,1)
attributes.zIndex=Int(alpha*10)
返回属性
}
覆盖开放函数targetContentOffset(forProposedContentOffset proposedContentOffset:CGPoint,使用CrollingVelocity:CGPoint)->CGPoint{
guard let collectionView=collectionView,!collection
import UIKit
public enum UPCarouselFlowLayoutSpacingMode {
case fixed(spacing: CGFloat)
case overlap(visibleOffset: CGFloat)
}
open class UPCarouselFlowLayout: UICollectionViewFlowLayout {
fileprivate struct LayoutState {
var size: CGSize
var direction: UICollectionViewScrollDirection
func isEqual(_ otherState: LayoutState) -> Bool {
return self.size.equalTo(otherState.size) && self.direction == otherState.direction
}
}
@IBInspectable open var sideItemScale: CGFloat = 0.6
@IBInspectable open var sideItemAlpha: CGFloat = 0.6
open var spacingMode = UPCarouselFlowLayoutSpacingMode.fixed(spacing: 40)
fileprivate var state = LayoutState(size: CGSize.zero, direction: .horizontal)
override open func prepare() {
super.prepare()
let currentState = LayoutState(size: self.collectionView!.bounds.size, direction: self.scrollDirection)
if !self.state.isEqual(currentState) {
self.setupCollectionView()
self.updateLayout()
self.state = currentState
}
}
fileprivate func setupCollectionView() {
guard let collectionView = self.collectionView else { return }
if collectionView.decelerationRate != UIScrollViewDecelerationRateFast {
collectionView.decelerationRate = UIScrollViewDecelerationRateFast
}
}
fileprivate func updateLayout() {
guard let collectionView = self.collectionView else { return }
let collectionSize = collectionView.bounds.size
let isHorizontal = (self.scrollDirection == .horizontal)
let yInset = (collectionSize.height - self.itemSize.height) / 2
let xInset = (collectionSize.width - self.itemSize.width) / 2
self.sectionInset = UIEdgeInsetsMake(yInset, xInset, yInset, xInset)
let side = isHorizontal ? self.itemSize.width : self.itemSize.height
let scaledItemOffset = (side - side*self.sideItemScale) / 2
switch self.spacingMode {
case .fixed(let spacing):
self.minimumLineSpacing = spacing - scaledItemOffset
case .overlap(let visibleOffset):
let fullSizeSideItemOverlap = visibleOffset + scaledItemOffset
let inset = isHorizontal ? xInset : yInset
self.minimumLineSpacing = inset - fullSizeSideItemOverlap
}
}
override open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let superAttributes = super.layoutAttributesForElements(in: rect),
let attributes = NSArray(array: superAttributes, copyItems: true) as? [UICollectionViewLayoutAttributes]
else { return nil }
return attributes.map({ self.transformLayoutAttributes($0) })
}
fileprivate func transformLayoutAttributes(_ attributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
guard let collectionView = self.collectionView else { return attributes }
let isHorizontal = (self.scrollDirection == .horizontal)
let collectionCenter = isHorizontal ? collectionView.frame.size.width/2 : collectionView.frame.size.height/2
let offset = isHorizontal ? collectionView.contentOffset.x : collectionView.contentOffset.y
let normalizedCenter = (isHorizontal ? attributes.center.x : attributes.center.y) - offset
let maxDistance = (isHorizontal ? self.itemSize.width : self.itemSize.height) + self.minimumLineSpacing
let distance = min(abs(collectionCenter - normalizedCenter), maxDistance)
let ratio = (maxDistance - distance)/maxDistance
let alpha = ratio * (1 - self.sideItemAlpha) + self.sideItemAlpha
let scale = ratio * (1 - self.sideItemScale) + self.sideItemScale
attributes.alpha = alpha
attributes.transform3D = CATransform3DScale(CATransform3DIdentity, scale, scale, 1)
attributes.zIndex = Int(alpha * 10)
return attributes
}
override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView , !collectionView.isPagingEnabled,
let layoutAttributes = self.layoutAttributesForElements(in: collectionView.bounds)
else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset) }
let isHorizontal = (self.scrollDirection == .horizontal)
let midSide = (isHorizontal ? collectionView.bounds.size.width : collectionView.bounds.size.height) / 2
let proposedContentOffsetCenterOrigin = (isHorizontal ? proposedContentOffset.x : proposedContentOffset.y) + midSide
var targetContentOffset: CGPoint
if isHorizontal {
let closest = layoutAttributes.sorted { abs($0.center.x - proposedContentOffsetCenterOrigin) < abs($1.center.x - proposedContentOffsetCenterOrigin) }.first ?? UICollectionViewLayoutAttributes()
targetContentOffset = CGPoint(x: floor(closest.center.x - midSide), y: proposedContentOffset.y)
}
else {
let closest = layoutAttributes.sorted { abs($0.center.y - proposedContentOffsetCenterOrigin) < abs($1.center.y - proposedContentOffsetCenterOrigin) }.first ?? UICollectionViewLayoutAttributes()
targetContentOffset = CGPoint(x: proposedContentOffset.x, y: floor(closest.center.y - midSide))
}
return targetContentOffset
}
}