点此下载源码下载:源码(会持续更新,欢迎star。保证炫酷,童叟无欺!)
从dribbble的设计师的作品中了解到City Guides上非常优秀的动画效果。感叹设计师很棒的设计的同时,小编也在心里默想这些动画是怎么实现的。于是从App Store上下载了APP,使用然后仔细研究后便有了此篇文章。
City Guides中几乎所有的界面展示与交互方式都是有动画效果的。小编也就分几部分来实现动画效果。
这一篇要实现的动画效果如下:
第一个动画效果:
第二个动画效果:
本篇文章只讲解实现思路,具体的可以参见源码。
动画效果一
第一个动画效果,实际上包括三个部分动画效果。选中动画效果、动画转场和切换动画效果。本篇文章中所有的动画,使用POP和Core Animation实现。
选中动画效果
此动画实现相对简单一些,中间部分是使用的UICollectionView,创建了四个cell。在可视的cell中实现被选中的cell放大,其余可视的cell缩放的同时透明度减少。使用POP实现的方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *selectedCell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; for (UICollectionViewCell *cell in collectionView.visibleCells) { if ([cell isEqual:selectedCell]) { //选中的cell的放大 POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY]; scaleAnimation.duration = 0.5; scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1.05, 1.05)]; [cell.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"]; POPBasicAnimation *alphaAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha]; alphaAnimation.duration = 0.5; alphaAnimation.toValue = @1.0; [cell pop_addAnimation:alphaAnimation forKey:nil]; [alphaAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) { // if (finished) { // // ZFMainTabBarController *mainTabBarVC = [[ZFMainTabBarController alloc] init]; // mainTabBarVC.modalPresentationStyle = UIModalPresentationOverCurrentContext; // mainTabBarVC.transitioningDelegate = self; // // self.interactionViewController = [ZFInteractiveTransition new]; // [self presentViewController:mainTabBarVC animated:YES completion:NULL]; // } }]; } else{ //未选中的可视cell的缩放 POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY]; scaleAnimation.duration = 0.5; scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(0.95, 0.95)]; [cell.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"]; POPBasicAnimation *alphaAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha]; alphaAnimation.duration = 0.5; alphaAnimation.toValue = @0.7; [cell pop_addAnimation:alphaAnimation forKey:nil]; } } } |
此部分实现的效果图:
动画转场
此部分的转场过渡动画效果,参考小编上篇文章讲解的自定义转场。里面涉及到手势交互,下一篇文章再做详细讲解。
切换效果动画
实际上是点击最上面的两个button后,对应的视图动画效果。
切换到SLIDES后,UICollectionView整个视图向左移,移出当前屏幕。与此同时可视的UICollectionViewCell随着变化,而且每个cell的变化有区别。区别是:移出的时候,第二个cell最后消失在视线里(偏移量最小),第四个cell旋转幅度较大(角度最大)。
实现的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
-(void)showSlideLayout:(UIButton *)sender{ if (_SliderButton.selected) { return; } [_scrollView setHidden:NO]; _SliderButton.selected = YES; _GridButton.selected = NO; _SliderButton.userInteractionEnabled = NO; [self canEnableClick]; _backgroundLayer.frame = sender.frame; int i = 0; for (UICollectionViewCell *cell in _cityCollectView.visibleCells) { NSArray *translationArray = @[@-300, @-200, @-600, @-600]; NSArray *angles = @[@(-15* M_PI/180), @(-30* M_PI/180), @(-15* M_PI/180), @(-60* M_PI/180)]; NSArray *scaleArray = @[@0, @0, @0, @-5]; //未选中的可视cell的缩放 POPBasicAnimation *rotationAnimation = [POPBasicAnimation easeInEaseOutAnimation]; rotationAnimation.property = [POPAnimatableProperty propertyWithName:kPOPLayerRotation]; rotationAnimation.duration = duration; rotationAnimation.fromValue = @(0); rotationAnimation.toValue = angles[i]; POPBasicAnimation *translationAnimation = [POPBasicAnimation easeInEaseOutAnimation]; translationAnimation.property = [POPAnimatableProperty propertyWithName:kPOPLayerTranslationX]; translationAnimation.duration = duration; translationAnimation.fromValue = @(0); translationAnimation.toValue = translationArray[i]; POPBasicAnimation *zPositionAnimation = [POPBasicAnimation easeInEaseOutAnimation]; zPositionAnimation.property = [POPAnimatableProperty propertyWithName:kPOPLayerZPosition]; zPositionAnimation.duration = duration; zPositionAnimation.toValue = scaleArray[i]; [cell.layer pop_addAnimation:rotationAnimation forKey:@"rotationAnimation"]; [cell.layer pop_addAnimation:translationAnimation forKey:@"translationAnimation"]; [cell.layer pop_addAnimation:zPositionAnimation forKey:@"zPositionAnimation"]; i++; } // collectionView 移动 CGRect fromFrame = _cityCollectView.frame; CGRect toFrome = fromFrame; fromFrame.origin.x -= fromFrame.size.width; _cityCollectView.frame = fromFrame; POPBasicAnimation *frameAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame]; frameAnimation.name = @"showSliderView"; frameAnimation.duration = duration; frameAnimation.fromValue = [NSValue valueWithCGRect:toFrome]; frameAnimation.toValue = [NSValue valueWithCGRect:fromFrame]; frameAnimation.delegate = self; [_cityCollectView pop_addAnimation:frameAnimation forKey:@"frameAnimation"]; [frameAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) { if (finished) { [_cityCollectView setHidden:YES]; } }]; } |
切换到GRID后,UICollectionView整个视图向右移,渐入到当前屏幕。UICollectionViewCell变化与上面有些不一样。第3个cell 和第4个cell最后完成动画,其动画变化较大。