{"id":28519,"date":"2022-12-31T13:31:26","date_gmt":"2022-12-31T08:01:26","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/"},"modified":"2022-12-31T13:31:26","modified_gmt":"2022-12-31T08:01:26","slug":"solved-create-a-custom-gauge-with-an-image-objective-c-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/","title":{"rendered":"[Solved] Create a custom gauge with an image Objective C [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-29680078\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"29680078\" data-parentid=\"29679410\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>One of the many solutions, given the fuzziness of the question, is to use Core Animation. The official Apple documentation is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Cocoa\/Conceptual\/CoreAnimation_guide\/Introduction\/Introduction.html\">here<\/a>.<\/p>\n<p>Let&#8217;s outline three steps:<\/p>\n<ul>\n<li><em>Conceptual step<\/em> Define what layers (conceptually) or masks you will use, and create the images that will be imported in them<\/li>\n<li><em>Definition step<\/em> Define your <code>CABasicAnimation<\/code> and program the layers<\/li>\n<li><em>Animation step<\/em> Apply your modifications <\/li>\n<\/ul>\n<p>Let&#8217;s go through the details.<\/p>\n<p><strong><em>Conceptual step<\/em><\/strong><\/p>\n<p>Let&#8217;s assume you choose to have three layers:<\/p>\n<ul>\n<li>background<\/li>\n<li>empty heart shape (for example, a nice silver line, or maybe just nothing)<\/li>\n<li>filled heart shape (for example, a nice gem cut in the shape of a heart, or jelly, or an Apple-red-skin&#8230; ask your artist)<\/li>\n<\/ul>\n<p>At this point, your artist have provided you with 6 files if you&#8217;re building for iPad. Let&#8217;s assume the following names and types.<\/p>\n<p><code>| Name | Type|<br \/>\n|:-----:|:\u2013\u2013\u2013\u2013:|<br \/>\n|background.jpg| background picture for iPad Mini |<br \/>\n|background@2x.jpg|background picture for iPad Retina and up |<br \/>\n|heartenclosure.png| heart border picture for iPad Mini |<br \/>\n|heartenclosure@2x.png|heart border picture for iPad Retina and up |<br \/>\n|heartbody.png| heart contents picture for iPad Mini |<br \/>\n|heartbody@2x.png|heart contents picture for iPad Retina and up |<br \/>\n<\/code><\/p>\n<p>Once you have these as JPG\/PNG files, you import them into Xcode. Pay attention to have the Retina (and iPhone 6+), with double and triple size. You will drag and drop these into a file called <code>Images.xcassets<\/code>, which is (pretty much) your &#8220;image store&#8221;.<\/p>\n<p><strong><em>Definition step<\/em><\/strong><\/p>\n<p>The plan is to have the background be static, the heart contents be animated from the current height to the new height anytime the jauge updates, and the heart border as the top layer.<\/p>\n<p>Let&#8217;s build the background image:<br \/>\n<code>UIImage* backgroundImg = [UIImage imageNamed:@\"background\"];<\/code><br \/>\nand the same code will create the top picture:<br \/>\n<code>UIImage* heartshapeImg = [UIImage imageNamed:@\"heartenclosure\"];<\/code><\/p>\n<p>iOS is smart enough to select the correct image, you don&#8217;t have to pass in the JPG or @2x information. By default, the z-index of images added to the display is &#8220;latest on top&#8221;.<\/p>\n<p>Once the picture is built, it looks like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/imgur.com\/9YUGZmz\">this<\/a> on my iPhone (my apologies to the original artist).<\/p>\n<p>What we&#8217;ll do, and again it&#8217;s only one of many possibilities, is create three <code>CALayers<\/code> and add them as the <code>UIView<\/code>&#8216;s layers sublayers.<br \/>\nFor my own sake I&#8217;ve made a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/imgur.com\/reX5jYn\">heart shape<\/a> based on a nice heart <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/vignette1.wikia.nocookie.net\/lostpedia\/images\/b\/b9\/6326-tatice-Coeur.png\/revision\/latest?cb=20070603001311&amp;path-prefix=fr\">found with Google<\/a>. In your project you&#8217;ll want the real art I was suggesting earlier \ud83d\ude09<\/p>\n<p>The code for building the layer goes into <code>- (void) viewDidAppear:(BOOL)animated<\/code> (because at this point, the frame is already set).<\/p>\n<p>Note the name being set on the heart layer, which will allow us to animate it.<\/p>\n<p>UIImage* backgroundImg = [UIImage imageNamed:@&#8221;background&#8221;];<br \/>\n    UIImage* heartImage = [UIImage imageNamed:@&#8221;Coeur&#8221;];<br \/>\n    UIImage* heartShapeImage = [UIImage imageNamed:@&#8221;Coeurshape&#8221;];<\/p>\n<pre><code>CALayer* backgroundLayer = [CALayer layer];\nbackgroundLayer.contentsGravity = kCAGravityResizeAspect;\nbackgroundLayer.opacity = 1;\nbackgroundLayer.opaque = YES;\nbackgroundLayer.hidden = NO;\nbackgroundLayer.cornerRadius = 20.0;\nbackgroundLayer.frame = CGRectInset(self.view.layer.frame, 20, 20);\nbackgroundLayer.backgroundColor = [[UIColor colorWithRed:1.1 green:1 blue:0 alpha:1.0]CGColor];\nbackgroundLayer.contents = (__bridge id) backgroundImg.CGImage;\nbackgroundLayer.contentsScale = [[UIScreen mainScreen] scale];\n[self.view.layer addSublayer:backgroundLayer ];\n\nCALayer* heartLayer =[CALayer layer];\nheartLayer.contentsGravity = kCAGravityResizeAspect;\nheartLayer.shadowOffset = CGSizeMake(0, 3);\nheartLayer.shadowRadius = 5.0;\nheartLayer.shadowColor = [UIColor blackColor].CGColor;\nheartLayer.shadowOpacity = 0.8;\nheartLayer.frame = CGRectInset(self.view.layer.frame, 80, 200);\nheartLayer.contents = (__bridge id) heartImage.CGImage;\nheartLayer.contentsScale = [[UIScreen mainScreen] scale];\n[heartLayer setName:@\"heartLayer\"];\n[self.view.layer addSublayer:heartLayer];\n\nCALayer* heartShapeLayer =[CALayer layer];\nheartShapeLayer.contentsGravity = kCAGravityResizeAspect;\nheartShapeLayer.frame = heartLayer.frame;\nheartShapeLayer.contents = (__bridge id)(heartShapeImage.CGImage);\nheartShapeLayer.contentsScale = [[UIScreen mainScreen] scale];\n[self.view.layer addSublayer:heartShapeLayer];\n<\/code><\/pre>\n<p><strong>* Animation Step*<\/strong><\/p>\n<p>By changing the bounds of the heartLayer using <code>CABasicAnimation<\/code>, with masks enabled: <code>heartLayer.masksToBounds = YES;<\/code> you can then show the amount of heart you wish.<\/p>\n<p>In order to access your sublayer, you&#8217;ll need to use its name.<br \/>\nThis subroutine does the trick:<\/p>\n<pre><code>- (CALayer *)findLayerByName:(NSString*) layerTagName inLayer:(CALayer*) layer{\n\n    for (CALayer *layer in [layer sublayers]) {\n\n        if ([[layer name] isEqualToString:layerTagName]) {\n            return layer;\n        }\n    }\n\n    return nil;\n}\n<\/code><\/pre>\n<p>Note that, as per my earlier comment, using Swift is a good option. Ray Wenderlich&#8217;s website has a nice article about <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.raywenderlich.com\/76200\/basic-uiview-animation-swift-tutorial\">UIView animations in Swift<\/a>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">8<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Create a custom gauge with an image Objective C [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] One of the many solutions, given the fuzziness of the question, is to use Core Animation. The official Apple documentation is here. Let&#8217;s outline three steps: Conceptual step Define what layers (conceptually) or masks you will use, and create the images that will be imported in them Definition step Define your CABasicAnimation and program &#8230; <a title=\"[Solved] Create a custom gauge with an image Objective C [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/\" aria-label=\"More on [Solved] Create a custom gauge with an image Objective C [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[5562,1192,470],"class_list":["post-28519","post","type-post","status-publish","format-standard","hentry","category-solved","tag-gauge","tag-image","tag-objective-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Create a custom gauge with an image Objective C [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Create a custom gauge with an image Objective C [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] One of the many solutions, given the fuzziness of the question, is to use Core Animation. The official Apple documentation is here. Let&#8217;s outline three steps: Conceptual step Define what layers (conceptually) or masks you will use, and create the images that will be imported in them Definition step Define your CABasicAnimation and program ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-31T08:01:26+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Create a custom gauge with an image Objective C [closed]\",\"datePublished\":\"2022-12-31T08:01:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/\"},\"wordCount\":480,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"gauge\",\"image\",\"objective-c\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/\",\"name\":\"[Solved] Create a custom gauge with an image Objective C [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-31T08:01:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Create a custom gauge with an image Objective C [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Create a custom gauge with an image Objective C [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Create a custom gauge with an image Objective C [closed] - JassWeb","og_description":"[ad_1] One of the many solutions, given the fuzziness of the question, is to use Core Animation. The official Apple documentation is here. Let&#8217;s outline three steps: Conceptual step Define what layers (conceptually) or masks you will use, and create the images that will be imported in them Definition step Define your CABasicAnimation and program ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/","og_site_name":"JassWeb","article_published_time":"2022-12-31T08:01:26+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Create a custom gauge with an image Objective C [closed]","datePublished":"2022-12-31T08:01:26+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/"},"wordCount":480,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["gauge","image","objective-c"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/","name":"[Solved] Create a custom gauge with an image Objective C [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-31T08:01:26+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-create-a-custom-gauge-with-an-image-objective-c-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Create a custom gauge with an image Objective C [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=28519"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28519\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=28519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=28519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=28519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}