質問編集履歴

1

成功したソースコード添付します。

2018/10/16 14:44

投稿

sakuradaiya
sakuradaiya

スコア24

test CHANGED
File without changes
test CHANGED
@@ -223,3 +223,195 @@
223
223
 
224
224
 
225
225
  storyboardでの作業は特に行なっていません。。必要でしょうか?
226
+
227
+ →成功したコードには必要ありませんでした。
228
+
229
+
230
+
231
+ ### 成功したソースコード添付
232
+
233
+
234
+
235
+ ```Swift
236
+
237
+ import UIKit
238
+
239
+
240
+
241
+ extension UIView {
242
+
243
+ func 波紋(touch: UITapGestureRecognizer) {
244
+
245
+
246
+
247
+ // ①: タップされた場所にlayerを置き、半径200の円を描画
248
+
249
+ let location = touch.location(in: self)
250
+
251
+ let layer = CAShapeLayer.init()
252
+
253
+ self.layer.addSublayer(layer)
254
+
255
+ layer.frame = CGRect.init(x: location.x, y: location.y, width: 100, height: 100)
256
+
257
+ layer.position = CGPoint.init(x: location.x, y: location.y)
258
+
259
+ layer.contents = {
260
+
261
+ let size: CGFloat = 200.0
262
+
263
+ UIGraphicsBeginImageContext(CGSize.init(width: size, height: size))
264
+
265
+ let context = UIGraphicsGetCurrentContext()!
266
+
267
+ context.saveGState()
268
+
269
+ context.setFillColor(UIColor.clear.cgColor)
270
+
271
+ context.fill(self.frame)
272
+
273
+ let r = CGFloat.init(size/2-10)
274
+
275
+ let center = CGPoint.init(x: size/2, y: size/2)
276
+
277
+ let path : CGMutablePath = CGMutablePath()
278
+
279
+ path.addArc(center: center, radius: r, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: false)
280
+
281
+ context.addPath(path)
282
+
283
+ context.setFillColor(UIColor.lightGray.cgColor)
284
+
285
+ context.setStrokeColor(UIColor.lightGray.cgColor)
286
+
287
+ context.drawPath(using: .fillStroke)
288
+
289
+ let image = UIGraphicsGetImageFromCurrentImageContext()
290
+
291
+ context.restoreGState()
292
+
293
+ return image!.cgImage
294
+
295
+ }()
296
+
297
+
298
+
299
+ // ②: 円を拡大しつつ透明化するAnimationを用意
300
+
301
+ let animationGroup: CAAnimationGroup = {
302
+
303
+ let animation: CABasicAnimation = {
304
+
305
+ let animation = CABasicAnimation(keyPath: "transform.scale")
306
+
307
+ animation.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeOut)
308
+
309
+ animation.duration = 0.5
310
+
311
+ animation.isRemovedOnCompletion = false
312
+
313
+ animation.fillMode = CAMediaTimingFillMode.forwards
314
+
315
+ animation.fromValue = NSNumber(value: 0.5)
316
+
317
+ animation.toValue = NSNumber(value: 5.0)
318
+
319
+ return animation
320
+
321
+ }()
322
+
323
+
324
+
325
+ let animation2: CABasicAnimation = {
326
+
327
+ let animation = CABasicAnimation(keyPath: "opacity")
328
+
329
+ animation.duration = 0.5
330
+
331
+ animation.isRemovedOnCompletion = false
332
+
333
+ animation.fillMode = CAMediaTimingFillMode.forwards
334
+
335
+ animation.fromValue = NSNumber(value: 0.5)
336
+
337
+ animation.toValue = NSNumber(value: 0.0)
338
+
339
+ return animation
340
+
341
+ }()
342
+
343
+
344
+
345
+ let group = CAAnimationGroup()
346
+
347
+ group.beginTime = CACurrentMediaTime()
348
+
349
+ group.animations = [animation, animation2]
350
+
351
+ group.isRemovedOnCompletion = false
352
+
353
+ group.fillMode = CAMediaTimingFillMode.backwards
354
+
355
+ return group
356
+
357
+ }()
358
+
359
+
360
+
361
+ // ③: layerにAnimationを適用
362
+
363
+ CATransaction.setAnimationDuration(5.0)
364
+
365
+ CATransaction.setCompletionBlock({
366
+
367
+ layer.removeFromSuperlayer()
368
+
369
+ })
370
+
371
+ CATransaction.begin()
372
+
373
+ layer.add(animationGroup, forKey: nil)
374
+
375
+ layer.opacity = 0.0
376
+
377
+ CATransaction.commit()
378
+
379
+ }
380
+
381
+ }
382
+
383
+
384
+
385
+ class ViewController: UIViewController {
386
+
387
+ override func awakeFromNib() {
388
+
389
+ super.awakeFromNib()
390
+
391
+
392
+
393
+ let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(self.tapContentView(touch:)))
394
+
395
+ self.View.addGestureRecognizer(tapGesture)
396
+
397
+ }
398
+
399
+
400
+
401
+ @objc func tapContentView(touch: UITapGestureRecognizer) {
402
+
403
+
404
+
405
+ self.View.波紋(touch: touch)
406
+
407
+ }
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+ }
416
+
417
+ ```