欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果

 更新時(shí)間:2016年09月25日 16:29:58   投稿:daisy  
不知道大家發(fā)現(xiàn)沒(méi)有蘋(píng)果在iOS7.0之后,很多系統(tǒng)界面都使用了毛玻璃效果,增加了界面的美觀性,所以這篇文章跟大家分享個(gè)iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果的方法,有需要的可以參考借鑒,下面來(lái)一起看看。

先來(lái)看看效果圖,由于錄屏軟件不給力,毛玻璃效果不明顯,請(qǐng)見(jiàn)諒。

步驟詳解:

說(shuō)下思路,很簡(jiǎn)單,首先自定義一個(gè)collectionView, 重寫(xiě)它的initWithFrame:collectionViewLayout:方法,在這里面做配置,這里用的是AXECollectionView.

與之對(duì)應(yīng)的自定義一個(gè)collectionViewCell,在cell里配置操作:設(shè)置layer涂層,加載圖片等操作,這里用的是AXECollectionViewCell.

最后在需要展示的控制器里調(diào)用AXECollectionView,給它傳入一個(gè)自定義的流水布局和圖片數(shù)組,大功告成.

示例代碼如下:

// viewController
@interface ViewController ()

@property (nonatomic, strong) AXECollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // 流水布局
  UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  flowLayout.minimumLineSpacing = kItem_Margin;
  flowLayout.minimumInteritemSpacing = [UIScreen mainScreen].bounds.size.height;
  flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - kItem_Margin, kItem_Height);
  flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  flowLayout.sectionInset = UIEdgeInsetsMake(0, kItem_Margin / 2, 0, kItem_Margin / 2);

  CGRect frame = self.view.bounds;
  _collectionView = [[AXECollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];

  // 傳入圖片數(shù)組
  _collectionView.imgArr = @[
                @"0",
                @"1",
                @"2",
                @"3",
                @"4",
                @"5",
                @"6",
                @"7",
                ];

  [self.view addSubview:_collectionView];
}

// AXECollectionView.h
@interface AXECollectionView : UICollectionView

@property (nonatomic, strong) NSArray *imgArr;

@end


// AXECollectionView.m
@interface AXECollectionView () <UICollectionViewDelegate, UICollectionViewDataSource>

// 背景imgView
@property (nonatomic, strong) UIImageView *bgImgView;

@end

@implementation AXECollectionView

static NSString *const AXECollectionViewCellID = @"AXECollectionViewCell";

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
  if(self = [super initWithFrame:frame collectionViewLayout:layout])
  {
    [self setup];
  }
  return self;
}

- (void)setup
{
  self.showsVerticalScrollIndicator = NO;
  self.showsHorizontalScrollIndicator = NO;
  self.pagingEnabled = YES;
  self.dataSource = self;
  self.delegate = self;
  [self registerClass:[AXECollectionViewCell class] forCellWithReuseIdentifier:AXECollectionViewCellID];

  // collectionView背景view
  UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.bounds];
  self.backgroundView = bgImgView;
  self.bgImgView = bgImgView;

  // 毛玻璃效果 (iOS8.0以后適用)
  UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
  effectView.frame = self.bounds;
  [self.backgroundView addSubview:effectView];
}

#pragma mark - UICollectionViewDataSource

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  AXECollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:AXECollectionViewCellID forIndexPath:indexPath];
  cell.img = self.imgArr[indexPath.row];

  // 設(shè)置毛玻璃圖片
  self.bgImgView.image = [UIImage imageNamed:self.imgArr[indexPath.row]];
  return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  return self.imgArr.count;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
  AXECollectionViewCell *myCell = (AXECollectionViewCell *)cell;
  [UIView animateWithDuration:0.5 animations:^{
    myCell.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.4);
  }];
}

補(bǔ)充一下

例子中我是用的UIBlurEffect做的毛玻璃效果,這個(gè)是iOS8以后出現(xiàn)的,如果你要適配7的系統(tǒng),那就要另做配置.

如果不用UIBlurEffect的話,下面這兩種同樣能做出模糊效果,只不過(guò)第一種性能較差,建議大家按需使用.

// 返回濾鏡處理后圖片
- (UIImage *)coreBlurImage:(UIImage *)image withBlurNumber:(CGFloat)blur
{
  CIContext *context = [CIContext contextWithOptions:nil];
  CIImage *inputImage= [CIImage imageWithCGImage:image.CGImage];

  // 設(shè)置filter
  CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
  [filter setValue:inputImage forKey:kCIInputImageKey];
  [filter setValue:@(blur) forKey: @"inputRadius"];

  // 模糊圖片
  CIImage *result=[filter valueForKey:kCIOutputImageKey];
  CGImageRef outImage=[context createCGImage:result fromRect:[result extent]];
  UIImage *blurImage=[UIImage imageWithCGImage:outImage];
  CGImageRelease(outImage);
  return blurImage;
}

