{"id":1376,"date":"2014-08-16T11:54:26","date_gmt":"2014-08-16T15:54:26","guid":{"rendered":"http:\/\/tim.cstj.qc.ca\/cours\/xcode\/wp\/?page_id=1376"},"modified":"2014-08-16T11:54:26","modified_gmt":"2014-08-16T15:54:26","slug":"uikit-playground","status":"publish","type":"page","link":"https:\/\/ve2cuy.com\/xcode\/uikit-playground\/","title":{"rendered":"UIKit &#8211; playground"},"content":{"rendered":"<p>Le framework &lsquo;UIKit&rsquo; propose des classes pour \u00e9laborer la partie &lsquo;interface utilisateur&rsquo; d&rsquo;une application. \u00a0Par exemple, une sc\u00e8ne (vue), un bouton, une image, du texte, &#8230;<br \/>\nLe nom des classes UIKit d\u00e9bute toujours par &lsquo;UI&rsquo;. \u00a0Par exemple UILabel, UIImage, UIView, UIButton, &#8230;<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<h1>La classe UIView &#8211; pr\u00e9sentation et de regroupement<\/h1>\n<pre class=\"lang:swift decode:true\">var uneVue:UIView = UIView(frame : CGRectMake(0,0,250,250))\nuneVue.backgroundColor = UIColor.purpleColor()\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1381\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.01.png\" alt=\"view.model.01\" width=\"612\" height=\"306\" \/><br \/>\n<a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/windowsviews\/conceptual\/viewpg_iphoneos\/WindowsandViews\/WindowsandViews.html\">Syst\u00e8me de coordonn\u00e9es de UIKit<\/a><br \/>\n&nbsp;<\/p>\n<div class=\"tableholder\" style=\"color: #000000;\">\n<table class=\"graybox\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">\n<caption class=\"tablecaption\">Dimensions des appareils iOS (en points)<\/caption>\n<tbody>\n<tr>\n<th class=\"TableHeading_TableRow_TableCell\" style=\"font-weight: 400; color: #ffffff;\" scope=\"col\">\n<p style=\"font-weight: bold;\">Device<\/p>\n<\/th>\n<th class=\"TableHeading_TableRow_TableCell\" style=\"font-weight: 400; color: #ffffff;\" scope=\"col\">\n<p style=\"font-weight: bold;\">Screen dimensions (in points)<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td scope=\"row\">iPhone et\u00a0iPod touch avec \u00e9cran retina 4 pouces<\/td>\n<td>320 x 568<\/td>\n<\/tr>\n<tr>\n<td scope=\"row\">Autres\u00a0iPhone et iPod<\/td>\n<td>320 x 480<\/td>\n<\/tr>\n<tr>\n<td scope=\"row\">iPad<\/td>\n<td>768 x 1024<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;\n<\/p><\/div>\n<pre class=\"lang:swift decode:true\">var uneAutreVue:UIView = UIView(frame : CGRectMake(10,10,40,40))\nuneAutreVue.backgroundColor = UIColor.greenColor()\nuneVue.addSubview(uneAutreVue)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1382\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.02.png\" alt=\"view.model.02\" width=\"608\" height=\"303\" \/><br \/>\nUIView.frame renseigne sur la position et la dimension de l&rsquo;objet<\/p>\n<pre class=\"lang:swift decode:true\">println(uneAutreVue.frame.origin.x)    \/\/ 10.0\nprintln(uneAutreVue.frame.size.width)  \/\/ 40.0\nprintln(uneVue.center)                 \/\/ (125.0, 125.0)<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:swift decode:true \">uneAutreVue.center = uneVue.center\nuneVue<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1383\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.02b.png\" alt=\"view.model.02b\" width=\"408\" height=\"405\" \/><br \/>\nUne transformation (rotation)<\/p>\n<pre class=\"lang:swift decode:true \">uneAutreVue.transform = CGAffineTransformRotate(uneAutreVue.transform, 3.14 \/ 4 \/* angle en radian *\/)\nuneVue\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1388\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.02b2.png\" alt=\"view.model.02b2\" width=\"360\" height=\"303\" \/><br \/>\n&nbsp;<br \/>\nLes coordonn\u00e9es absolues d&rsquo;une Vue:<\/p>\n<pre class=\"lang:swift decode:true \">println(uneAutreVue.bounds)  \/\/ (0.0, 0.0, 40.0, 40.0)\n<\/pre>\n<p>Ajouter un objet &lsquo;UILabel&rsquo;<\/p>\n<pre class=\"lang:swift decode:true \">var unTitre = UILabel(frame: CGRectMake(10,0,uneVue.bounds.width - 10, 25))\nunTitre.text = \"Bonjour TIM\"\nunTitre.textColor = UIColor.whiteColor()\nunTitre.textAlignment = NSTextAlignment.Center\nuneVue.addSubview(unTitre)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1384\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.02c.png\" alt=\"view.model.02c\" width=\"405\" height=\"305\" \/><br \/>\n&nbsp;<\/p>\n<pre class=\"lang:swift decode:true\">let largeur:CGFloat = 20, hauteur:CGFloat = 20, marge:CGFloat = 10\nfor ligne in 0...7 {\n    for colonne in 0...7 {\n        \/\/ G\u00e9n\u00e9rer une valeur rgb al\u00e9atoire\n        \/\/ arc4random_uniform(n) retourne une valeur al\u00e9atoire entre 0 et n-1\n        let rouge = CGFloat(arc4random_uniform(256)) \/ 255\n        let vert  = CGFloat(arc4random_uniform(256)) \/ 255\n        let bleu  = CGFloat(arc4random_uniform(256)) \/ 255\n        var carre = UIView(frame: CGRectMake(CGFloat(colonne) * (largeur + marge) + marge, CGFloat(ligne) * (hauteur + marge)  + marge, largeur, hauteur))\n        carre.backgroundColor = UIColor(red: rouge, green: vert, blue: bleu, alpha: 1)\n        uneVue.addSubview(carre)\n    } \/\/ colonne\n} \/\/ ligne\nuneVue<\/pre>\n<p>Un peu de normalisation<\/p>\n<pre class=\"lang:swift decode:true \">let largeur:CGFloat = 20, hauteur:CGFloat = 20, marge:CGFloat = 10\nlet lignes = 7, colonnes = 7\nfunc couleurHasard() -&gt; CGFloat {\n  return CGFloat(arc4random_uniform(256)) \/ 255\n}\nfor ligne in 0...lignes {\n    for colonne in 0...colonnes {\n        \/\/ G\u00e9n\u00e9rer une valeur rgb al\u00e9atoire\n        \/\/ arc4random_uniform(n) retourne une valeur al\u00e9atoire entre 0 et n-1\n        let rouge = couleurHasard()\n        let vert  = couleurHasard()\n        let bleu  = couleurHasard()\n        var carre = UIView(frame: CGRectMake(CGFloat(colonne) * (largeur + marge) + marge, CGFloat(ligne) * (hauteur + marge)  + marge, largeur, hauteur))\n        carre.backgroundColor = UIColor(red: rouge, green: vert, blue: bleu, alpha: 1)\n        uneVue.addSubview(carre)\n    } \/\/ colonne\n} \/\/ ligne\nuneVue<\/pre>\n<p>&nbsp;<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1377\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.03.png\" alt=\"view.model.03\" width=\"607\" height=\"607\" \/><br \/>\nAfficher une image (UIImageView + UIImage)<br \/>\nNote: Pour qu&rsquo;Xcode puisse localiser les images, Il faut renseigner le dossier des Ressources du projet &lsquo;playground&rsquo;<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1390\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.04a.png\" alt=\"view.model.04a\" width=\"509\" height=\"297\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1393\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.04b.png\" alt=\"view.model.04b\" width=\"261\" height=\"409\" \/>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1392\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.04c.png\" alt=\"view.model.04c\" width=\"509\" height=\"79\" \/><br \/>\nNote: Il faudra peut-\u00eatre red\u00e9marrer le projet apr\u00e8s cette op\u00e9ration.<br \/>\n&nbsp;<\/p>\n<pre class=\"lang:swift decode:true \">var uneImage = UIImageView(image: UIImage(named:\"tintin.jpg\"))\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1395\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.04d.png\" alt=\"view.model.04d\" width=\"463\" height=\"341\" \/><br \/>\nAjouter l&rsquo;image \u00e0 une vue<\/p>\n<pre class=\"lang:swift decode:true \">var uneScene = UIView(frame : CGRectMake(0,0,320,568))\n    uneScene.backgroundColor = UIColor.lightGrayColor()\nuneScene.addSubview(uneImage)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1396\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.05a.png\" alt=\"view.model.05a\" width=\"371\" height=\"356\" \/><br \/>\nContr\u00f4ler la taille et la position de l&rsquo;image dans la vue.<\/p>\n<pre class=\"lang:swift decode:true \">var uneAutreImage = UIImageView(frame: CGRectMake(0,0,150 ,150))\nuneAutreImage.image = UIImage(named:\"tintin.jpg\")\nuneAutreImage.center = uneScene.center\nuneScene.addSubview(uneAutreImage)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1397\" src=\"\/xcode\/wp-content\/uploads\/2014\/08\/view.model_.05b.png\" alt=\"view.model.05b\" width=\"370\" height=\"350\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Le framework &lsquo;UIKit&rsquo; propose des classes pour \u00e9laborer la partie &lsquo;interface utilisateur&rsquo; d&rsquo;une application. \u00a0Par exemple, une sc\u00e8ne (vue), un bouton, une image, du texte, &#8230; Le nom des classes UIKit d\u00e9bute toujours par &lsquo;UI&rsquo;. \u00a0Par exemple UILabel, UIImage, UIView, UIButton, &#8230; &nbsp; La classe UIView &#8211; pr\u00e9sentation et de regroupement var uneVue:UIView = UIView(frame [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1376","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ve2cuy.com\/xcode\/wp-json\/wp\/v2\/pages\/1376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ve2cuy.com\/xcode\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ve2cuy.com\/xcode\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/xcode\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/xcode\/wp-json\/wp\/v2\/comments?post=1376"}],"version-history":[{"count":0,"href":"https:\/\/ve2cuy.com\/xcode\/wp-json\/wp\/v2\/pages\/1376\/revisions"}],"wp:attachment":[{"href":"https:\/\/ve2cuy.com\/xcode\/wp-json\/wp\/v2\/media?parent=1376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}