Skip to content

Instantly share code, notes, and snippets.

@uruly
Created April 13, 2018 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uruly/63ca749ee9a51fb8466f20b5799d8c3e to your computer and use it in GitHub Desktop.
Save uruly/63ca749ee9a51fb8466f20b5799d8c3e to your computer and use it in GitHub Desktop.
import UIKit
class SwipeAnimationCollectionView: UICollectionView {
var reloadCount:Int = 0
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
}
convenience init(frame: CGRect) {
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width:50,height:50)
layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0)
self.init(frame: frame, collectionViewLayout: layout)
setup()
}
func setup() {
self.register(SwipeAnimationCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
self.delegate = self
self.dataSource = self
self.backgroundColor = UIColor.white
//スクロールできないようにしておく
self.isScrollEnabled = false
//ジェスチャーをつける
let swipeUp = UISwipeGestureRecognizer()
swipeUp.direction = .up
swipeUp.addTarget(self, action: #selector(self.swipeAction(sender:)))
self.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer()
swipeDown.direction = .down
swipeDown.addTarget(self,action:#selector(self.swipeAction(sender:)))
self.addGestureRecognizer(swipeDown)
let swipeLeft = UISwipeGestureRecognizer()
swipeLeft.direction = .left
swipeLeft.addTarget(self,action:#selector(self.swipeAction(sender:)))
self.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer()
swipeRight.direction = .right
swipeRight.addTarget(self,action:#selector(self.swipeAction(sender:)))
self.addGestureRecognizer(swipeRight)
}
//スワイプした時のアクション
@objc func swipeAction(sender:UISwipeGestureRecognizer){
//向きごとに変える
switch sender.direction{
case .up:
reloadCount += 1
self.layer.add(swipeTransition(tag: 0), forKey: nil)
case .down:
reloadCount -= 1
self.layer.add(swipeTransition(tag: 1), forKey: nil)
case .left:
backgroundColor = UIColor.red
self.layer.add(swipeTransition(tag: 2), forKey: nil)
case .right:
backgroundColor = UIColor.yellow
self.layer.add(swipeTransition(tag: 3), forKey: nil)
default:break
}
self.reloadData()
}
//更新時のアニメーション
func swipeTransition(tag:Int) -> CATransition{
let transition:CATransition = CATransition()
transition.startProgress = 0
transition.endProgress = 1.0
transition.type = kCATransitionPush
switch tag{
case 0:
transition.subtype = kCATransitionFromTop
case 1:
transition.subtype = kCATransitionFromBottom
case 2:
transition.subtype = kCATransitionFromRight
case 3:
transition.subtype = kCATransitionFromLeft
default:
transition.subtype = kCATransitionFromTop
}
transition.duration = 0.3
return transition
}
}
extension SwipeAnimationCollectionView: UICollectionViewDelegate {
//セルを選択した時
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
extension SwipeAnimationCollectionView : UICollectionViewDataSource {
//セルの数
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 30
}
//セルの内容
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SwipeAnimationCollectionViewCell
cell.textLabel.text = "\(reloadCount)"
cell.contentView.backgroundColor = backgroundColor
return cell
}
}
import UIKit
class SwipeAnimationCollectionViewCell: UICollectionViewCell {
public var textLabel:UILabel!
override init(frame:CGRect){
super.init(frame:frame)
//日付ラベル
textLabel = UILabel()
textLabel.frame = CGRect(x:0,y:0,width:self.frame.height,height:self.frame.height)
textLabel.center = CGPoint(x:self.frame.width / 2,y:self.frame.height / 2)
textLabel.textAlignment = .center
textLabel.font = UIFont.systemFont(ofSize: 14)
textLabel.textColor = UIColor.blue
self.contentView.addSubview(textLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
import UIKit
class NinethViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let width = self.view.frame.width
let collectionView = SwipeAnimationCollectionView(frame:CGRect(x:0,y:0,width:width,height:300))
self.view.addSubview(collectionView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment