質問編集履歴

3

add detail source code

2017/04/05 08:09

投稿

yousei
yousei

スコア7

test CHANGED
File without changes
test CHANGED
@@ -24,13 +24,49 @@
24
24
 
25
25
  ###該当のソースコード
26
26
 
27
+
28
+
29
+ [PagingMenuController](https://github.com/kitasuke/PagingMenuController)を使用しており、メインのビューコントロラーから`imageUrlList`をセットして`MyViewController`を呼び出しています
30
+
31
+
32
+
33
+
34
+
27
35
  ```swift
28
36
 
29
37
  class MyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
30
38
 
39
+
40
+
31
-
41
+ var imageUrlList: Array<String>!
42
+
43
+
44
+
32
-
45
+ override func loadView() {
46
+
33
- // 中略
47
+ super.loadView()
48
+
49
+ // Do any additional setup after loading the view, typically from a nib.
50
+
51
+
52
+
53
+ let myCollectionView = UICollectionView(frame: CGRect(origin: CGPoint.zero, size: self.view.frame.size), collectionViewLayout: UICollectionViewFlowLayout())
54
+
55
+ myCollectionView.backgroundColor = UIColor.white
56
+
57
+ myCollectionView.register(MyCollection.self, forCellWithReuseIdentifier: "MyCell")
58
+
59
+
60
+
61
+ myCollectionView.delegate = self
62
+
63
+ myCollectionView.dataSource = self
64
+
65
+
66
+
67
+ self.view.addSubview(myCollectionView)
68
+
69
+ }
34
70
 
35
71
 
36
72
 
@@ -50,8 +86,150 @@
50
86
 
51
87
 
52
88
 
89
+
90
+
91
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
92
+
93
+
94
+
95
+ let collection = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCollection
96
+
97
+ let url = imageUrlList[indexPath.row]
98
+
99
+
100
+
101
+ let thumbnail = collection.contentView.viewWithTag(MyCollection.TAG_THUMBNAIL) as! ThumbnailImageView
102
+
103
+ thumbnail.loadImage(urlString: url)
104
+
105
+
106
+
107
+ return collection
108
+
109
+ }
110
+
53
111
  }
54
112
 
113
+
114
+
115
+ class MyCollection: UICollectionViewCell {
116
+
117
+
118
+
119
+ static let TAG_THUMBNAIL: Int = 0
120
+
121
+
122
+
123
+ required init(coder aDecoder: NSCoder) {
124
+
125
+ super.init(coder: aDecoder)!
126
+
127
+ }
128
+
129
+
130
+
131
+ override init(frame: CGRect) {
132
+
133
+ super.init(frame: frame)
134
+
135
+
136
+
137
+ initView(width: frame.size.width, height: frame.size.height)
138
+
139
+ }
140
+
141
+
142
+
143
+ func initView(width: CGFloat, height: CGFloat) {
144
+
145
+
146
+
147
+ let thumbnail: ThumbnailImageView = ThumbnailImageView(frame: CGRect(x: 0, y: 0, width: width, height: height * 0.3))
148
+
149
+ thumbnail.tag = MyCollection.TAG_THUMBNAIL
150
+
151
+ self.contentView.addSubview(thumbnail)
152
+
153
+ }
154
+
155
+ }
156
+
157
+
158
+
159
+ class ThumbnailImageView: UIImageView {
160
+
161
+
162
+
163
+ let CACHE_SEC: TimeInterval = 60 * 60 * 24 // 1 day
164
+
165
+
166
+
167
+ var baseWidth: CGFloat = 0
168
+
169
+ var baseHeight: CGFloat = 0
170
+
171
+
172
+
173
+ required init(coder aDecoder: NSCoder) {
174
+
175
+ super.init(coder: aDecoder)!
176
+
177
+ }
178
+
179
+
180
+
181
+ override init (frame: CGRect) {
182
+
183
+ super.init(frame: frame)
184
+
185
+ }
186
+
187
+
188
+
189
+ func loadImage(urlString: String) {
190
+
191
+ self.alpha = 0
192
+
193
+ let req = URLRequest(url: NSURL(string: urlString)! as URL, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: CACHE_SEC);
194
+
195
+ let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: OperationQueue.main);
196
+
197
+
198
+
199
+ session.dataTask(with: req, completionHandler: { (data, resp, err) in
200
+
201
+ if ((err) == nil) {
202
+
203
+
204
+
205
+ // Success
206
+
207
+ self.image = UIImage(data: data!);
208
+
209
+
210
+
211
+ UIView.animate(withDuration: 1.0) { () -> Void in
212
+
213
+ self.alpha = 1
214
+
215
+ }
216
+
217
+
218
+
219
+ } else {
220
+
221
+ // TODO: show default if error
222
+
223
+ print("AsyncImageView:Error \(err?.localizedDescription)");
224
+
225
+ }
226
+
227
+ }).resume();
228
+
229
+ }
230
+
231
+ }
232
+
55
233
  ```
56
234
 
57
235
 

2

update image

2017/04/05 08:09

投稿

yousei
yousei

スコア7

test CHANGED
File without changes
test CHANGED
@@ -14,13 +14,11 @@
14
14
 
15
15
 
16
16
 
17
- 表示はできたのですが、画面を下にスクロールし、再度一番上に戻ると1行目の横幅が半分になり、途中の行の片方の画像の横幅が広がってしまいます。スクロールしても最初に表示された状態を保つためにはどのようにすべきでしょうか。
17
+ 初回表示はされるですが、画面を下にスクロールし、再度一番上に戻ると1行目の横幅が半分になり、途中の行の片方の画像の横幅が広がってしまいます。スクロールしても最初に表示された状態を保つためにはどのようにすべきでしょうか。
18
18
 
19
19
 
20
20
 
21
- ![イメージ説明](87cdb53a49802c355f2dbaddd6cbb204.png)
21
+ ![イメージ説明](03680da8b0eda5f2fa9724c1ba266f4e.png)
22
-
23
-
24
22
 
25
23
 
26
24
 

1

fix typo

2017/04/05 07:31

投稿

yousei
yousei

スコア7

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- 表示はできたのですが、画面を下にスクロールし、再度一番上に戻ると1行目の横幅が半分になり、途中の行の片方の画像の横幅が広がってしまいます。スクロールし元に戻しても最初に表示された状態()を保つためにはどのようにs
17
+ 表示はできたのですが、画面を下にスクロールし、再度一番上に戻ると1行目の横幅が半分になり、途中の行の片方の画像の横幅が広がってしまいます。スクロールしても最初に表示された状態を保つためにはどのようにすべきでしょうか。
18
18
 
19
19
 
20
20