Skip to content

Instantly share code, notes, and snippets.

@bbheck
Last active April 22, 2016 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bbheck/6e98b6b7d059eb2b91e6 to your computer and use it in GitHub Desktop.
Save bbheck/6e98b6b7d059eb2b91e6 to your computer and use it in GitHub Desktop.
Protocolo que permite que a UITableViewCell faça um efeito de parallax.
/**
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