Created
May 4, 2017 00:46
-
-
Save gyubokbaik/60dcc9fc8857d5a152884d569579f1cd to your computer and use it in GitHub Desktop.
Blog - Swipe Content with Subtle Transition
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
import UIKit | |
class SwipeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate { | |
@IBOutlet var imageViewBackground: UIImageView! | |
@IBOutlet var collectionView: UICollectionView! | |
let arraySampleContent:[[String: String]] = [ | |
["text": "content 1", "image_name": "img_background_1"], | |
["text": "content 2", "image_name": "img_background_2"], | |
["text": "content 3", "image_name": "img_background_3"] | |
] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
/* | |
// MARK: - Navigation | |
// In a storyboard-based application, you will often want to do a little preparation before navigation | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
// Get the new view controller using segue.destinationViewController. | |
// Pass the selected object to the new view controller. | |
} | |
*/ | |
// MARK: - UICollectionViewDataSource | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
// number of sample data | |
return arraySampleContent.count | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
// get content/text | |
let content = arraySampleContent[indexPath.row] | |
let text = content["text"] | |
// get cell | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) | |
// get label via tag then set text to label | |
let label = cell.viewWithTag(100) as! UILabel | |
label.text = text | |
// return cell | |
return cell | |
} | |
// MARK: - UICollectionViewDelegate | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { | |
// return collectionView's width and size | |
return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height) | |
} | |
// MARK: - UIScrollViewDelegate | |
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { | |
// determine the index of the new page | |
let pageWidth = collectionView.frame.size.width | |
let index:Int = Int(collectionView.contentOffset.x / pageWidth) | |
// get sample content | |
let currentSampleContent = arraySampleContent[index] | |
let stringImageName = currentSampleContent["image_name"]! | |
let imageTo = UIImage(named: stringImageName) | |
// transition animation for new background image | |
UIView.transition(with: imageViewBackground, duration: 0.8, options: .transitionCrossDissolve, animations: { | |
// set image to imageView | |
self.imageViewBackground.image = imageTo | |
}, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment