I found this a pretty cool part of the UI in iOS 7, and here is how to do it with Xamarin.iOS.
1. The Code
Setup our UI with a controller for our initial screen, background image, and a button. We haven't added the ApplyBackgroundToButton() method yet, so don't worry.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class-level declarations | |
UIWindow window; | |
UIViewController controller; | |
public override bool FinishedLaunching (UIApplication app, NSDictionary options) | |
{ | |
// create a new window instance based on the screen size | |
window = new UIWindow (UIScreen.MainScreen.Bounds); | |
// Initialize the main screen | |
controller = new UIViewController (); | |
// Load our background image | |
UIImageView backgroundImage = new UIImageView (UIImage.FromFile ("background.png")); | |
// Setup the button control. We will blur the background of this. | |
UIButton button = UIButton.FromType (UIButtonType.System); | |
button.Frame = new RectangleF (115, 175, 100, 44); | |
button.SetTitle ("Hello Blur", UIControlState.Normal); | |
controller.View.AddSubviews (backgroundImage, button); | |
// If you have defined a root view controller, set it here: | |
window.RootViewController = controller; | |
// make the window visible | |
window.MakeKeyAndVisible (); | |
// Applies the blurred image as the background image of the button | |
ApplyBackgroundToButton (button, controller.View); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ApplyBackgroundToButton(UIButton button, UIView backgroundView) | |
{ | |
// This makes sure we have the coordinates relative to the backgroundView. Without this, the image drawn | |
// for the button would be at the incorrect place of the background. | |
RectangleF buttonRectInBGViewCoords = button.ConvertRectToView (button.Bounds, backgroundView); | |
UIGraphics.BeginImageContextWithOptions (button.Frame.Size, false, window.Screen.Scale); | |
// Make a new image of the backgroundView (basically a screenshot of the view) | |
backgroundView.DrawViewHierarchy (new RectangleF (-buttonRectInBGViewCoords.X, -buttonRectInBGViewCoords.Y, | |
backgroundView.Frame.Width, backgroundView.Frame.Height), true); | |
UIImage newBGImage = UIGraphics.GetImageFromCurrentImageContext (); | |
UIGraphics.EndImageContext (); | |
// Apply the blur effect | |
newBGImage = newBGImage.ApplyLightEffect (); | |
// Set the blurred image as the background for the button | |
button.SetBackgroundImage (newBGImage, UIControlState.Normal); | |
button.Layer.CornerRadius = 4.0f; | |
button.Layer.MasksToBounds = true; | |
} |
Hey! It's great! BUT does not work! when i am using methods with parameter, such as applyBlurWithRadius or applyTintEffectWithColor.
ReplyDeleteI get error:
Objectiv-C exception thrown.
Name: NSInvalidArgumentException
Reason: -[UIImage applyTintEffectWithColor]: unrecognized selector sent to instance 0x7bd48ec0
I get this when run your ImageEffects.Example project. with changes:
// Apply the blur effect
//newBGImage = newBGImage.ApplyLightEffect ();
UIColor tintColor = UIColor.FromWhiteAlpha(0.5f, 0.3f);
newBGImage = newBGImage.ApplyTintEffectWithColor(tintColor);
Hey!
ReplyDeleteSorry you found this to be not working! I don't manage this blog anymore. I have moved to jmillerdev.net
I have a post on my new blog there with a binding project that will do this for you and it works. Take a look:
http://jmillerdev.net/blog/2013/10/21/ios-7-blur-with-xamarin-dot-ios/
Thanks!
Reuse existing code: The developer can Use its favorite .NET libraries in Xamarin.Android applications. Easily bind third-party native frameworks and libraries. The Xamarin development Component Store offers tons of third-party libraries packaged
ReplyDeleteI have read this post. Collection of post is a nice one ios swift online training
ReplyDelete