-
-
Save unnamedd/3426d8fec4b409875818b0671444a4e5 to your computer and use it in GitHub Desktop.
Protocolo que permite que a UITableViewCell faça um efeito de parallax.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Protocolo responsável por fazer o efeito de parallax nas imagens que, principalmente, são usadas como background. | |
É necessário chamar a função `setupParallaxInitialValues` no `awakeFromNib` da `UITableViewCell` para configurar os valores iniciais das variáveis. | |
Além disso, para total funcionamento do efeito, é necessário implementar algumas chamadas na `UIViewController` ou `UITableViewController` que a `tableView` está instanciada. | |
Chamar a função `setParallaxCellImageOffsetWithTableView` no `willDisplayCell` da `UITableViewDelegate` e para todas as células visíveis no método `scrollViewDidScroll`. | |
Implementação: | |
```swift | |
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { | |
(cell as! CustomTableViewCell).setParallaxCellImageOffsetWithTableView(tableView) | |
} | |
``` | |
```swift | |
func scrollViewDidScroll(scrollView: UIScrollView) { | |
for cell in tableView.visibleCells as! [CustomTableViewCell] { | |
cell.setParallaxCellImageOffsetWithTableView(tableView) | |
} | |
} | |
``` | |
- Note: Necessário passar o `class` por problemas com funções mutáveis. | |
*/ | |
protocol Parallaxable: class { | |
/// A constraint do topo da imagem de background. | |
var imageTopConstraint: NSLayoutConstraint! { get set } | |
/// A constraint da base da imagem de background. | |
var imageBottomConstraint: NSLayoutConstraint! { get set } | |
var imageTopInitial: CGFloat { get set } | |
var imageBottomInitial: CGFloat { get set } | |
var parallaxFactor: CGFloat { get } | |
func setupParallaxInitialValues() | |
func setParallaxCellImageOffsetWithTableView(tableView: UITableView) | |
} | |
extension Parallaxable where Self: UITableViewCell { | |
var parallaxFactor: CGFloat { return 20.0 } | |
func setupParallaxInitialValues() { | |
clipsToBounds = true | |
imageBottomConstraint.constant -= 2 * parallaxFactor | |
imageTopInitial = imageTopConstraint.constant | |
imageBottomInitial = imageBottomConstraint.constant | |
} | |
func setParallaxBackgroundOffset(offset: CGFloat) { | |
let boundOffset = max(0, min(1, offset)) | |
let pixelOffset = (1 - boundOffset) * 2 * parallaxFactor | |
imageTopConstraint.constant = imageTopInitial - pixelOffset | |
imageBottomConstraint.constant = imageBottomInitial + pixelOffset | |
} | |
func setParallaxCellImageOffsetWithTableView(tableView: UITableView) { | |
let cellFrameInTable = tableView.convertRect(frame, toView: tableView.superview) | |
let cellOffset = cellFrameInTable.origin.y + cellFrameInTable.size.height | |
let tableHeight = tableView.bounds.size.height + cellFrameInTable.size.height | |
setParallaxBackgroundOffset(cellOffset / tableHeight) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment