After iOS8 Apple provides an API for making frosted glass effects
This is the Uivisualeffectview, with this initwitheffect: to initialize, and then, he has two effects inherited from Uivisualeffect. This parent does not care, do nothing, mainly see his two sub-class Uiblureffect and Uivibrancyeffect.
Uiblureffect: This is the one that affects the rear view of the frosted glass
:
Uivibrancyeffect: This is the view that affects the frosted glass.
is not very beautiful, it is not difficult to do it.
First say the effect of the hair glass under the practice.
You first initialize a Uiblureffect object, he has three styles
typedef ns_enum (Nsinteger, Uiblureffectstyle) {
Uiblureffectstyleextralight,
Uiblureffectstylelight,
Uiblureffectstyledark
}
The above effects are all made out of uiblureffectstylelight.
The Uiblureffectstyleextralight effect is as follows
The Uiblureffectstyledark effect is as follows
And then we create a Uivisualeffectview object with this Uiblureffect object. Add this Uivisualeffectview object as a child view with the view you need to be virtualized.
And then say the effect on the frosted glass.
Create a Uivibrancyeffect object, initialize it with the previously created Blur, and then create a Uivisualeffectview object, initialized with this Uivibrancyeffect object, Finally, add the sub-view you want to add to Uivisualeffectview's Contentview.
The complete code is as follows:
// UIBlurEffect效果UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];imageView.image = [UIImage imageNamed:@"pic"];UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];UIVisualEffectView *ev = [[UIVisualEffectView alloc] initWithEffect:blur];ev.frame = self.view.frame;[imageView addSubview:ev];// UIVibrancyEffect效果UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blur];UIVisualEffectView *ano = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];ano.frame = self.view.frame;UILabel *label = [[UILabel alloc] init];label.font = [UIFont systemFontOfSize:40];label.frame = CGRectMake(100, 100, 400, 100);label.text = @"Beautiful View";[ev.contentView addSubview:ano];[ano.contentView addSubview:label];[self.view addSubview:imageView];