Thursday, July 28, 2016

HTML, CSS - Center Vertically (SLIDE MENU)

HTML:
CSS:
div.container {
    position: relative ;
    width: 250px;
    height: 250px;
    background-color: green;

 }
 
div.left {
    margin: 0;
    background: black;
    opacity: 0.65;
    position: absolute;
    top: 50%;
    transform: translate(-0%, -50%);
   height: 61px;
   width: 38px;
   left: 0px;
}
   
 div.right {
    margin: 0;
    background: black;
    opacity: 0.65;
    position: absolute;
    top: 50%;
    transform: translate(-0%, -50%);
   height: 61px;
   width: 38px;
   right: 0px;
}
   
(jsFiddle demo here)
Credit:
* Centering Things
Read More

Tuesday, July 26, 2016

Swift Request for Authorization

* Photos
func requestForAccessPhotos(view : UIViewController, completionHandler: (accessGranted: Bool) -> Void){
        let authorizationStatus = PHPhotoLibrary.authorizationStatus()
        
        switch authorizationStatus {
        case .Authorized:
            completionHandler(accessGranted: true)
        case .Denied, .NotDetermined:
            PHPhotoLibrary.requestAuthorization() {(status) -> Void in
                switch status {
                case .Authorized:
                    completionHandler(accessGranted: true)
                case .Denied, .Restricted:
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        Handler().permissionAlert(view, title: "Access Denied", message: "Please allow the app to access your photos through the Settings.")
                    })
                default:
                    completionHandler(accessGranted: false)
                }
            }
        default:
            completionHandler(accessGranted: false)
        }
    }


* Contacts
func requestForAccessContacts(view : UIViewController, completionHandler: (accessGranted: Bool) -> Void) {
        let authorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts)
        
        switch authorizationStatus {
        case .Authorized:
            completionHandler(accessGranted: true)
            
        case .Denied, .NotDetermined:
             contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (access, accessError) -> Void in
                if access {
                    completionHandler(accessGranted: access)
                }
                else {
                    if authorizationStatus == CNAuthorizationStatus.Denied {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            Handler().permissionAlert(view, title: "Access Denied", message: "Please allow the app to access your contacts through the Settings.")
                        })
                    }
                }
            })
            
        default:
            completionHandler(accessGranted: false)
        }
    }


* Photos
func requestForAccessCalendar(view : UIViewController, send : UISwitch, completionHandler : (accessGranted : Bool) -> Void){
        let authorizationStatus = EKEventStore.authorizationStatusForEntityType(EKEntityType.Event)
        
        switch authorizationStatus {
        case .Authorized:
            completionHandler(accessGranted: true)
        case .Denied, .NotDetermined:
            let eventStore = EKEventStore()
            eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (access, accessError) -> Void in
                if access {
                    completionHandler(accessGranted: access)
                }
                else {
                    if authorizationStatus == EKAuthorizationStatus.Denied {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                             Handler().permissionAlert(view, title: "Access Denied", message: "Please allow the app to access your calendar through the Settings.")
                            send.setOn(false, animated:true)
                        })
                    }
                }
            })
            
        default:
             completionHandler(accessGranted: false)
        }
        
    }


* Camera
func requestForAVCature(view : UIViewController, completionHandler : (accessGranted : Bool) -> Void){
        let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
      
        switch authorizationStatus {
        case .Authorized:
            completionHandler(accessGranted: true)
        case .Denied, .NotDetermined:
            AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted : Bool) -> Void in
                if(granted){ // Access has been granted ..do something
                    completionHandler(accessGranted: true)
                } else { // Access denied ..do something
                    if authorizationStatus == AVAuthorizationStatus.Denied {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            Handler().permissionAlert(view, title: "Access Denied", message: "Please allow the app to access your calendar through the Settings.")
                        })
                    }
                }
            })

        default:
            completionHandler(accessGranted: false)
        }
        
    }


* Inside the 'Handler' class
func permissionAlert(view: UIViewController, title : String?, message : String?){
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
        
        view.presentViewController(alertController, animated: true, completion: nil)
    }
Read More

Monday, July 25, 2016

Swift Infix Custom Threading

Code:
func ~> (task: ()->Void, update: ()-> Void)
{
    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {
        // do some task
        task()
        dispatch_async(dispatch_get_main_queue()) {
            // update some UI
            update()
        }
    }
}

Usage:
 ({
     for i in 0..<100000{
            print("count: \(i)")
         }
     })~>({
         print("Finished counting")
 })

CREDITS:
* Background Threads
* Custom Operators in Swift

Read More