图片打碎还原效果

2017-03-22  本文已影响28人  草书行者

//实现原理是用layer.contensRect实现的,参考文献:http://blog.csdn.net/mamong/article/details/8534999

CGFloat ratio = 0.1;
for (int i = 0; i < 100; i++) {
UIImageView imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor redColor];
imageView.image = image;
NSInteger column = i % 10; // 列
NSInteger row = i / 10; // 行
//层内容的可视区域
imageView.layer.contentsRect = CGRectMake(ratio
column, rowratio, ratio, ratio);
imageView.frame = CGRectMake(self.frame.size.width
ratiocolumn, row(self.frame.size.heightratio), self.frame.size.widthratio, self.frame.size.height*ratio);
[self addSubview:imageView];//将控件添加到self
}

//然后用UIView animateWithDuration动画改变所有self的所有子控件layer.transform属性就可以实现打碎效果,还原也一样的原理
// 打碎

-(void)smash {
[UIView animateWithDuration:1 animations:^{
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOLBOOL * _Nonnull stop) {
UIImageView *imageView = obj;
imageView.layer.transform = [self configTransform3DWithRotateAngle:[self getRandomNumber:0 to:360] andPositionX:[self getRandomNumber:0 to:100] andPositionY:[self getRandomNumber:0 to:100]];
}];
}];

}

//该方法是传一个角度,x,y值进来 returny一个CATransform3D类型的对象

//传入两个int数据类型生成一个范围的随机数
-(CGFloat)getRandomNumber:(int)from to:(int)to
{
return (from+ 1 + (arc4random() % (to - from + 1)));
}

源代码地址:https://github.com/PanDongGG/smashImageView

20160714151801208.gif
上一篇 下一篇

猜你喜欢

热点阅读