-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathImageThumbnailView.swift
More file actions
62 lines (50 loc) · 2.26 KB
/
ImageThumbnailView.swift
File metadata and controls
62 lines (50 loc) · 2.26 KB
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
//
// ImageThumbnailView.swift
// StackViewController
//
// Created by Indragie Karunaratne on 2016-04-24.
// Copyright © 2016 Seed Platform, Inc. All rights reserved.
//
import UIKit
import StackViewController
protocol ImageThumbnailViewDelegate: AnyObject {
func imageThumbnailViewDidTapDeleteButton(_ view: ImageThumbnailView)
}
open class ImageThumbnailView: UIView {
fileprivate struct Appearance {
static let ImageCornerRadius: CGFloat = 8.0
}
weak var delegate: ImageThumbnailViewDelegate?
init(thumbnail: UIImage) {
super.init(frame: CGRect.zero)
let deleteButtonImage = UIImage(named: "delete-button")!
let deleteButton = UIButton(type: .custom)
deleteButton.translatesAutoresizingMaskIntoConstraints = false
deleteButton.setBackgroundImage(deleteButtonImage, for: UIControl.State())
deleteButton.addTarget(self, action: #selector(ImageThumbnailView.didTapDelete(_:)), for: .touchUpInside)
let imageView = UIImageView(image: thumbnail)
imageView.layer.cornerRadius = Appearance.ImageCornerRadius
imageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(imageView)
addSubview(deleteButton)
let metrics = [
"imageViewLeft": -(deleteButtonImage.size.width / 2),
"imageViewTop": -(deleteButtonImage.size.height / 2)
]
let views = [
"deleteButton": deleteButton,
"imageView": imageView
]
let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[deleteButton]-imageViewTop-[imageView]|", options: [], metrics: metrics, views: views)
let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[deleteButton]-imageViewLeft-[imageView]|", options: [], metrics: metrics, views: views)
NSLayoutConstraint.activate(verticalConstraints)
NSLayoutConstraint.activate(horizontalConstraints)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: Actions
@objc fileprivate func didTapDelete(_ sender: UIButton) {
delegate?.imageThumbnailViewDidTapDeleteButton(self)
}
}