質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

5019閲覧

【Swift,Xcode】スワイプでページをめくるアニメーション

nekokichi

総合スコア54

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/10/03 15:33

編集2018/10/04 05:11

右から左にスワイプすると、画面がスライドするようなアニメーションを実装するのが目的です。

UICollectionViewを使用したもので、下記のサイトを参考にしました。

Qiita そのまま使える!iOSアプリを作るためのswiftサンプル集

上記の記事のサンプルコードを見ながら、模写したのですが、
・色が反映されない
・スライドしない
という問題に直面しています。

サンプルコードの方では動作するのでサンプルコードを1度全部コピペしましたが、ダメでした。

Swift4以上では、仕様が変わったのでしょうか?

エラーが1つも起こらないので、解決方法がわからず困っています。

どうかご指摘願います。

Swift

1import UIKit 2 3class ViewController: UIViewController,UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 4 5 @IBOutlet weak var collectionView: UICollectionView! { 6 didSet { 7 collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell") 8 } 9 } 10 11 private let dataSource: [UIColor] = [.red, .green, .blue, .cyan, .yellow, .magenta] 12 13 // MARK: - UICollectionViewDataSource 14 15 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 16 return dataSource.count 17 } 18 19 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 20 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 21 let viewController = UIViewController() 22 viewController.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height) 23 viewController.view.backgroundColor = dataSource[indexPath.row] 24 cell.addSubview(viewController.view) 25 return cell 26 } 27 28 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 29 return CGSize(width: view.frame.width, height: view.frame.height) 30 } 31} 32

Swift

1import UIKit 2 3class CollectionViewCell: UICollectionViewCell { 4 5 override func awakeFromNib() { 6 super.awakeFromNib() 7 // Initialization code 8 } 9 10}

イメージ説明

イメージ説明

※追記

Swift

1func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 2 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 3 let viewController = UIViewController() 4 viewController.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height) 5// viewController.view.backgroundColor = dataSource[indexPath.row] 6 cell.contentView.backgroundColor = dataSource[indexPath.row] 7 cell.addSubview(viewController.view) 8 return cell 9 }

と書きましたが、変わりませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

スワイプでページめくりしたいのであれば、これでどうですか?
http://docs.fabo.io/swift/
を参考に作りました。

swift

1import UIKit 2 3let devWidth = UIScreen.main.bounds.size.width 4let devHeight = UIScreen.main.bounds.size.height 5 6class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate { 7 8 private let dataSource: [UIColor] = [.red, .green, .blue, .cyan, .yellow, .magenta] 9 10 var myCollectionView : UICollectionView! 11 var count = 0 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 // Do any additional setup after loading the view, typically from a nib. 16 17 let layout = UICollectionViewFlowLayout() 18 layout.itemSize = CGSize(width: 50.0, height: 50.0) 19 layout.sectionInset = UIEdgeInsets(top: 16.0, left: 16.0, bottom: 32.0, right: 16.0) 20 layout.headerReferenceSize = CGSize(width: 100.0, height: 30.0) 21 22 myCollectionView = UICollectionView(frame: CGRect(x: 0.0, y: 20.0, width:devWidth, height: devHeight - 20.0), collectionViewLayout: layout) 23 // Cellに使われるクラスを登録. 24 myCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") 25 myCollectionView.delegate = self 26 myCollectionView.dataSource = self 27 myCollectionView.backgroundColor = dataSource[count] 28 myCollectionView.isScrollEnabled = true 29 self.view.addSubview(myCollectionView) 30 31 // Swipe の判定 32 let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(_:))) 33 rightSwipe.direction = .right 34 self.view.addGestureRecognizer(rightSwipe) 35 36 let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(_:))) 37 leftSwipe.direction = .left 38 self.view.addGestureRecognizer(leftSwipe) 39 } 40 41 42 @objc internal func swipeGesture(_ sender: UISwipeGestureRecognizer) { 43 // UIViewアニメーションの設定開始 44 UIView.beginAnimations("transition", context: nil) 45 UIView.setAnimationDuration(0.5) 46 47 // トランジションアニメーションの設定 48 var transition = UIView.AnimationTransition.curlUp 49 50 if sender.direction == .right { 51 transition = UIView.AnimationTransition.curlDown 52 count = count - 1 53 if count < 0 { 54 count = dataSource.count - 1 55 } 56 } else if sender.direction == .left { 57 transition = UIView.AnimationTransition.curlUp 58 count = count + 1 59 if count >= dataSource.count { 60 count = 0 61 } 62 } 63 64 myCollectionView.backgroundColor = dataSource[count] 65 66 UIView.setAnimationTransition(transition, for: self.view, cache: true) 67 // UIViewアニメーション開始 68 UIView.commitAnimations() 69 } 70 71 // Cellが選択された際に呼び出される 72 73 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 74 print("Num: (indexPath.row)") 75 print("Value:(collectionView)") 76 } 77 // Cellの総数を返す 78 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 79 return 10 80 } 81 // Cellに値を設定する 82 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 83 let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) 84 cell.backgroundColor = UIColor.gray 85 return cell 86 } 87} 88

投稿2018/10/04 06:07

編集2018/10/04 06:09
t.harima

総合スコア55

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

https://teratail.com/questions/149958の質問と同様かと思いますが、セルにはcontentViewが乗っていて、その上にUIを並べます。背景色を変えたいときも、contentViewの背景色を変えて下さい。

どうしてもセルの背景色を変更することで色を変えたい、ということであれば、contentViewの背景色を透明(.clear)にして下さい。

投稿2018/10/04 02:40

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2018/10/04 05:33

あちらにも同じことを書きましたが、collectionView(_:cellForItemAt:)は呼ばれていますか? dataSourceは設定していますか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問