Friday, 27 November 2015

IBInspectable and IBDesignable in action

Apple has introduced an awesome feature called IBInspectable and IBDesignable with xCode6, but few of people is using this a productive and awesome feature.

Let's start!

  • IBInspectable: A property that provides access of user defined runtime attributes, so now you can update all user defined runtime attributes from "Attribute Inspector".
    - By adding IBInspectable property in your file you can see that in Interface builder instantly.

  • IBDesignable: This will notify the interface builder to render view in canvas live, no need to run and check borderWidth,cornerRadius etc.

  1. IBInspectable,
     - Currently these types are supported as IBInspectable.

  • CGFloat
  • CGRect
  • CGPoint
  • CGSize
  • UIImage
  • UIColor
  • Int
  • Bool
  • Double
  • String

    2. IBDesignable,

    -  In Swift adding @IBDesignable keyword before class declaration does everything to render view live on Interface Builder,


Use it:
  • Create UIView subclass and add this below code there.
CustomView.swift

import UIKit

@IBDesignable
class CustomView: UIView {
    
@IBInspectable var cornerRadius: CGFloat = 0{
    didSet{
       layer.cornerRadius = cornerRadius
       layer.masksToBounds = cornerRadius > 0
     }
}

@IBInspectable var borderWidth: CGFloat = 0{
       didSet{
       layer.borderWidth = borderWidth
     }
}

@IBInspectable var borderColor: UIColor?{
       didSet{
       layer.borderColor = borderColor!.CGColor
     }
}

@IBInspectable var layerBackgroundColor: UIColor?{
        didSet{
       layer.backgroundColor = layerBackgroundColor?.CGColor
     }
   }
}


  • Here didSet is Swift property observer and called whenever a value changes.
  • Now, go to your storyboard or xib and add one UIView object
  • Then go to file inspector of UIView and add class name "CustomView"
  • Now go to Attribute inspector and see IBInspectable and IBDesignable in action.


Monday, 31 August 2015

Getting Specialised knowledge on Apple Watch in just 10 minutes

Welcome,

Here is an another tremendous yet elegant 10 minutes note for you.

 AppleWatch:
- Technology has been changed from a room sized computer to a small wrist watch, have you ever think why ?, the answer is because everyone has mind set of looking different than others, to prove that they are getting updated every day.

- After completing this note you will surely know every core properties of AppleWatch as an AppleWatch developer.

Let's jump into the ocean of the musk, Apple Watch accessible components for developers.

 At Glance:
These are few main components that you use while development including hardware and softwares.

1. Digital Crown
2. Taptic Engine
3. Heart Rate
4. Glance
5. Notifications
6. Animation with image sequences
7. Communication with iPhone app
and list is growing...


Quick look:

1. Digital Crown:

- It is a new way to interact with digital watch, users can scroll their long lists and menus with this easily accessible components, even a smart developer can make game like CrossyRoad that can be played using digital crown, how smart is this - isn't it?

2. Taptic Engine:

- A smart developer can use this taptic engine's haptic feedback which causes a small vibrate on users wrist so as a notification.
- There is few lines of code to do that.

3. Heart Rate:

- One of my favourite AppleWatch feature that is extremely easy to access as developers.
- HKWorkout explains it efficiently, just do few lines of code and get heart beats in your app.
- A smart iOS game developer can measure user's heart rate while user play his game and show that after level ends, pretty cool-isn't it?

4. Glance:

- Glance is a quick and informative screen, same as iPhone's Launch screen but with some updated informations.
- Glance has one and only action which is Tap, if user tap on Glance it will open the app, else the watch go to sleeping mode.

5. Notifications:


- A beautiful  Apple Watch supports both push and local notifications.
- Whichever your device is unlocked will show notification - either iPhone or AppleWatch.

6. Animation with image sequences:

- Every best apps are the best because of their UI and Animations.
- Creating apps with animation is the essential part of every developers.
- You need to have image sequences and play them to show animation, here is few lines of code.
- Currently animation is only supported with image sequences. a sad news for UIView.animate block users.


7. Communication with iPhone app:


- Yes, every Watch app must have iPhone app named containing app.
- You can communicate between both of them, for example get heart rate on your iPhone or send WatchGame data on iPhone, or iPhone battery indicator on Watch.

Thanks,
Zaid


Tuesday, 23 June 2015

How to create Popup view in iOS using AutoLayout and Swift with animation.

Welcome to this new Tutorial,

Aim:- Creating a popup view just feel like floating over any screen using Autolayout and Swift with animation.

