iOS - 毛半透明效果

來源:互聯網
上載者:User

標籤:typedef   ram   圖片   ref   oom   label   view   visual   分享   

  iOS - 毛半透明效果

iOS8之後蘋果提供了製作毛半透明效果的API

就是這個UIVisualEffectView,用這個initWithEffect:去初始化,然後呢,他有兩種效果繼承自UIVisualEffect。這個父類不用管,什麼也不做,主要看他的兩個子類UIBlurEffect和UIVibrancyEffect。

UIBlurEffect : 這個是影響毛玻璃後面視圖的

 

 

UIVibrancyEffect : 這個是影響毛玻璃上的視圖的

 

 

是不是很漂亮,做起來也不難呢。

先說毛玻璃下面的效果的做法。

你先初始化一個UIBlurEffect對象,他有三種風格

typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {

UIBlurEffectStyleExtraLight,

UIBlurEffectStyleLight,

UIBlurEffectStyleDark

}

上面的效果都是用UIBlurEffectStyleLight做出來的

UIBlurEffectStyleExtraLight效果如下

 

 

UIBlurEffectStyleDark效果如下

 

 

然後呢用這個UIBlurEffect對象建立一個UIVisualEffectView對象。用你需要被虛化的視圖添加這個UIVisualEffectView對象為子視圖就可以了。

接著說毛玻璃上面的效果

建立一個UIVibrancyEffect對象,用之前建立的blur去初始化,然後建立一個UIVisualEffectView對象,用這個UIVibrancyEffect對象初始化,最後將你想要添加的子視圖添加到UIVisualEffectView的contentView上就可以了。

完整代碼如下:

// 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];

iOS - 毛半透明效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.