@interface UIView (Debris)
/*
pop返回碎片动画
@property 调用示例
[[UIApplication sharedApplication].keyWindow addSubview:self.view]
[self.view fragmenttationAnimation]
[self.navigationController popViewControllerAnimated:NO]
*/
- (void)fragmenttationAnimation
@end
@implementation UIView (Debris)
- (void)fragmenttationAnimation {
// 先截图
UIView *snapView = [self snapshotViewAfterScreenUpdates:YES]
// 隐藏容器中的子控件
for (UIView *view in self.subviews) {
view.hidden = YES
}
// 保存x坐标的数组
NSMutableArray *xArray = [[NSMutableArray alloc] init]
// 保存y坐标
NSMutableArray *yArray = [[NSMutableArray alloc] init]
for (NSInteger i = 0
@autoreleasepool{
[xArray addObject:@(i)]
}
}
for (NSInteger i = 0
@autoreleasepool{
[yArray addObject:@(i)]
}
}
//这个保存所有的碎片
NSMutableArray *snapshots = [[NSMutableArray alloc] init]
for (NSNumber *x in xArray) {
for (NSNumber *y in yArray) {
@autoreleasepool{
CGRect snapshotRegion = CGRectMake([x doubleValue], [y doubleValue], 10, 10)
// resizableSnapshotViewFromRect 这个方法就是根据frame 去截图
UIView *snapshot = [snapView resizableSnapshotViewFromRect:snapshotRegion afterScreenUpdates:NO withCapInsets:UIEdgeInsetsZero]
snapshot.frame = snapshotRegion
[self addSubview:snapshot]
[snapshots addObject:snapshot]
}
}
}
[UIView animateWithDuration:1.5
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
for (UIView *view in snapshots) {
@autoreleasepool{
view.frame = CGRectOffset(view.frame, [self randomRange:200 offset:-100], [self randomRange:200 offset:self.frame.size.height/2])
self.alpha = 0.1
}
}
}completion:^(BOOL finished) {
for (UIView *view in snapshots) {
[view removeFromSuperview]
}
[self removeFromSuperview]
}]
}
- (CGFloat)randomRange:(NSInteger)range offset:(NSInteger)offset {
return (CGFloat)(arc4random()%range + offset)
}
@end
@interface ViewController ()
@property (nonatomic, strong) UIImageView * bgImageView
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
self.title = @"主视图"
[self.view addSubview:self.bgImageView]
}
- (UIImageView *)bgImageView {
if (!_bgImageView) {
_bgImageView = [[UIImageView alloc]init]
_bgImageView.image = [UIImage imageNamed:@"m8.jpg"]
_bgImageView.center = CGPointMake(Main_Width/2, Main_Height/2)
_bgImageView.bounds = CGRectMake(0, 0, 300, 300)
_bgImageView.contentMode = UIViewContentModeScaleAspectFill
_bgImageView.userInteractionEnabled = YES
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushNextVC)]
[_bgImageView addGestureRecognizer:tap]
}
return _bgImageView
}
- (void)pushNextVC {
SecondViewController * vc = [[SecondViewController alloc]init]
[self.navigationController pushViewController:vc animated:YES]
}
@end
@interface SecondViewController ()
@property (nonatomic, strong) UIImageView * debrisAnimationImageView
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad]
self.title = @"碎片动画返回效果"
[self.view addSubview:self.debrisAnimationImageView]
self.automaticallyAdjustsScrollViewInsets = NO
}
- (UIImageView *)debrisAnimationImageView{
if (!_debrisAnimationImageView) {
_debrisAnimationImageView = [[UIImageView alloc]init]
_debrisAnimationImageView.frame = CGRectMake(0, 64, Main_Width, Main_Height-64)
_debrisAnimationImageView.contentMode = UIViewContentModeScaleAspectFill
_debrisAnimationImageView.image = [UIImage imageNamed:@"we.jpeg"]
_debrisAnimationImageView.userInteractionEnabled = YES
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didSelectDebrisImageView)]
[_debrisAnimationImageView addGestureRecognizer:tap]
}
return _debrisAnimationImageView
}
- (void)didSelectDebrisImageView {
[[UIApplication sharedApplication].keyWindow addSubview:self.view]
[self.view fragmenttationAnimation]
[self.navigationController popViewControllerAnimated:NO]
}
@end