Result:- After following this tutorial successfully you will easily create a popup view using Autolayout and Swift, which will look like this.



Let's start:-

1. Create xCode project.
2.Open your Main.storyboard file, and drag-drop a button in it.
3.Create IBAction of that button to your view controller file.
4.Popup view configuration

- The popup view will have gray background color and close button

Replace your view controller with this code:
       

//
//  ViewController.swift
//  CustomPopupView
//
//  Created by Zaid Pathan on 20/04/15.
//  Copyright (c) 2015 ZaidPathan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

 var grayBackgroundView = UIView()
 var popupView:PopupView?
 var popupViewDic:[String:PopupView] = [:]
 
 var constX:NSLayoutConstraint?
 var constY:NSLayoutConstraint?
 
 override func viewDidLoad() {
  super.viewDidLoad()
 }
 

 override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
 }
 
 func showPopupView(){
  
  //Show GrayView Behind popup view
  self.showGrayBGView(self, grayView: grayBackgroundView)
  
  //Load nib, and get reference to variable 'popupView'.
  popupView = NSBundle.mainBundle().loadNibNamed("PopupView", owner: self, options: nil)[0] as? PopupView
  self.view.addSubview(popupView!)
  
  //Configure popupview
  popupView?.frame.size = CGSizeMake(0, 0)
  popupView?.center = self.view.center
  popupView?.alpha = 0.5
  popupView?.clipsToBounds = false
  
  
  //Autolayout part
  popupViewDic["popupView"] = popupView
  
  var dView:[String:UIView] = [:]
  dView["popupView"] = popupView
  
  popupView?.setTranslatesAutoresizingMaskIntoConstraints(false)
  
  var h_Pin = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(36)-[popupView]-(36)-|", options: nil, metrics: nil, views: dView)
  self.view.addConstraints(h_Pin)
  
  var v_Pin = NSLayoutConstraint.constraintsWithVisualFormat("V:|-(36)-[popupView]-(36)-|", options: nil, metrics: nil, views: dView)
  self.view.addConstraints(v_Pin)
  
  constY = NSLayoutConstraint(item: popupView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
  self.view.addConstraint(constY!)
  
  
  constX = NSLayoutConstraint(item: popupView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
  self.view.addConstraint(constX!)
  
  UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.50, options: UIViewAnimationOptions.LayoutSubviews, animations: { () -> Void in
   self.popupView?.alpha = 1
   self.view.layoutIfNeeded()
   }) { (value:Bool) -> Void in
    
  }
  
 }

 
 //Gray bg view configuration
 func showGrayBGView(viewController:UIViewController,grayView:UIView){
  
  var viewController:UIViewController = viewController
  var GrayView:UIView = grayView
  
  viewController.view.addSubview(GrayView)
  
  var dView:[String:UIView] = [:]
  dView["GrayView"] = GrayView
  
  GrayView.frame = viewController.view.frame
  GrayView.backgroundColor = UIColor.clearColor()
  GrayView.setTranslatesAutoresizingMaskIntoConstraints(false)
  UIView.animateWithDuration(0.5, animations: {
   GrayView.backgroundColor = UIColor.blackColor()
   GrayView.alpha = 0.75
  })
  
  var h_Pin = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(-16)-[GrayView]-(-16)-|", options: nil, metrics: nil, views: dView)
  viewController.view.addConstraints(h_Pin)
  
  var v_Pin = NSLayoutConstraint.constraintsWithVisualFormat("V:|-(-16)-[GrayView]-(-16)-|", options: nil, metrics: nil, views: dView)
  viewController.view.addConstraints(v_Pin)
  
 }
 
 func hidePopupView(){
  
  UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.50, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
   
   self.grayBackgroundView.alpha = 0
   self.popupView!.alpha = 0
   
   }) { (value:Bool) -> Void in
    self.popupView!.removeFromSuperview()
    self.grayBackgroundView.removeFromSuperview()
    
  }
  
 }
 
 
 @IBAction func IBActionShowPopupView(sender: UIButton) {
  self.showPopupView()
 }
 
 
 @IBAction func IBActionClose(sender: UIButton) {
  
  hidePopupView()
  
 }
}



       
 


5.Connect your button with IBActionShowPopupView.
6.Create a UIView subclass and a xib with that subclass.
7.Give FileOwner name as ViewController.
8.Add button to popupview and connect that to IBActionClose in ViewController.
9.Run app
10. Press on button you added in ViewController.

Wow!
You will see popup view will appear when pressing button.

11.Now click on button you added in popup xib,
Wow!
It disappeared.

Download sample project here: Download