鄭州iOS·點-毛玻璃效果

大點哥發表於2016-04-06

   

    毛玻璃效果

        先上圖:

                  

      這裡介紹一種比較簡單,效率比較高的方法,在使用的時候只需要注意以下兩點:

     1.[NS_CLASS_AVAILABLE_IOS(8_0)];

     2.建議不要使用這個UIVisualEffectView父類的 alpha 屬性.


  UIVisualEffectView: 視覺效果檢視類

  父類是UIView,遵循NSSecureCoding協議

  有兩個屬性:(UIView *)contentView  和 (UIVisualEffect *)effect

  需要注意的是:不要將自定義的子檢視直接新增到這個類的物件上,用它原生的contentView就好;
  另外系統還提供了兩個初始化方法:
- (instancetype)initWithEffect:(nullable UIVisualEffect *)effect NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;


這裡擴充一下  NS_DESIGNATED_INITIALIZER,有位碼友分析的特別到位,分享至此供大夥兒參考延伸:https://yq.aliyun.com/articles/5847


以碼會友,還望眾大神多多指點~

毛玻璃實現程式碼如下:

.m

@interface

@property (nonatomic, strong)UIImageView *gsImgView;

@end


@implementation

- (void)viewDidLoad {

    [super viewDidLoad];

    [self configureGsImgView];

}


- (void)configureGsImgView {

    self.gsImgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    NSString *urlStr = @"http://mg.soupingguo.com/bizhi/big/10/375/018/10375018.jpg";
    [_gsImgView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
    [self.view addSubview:self.gsImgView];

    UIVisualEffectView *VEView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    VEView.frame = CGRectMake(0, 0, _gsImgView.frame.size.width, _gsImgView.frame.size.height);
    [self.gsImgView addSubview:VEView];
}    

@end



相關文章