Last active
December 8, 2018 00:46
-
-
Save iahmedhendi/505104dd0df4daedb8bbc388b0a0c51d to your computer and use it in GitHub Desktop.
Class to make A double sides poll.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LinePollView.swift | |
// | |
// Created by Hendi on 12/8/18. | |
// Copyright © 2018 Hendi. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
@IBDesignable class LinePollView: UIView { | |
@IBInspectable var rightImage: UIImage! = UIImage(named: "img1") | |
@IBInspectable var leftImage: UIImage! = UIImage(named: "img2") | |
@IBInspectable var electricImage: UIImage! = UIImage(named: "power") | |
@IBInspectable var rightTotal: CGFloat = 20 | |
@IBInspectable var leftTotal: CGFloat = 10 | |
override func awakeFromNib() { | |
updateView() | |
} | |
let baseView: UIView = { | |
let view = UIView() | |
view.backgroundColor = .gray | |
return view | |
}() | |
let colordView: UIView = { | |
let view = UIView() | |
view.backgroundColor = .blue | |
return view | |
}() | |
func updateView() { | |
let width = self.frame.width | |
let height = self.frame.height | |
let total = leftTotal + rightTotal | |
let progressValue = leftTotal > rightTotal ? leftTotal / total : rightTotal / total | |
print("Right value is \(rightTotal) , Left Value is \(leftTotal), Total is \(total), Progress Value is \(progressValue)") | |
let rightImageView = UIImageView(image: rightImage) | |
let leftImageView = UIImageView(image: leftImage) | |
let electricImageView = UIImageView(image: electricImage) | |
self.addSubview(baseView) | |
self.addSubview(colordView) | |
self.addSubview(rightImageView) | |
self.addSubview(leftImageView) | |
self.addSubview(electricImageView) | |
baseView.translatesAutoresizingMaskIntoConstraints = false | |
colordView.translatesAutoresizingMaskIntoConstraints = false | |
rightImageView.translatesAutoresizingMaskIntoConstraints = false | |
leftImageView.translatesAutoresizingMaskIntoConstraints = false | |
electricImageView.translatesAutoresizingMaskIntoConstraints = false | |
rightImageView.layer.cornerRadius = height / 2 | |
leftImageView.layer.cornerRadius = height / 2 | |
rightImageView.layer.masksToBounds = true | |
leftImageView.layer.masksToBounds = true | |
NSLayoutConstraint.activate([ | |
// base View | |
baseView.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: -20), | |
baseView.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 20), | |
baseView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0), | |
baseView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0), | |
// rightImageView | |
rightImageView.trailingAnchor.constraint(equalTo: self.baseView.trailingAnchor, constant: 12), | |
rightImageView.widthAnchor.constraint(equalToConstant: height), | |
rightImageView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0), | |
rightImageView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0), | |
// leftImageView | |
leftImageView.leadingAnchor.constraint(equalTo: self.baseView.leadingAnchor, constant: -12), | |
leftImageView.widthAnchor.constraint(equalToConstant: height), | |
leftImageView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0), | |
leftImageView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0), | |
// Colored View | |
leftTotal > rightTotal ? | |
colordView.leadingAnchor.constraint(equalTo: self.baseView.leadingAnchor, constant: 0): | |
colordView.trailingAnchor.constraint(equalTo: self.baseView.trailingAnchor, constant: 0), | |
leftTotal > rightTotal ? | |
colordView.widthAnchor.constraint(equalToConstant: (progressValue) * width): | |
colordView.widthAnchor.constraint(equalToConstant: (progressValue) * width), | |
colordView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0), | |
colordView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0), | |
// Electric View | |
]) | |
NSLayoutConstraint.activate([ | |
electricImageView.heightAnchor.constraint(equalToConstant: height), | |
electricImageView.widthAnchor.constraint(equalToConstant: height), | |
electricImageView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0), | |
electricImageView.centerXAnchor.constraint(equalTo: | |
leftTotal > rightTotal ? self.colordView.trailingAnchor : self.colordView.leadingAnchor | |
, constant: 0) | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment