Tuesday, 29 August 2017

Thursday, 3 August 2017

Longpress event in swift 3



override func viewDidLoad() {
        super.viewDidLoad() 
var longPressGesture : UILongPressGestureRecognizer!
 longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(MainView.handleLongGesture))
        self.view?.addGestureRecognizer(longPressGesture)

  }


    func handleLongGesture() {
        
        print("long press")
       // do Actions 
        }
    }

Wednesday, 2 August 2017

Solve Gradient covers the background view in swift 3



Apply Gradient to View

class Testing: UIViewController {
    
   
    override func viewDidLoad() {
        super.viewDidLoad()
       setGradientBackground()
    }
    
    func setGradientBackground() {
        let colorTop =  UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
        let colorBottom = UIColor(red: 255.0/255.0, green: 94.0/255.0, blue: 58.0/255.0, alpha: 1.0).cgColor
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [ colorTop, colorBottom]
        gradientLayer.locations = [ 0.0, 1.0]
        gradientLayer.frame = self.view.bounds
        
        self.view.layer.addSublayer(gradientLayer) // --> This will hides the background view

    }



-->Some times it will apply full view,  better to apply gradient in viewDidLoad()

replace self.view.layer.addSublayer(gradientLayer) with 
self.view.layer.insertSublayer(gradientLayer, at: 0), this will put the layer "below" all others 

--------------------------------------------------------------------------------------------------------------


background gradient to fill Landscape mode

-> some times gradient view will not cover total view when rotate screen .


we can solve this one to add below code


override func willAnimateRotation(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
        self.view.layer.sublayers?.first?.frame = self.view.bounds
}


        
   

Monday, 31 July 2017

Sliding with fade in fade out of label in swift 3

class Testing: UIViewController {
    
    
    @IBOutlet weak var slidingView: UIView!
    
    @IBOutlet weak var sliding_label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
       sliding_label.layer.cornerRadius=10
        sliding_label.layer.masksToBounds = true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.sliding_label.slideInFromLeft()
        //      self.slidingTextLabel.slideInFromLeft(duration: 1.0, completionDelegate: self) // Use this line to specify a duration or completionDelegate
        self.sliding_label.text = "  "+"Sliding Registration and select location"+"  "
        self.sliding_label.alpha = 0
        self.sliding_label.fadeIn(completion: {
            (finished: Bool) -> Void in
            self.sliding_label.fadeOut()
        })
        self.sliding_label.isHidden = false
        DispatchQueue.main.asyncAfter(deadline: .now() + 7) {
            self.sliding_label.fadeOut()
        }

    }
    


}

-------------------------------------------------------------------------------------------------

extensions:

extension UIView {
    // Name this function in a way that makes sense to you...
    // slideFromLeft, slideRight, slideLeftToRight, etc. are great alternative names
    func slideInFromLeft(duration: TimeInterval = 1.0, completionDelegate: AnyObject? = nil) {
        // Create a CATransition animation
        let slideInFromLeftTransition = CATransition()
        
        // Set its callback delegate to the completionDelegate that was provided (if any)
        if let delegate: AnyObject = completionDelegate {
            slideInFromLeftTransition.delegate = delegate as? CAAnimationDelegate
        }
        
        // Customize the animation's properties
        slideInFromLeftTransition.type = kCATransitionPush
        slideInFromLeftTransition.subtype = kCATransitionFromLeft
        slideInFromLeftTransition.duration = duration
        slideInFromLeftTransition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        slideInFromLeftTransition.fillMode = kCAFillModeRemoved
        
        // Add the animation to the View's layer
        self.layer.add(slideInFromLeftTransition, forKey: "slideInFromLeftTransition")
    }
    
    func fadeIn(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
        UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
            self.alpha = 1.0
        }, completion: completion)  }
    
    func fadeOut(duration: TimeInterval = 1.0, delay: TimeInterval = 3.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
        UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
            self.alpha = 0.0
        }, completion: completion)
    }
}


Sunday, 30 July 2017

set Image in circlein swift 3

   override func viewDidLayoutSubviews() {
        
        imageView.layer.cornerRadius = imageView.frame.size.height/2
        imageView.clipsToBounds = true 
        

    }

Friday, 28 July 2017

How to underline a UILabel and UIButton programatically in swift 3



For Label:labelname.attributedText = NSAttributedString(string: "text", attributes:            [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])

o/p: text




For Button: let linkbuttonAttributes : [String: Any] = [NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue] let attributeString = NSMutableAttributedString(string: "Terms and Conditions",                                                    attributes: linkbuttonAttributes) termsofServices.setAttributedTitle(attributeString, for: .normal)

o/p: Terms and Conditions

Thursday, 27 July 2017

change Navigation bar title colour programatically in swift 3

override func viewDidLoad() {        super.viewDidLoad()        let nav = self.navigationController?.navigationBar        self.title="Address Details"        nav?.tintColor = UIColor.white            }


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let nav = self.navigationController?.navigationBar
        nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    }

Remove bottom line in navigation bar

navigationController ?. navigationBar . setBackgroundImage ( UIImage (), for: . any , barMetrics: . default )          navigat...