{"id":12405,"date":"2014-07-09T15:23:53","date_gmt":"2014-07-09T19:23:53","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=12405"},"modified":"2014-07-09T15:23:53","modified_gmt":"2014-07-09T19:23:53","slug":"adding-view-effects-in-ios-8","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/adding-view-effects-in-ios-8\/","title":{"rendered":"Adding View Effects in iOS 8"},"content":{"rendered":"<p>\t\t\t\tIn iOS 8, Apple has added UIKit level support for effects such as blur and vibrancy. These effects are seen in system-level UIs such as the blur effect shown when opening a folder or swiping to the lock screen, and can now be added to your own applications with just a few lines of code.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-12410\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/vieweffects.png\" alt=\"view effects\" width=\"285\" height=\"598\" \/><\/p>\n<h2>Blur Effect<\/h2>\n<p>The first effect you can use is the blur effect, represented by the <strong>UIBlurEffect<\/strong> class. Adding a blur effect is easy. Create a <strong>UIBlurEffect<\/strong> and a <strong>UIVisualEffectView<\/strong> from the effect. Then just add the effect view to the view hierarchy.<\/p>\n<p>For example, the following code adds a blur effect:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar blur = UIBlurEffect.FromStyle (UIBlurEffectStyle.Light);\nvar blurView = new UIVisualEffectView (blur) {\n  Frame = new RectangleF (0, 0, imageView.Frame.Width, 400)\n};\n\nView.Add (blurView);\n<\/pre>\n<p>This code dynamically blurs the content beneath it. For instance, when added to a view hierarchy containing a scrollable image, the effect of the blur changes at runtime as the image is moved:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-12411\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/blur.png\" alt=\"blur\" width=\"598\" height=\"598\" \/><\/p>\n<p>The blur effect comes in three styles:<\/p>\n<ul>\n<li>UIBlurEffectStyle.Light<\/li>\n<li>UIBlurEffectStyle.ExtraLight<\/li>\n<li>UIBlurEffectStyle.Dark<\/li>\n<\/ul>\n<p>These change the appearance of the blur as shown below:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-12412\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/blur-style.png\" alt=\"blur style\" width=\"897\" height=\"647\" \/><\/p>\n<h2>Vibrancy Effect<\/h2>\n<p>In addition to blur, iOS includes a vibrancy effect (<strong>UIVibrancyEffect<\/strong>), which allows content displayed over a blur to remain legible. Vibrancy effects are created from blur effects, and are also displayed using a <strong>UIVisualEffectView<\/strong>. Any view the effect should be applied to is added as a subview of the <strong>UIVisualEffectView<\/strong>&#8216;s <strong>ContentView<\/strong>.<\/p>\n<p>For example, the following code adds a label to be displayed over the blurred view created above:<\/p>\n<pre class=\"lang:csharp decode:true\">\n\/\/ vibrancy view\nvar frame = new Rectangle (10, 10, 100, 50);\nvar vibrancy = UIVibrancyEffect.FromBlurEffect (blur);\nvar vibrancyView = new UIVisualEffectView (vibrancy) {\n  Frame = frame\n};\n\nlabel = new UILabel {\n  Text = &quot;Hello iOS 8!&quot;,\n  Frame = vibrancyView.Bounds\n};\n\nvibrancyView.ContentView.Add (label);\nblurView.ContentView.Add (vibrancyView);\n<\/pre>\n<p>When the user scrolls the image, the blur changes and the label&#8217;s text is modified dynamically such that it remains readable:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-12413\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/vibrancy.png\" alt=\"vibrancy\" width=\"285\" height=\"598\" \/><\/p>\n<p>These effects are useful when you want them applied dynamically. Of course, rendering them has some cost, so if you can get the results you are looking for with a static effect, that should be used. However, for creating a level of polish, with a sense of depth on par with iOS itself, it&#8217;s nice to now have these features available.<\/p>\n<p>The code from this post is available <a href=\"https:\/\/gist.github.com\/mikebluestein\/3443c3d86c01566b51fb\" title=\"iOS 8 visual effects code\" target=\"_blank\">here<\/a>.<\/p>\n<p><a href=\"http:\/\/forums.xamarin.com\/discussion\/20017\" title=\"Discuss in Forums\" target=\"_blank\"><em>Discuss this blog post in the Xamarin Forums<\/em><\/a><\/p>\n<p>&nbsp;\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In iOS 8, Apple has added UIKit level support for effects such as blur and vibrancy. These effects are seen in system-level UIs such as the blur effect shown when opening a folder or swiping to the lock screen, and can now be added to your own applications with just a few lines of code. [&hellip;]<\/p>\n","protected":false},"author":1932,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[6,4],"class_list":["post-12405","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>In iOS 8, Apple has added UIKit level support for effects such as blur and vibrancy. These effects are seen in system-level UIs such as the blur effect shown when opening a folder or swiping to the lock screen, and can now be added to your own applications with just a few lines of code. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/12405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1932"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=12405"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/12405\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=12405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=12405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=12405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}