Imitate SVProgressHUD behavior - ios

I`ve implemented a UIView that display a CustomLottie in the center of the screen, it has show() and hide() methods.
How can I give it the ability of hide() it from another place than where show() was called??
This is the code:
class LottieProgressHUD: UIView {
static let shared = LottieProgressHUD()
let hudView: UIView
var animationView: AnimationView
//options
var hudWidth:CGFloat = 200
var hudHeight:CGFloat = 200
var animationFileName = "coinLoading"
override init(frame: CGRect) {
self.hudView = UIView()
self.animationView = AnimationView()
super.init(frame: frame)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
self.hudView = UIView()
self.animationView = AnimationView()
super.init(coder: aDecoder)
self.setup()
}
func setup() {
self.addSubview(hudView)
show()
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
if let superview = self.superview {
self.animationView.removeFromSuperview()
let screenRect = UIScreen.main.bounds
let screenWidth = screenRect.size.width
let screenHeight = screenRect.size.height
let width: CGFloat = self.hudWidth
let height: CGFloat = self.hudHeight
self.frame = CGRect(x: (screenWidth / 2) - (width / 2) ,y: (screenHeight / 2) - (height / 2), width: width, height: height)
hudView.frame = self.bounds
layer.masksToBounds = true
self.animationView = AnimationView(name: self.animationFileName)
self.animationView.frame = CGRect(x: 0, y: 0, width: self.hudView.frame.width, height: self.hudView.frame.size.height)
self.animationView.contentMode = .scaleAspectFill
self.hudView.addSubview(animationView)
self.animationView.loopMode = .loop
self.animationView.play()
self.hide()
}
}
func show() {
self.isHidden = false
}
func hide() {
self.isHidden = true
}
}
This is how I call it inside the VC:
let progressHUD = LottieProgressHUD.shared
self.view.addSubview(progressHUD)
progressHUD.show()
I would like to call progressHUD.hide() inside the VM

You already have a static property that you can reference the same instance from anywhere. Should be able to call your show and hide methods from any class.
LottieProgressHUD.shared.show()
LottieProgressHUD.shared.hide()

Related

Zoom on UIView contained in UIScrollView

I have some trouble handling zoom on UIScrollView that contains many subviews. I already saw many answers on SO, but none of them helped me.
So, basically what I'm doing is simple : I have a class called UIFreeView :
import UIKit
class UIFreeView: UIView, UIScrollViewDelegate {
var mainUIView: UIView!
var scrollView: UIScrollView!
var arrayOfView: [UIView]?
override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
fileprivate func setupView() {
// MainUiView
self.mainUIView = UIView(frame: self.frame)
self.addSubview(mainUIView)
// ScrollView
self.scrollView = UIScrollView(frame: self.frame)
self.scrollView.delegate = self
self.addSubview(self.scrollView)
}
func reloadViews(postArray:[Post]?) {
if let postArray = postArray {
print("UIFreeView::reloadVIew.postArraySize = \(postArray.count)")
let size: CGFloat = 80.0
let margin: CGFloat = 20.0
scrollView.contentSize.width = (size * CGFloat(postArray.count))+(margin*CGFloat(postArray.count))
scrollView.contentSize.height = (size * CGFloat(postArray.count))+(margin*CGFloat(postArray.count))
for item in postArray {
let view = buildPostView(item)
self.scrollView.addSubview(view)
}
}
}
fileprivate func buildPostView(_ item:Post) -> UIView {
// Const
let size: CGFloat = 80.0
let margin: CGFloat = 5.0
// Var
let view = UIView()
let textView = UITextView()
let backgroundImageView = UIImageView()
// Setup view
let x = CGFloat(UInt64.random(lower: UInt64(0), upper: UInt64(self.scrollView.contentSize.width)))
let y = CGFloat(UInt64.random(lower: UInt64(0), upper: UInt64(self.scrollView.contentSize.height)))
view.frame = CGRect(x: x,
y: y,
width: size,
height: size)
// Setup background view
backgroundImageView.frame = CGRect(x: 0,
y: 0,
width: view.frame.size.width,
height: view.frame.size.height)
var bgName = ""
if (item.isFromCurrentUser) {
bgName = "post-it-orange"
} else {
bgName = "post-it-white"
}
backgroundImageView.contentMode = .scaleAspectFit
backgroundImageView.image = UIImage(named: bgName)
view.addSubview(backgroundImageView)
// Setup text view
textView.frame = CGRect(x: margin,
y: margin,
width: view.frame.size.width - margin*2,
height: view.frame.size.height - margin*2)
textView.backgroundColor = UIColor.clear
textView.text = item.content
textView.isEditable = false
textView.isSelectable = false
textView.isUserInteractionEnabled = false
view.addSubview(textView)
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
view.addGestureRecognizer(gestureRecognizer)
return view
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.scrollView
}
func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.scrollView)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.scrollView)
}
}
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}
This class is working perfectly, I can scroll through all my views, but I can't zoom-in and zoom-out in order to see less/more views on my screen.
I think the solution is simple, but I can't seem to find a real solution for my problem ..
Thanks !

UIScrollView how to restore to original size after UIImageView pinch zoom

I have a collection view containing several images. I can zoom an image in the cell but I want it and the scroll view to return to its original size on a tap gesture. My tap gesture restores the image to it's original size but it's off the screen and the scroll view remains it's zoomed size. Here is my code:
import UIKit
class ZoomingCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate {
let zoomScrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.backgroundColor = .white
scrollView.isUserInteractionEnabled = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 6.0
scrollView.clipsToBounds = true
return scrollView
}()
var itemImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.backgroundColor = .white
imageView.isUserInteractionEnabled = true
return imageView
}()
var originalSize: CGRect!
override init(frame: CGRect) {
super.init(frame: frame)
contentView.backgroundColor = .white
zoomScrollView.delegate = self
itemImageView.frame = CGRect(x: 0, y: 0, width: contentView.frame.size.width, height: contentView.frame.size.height)
zoomScrollView.frame = itemImageView.frame
originalSize = itemImageView.frame
let tap = UITapGestureRecognizer(target: self, action: #selector(tapZoomingGesture(_:)))
tap.numberOfTapsRequired = 2
zoomScrollView.addGestureRecognizer(tap)
zoomScrollView.addSubview(itemImageView)
contentView.addSubview(zoomScrollView)
}
func viewForZooming(in zoomScrollView: UIScrollView) -> UIView? {
return itemImageView
}
func tapZoomingGesture(_ sender: UIPinchGestureRecognizer) {
itemImageView.center = itemImageView.superview!.center
itemImageView.transform = .identity
zoomScrollView.frame = originalSize
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
This works for me. I just noticed that I used UIPinchGestureRecognizer as the sender but for some reason it works anyway.
func tapZoomingGesture(_ sender: UIPinchGestureRecognizer) {
let newZoomScale: CGFloat = 1.0000001
let scrollViewSize = zoomScrollView.bounds.size
let w = (scrollViewSize.width / newZoomScale)
let h = (scrollViewSize.height / newZoomScale)
let x = w / 6.0
let y = h / 6.0
let rectToZoomTo = CGRect(x, y, w, h)
zoomScrollView.zoom(to: rectToZoomTo, animated: true)
zoomScrollView.frame = itemImageView.frame
itemImageView.center = zoomScrollView.center
}

TapGestureRecogniser not getting called when a subview

I have a view A that handles UITapGestureRecogniser. When it's on its own everything works great.
I have another View B that holds six of the View A objects. When I add View B to my ViewController the UITapGestureRecogniser stops working.
I have isUserInteractionEnabled = true on all the views.
Can anyone spot why it's not working?
How can I check if the tapGestures are being activated upon touch?
Thanks
Note: The ViewController doesn't have any UIGestureRecognisers on it
SingleView
class QTSingleSelectionView: UIView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
fileprivate func initialize() {
let tap = UITapGestureRecognizer(target: self, action: #selector(onTapListener))
addGestureRecognizer(tap)
}
func onTapListener() {
print("tap")
_isSelected.toggle()
setSelection(isSelected: _isSelected, animated: true)
}
}
Multiple views
class QTMutipleChoiceQuestionSelector: UIView {
var selectors: [QTSingleSelectionView] = []
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
fileprivate func initialize() {
for selector in selectors {
selector.removeFromSuperview()
}
selectors.removeAll()
guard let dataSource = dataSource else { return }
let count = dataSource.numberOfAnswers(self)
for index in 0..<count {
let selector = QTSingleSelectionView()
selector.frame = CGRect(x: 0, y: topMargin + heightOfSelector*CGFloat(index), width: frame.width, height: heightOfSelector)
selector.text = dataSource.mutipleChoiceQuestionSelector(self, textForAnswerAtIndex: index)
selector.selected = dataSource.mutipleChoiceQuestionSelector(self, isItemSelectedAtIndex: index)
selector.isUserInteractionEnabled = true
addSubview(selector)
}
}
}
ViewController
lazy var multipleChoiceView: QTMutipleChoiceQuestionSelector = {
let selector = QTMutipleChoiceQuestionSelector()
selector.dataSource = self
selector.isUserInteractionEnabled = true
return selector
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addSubview(multipleChoiceView)
}
I have tested your code. The problem is in line:
let selector = QTMutipleChoiceQuestionSelector() //Wrong!
This line initializes the view with frame (0,0,0,0), that means it cannot receive any touch event, even if you could see it.
The solution is giving the view a size to receive events:
let selector = QTMutipleChoiceQuestionSelector(frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height))
Try This -
Instead of :
for index in 0..<count {
let selector = QTSingleSelectionView()
selector.frame = CGRect(x: 0, y: topMargin + heightOfSelector*CGFloat(index), width: frame.width, height: heightOfSelector)
selector.text = dataSource.mutipleChoiceQuestionSelector(self, textForAnswerAtIndex: index)
selector.selected = dataSource.mutipleChoiceQuestionSelector(self, isItemSelectedAtIndex: index)
selector.isUserInteractionEnabled = true
addSubview(selector)
}
You should use :
for index in 0..<count {
let aFrame: CGRect = CGRect(x: 0, y: topMargin + heightOfSelector*CGFloat(index), width: frame.width, height: heightOfSelector)
let selector = QTSingleSelectionView(frame: aFrame)
selector.text = dataSource.mutipleChoiceQuestionSelector(self, textForAnswerAtIndex: index)
selector.selected = dataSource.mutipleChoiceQuestionSelector(self, isItemSelectedAtIndex: index)
selector.isUserInteractionEnabled = true
addSubview(selector)
}
This will call the appropriate initializer which is init(frame: CGRect) which has the initializer() inside it. But right now your code call's default init() function of the UIView which does not have your custom initialize.

UIscrollView Paging and Zooming images in swift

Having a image array(Download from server) which i want to show in scrollview with paging and zooming enable but i didn't find any solution. I am searching a lot but i didn't get any solution.
Any help is really appreciating.
Thanks!
In MainController
var mainScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
var x:CGFloat = 0
mainScrollView = UIScrollView(frame: view.bounds)
mainScrollView.pagingEnabled = true
let images=["image.png","photo.jpg","3.png"]
for var i = 0; i < images.count; i++
{
let imageView = UIImageView(image: UIImage(named: images[i]))
let scrollView = SlideShow(image: imageView, scrollFrame: CGRectMake(x, view.bounds.origin.y, view.bounds.width, view.bounds.height))
x = x + UIScreen.mainScreen().bounds.size.width
mainScrollView.addSubview(scrollView)
}
mainScrollView.contentSize=CGSizeMake(x, self.mainScrollView.frame.size.height);
mainScrollView.contentOffset=CGPointMake(0, 0);
view.addSubview(mainScrollView)
// Do any additional setup after loading the view.
}
In SubClass
public class SlideShow: UIScrollView, UIScrollViewDelegate {
public var imageView = UIImageView()
// MARK: - Life cycle
init(image: UIImageView, scrollFrame: CGRect) {
super.init(frame: scrollFrame)
//image.setToImageView(imageView)
imageView = image
imageView.clipsToBounds = true
imageView.userInteractionEnabled = true
// UIScrollView(frame: bounds)
delegate = self
showsVerticalScrollIndicator = false
showsHorizontalScrollIndicator = false
backgroundColor = UIColor.blackColor()
// contentSize = imageView.bounds.size
// autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
contentOffset = CGPoint(x: 1000, y: 450)
addSubview(imageView)
// setZoomScale()
setupGestureRecognizer()
}
func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (zoomScale > minimumZoomScale) {
setZoomScale(minimumZoomScale, animated: true)
} else {
setZoomScale(maximumZoomScale, animated: true)
}
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func layoutSubviews() {
setZoomScale()
//setZoomScale(minimumZoomScale, animated: false)
}
public func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
minimumZoomScale = min(widthScale, heightScale)
zoomScale = 1.0
}
public func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
}
Solution 1 :- Agrume Library with load photo url.
There are multiple ways you can use the image viewer (and the included Example project shows them all).
This one is best library for that.
Solution 2 :-
This library provide multiple images and zoom also.

iOS custom view doesn't show image immediately

I made a simple custom view in Swift which has a background image view with a photo and some labels in front. I then added the custom view to my storyboard and it displays well, I can see the background image and labels.
But when I run my app on my device the image view doesn't show immediately, if I navigate to another view then back, it is displayed. I only have a timer which scheduled some tasks in background in my scene. Did I miss anything here?
Here is the code of my custom view
import UIKit
#IBDesignable class GaugeView: UIImageView {
#IBOutlet weak var speedLabel: UILabel!
let circlePathLayer = CAShapeLayer()
var circleRadius: CGFloat = 50.0
var speed: CGFloat {
get {
return circlePathLayer.strokeEnd
}
set {
speedLabel.text = String(format: "%.2f", newValue)
if newValue < 20.0 {
circlePathLayer.strokeColor = UIColor.blueColor().CGColor
} else if newValue >= 25.0 {
circlePathLayer.strokeColor = UIColor.redColor().CGColor
} else {
circlePathLayer.strokeColor = UIColor.yellowColor().CGColor
}
if (newValue > 50) {
circlePathLayer.strokeEnd = 1
} else if (newValue < 0) {
circlePathLayer.strokeEnd = 0
} else {
circlePathLayer.strokeEnd = newValue / 49.9
}
}
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
override func layoutSubviews() {
super.layoutSubviews()
circleRadius = bounds.width / 2 - 2.6
circlePathLayer.frame = bounds
circlePathLayer.path = circlePath().CGPath
}
// Our custom view from the XIB file
var view: UIView!
let nibName = "GaugeView"
func xibSetup() {
view = loadViewFromNib()
// use bounds not frame or it'll be offset
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
// Adding custom subview on top of our view (over any custom drawing > see note below)
addSubview(view)
// Configure speed circle layer
configure()
}
func configure() {
speed = 0.0
circlePathLayer.frame = bounds
circlePathLayer.lineWidth = 6
circlePathLayer.fillColor = UIColor.clearColor().CGColor
circlePathLayer.strokeColor = UIColor.blueColor().CGColor
layer.addSublayer(circlePathLayer)
backgroundColor = UIColor.whiteColor()
}
func circleFrame() -> CGRect {
var circleFrame = CGRect(x: 0, y: 0, width: 2*circleRadius, height: 2*circleRadius)
circleFrame.origin.x = CGRectGetMidX(circlePathLayer.bounds) - CGRectGetMidX(circleFrame)
circleFrame.origin.y = CGRectGetMidY(circlePathLayer.bounds) - CGRectGetMidY(circleFrame)
return circleFrame
}
func circlePath() -> UIBezierPath {
let center: CGPoint = CGPointMake(CGRectGetMidX(circlePathLayer.bounds), CGRectGetMidY(circlePathLayer.bounds))
let start = CGFloat(1.33 * M_PI_2)
let end = CGFloat( 1.64 * M_2_PI)
return UIBezierPath(arcCenter: center, radius: circleRadius, startAngle: start, endAngle: end, clockwise: true)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
}
Then I added the custom view to my storyboard and navigate to the scene programatically with performSegueWithIdentifier("dashboardSegue", sender: nil). I just noticed not only the custome view, but two progress bar on the scene are not displayed as well.
It's better show your code, but i am guessing your timer stuck the main UI thread. When you navigate to another view, your timer.invalidate() was called. Try to remove the NSTimer to see if it works well.

Resources