缩放轮播 (ScalingCarousel)

CI Status Version License Platform Carthage compatible Swift Package Manager compatible

ScalingCarousel 提供了一个简单的轮播样式集合视图。它负责单元格的呈现,并在集合视图滚动时缩放每个单元格。

它被用于 Bikey 来展示自行车站点信息,如下所示;

Bikey ScalingCarousel example

用法

ScalingCarousel 可以通过 Storyboard/xib 和代码添加,如下所述。

Storyboard

cell.setNeedsLayout()
cell.layoutIfNeeded()
super.viewWillTransition(to: size, with: coordinator)
scalingCarousel.deviceRotated()

代码

override init(frame: CGRect) {
  super.init(frame: frame)

  // Initialize the mainView property and add it to the cell's contentView
  mainView = UIView(frame: contentView.bounds)
  contentView.addSubview(mainView)
  mainView.translatesAutoresizingMaskIntoConstraints = false
  NSLayoutConstraint.activate([
      mainView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
      mainView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
      mainView.topAnchor.constraint(equalTo: contentView.topAnchor),
      mainView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
  ])
}
// Create our carousel
let scalingCarousel = ScalingCarouselView(withFrame: frame, andInset: 20)
scalingCarousel.dataSource = self
scalingCarousel.delegate = self
scalingCarousel.translatesAutoresizingMaskIntoConstraints = false

// Register our custom cell for dequeueing
scalingCarousel.register(Cell.self, forCellWithReuseIdentifier: "cell")

// Add our carousel as a subview        
view.addSubview(scalingCarousel)

// Add Constraints
scalingCarousel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true
scalingCarousel.heightAnchor.constraint(equalToConstant: 300).isActive = true
scalingCarousel.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scalingCarousel.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
cell.setNeedsLayout()
cell.layoutIfNeeded()
super.viewWillTransition(to: size, with: coordinator)
if scalingCarousel != nil {
    scalingCarousel.deviceRotated()
}

示例

要运行示例项目,请克隆存储库,然后首先从 Example 目录运行 pod install

要求

iOS 10

安装

ScalingCarousel 可通过 CocoaPods, CarthageSwift Package Manager获得。

要通过 Cocoapods 安装,请将以下行添加到您的 Podfile

pod "ScalingCarousel"

要通过 Carthage 安装,请将以下行添加到您的 Podfile

github "superpeteblaze/ScalingCarousel"

要通过 Swift package manager 安装

注意:以下说明适用于在没有 Xcode UI 的情况下使用 SwiftPM。最简单的方法是转到您的 Project Settings -> Swift Packages 并从那里添加 ScalingCarousel。

要使用 Apple 的 Swift package manager 进行集成,无需 Xcode 集成,请将以下内容作为依赖项添加到您的 Package.swift

.package(url: "https://github.com/superpeteblaze/ScalingCarousel.git", .upToNextMajor(from: "3.2.0"))

作者

Pete Smith, peadar81@gmail.com

许可证

ScalingCarousel 在 MIT 许可证下可用。有关更多信息,请参见 LICENSE 文件。