-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageViewController.swift
executable file
·96 lines (70 loc) · 3.05 KB
/
ImageViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// ImageViewController.swift
// Project30
//
// Created by TwoStraws on 20/08/2016.
// Copyright (c) 2016 TwoStraws. All rights reserved.
//
import UIKit
class ImageViewController: UIViewController {
weak var owner: SelectionViewController!
var image: String!
var animTimer: Timer!
var imageView: UIImageView!
override func loadView() {
super.loadView()
view.backgroundColor = UIColor.black
// create an image view that fills the screen
imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.alpha = 0
view.addSubview(imageView)
// make the image view fill the screen
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
// schedule an animation that does something vaguely interesting
animTimer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { _ in
// do something exciting with our image
self.imageView.transform = CGAffineTransform.identity
UIView.animate(withDuration: 3) {
self.imageView.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
title = image.replacingOccurrences(of: "-Large.jpg", with: "")
// When you use this code the image is loaded into an image content cache for later reuse, but we aren't going to use it again!
guard let path = Bundle.main.path(forResource: image, ofType: nil) else { return }
guard let original = UIImage(contentsOfFile: path) else { return }
let renderer = UIGraphicsImageRenderer(size: original.size)
let rounded = renderer.image { ctx in
ctx.cgContext.addEllipse(in: CGRect(origin: CGPoint.zero, size: original.size))
ctx.cgContext.closePath()
original.draw(at: CGPoint.zero)
}
imageView.image = rounded
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
imageView.alpha = 0
UIView.animate(withDuration: 3) { [unowned self] in
self.imageView.alpha = 1
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
animTimer.invalidate()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let defaults = UserDefaults.standard
var currentVal = defaults.integer(forKey: image)
currentVal += 1
defaults.set(currentVal, forKey: image)
// tell the parent view controller that it should refresh its table counters when we go back
owner.dirty = true
}
}