iOS - 圖片實現多層折疊效果
本文是投稿文章,作者: Resory
序
在網上能找到挺多圖片折疊效果的教程,但大多數是一層折疊,在他們的教程的基礎上,我試著實現了一下多層折疊的效果。操作如下~
效果
Demo
Demo地址: https://github.com/Resory/RYMutipleFoldImageView
如果官人喜歡的話不妨給個星星吧。
邏輯
-
在做圖片折疊效果的時候,我們可以理解為把圖片分成幾部分,然后分別對各部分做動效來實現折疊效果。
-
根據動態圖,可以看到這是一張大圖"分成"4個小imageView。從上至下,我們分別命名為one,two,three,four
-
對one,two,three,four這四個小imageView進行旋轉+移動。旋轉的時候,關鍵是看各個imageView的anchorPoint是多少.而我們這里,可看p1圖中的紅點。
1.a1代表one的anchorPoint為(0.5,0.0)
2.a2代表two的anchorPoint為(0.5,1.0)
3.a3代表three的anchorPoint為(0.5,0.0)
4.a4代表four的anchorPoint為(0.5,1.0)
-
旋轉: 我們這里的imageView都是旋轉45°或者是-45°,這個用CATransform3DRotate即可完成。
-
移動(關鍵):
1.旋轉后,各個imageView都會變形并且都一樣大小,只有位置不一樣,我們要根據這個旋轉后的imageView高度來進行移動。
2.比如two要和one對接。根據動態圖,one只有旋轉,沒有移動。而two則旋轉和移動了。那么移動了多少呢。在沒有折疊前,所有的imageView高度都是50px。也就是one和two總共加起來是100px。而折疊后。one和two都變小了。就是因為他們兩個都變小了。所以中間就出現了縫隙,這個縫隙就是我們要移動的距離。而我們知道在二維空間中,總長度是100px,one,two的高度在旋轉后是可以算出來的,也就是說縫隙的二維空間距離是:100-2*(one.frame.size.height)。,然后再經過CATransform3DMakeAffineTransform方法的轉換得到真實地三維空間移動的距離。
實現
-
初始化4個小imageView(contentsRect的運用)
- (void)configFourFoldImage { UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 300, IMAGE_PER_HEIGIT*4)]; [self.view addSubview:bgView]; // 把kiluya這張圖,分成平均分成4個部分的imageview _one = [[UIImageView alloc] init]; _one.image = [UIImage imageNamed:@"Kiluya.jpg"]; _one.layer.contentsRect = CGRectMake(0, 0, 1, 0.25); _one.layer.anchorPoint = CGPointMake(0.5, 0.0); _one.frame = CGRectMake(0, 0, 300, IMAGE_PER_HEIGIT); _two = [[UIImageView alloc] init]; _two.image = [UIImage imageNamed:@"Kiluya.jpg"]; _two.layer.contentsRect = CGRectMake(0, 0.25, 1, 0.25); _two.layer.anchorPoint = CGPointMake(0.5, 1.0); _two.frame = CGRectMake(0, IMAGE_PER_HEIGIT, 300, IMAGE_PER_HEIGIT); _three = [[UIImageView alloc] init]; _three.image = [UIImage imageNamed:@"Kiluya.jpg"]; _three.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.25); _three.layer.anchorPoint = CGPointMake(0.5, 0.0); _three.frame = CGRectMake(0, IMAGE_PER_HEIGIT*2, 300, IMAGE_PER_HEIGIT); _four = [[UIImageView alloc] init]; _four.image = [UIImage imageNamed:@"Kiluya.jpg"]; _four.layer.contentsRect = CGRectMake(0, 0.75, 1, 0.25); _four.layer.anchorPoint = CGPointMake(0.5, 1.0); _four.frame = CGRectMake(0, IMAGE_PER_HEIGIT*3, 300, IMAGE_PER_HEIGIT); [bgView addSubview:_one]; [bgView addSubview:_two]; [bgView addSubview:_three]; [bgView addSubview:_four]; // 給第一張和第三張添加陰影 _oneShadowView = [[UIView alloc] initWithFrame:_one.bounds]; _oneShadowView.backgroundColor = [UIColor blackColor]; _oneShadowView.alpha = 0.0; _threeShadowView = [[UIView alloc] initWithFrame:_three.bounds]; _threeShadowView.backgroundColor = [UIColor blackColor]; _threeShadowView.alpha = 0.0; [_one addSubview:_oneShadowView]; [_three addSubview:_threeShadowView]; }
生成折疊動效需要的CATransform3D
- (CATransform3D)config3DTransformWithRotateAngle:(double)angle andPositionY:(double)y { CATransform3D transform = CATransform3DIdentity; // 立體 transform.m34 = -1/1000.0; // 旋轉 CATransform3D rotateTransform = CATransform3DRotate(transform, M_PI*angle/180, 1, 0, 0); // 移動(這里的y坐標是平面移動的的距離,我們要把他轉換成3D移動的距離.這是關鍵,沒有它,圖片就沒辦法很好地對接。) CATransform3D moveTransform = CATransform3DMakeAffineTransform(CGAffineTransformMakeTranslation(0, y)); // 合并 CATransform3D concatTransform = CATransform3DConcat(rotateTransform, moveTransform); return concatTransform; }
-
折疊
// 動效是否執行中 static bool isFolding = NO; - (IBAction)fold:(id)sender { if(!isFolding) { isFolding = YES; [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{ // 陰影顯示 _oneShadowView.alpha = 0.2; _threeShadowView.alpha = 0.2; // 折疊 _one.layer.transform = [self config3DTransformWithRotateAngle:-45.0 andPositionY:0]; _two.layer.transform = [self config3DTransformWithRotateAngle:45.0 andPositionY:-100+2*_one.frame.size.height]; _three.layer.transform = [self config3DTransformWithRotateAngle:-45.0 andPositionY:-100+2*_one.frame.size.height]; _four.layer.transform = [self config3DTransformWithRotateAngle:45.0 andPositionY:-200+4*_one.frame.size.height]; } completion:^(BOOL finished) { if(finished) { isFolding = NO; } }]; } }
-
恢復
- (IBAction)reset:(id)sender { isFolding = NO; [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{ // 陰影隱藏 _oneShadowView.alpha = 0.0; _threeShadowView.alpha = 0.0; // 圖片恢復原樣 _one.layer.transform = CATransform3DIdentity; _two.layer.transform = CATransform3DIdentity; _three.layer.transform = CATransform3DIdentity; _four.layer.transform = CATransform3DIdentity; } completion:^(BOOL finished) { }]; }
末
這里關鍵是縫隙的計算,這個想明白了。其他就沒什么了。
如果你有疑問或者發現錯誤請留言給我,喜歡就點個贊吧!3Q