You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
7.9 KiB
185 lines
7.9 KiB
//
|
|
// AttachmentManager.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 10/6/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import AlamofireImage
|
|
import FLAnimatedImage
|
|
import UIKit
|
|
|
|
protocol AttachmentManagerDelegate {
|
|
func attachmentTapped(index: Int)
|
|
}
|
|
|
|
class AttachmentManager: NSObject {
|
|
var delegate: AttachmentManagerDelegate? = nil
|
|
|
|
@objc func attachmentTapped(_ gestureRecognizer: UITapGestureRecognizer) {
|
|
if let delegate = delegate, let name = gestureRecognizer.name {
|
|
delegate.attachmentTapped(index: Int(name)!)
|
|
}
|
|
}
|
|
|
|
func setupAttachmentView(_ view: UIView, withAttachments attachments: NSOrderedSet?) {
|
|
guard let attachments = attachments, attachments.count > 0 else {
|
|
return
|
|
}
|
|
|
|
let halfWidth = (view.frame.width / 2) - 1
|
|
|
|
for subview in view.subviews as [UIView] {
|
|
subview.removeFromSuperview()
|
|
}
|
|
|
|
switch attachments.count {
|
|
case 0:
|
|
return
|
|
case 1:
|
|
let attachment = attachments.firstObject as! AttachmentMO
|
|
let imageView = FLAnimatedImageView()
|
|
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
|
|
|
|
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped))
|
|
tapGestureRecognizer.delegate = self
|
|
tapGestureRecognizer.numberOfTapsRequired = 1
|
|
tapGestureRecognizer.numberOfTouchesRequired = 1
|
|
tapGestureRecognizer.name = "0"
|
|
|
|
imageView.addGestureRecognizer(tapGestureRecognizer)
|
|
imageView.isUserInteractionEnabled = true
|
|
|
|
imageView.setImage(withURL: attachment.url!)
|
|
|
|
view.addSubview(imageView)
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
])
|
|
case 2:
|
|
let imageViews = [
|
|
FLAnimatedImageView(),
|
|
FLAnimatedImageView(),
|
|
]
|
|
|
|
for (index, imageView) in imageViews.enumerated() {
|
|
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
|
|
|
|
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped))
|
|
tapGestureRecognizer.delegate = self
|
|
tapGestureRecognizer.numberOfTapsRequired = 1
|
|
tapGestureRecognizer.numberOfTouchesRequired = 1
|
|
tapGestureRecognizer.name = String(index)
|
|
|
|
imageView.addGestureRecognizer(tapGestureRecognizer)
|
|
imageView.isUserInteractionEnabled = true
|
|
|
|
imageView.setImage(withURL: (attachments[index] as! AttachmentMO).url!)
|
|
|
|
view.addSubview(imageView)
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
imageView.widthAnchor.constraint(equalToConstant: halfWidth),
|
|
])
|
|
}
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageViews[0].leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
imageViews[1].trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
])
|
|
case 3:
|
|
let imageViews = [
|
|
FLAnimatedImageView(),
|
|
FLAnimatedImageView(),
|
|
FLAnimatedImageView(),
|
|
]
|
|
|
|
for (index, imageView) in imageViews.enumerated() {
|
|
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
|
|
|
|
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped))
|
|
tapGestureRecognizer.delegate = self
|
|
tapGestureRecognizer.numberOfTapsRequired = 1
|
|
tapGestureRecognizer.numberOfTouchesRequired = 1
|
|
tapGestureRecognizer.name = String(index)
|
|
|
|
imageView.addGestureRecognizer(tapGestureRecognizer)
|
|
imageView.isUserInteractionEnabled = true
|
|
|
|
imageView.setImage(withURL: (attachments[index] as! AttachmentMO).url!)
|
|
|
|
view.addSubview(imageView)
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
if index == 0 {
|
|
NSLayoutConstraint.activate([
|
|
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
imageView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
imageView.widthAnchor.constraint(equalToConstant: halfWidth),
|
|
])
|
|
} else {
|
|
NSLayoutConstraint.activate([
|
|
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
imageView.widthAnchor.constraint(equalToConstant: halfWidth),
|
|
imageView.heightAnchor.constraint(equalToConstant: halfWidth),
|
|
])
|
|
}
|
|
}
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageViews[1].topAnchor.constraint(equalTo: view.topAnchor),
|
|
imageViews[2].bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
])
|
|
default:
|
|
let imageViews = [
|
|
FLAnimatedImageView(),
|
|
FLAnimatedImageView(),
|
|
FLAnimatedImageView(),
|
|
FLAnimatedImageView(),
|
|
]
|
|
|
|
for (index, imageView) in imageViews.enumerated() {
|
|
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
|
|
|
|
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped))
|
|
tapGestureRecognizer.delegate = self
|
|
tapGestureRecognizer.numberOfTapsRequired = 1
|
|
tapGestureRecognizer.numberOfTouchesRequired = 1
|
|
tapGestureRecognizer.name = String(index)
|
|
|
|
imageView.addGestureRecognizer(tapGestureRecognizer)
|
|
imageView.isUserInteractionEnabled = true
|
|
|
|
imageView.setImage(withURL: (attachments[index] as! AttachmentMO).url!)
|
|
|
|
view.addSubview(imageView)
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageViews[0].topAnchor.constraint(equalTo: view.topAnchor),
|
|
imageViews[0].leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
imageViews[1].topAnchor.constraint(equalTo: view.topAnchor),
|
|
imageViews[1].trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
imageViews[2].leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
imageViews[2].bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
imageViews[3].bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
imageViews[3].trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
])
|
|
}
|
|
}
|
|
}
|
|
|
|
extension AttachmentManager: UIGestureRecognizerDelegate {
|
|
|
|
}
|