【iOS Tips】毛玻璃效果

方法1、將圖片「模糊化」、即高斯模糊算法,咱們採用 "UIImage+ImageEffects.h"的類別方法。是將Image模糊處理後得到一個新的Image。

定義.h文件:

- (UIImage *)applyLightEffect;  算法

- (UIImage *)applyExtraLightEffect; app

 - (UIImage *)applyDarkEffect;  spa

- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor; 圖片

- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage; it

定義.m文件:

 //_blurStyle 表明風格  io

  //_defaultImage 是咱們想要處理的圖片  方法

  //_value 模糊度  im

  //_arcColor 隨機生成的顏色  apply

  switch (_blurStyle) {  img

     case BlurStyleNone://原圖  

         _imgView.image=_defaultImage;  

        break;  

     case BlurStyleLight://透白風  

          _imgView.image=[_defaultImage applyLightEffect];  

          break;  

      case BlurStyleExtraLight://亮白風  

          _imgView.image=[_defaultImage applyExtraLightEffect];  

          break;  

      case BlurStyleDark://黑暗風  

          _imgView.image=[_defaultImage applyDarkEffect];  

          break;  

      case BlurStyleTintEffect://自定義顏色  

          _imgView.image= [_defaultImage applyTintEffectWithColor:_arcColor];  

          break;  

      case BlurStyleCustom://自定義顏色和模糊度  

          _imgView.image=[_defaultImage applyBlurWithRadius:_value tintColor:_arcColor  saturationDeltaFactor:1.8 maskImage:nil];  

         break;  

      default:  

          break;  

  }  

方法2、

iOS8,蘋果便加入了UIVisualEffectView類,這就是真正的"毛玻璃",你能夠將它放在你想放的任何視圖上。

//系統枚舉的毛玻璃風格

typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {      

UIBlurEffectStyleExtraLight,     

 UIBlurEffectStyleLight,     

 UIBlurEffectStyleDark  } 

NS_ENUM_AVAILABLE_IOS(8_0);  

 那麼如何建立呢?

_backgroundView=[[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]]; //這裏可自定義是暗黑風格,仍是白色風格

 _backgroundView.frame=CGRectMake(0, 0, ScreenSize.width, ScreenSize.height); 

  [self.view addSubview:_backgroundView]; 

瞧!是否是很簡單啊! 

相關文章
相關標籤/搜索