9 Notes for me in Desember Swift 4.2

Note:  December-2018

1. Set image opacity in programmatically

-- imageView.alpha = 0.5 // 50% opacity
-- imageView.alpha = 0 // 0% opacity - completely transparent
-- imageView.alpha = 1 // 100% opacity - completely opaque

2. Time Stamp / Date Format 

Use GMT format in a format date

-- yyyy-MM-dd'T'HH:mm:ssZ  =  2018-11-23T04:02:11+0700
--yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX = 2018-11-22T15:39:59.568+07:00

3. Asynchronous use time after

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {

        }

4. Play video in UIImageView 
 use imageView as player video :

* imageView.layer.insertSublayer(avPlayerLayer, at: 0)


import AVFoundation
import AVKit
import UIKit

class AutoplayViewController: UIViewController {

    @IBOutlet weak var imageview: UIImageView!
    @IBOutlet weak var viewView: UIView!

    override func viewDidLoad() {

        super.viewDidLoad()

        // Video

        let url = URL(string: "https://Video_20181203_102048_.mp4")!
        let avPlayer = AVPlayer(playerItem: AVPlayerItem(url: url))
        let avPlayerLayer = AVPlayerLayer(player: avPlayer)
        avPlayerLayer.frame = viewView.bounds
        imageview.layer.insertSublayer(avPlayerLayer, at: 0)
        avPlayer.play()
    }
}

Or can use this library -> https://github.com/ashish0309/AutoVideoPlayer/tree/master/videoplayer
 
for autoplay video



5.  D
ifference viewWillAppear & viewDidAppear

 Whenever You push a view controller, It will call viewDidLoad then viewWillAppear and lastviewDidAppear. But if you are coming back to viewController only viewWillAppear and viewDidAppear will call.



enter image description here







6.  Process scroll view if the condition did end.

--  here is one of the option available which is fairly simple

import UIKit

class ContentOffsetViewController: UIViewController, UIScrollViewDelegate {
    @IBOutlet weak var scrollViewR: UIScrollView!
    @IBOutlet weak var labelScroll: UILabel!
    var imageArray = [UIImage]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollViewR.frame = view.frame
        scrollViewR.delegate = self
        labelScroll.text = "Naruto"
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let pageWidth: CGFloat = scrollViewR.frame.width
        let currentPage: CGFloat = floor((scrollViewR.contentOffset.x - pageWidth / 2) / pageWidth) + 1
        
        // Change the text accordingly
        if Int(currentPage) == 0 {
            labelScroll.text = "Boruto"
        } else {
            labelScroll.text = "Other character"
        }
    }
}


7. Stashing (Git)
Stashing your work to demonstrate, you’ll go into your project and start working on a couple of files and possibly stage one of the changes. If you run git status, you can see your dirty stat. use in your terminal.

$ git stash  //saves it on a stack of unfinished changes that you can reapply at any time

$ git stash apply // call your data on a stack


8. How to use "didSet" & "willSet"

 With willSet and didSet, you can take action when the value is modified without needing another field. For instance

var person: Person? {
        didSet{

            print("Called after setting the new value")
            if let name = person?.name {
                print("New name is \(name) and old name is \(String(describing: oldValue?.name))")
            }
        }


9. Use thumbnail if format video.webp (use library Nuke)

-- This  example how use thumbnail video if you use library image Nuke, can use like code below
-- link: https://github.com/kean/Nuke

// in view did load
if let url = URL(string: "yourVideoUrl.webp") {
         self.setImageUrl(with: url, self.postImageView)  
  }

// in function
  func setImageUrl(with url: URL, _ imageView: UIImageView) {
        DispatchQueue.main.async {
            let request = self.makeRequest(with: url)
            var options = ImageLoadingOptions(transition: .fadeIn(duration: 0.25))
            options.pipeline = self.pipeline
            Nuke.loadImage(with: request, options: options, into: imageView)
        }
    }


Thanks for :

http://nsdateformatter.com/
https://stackoverflow.com/questions/43768201/change-image-opacity-when-touched-swift-3-xcode
https://stackoverflow.com/questions/37651259/viewdidappear-and-viewwillappear-is-being-called-multiple-times-on-one-viewdidlo/37651624

https://stackoverflow.com/questions/46930402/change-label-text-while-image-in-uiscrollview-changes
https://git-scm.com/book/en/v1/Git-Tools-Stashing

https://medium.com/the-andela-way/property-observers-didset-and-willset-in-swift-4-c3730f26b1e9



No comments: