Created
May 22, 2018 20:27
-
-
Save dotWasim/21bfac6d0e125d106b4b10f18cc901c4 to your computer and use it in GitHub Desktop.
Crop and shadow a view by adding a new layer
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
// | |
// ViewController.swift | |
// ViewCropAndShadow | |
// | |
// Created by Wasim Alatrash on 5/22/18. | |
// Copyright © 2018 Wasim Alatrash. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var LogoImageView: UIImageView! | |
var tmpImage : UIImage = #imageLiteral(resourceName: "logo") | |
let imageLayer = CALayer() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
LogoImageView.layer.addSublayer(imageLayer) | |
} | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
// add cornerRadius to the added layer | |
imageLayer.frame = LogoImageView.bounds | |
imageLayer.contents = tmpImage.cgImage | |
imageLayer.cornerRadius = 20 | |
imageLayer.masksToBounds = true | |
// add shadow to main layer | |
LogoImageView.layer.shadowColor = UIColor.black.cgColor | |
LogoImageView.layer.shadowOffset = CGSize(width: 4, height: 4) | |
LogoImageView.layer.shadowRadius = 6 | |
LogoImageView.layer.cornerRadius = 20 | |
LogoImageView.backgroundColor = UIColor.lightGray | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment