Programming on iOS

 

[iOS, iPadOS]How to use Core Data with CloudKit

Core Data and CloudKit can now be linked automatically from iOS 13 and iPadOS 13, and I tried out immediately.

 

Environment: swift 5, Xcode 11.0

 

Preparation

1.Check both [Use Core Data] and [Use CloudKit] when creating a project

Read More

[iOS]Sending UIView to Back or Front

[日本語]

 

I will introduce how to change stacking order of UIView as below picture.

 

Environment : Xcode 8.3, Swift 3

 

In the case of iOS, use sendSubView(toBack:) for sending UIView to back and use bringSubView(toFront:) for sending UIView to front.

 

Example

import UIKit

 

class ViewController: UIViewController {

  let label1:UILabel = UILabel();

  let label2:UILabel = UILabel();

  let button1:UIButton = UIButton();

  let button2:UIButton = UIButton();

 

  override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    label1.frame = CGRect(x: 0, y: 0, width: 300, height: 300)

    label1.text = "Label1"

    label1.backgroundColor = UIColor.blue self.view.addSubview(label1)

 

    label2.frame = CGRect(x: 50, y: 50, width: 300, height: 300)

    label2.text = "Label2"

    label2.backgroundColor = UIColor.yellow

    self.view.addSubview(label2)

 

    button1.frame = CGRect(x: 0, y: 400, width: 200, height: 50)

    button1.backgroundColor = UIColor.blue

    button1.setTitle("Send Label1 to Back", for: .normal)

    button1.addTarget(self, action: #selector(self.touchUpButton1), for: .touchUpInside)

    self.view.addSubview(button1)

 

    button2.frame = CGRect(x: 210, y: 400, width: 200, height: 50)

    button2.backgroundColor = UIColor.red

    button2.setTitle("Send Label1 to Front", for: .normal)

    button2.addTarget(self, action: #selector(self.touchUpButton2), for: .touchUpInside) self.view.addSubview(button2)

  }

 

  override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

  }

 

  func touchUpButton1(){

   //To Back

   self.view.sendSubview(toBack: label1)

  }

 

  func touchUpButton2(){

   //To Front self.view.bringSubview(toFront: label1)

  }

}

[iOS]Color gradation and rounding corners

[日本語]

 

I will introduce how to set gradation on a background color of View and round corners of View on iOS.

 

Environment:Xcode 8, Swift 3

 

Use CAGradientLayer to set color gradation to UIView on iOS.

Set gradient colors to CAGradientLayer's colors method and set gradation direction using startPoint and endPoint of CAGradientLayer.

At last, set CAGradientLayer to UIView's layer using insertSublayer.

let buttonSample:UIButton = UIButton()

let gradientLayer = CAGradientLayer()

gradientLayer.frame = buttonSample.bounds

let color1:CGColor = UIColor.blue.cgColor

let color2:CGColor = UIColor.white.cgColor

gradientLayer.colors = [color1, color2]

gradientLayer.startPoint = CGPoint(x: 0, y: 0)

gradientLayer.endPoint = CGPoint(x: 1, y: 1)

buttonSample.layer.insertSublayer(gradientLayer, at: 0)

 

Use cornerRadius of UIView's CALayer for rounding corners of UIView.

Read More

[iOS]How to capture screen

[日本語]

 

I introduce how to capture screen and make a bitmap image on iOS this time.

 

Environment:Xcode 8.3, Swift 3

 

Sample code is below.

import UIKit

 

class ViewController: UIViewController {

  let label1:UILabel = UILabel()

  let imageView1:UIImageView = UIImageView()

  let button1:UIButton = UIButton()

 

  override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    label1.frame = CGRect(x: 10, y: 20, width: 100, height: 100)

    label1.text = "Label1"

    label1.backgroundColor = UIColor.blue

    self.view.addSubview(label1)

 

    imageView1.frame = CGRect(x: 10, y: 125, width: 300, height: 300)

    imageView1.layer.borderWidth = 0.5

    imageView1.contentMode = .scaleAspectFit

    self.view.addSubview(imageView1)

 

    button1.frame = CGRect(x: 10, y: 430, width: 100, height: 50)

    button1.backgroundColor = UIColor.blue

    button1.layer.cornerRadius = 5

    button1.setTitle("Button1", for: .normal)

    button1.addTarget(self, action: #selector(self.touchUpButton1), for: .touchUpInside)

    self.view.addSubview(button1)

  }

 

  override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

  }

 

  func touchUpButton1(){

    //Get context

    let rect = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)

    let context: CGContext = UIGraphicsGetCurrentContext()!

 

    //Write screen's objects' info at context

    self.view.layer.render(in: context)

 

    //Get UIImage

    let capturedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!

 

    //Close context

    UIGraphicsEndImageContext()

    imageView1.image = capturedImage

  }

}

At first, use UIGraphicsBeginImageContextWithOptions, and make context of bitmap for capturing screen.

And write screen's objects' info at context.("self.view.layer.render(in:context)")

 

And use UIGraphicsGetImageFromCurrentImageContext, and get UIImage from context.

At last, use UIGraphicsEndImageContext, and close context.

[iOS]Core Data Programming

[日本語]

 

You can do database programming by Core Data, O/R mapping tool for Sqlite, on iOS.

 

Environment: Xcode 8.3, Swift 3

 

1.Preparation

At first, Check "Use Core Data" when project is created.

And code of Core Data Objects are created in AppDelegate.

 

Next, select "[project name].xcdatamodeld" and define table layout.

 

To save UIImage, select "Binary Data" in Type and check "Allow External Storage". 

Read More