// 返回高斯效果模糊圖片
- (UIImage *)boxblurImage:(UIImage *)image withBlurNumber:(CGFloat)blur
{
  if (blur < 0.f || blur > 1.f) {
    blur = 0.5f;
  }
  int boxSize = (int)(blur * 40);
  boxSize = boxSize - (boxSize % 2) + 1;
  CGImageRef img = image.CGImage;
  vImage_Buffer inBuffer, outBuffer;
  vImage_Error error;
  void *pixelBuffer;

  // 從CGImage中獲取數(shù)據(jù)
  CGDataProviderRef inProvider = CGImageGetDataProvider(img);
  CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

  // 設(shè)置從CGImage獲取對(duì)象的屬性
  inBuffer.width = CGImageGetWidth(img);
  inBuffer.height = CGImageGetHeight(img);
  inBuffer.rowBytes = CGImageGetBytesPerRow(img);
  inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
  pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
  if(pixelBuffer == NULL)
    NSLog(@"No pixelbuffer");
  outBuffer.data = pixelBuffer;
  outBuffer.width = CGImageGetWidth(img);
  outBuffer.height = CGImageGetHeight(img);
  outBuffer.rowBytes = CGImageGetBytesPerRow(img);
  error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
  if (error) {
    NSLog(@"error from convolution %ld", error);
  }
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef ctx = CGBitmapContextCreate( outBuffer.data, outBuffer.width, outBuffer.height, 8, outBuffer.rowBytes, colorSpace, kCGImageAlphaNoneSkipLast);
  CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  UIImage *returnImage = [UIImage imageWithCGImage:imageRef];

  // clean up
  CGColorSpaceRelease(colorSpace);
  free(pixelBuffer);
  CFRelease(inBitmapData);
  CGColorSpaceRelease(colorSpace);
  CGImageRelease(imageRef);
  return returnImage;
}

總結(jié)

以上就是iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果的全部?jī)?nèi)容,希望能對(duì)各位iOS開(kāi)發(fā)者們有一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • iOS中視頻播放器的簡(jiǎn)單封裝詳解

    iOS中視頻播放器的簡(jiǎn)單封裝詳解

    要實(shí)現(xiàn)封裝視頻播放器,首先需要實(shí)現(xiàn)視頻播放器,然后再去考慮怎樣封裝可以讓以后自己使用起來(lái)方便快捷。iOS9之前可以使用MediaPlayer來(lái)進(jìn)行視頻的播放,iOS9之后系統(tǒng)推薦使用AVFoundation框架實(shí)現(xiàn)視頻的播放。下面通過(guò)本文來(lái)看看詳細(xì)的介紹吧。
    2016-10-10
  • Objective-C Json 實(shí)例詳解

    Objective-C Json 實(shí)例詳解

    這篇文章主要介紹了 Objective-C Json 實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家掌握Object-C Json的使用,需要的朋友可以參考下
    2017-10-10
  • iOS 10 和Xcode8 一起 創(chuàng)建 Siri 功能步驟詳解(OC寫(xiě)的 )

    iOS 10 和Xcode8 一起 創(chuàng)建 Siri 功能步驟詳解(OC寫(xiě)的 )

    這篇文章主要介紹了iOS 10 和Xcode8 一起 創(chuàng)建 Siri 功能(OC寫(xiě)的 ),本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2017-12-12
  • iOS動(dòng)態(tài)驗(yàn)證碼實(shí)現(xiàn)代碼

    iOS動(dòng)態(tài)驗(yàn)證碼實(shí)現(xiàn)代碼

    本文通過(guò)實(shí)例代碼給大家介紹了ios動(dòng)態(tài)驗(yàn)證碼的實(shí)現(xiàn)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2018-04-04
  • IOS 圓球沿著橢圓軌跡做動(dòng)畫(huà)

    IOS 圓球沿著橢圓軌跡做動(dòng)畫(huà)

    這篇文章主要介紹了IOS 圓球沿著橢圓軌跡做動(dòng)畫(huà)的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • iOS中Navbar設(shè)置漸變色效果的方法示例

    iOS中Navbar設(shè)置漸變色效果的方法示例

    這篇文章主要給大家介紹了iOS中Navbar設(shè)置漸變色效果的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • iOS開(kāi)發(fā)中UIDatePicker控件的使用方法簡(jiǎn)介

    iOS開(kāi)發(fā)中UIDatePicker控件的使用方法簡(jiǎn)介

    這篇文章主要介紹了iOS開(kāi)發(fā)中UIDatePicker控件的使用方法簡(jiǎn)介,用來(lái)處理各種時(shí)間日期的選擇,需要的朋友可以參考下
    2015-11-11
  • 深入淺析IOS中UIControl

    深入淺析IOS中UIControl

    UIControl,相信大家對(duì)其并不陌生吧,比如平常最常用的UIButton就是繼承自UIControl的。下面通過(guò)本篇文章給大家介紹ios中UIControl,感興趣的朋友一起學(xué)習(xí)吧
    2015-10-10
  • IOS實(shí)現(xiàn)圓形圖片效果的兩種方法

    IOS實(shí)現(xiàn)圓形圖片效果的兩種方法

    這篇文章介紹在IOS中如何實(shí)現(xiàn)圓形圖片,實(shí)現(xiàn)后的效果很贊,有需要的可以參考借鑒。
    2016-08-08
  • IOS 開(kāi)發(fā)之UIView動(dòng)畫(huà)的實(shí)例詳解

    IOS 開(kāi)發(fā)之UIView動(dòng)畫(huà)的實(shí)例詳解

    這篇文章主要介紹了IOS 開(kāi)發(fā)之UIView動(dòng)畫(huà)的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07

最新評(píng)論