回答編集履歴
2
修正
test
CHANGED
@@ -101,3 +101,155 @@
|
|
101
101
|
}
|
102
102
|
|
103
103
|
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
回答追記 複数のViewの場合
|
108
|
+
|
109
|
+
---
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
```swift
|
114
|
+
|
115
|
+
import UIKit
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
class ViewController: UIViewController, UIScrollViewDelegate {
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
var scrollView = UIScrollView()
|
124
|
+
|
125
|
+
var targetViewArray: [ViewAnimation] = []
|
126
|
+
|
127
|
+
var isAnimation = false
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
override func viewDidLoad() {
|
132
|
+
|
133
|
+
super.viewDidLoad()
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
let screenSize = UIScreen.mainScreen().bounds
|
138
|
+
|
139
|
+
scrollView.frame = screenSize
|
140
|
+
|
141
|
+
scrollView.backgroundColor = UIColor.greenColor()
|
142
|
+
|
143
|
+
self.scrollView.contentSize = CGSize(width: screenSize.width, height: screenSize.height * 5)
|
144
|
+
|
145
|
+
scrollView.backgroundColor = UIColor.greenColor()
|
146
|
+
|
147
|
+
scrollView.delegate = self
|
148
|
+
|
149
|
+
view.addSubview(scrollView)
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
let targetView = UIView(frame: CGRect(x: 20, y: 1500, width: 50, height: 100))
|
154
|
+
|
155
|
+
targetView.backgroundColor = UIColor.redColor()
|
156
|
+
|
157
|
+
scrollView.addSubview(targetView)
|
158
|
+
|
159
|
+
targetViewArray.append(ViewAnimation(view: targetView))
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
let targetView1 = UIView(frame: CGRect(x: 120, y: 1500, width: 50, height: 200))
|
164
|
+
|
165
|
+
targetView1.backgroundColor = UIColor.redColor()
|
166
|
+
|
167
|
+
scrollView.addSubview(targetView1)
|
168
|
+
|
169
|
+
targetViewArray.append(ViewAnimation(view: targetView1))
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
let targetView2 = UIView(frame: CGRect(x: 220, y: 1500, width: 50, height: 150))
|
174
|
+
|
175
|
+
targetView2.backgroundColor = UIColor.redColor()
|
176
|
+
|
177
|
+
scrollView.addSubview(targetView2)
|
178
|
+
|
179
|
+
targetViewArray.append(ViewAnimation(view: targetView2))
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
func scrollViewDidScroll(scrollView: UIScrollView) {
|
188
|
+
|
189
|
+
let visibleRect = CGRect(origin: scrollView.contentOffset, size: scrollView.bounds.size)
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
for target in targetViewArray {
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
if CGRectIntersectsRect(visibleRect, target.view.frame) {
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
if !target.isAnimation {
|
202
|
+
|
203
|
+
target.isAnimation = true
|
204
|
+
|
205
|
+
startAnimation(target.view)
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
} else {
|
210
|
+
|
211
|
+
target.isAnimation = false
|
212
|
+
|
213
|
+
target.view.backgroundColor = UIColor.redColor()
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
func startAnimation(targetView: UIView) {
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
UIView.animateWithDuration(1.5, animations: {
|
228
|
+
|
229
|
+
targetView.backgroundColor = UIColor.yellowColor()
|
230
|
+
|
231
|
+
})
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
class ViewAnimation {
|
240
|
+
|
241
|
+
var view: UIView!
|
242
|
+
|
243
|
+
var isAnimation = false
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
init(view: UIView) {
|
248
|
+
|
249
|
+
self.view = view
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
```
|
1
修正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
以下を空のプロジェクトにそのまま貼り付けて実行してみてください。
|
1
|
+
以下のコードを空のプロジェクトにそのまま貼り付けて実行してみてください。
|
2
2
|
|
3
3
|
`UIScrollView`に乗せた`UIView`が画面に現れた時に背景色をアニメーションで変えています。
|
4
4
|
|