質問編集履歴
3
add detail source code
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,11 +11,29 @@
|
|
11
11
|

|
12
12
|
|
13
13
|
###該当のソースコード
|
14
|
+
|
15
|
+
[PagingMenuController](https://github.com/kitasuke/PagingMenuController)を使用しており、メインのビューコントロラーから`imageUrlList`をセットして`MyViewController`を呼び出しています
|
16
|
+
|
17
|
+
|
14
18
|
```swift
|
15
19
|
class MyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
|
20
|
+
|
21
|
+
var imageUrlList: Array<String>!
|
16
22
|
|
23
|
+
override func loadView() {
|
17
|
-
|
24
|
+
super.loadView()
|
25
|
+
// Do any additional setup after loading the view, typically from a nib.
|
18
26
|
|
27
|
+
let myCollectionView = UICollectionView(frame: CGRect(origin: CGPoint.zero, size: self.view.frame.size), collectionViewLayout: UICollectionViewFlowLayout())
|
28
|
+
myCollectionView.backgroundColor = UIColor.white
|
29
|
+
myCollectionView.register(MyCollection.self, forCellWithReuseIdentifier: "MyCell")
|
30
|
+
|
31
|
+
myCollectionView.delegate = self
|
32
|
+
myCollectionView.dataSource = self
|
33
|
+
|
34
|
+
self.view.addSubview(myCollectionView)
|
35
|
+
}
|
36
|
+
|
19
37
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
20
38
|
if indexPath.row == 0 {
|
21
39
|
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height * 0.3)
|
@@ -24,7 +42,78 @@
|
|
24
42
|
return CGSize(width: collectionView.frame.width * 0.5, height: collectionView.frame.height * 0.3)
|
25
43
|
}
|
26
44
|
|
45
|
+
|
46
|
+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
47
|
+
|
48
|
+
let collection = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCollection
|
49
|
+
let url = imageUrlList[indexPath.row]
|
50
|
+
|
51
|
+
let thumbnail = collection.contentView.viewWithTag(MyCollection.TAG_THUMBNAIL) as! ThumbnailImageView
|
52
|
+
thumbnail.loadImage(urlString: url)
|
53
|
+
|
54
|
+
return collection
|
55
|
+
}
|
27
56
|
}
|
57
|
+
|
58
|
+
class MyCollection: UICollectionViewCell {
|
59
|
+
|
60
|
+
static let TAG_THUMBNAIL: Int = 0
|
61
|
+
|
62
|
+
required init(coder aDecoder: NSCoder) {
|
63
|
+
super.init(coder: aDecoder)!
|
64
|
+
}
|
65
|
+
|
66
|
+
override init(frame: CGRect) {
|
67
|
+
super.init(frame: frame)
|
68
|
+
|
69
|
+
initView(width: frame.size.width, height: frame.size.height)
|
70
|
+
}
|
71
|
+
|
72
|
+
func initView(width: CGFloat, height: CGFloat) {
|
73
|
+
|
74
|
+
let thumbnail: ThumbnailImageView = ThumbnailImageView(frame: CGRect(x: 0, y: 0, width: width, height: height * 0.3))
|
75
|
+
thumbnail.tag = MyCollection.TAG_THUMBNAIL
|
76
|
+
self.contentView.addSubview(thumbnail)
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
class ThumbnailImageView: UIImageView {
|
81
|
+
|
82
|
+
let CACHE_SEC: TimeInterval = 60 * 60 * 24 // 1 day
|
83
|
+
|
84
|
+
var baseWidth: CGFloat = 0
|
85
|
+
var baseHeight: CGFloat = 0
|
86
|
+
|
87
|
+
required init(coder aDecoder: NSCoder) {
|
88
|
+
super.init(coder: aDecoder)!
|
89
|
+
}
|
90
|
+
|
91
|
+
override init (frame: CGRect) {
|
92
|
+
super.init(frame: frame)
|
93
|
+
}
|
94
|
+
|
95
|
+
func loadImage(urlString: String) {
|
96
|
+
self.alpha = 0
|
97
|
+
let req = URLRequest(url: NSURL(string: urlString)! as URL, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: CACHE_SEC);
|
98
|
+
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: OperationQueue.main);
|
99
|
+
|
100
|
+
session.dataTask(with: req, completionHandler: { (data, resp, err) in
|
101
|
+
if ((err) == nil) {
|
102
|
+
|
103
|
+
// Success
|
104
|
+
self.image = UIImage(data: data!);
|
105
|
+
|
106
|
+
UIView.animate(withDuration: 1.0) { () -> Void in
|
107
|
+
self.alpha = 1
|
108
|
+
}
|
109
|
+
|
110
|
+
} else {
|
111
|
+
// TODO: show default if error
|
112
|
+
print("AsyncImageView:Error \(err?.localizedDescription)");
|
113
|
+
}
|
114
|
+
}).resume();
|
115
|
+
}
|
116
|
+
}
|
28
117
|
```
|
29
118
|
|
30
119
|
###試したこと
|
2
update image
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,11 +6,10 @@
|
|
6
6
|
|
7
7
|
### 発生している問題・エラーメッセージ
|
8
8
|
|
9
|
-
表示はで
|
9
|
+
初回表示はされるですが、画面を下にスクロールし、再度一番上に戻ると1行目の横幅が半分になり、途中の行の片方の画像の横幅が広がってしまいます。スクロールしても最初に表示された状態を保つためにはどのようにすべきでしょうか。
|
10
10
|
|
11
|
-

|
12
12
|
|
13
|
-
|
14
13
|
###該当のソースコード
|
15
14
|
```swift
|
16
15
|
class MyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
|
1
fix typo
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
### 発生している問題・エラーメッセージ
|
8
8
|
|
9
|
-
表示はできたのですが、画面を下にスクロールし、再度一番上に戻ると1行目の横幅が半分になり、途中の行の片方の画像の横幅が広がってしまいます。スクロールし
|
9
|
+
表示はできたのですが、画面を下にスクロールし、再度一番上に戻ると1行目の横幅が半分になり、途中の行の片方の画像の横幅が広がってしまいます。スクロールしても最初に表示された状態を保つためにはどのようにすべきでしょうか。
|
10
10
|
|
11
11
|

|
12
12
|
|