Praise the motherland! Use OC to simply write a small demo.

Step 1 Create an image slice storage object

@interface** KDSClipImageModel : NSObject



@property (nonatomic,assign) CGRect rect;

@property (nonatomic,strong) UIImage * image;

@property (nonatomic,assign) CGFloat scaleW;

@property (nonatomic,assign**) CGFloat scaleH;

@property (nonatomic,strong) UIImageView * imageView;



@end
Copy the code

Step 2: Management

1. Management

static dispatch_source_t timer;
static bool isCanDismiss = true;

@interface KDSClipImageTool : NSObject
Copy the code

2. A whole graph generates a cut graph

+ (NSMutableArray *)clipAllCellImagesWithImage:(UIImage *)image row:(NSInteger)row column:(NSInteger)column rect:(CGRect)rect

{

    NSMutableArray * saveCellImagesArr = @[].mutableCopy;

    CGFloat imageWidth = image.size.width;

    CGFloat imageHeight = image.size.height;

    CGFloat width = (imageWidth / column);

    CGFloat height = (imageHeight / row);

    CGFloat rw = (rect.size.width / imageWidth);

    CGFloat rh = (rect.size.height / imageHeight);

    for (int i = 0; i < column; i++) {

        for (int j = 0; j < row; j++) {

            CGRect imageRect = CGRectMake(width * i, height * j, width, height);

            UIImage * cellImage = [**self** clipImageWithAllImage:image rect:imageRect];

            KDSClipImageModel * model = [[KDSClipImageModel alloc] init];

            model.image = cellImage;

            model.scaleW = rw;

            model.scaleH = rh;

            CGRect imageRRect = CGRectMake(width * rw * i, height * rh * j, width * rw, height * rh);

            model.imageView = [[UIImageView alloc] initWithFrame:imageRRect];

            model.imageView.image = cellImage;

            model.rect = imageRRect;

            [saveCellImagesArr addObject:model];

        }

    }

    return saveCellImagesArr;

}

+ (UIImage *)clipImageWithAllImage:(UIImage *)image rect:(CGRect)rect

{

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);

    UIImage * imageResult = [UIImage imageWithCGImage:imageRef];

    CGImageRelease(imageRef);

    return imageResult;

}
Copy the code

3. Show the disordered arrangement of the cut graph group

+ (void)showImageModels:(NSMutableArray <KDSClipImageModel *>*)clipImageModels pushView:(UIView *)pushView canDismiss:(void(^)(void))dismissBlock { isCanDismiss = **true**; NSArray *tempClipImageModels = [clipImageModels sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return (arc4random_uniform(2) == 0); }]; NSInteger index = 0; for (KDSClipImageModel * clipImageModel **in** tempClipImageModels) { KDSClipImageModel * imageModel = clipImageModels[index]; clipImageModel.imageView.frame = imageModel.rect; index ++; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{for (KDSClipImageModel * clipImageModel **in** tempClipImageModels) {[UIView animateWithDuration:0.3 animations:^{ clipImageModel.imageView.frame = clipImageModel.rect; } completion:^(BOOL finished) {dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (isCanDismiss == true) { if (dismissBlock) { dismissBlock(); } isCanDismiss = false; }});}]; }}); }Copy the code

4. Particles disappear

+ (void)setupEmitterWithClipImageModels:(NSMutableArray <KDSClipImageModel *>*)clipImageModels pushView:(UIView *)pushView{ NSInteger radomNum = 10; dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); Dispatch_source_set_timer (timer, DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); dispatch_source_set_event_handler(timer, ^{ if (clipImageModels.count <= 0) { dispatch_suspend(timer); } else { for (int i = 0; i < radomNum; i++) { NSInteger index = arc4random() % clipImageModels.count; KDSClipImageModel * model = clipImageModels[index]; [self createCAEmitterCellWithImage:model pushView:pushView]; [clipImageModels removeObject:model]; }}}); dispatch_resume(timer); } + (void)createCAEmitterCellWithImage:(KDSClipImageModel *)model pushView:(UIView *)pushView { CAEmitterLayer *emitter = [[CAEmitterLayer alloc]init]; emitter.preservesDepth = YES; CAEmitterCell *cell = [[CAEmitterCell alloc]init]; cell.velocity = 150; cell.velocityRange = 100; cell.scale = 1 * model.scaleW; cell.scaleRange = 1 * model.scaleW; cell.emissionLongitude = -M_PI_2; cell.emissionRange = M_PI_2 / 4; cell.lifetime = 3; cell.lifetimeRange = .5; cell.spin = M_PI_2; cell.spinRange = M_PI_2 / 2; cell.birthRate = 1; emitter.emitterPosition = CGPointMake(model.rect.origin.x, model.rect.origin.y); cell.contents = ( **__bridge** **id** **_Nullable**)(model.image.CGImage); emitter.emitterCells = @[cell]; [pushView.layer addSublayer:emitter]; [model.imageView removeFromSuperview]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [emitter removeFromSuperlayer]; }); }Copy the code

Step 3. Show the control class

#import "KDSClipImageView.h" #import "KDSClipImageTool.h" #import "KDSClipImageModel.h" @interface KDSClipImageView() @property (nonatomic,strong) NSMutableArray * saveClipImages; @end @implementation** KDSClipImageView - (void)clipImagesCanDismiss:(void(^)(void))disMissBlock { if (! self.image) { return; } [self layoutIfNeeded]; self.saveClipImages = [KDSClipImageTool clipAllCellImagesWithImage:self.image row:30 column:45 rect:self.bounds]; for (KDSClipImageModel * imageModel in self.saveClipImages) { [self addSubview:imageModel.imageView]; } [self showCanDismiss:disMissBlock]; } - (**void**)showCanDismiss:(void(^)(void))disMissBlock { @weakify(self); [KDSClipImageTool showImageModels:self.saveClipImages pushView:self canDismiss:^{ @strongify(self);  if (disMissBlock) { disMissBlock(); } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ @strongify(self); [self dismiss]; }); }]; } - (void)dismiss { [KDSClipImageTool setupEmitterWithClipImageModels:**self**.saveClipImages pushView:self]; }Copy the code

Step 4. Initialize

UIImageView * gqImageView = [[UIImageView alloc] init]; gqImageView.contentMode = UIViewContentModeScaleAspectFit; gqImageView.hidden = YES; gqImageView.image = [UIImage imageNamed:@"gq"]; UIImage * image = [UIImage imageNamed:@"kmyc"]; KDSClipImageView * imageBaseView = [[KDSClipImageView alloc] init]; [imageBaseView addSubview:gqImageView]; [gqImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(imageBaseView); }]; imageBaseView.image = image; [self addSubview:imageBaseView]; CGFloat imageViewWidth = image.size.width < self.width ? : self.width; [imageBaseView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self).offset(0);  make.width.mas_equalTo(imageViewWidth);  make.height.mas_equalTo((imageViewWidth) * (image.size.height image.size.width)); }]; [imageBaseView clipImagesCanDismiss:^{ gqImageView.hidden = NO; }];Copy the code

Bad code, big god do not spray.