teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

訂正

2020/05/17 10:07

投稿

退会済みユーザー
answer CHANGED
@@ -55,8 +55,10 @@
55
55
  ---
56
56
  追記
57
57
 
58
- [サンプル](https://github.com/tyobigoro/tScrollView)
59
58
 
59
+
60
+ [ラベルだけのサンプルあげときます。ご自身でimageViewを追加してください](https://github.com/tyobigoro/tScrollView)
61
+
60
62
  ```swift
61
63
  import UIKit
62
64
 

2

追記

2020/05/17 10:07

投稿

退会済みユーザー
answer CHANGED
@@ -50,4 +50,46 @@
50
50
  height: view.frame.size.height)
51
51
  imageView.contentMode = .scaleAspectFit
52
52
  }
53
+ ```
54
+
55
+ ---
56
+ 追記
57
+
58
+ [サンプル](https://github.com/tyobigoro/tScrollView)
59
+
60
+ ```swift
61
+ import UIKit
62
+
63
+ class ViewController: UIViewController {
64
+
65
+ let scrollView = UIScrollView()
66
+
67
+ var strs = ["A", "B", "C", "D", "E"]
68
+
69
+ override func viewDidLoad() {
70
+ super.viewDidLoad()
71
+
72
+ scrollView.frame = self.view.frame
73
+ scrollView.backgroundColor = UIColor.red
74
+ scrollView.contentSize = CGSize(width: self.view.bounds.width * CGFloat(strs.count) , height: self.view.bounds.height)
75
+ self.view.addSubview(scrollView)
76
+
77
+ //ページングを有効にする
78
+ scrollView.isPagingEnabled = true
79
+
80
+ for i in strs.enumerated() {
81
+
82
+ let label = UILabel(frame: CGRect(x: CGFloat(i.offset)*view.bounds.size.width,
83
+ y: 0,
84
+ width: view.bounds.size.width,
85
+ height: view.bounds.size.height))
86
+ label.textAlignment = .center
87
+ label.backgroundColor = .blue
88
+ label.text = i.element
89
+
90
+ scrollView.addSubview(label)
91
+ }
92
+ }
93
+
94
+ }
53
95
  ```

1

訂正

2020/05/17 10:04

投稿

退会済みユーザー
answer CHANGED
@@ -17,11 +17,11 @@
17
17
  の部分が
18
18
 
19
19
  ```swift
20
- for i in imageArray{
20
+ for i in imageArray.enumrated(){
21
21
  let imageView = UIImageView()
22
- imageView.image = UIImage(named: i)
22
+ imageView.image = UIImage(named: i.element)
23
23
  imageView.frame
24
- = CGRect(x: CGFloat(i)*view.frame.size.width,
24
+ = CGRect(x: CGFloat(i.offset + 1)*view.frame.size.width,
25
25
  y: 0,
26
26
  width: view.frame.size.width,
27
27
  height: view.frame.size.height)
@@ -32,4 +32,22 @@
32
32
  かな?
33
33
  試してみてください
34
34
 
35
- scrollViewの部分はわかりません。
35
+ scrollViewの部分はわかりません。
36
+
37
+ ---
38
+
39
+ 訂正
40
+
41
+
42
+ ```swift
43
+ for i in imageArray.enumerated(){
44
+ let imageView = UIImageView()
45
+ imageView.image = UIImage(named: i.element)
46
+ imageView.frame
47
+ = CGRect(x: CGFloat(i.offset + 1)*view.frame.size.width,
48
+ y: 0,
49
+ width: view.frame.size.width,
50
+ height: view.frame.size.height)
51
+ imageView.contentMode = .scaleAspectFit
52
+ }
53
+ ```