iOS SDK詳解之模糊(毛玻璃)效果效果,iossdk

來源:互聯網
上載者:User

iOS SDK詳解之模糊(毛玻璃)效果效果,iossdk

原創blog,轉載請註明出處
http://blog.csdn.net/hello_hwc?viewmode=list

前言:
在iOS 8 之前,想要實現模糊效果,一般會使用一些Github庫,當然自己定製也可以,其原理就是用Core Image進行一些數位影像處理(因為電子出身,本課的時候做過,用矩陣來做)。不過,到了iOS 8之後,這一切變的非常簡單,因為Apple公開了之前的幾個私人API。

Demo效果

三種Blur

Vibrancy(也就是在Blur上加一些想要強調的部分)

Demo下載連結

http://download.csdn.net/detail/hello_hwc/8678439

添加Blur

原理很簡單

  • UIBlurEffect初始化一個blurEffect
  • 制定一個VisualEffectView,這個View定義了blur的地區
  • 把這個View作為Subview添加到想要blur的view上
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];    UIVisualEffectView *bluredEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];    [bluredEffectView setFrame:  CGRectInset(self.imageview.bounds, 20, 20);];    bluredEffectView.layer.cornerRadius = 15;    bluredEffectView.layer.masksToBounds = YES;    [self.imageview addSubview:bluredEffectView];

其中Blur有三種,對應上文Demo圖中的三種:

  • dark
  • light
  • extraLihgt

幾點要注意的是

1. 不要對visualView設定alpha < 1
2. 可以對visualView設定Mask,來定製模糊的地區

添加Vibrancy

添加Vibrancy的原理就是在Blur的基礎上再添加一個VisualView,並且在這個VisualView的contentView上添加想要的控制項

   UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];    UIVisualEffectView *bluredEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];    [bluredEffectView setFrame:CGRectMake(30,self.imageview.bounds.size.height - 50,self.imageview.bounds.size.width - 60, 40)];    bluredEffectView.layer.cornerRadius = 15;    bluredEffectView.layer.masksToBounds = YES;    [self.imageview addSubview:bluredEffectView];    UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];    UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];    [vibrancyEffectView setFrame:self.imageview.bounds];    [bluredEffectView.contentView addSubview:vibrancyEffectView];    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,self.imageview.bounds.size.width - 60, 40)];    label.text = @"Highlight";    label.textAlignment = NSTextAlignmentCenter;    label.textColor = [UIColor blackColor];    [label setTextColor:[UIColor blackColor]];    [vibrancyEffectView.contentView addSubview:label];

效果

簡單介紹下我寫的Demo的一些設計原理

由兩個數組儲存Model

-(NSArray *)blurEffectArray{    return @[[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark],             [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight],             [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight],             ];}-(NSArray *)titleArray{    return @[@"Dark",@"Light",@"ExtraLight"];}

由CurrentIndex來擷取/設定當前選擇效果,在CurrentIndex的set函數裡進行Model和View的同步

-(void)setCurrentIndex:(NSInteger)currentIndex{    if (self.visualview != nil) {        [self.visualview removeFromSuperview];    }    self.visualview = [[UIVisualEffectView alloc] initWithEffect:[[self blurEffectArray] objectAtIndex:currentIndex]];    self.visualview.frame = CGRectInset(self.imageview.bounds, 20, 20);    self.visualview.layer.cornerRadius = 15;    self.visualview.layer.masksToBounds = YES;    self.navigationItem.title = [[self titleArray] objectAtIndex:currentIndex];    [self.imageview addSubview:self.visualview];    _currentIndex = currentIndex;}

手勢觸摸,只需要改變CurrentIndex即可

- (IBAction)swipt:(UISwipeGestureRecognizer *)sender {    self.currentIndex = (self.currentIndex + 1)%[self blurEffectArray].count;}

初始化的時候,指定最初的Index

- (void)viewDidLoad {    [super viewDidLoad];    self.imageview.userInteractionEnabled = YES;    self.currentIndex = 0;    // Do any additional setup after loading the view, typically from a nib.}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.