iOS9多任务处理
升级到iOS9的小伙伴都会发现ios9有一个非常炫酷的界面,之前使用的CollectionView实现,但是现在却给用户不一样的体验,笔者就简单研究了一下,并且找了一个小例子。
首先pod一下:
- platform :ios, "8.0"
- pod "iCarousel"
关于iCarousel(牛逼第三方)这里就不多说,等笔者有机会深入研究源码在分享,
然后撒野别不管,把下面的代码拷贝到你的控制器实现文件中,运行
1 /** 2 * iCarousel原理 3 * 4 * @param <iCarouselDelegate <iCarouselDelegate description 5 * @param nonatomic nonatomic description 6 * @param strong strong description 7 * 8 * @return 9 */ 10 11 /* 12 iCarousel并不是一个UIScrollView 也并没有包含任何UIScrollView作为subView 13 iCarousel通过UIPanGestureRecognizer来计算和维护scrollOffset这个变量 14 iCarousel通过scrollOffset来驱动整个动画过程 15 iCarousel本身并不会改变itemView的位置 而是靠修改itemView的layer.transform来实现位移和形变 16 */ 17 18 #import "iCocosController.h" 19 20 #import <iCarousel.h> 21 22 @interface iCocosController ()<iCarouselDelegate,iCarouselDataSource> 23 24 @property (nonatomic, strong) iCarousel *carousel; 25 @property (nonatomic, assign) CGSize cardSize; 26 27 @end 28 29 @implementation iCocosController 30 31 - (void)viewDidLoad { 32 [super viewDidLoad]; 33 34 CGFloat cardWidth = [UIScreen mainScreen].bounds.size.width*5.0f/7.0f; 35 self.cardSize = CGSizeMake(cardWidth, cardWidth*16.0f/9.0f); 36 self.view.backgroundColor = [UIColor blackColor]; 37 38 self.carousel = [[iCarousel alloc] initWithFrame:[UIScreen mainScreen].bounds]; 39 [self.view addSubview:self.carousel]; 40 self.carousel.delegate = self; 41 self.carousel.dataSource = self; 42 self.carousel.type = iCarouselTypeCustom; 43 self.carousel.bounceDistance = 0.2f; 44 } 45 46 47 - (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel 48 { 49 return 15; 50 } 51 - (CGFloat)carouselItemWidth:(iCarousel *)carousel 52 { 53 return self.cardSize.width; 54 } 55 - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view 56 { 57 UIView *cardView = view; 58 59 if ( !cardView ) 60 { 61 cardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.cardSize.width, self.cardSize.height)]; 62 63 UIImageView *imageView = [[UIImageView alloc] initWithFrame:cardView.bounds]; 64 [cardView addSubview:imageView]; 65 imageView.contentMode = UIViewContentModeScaleAspectFill; 66 imageView.backgroundColor = [UIColor whiteColor]; 67 68 cardView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:imageView.frame cornerRadius:5.0f].CGPath; 69 cardView.layer.shadowRadius = 3.0f; 70 cardView.layer.shadowColor = [UIColor blackColor].CGColor; 71 cardView.layer.shadowOpacity = 0.5f; 72 cardView.layer.shadowOffset = CGSizeMake(0, 0); 73 74 CAShapeLayer *layer = [CAShapeLayer layer]; 75 layer.frame = imageView.bounds; 76 layer.path = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:5.0f].CGPath; 77 imageView.layer.mask = layer; 78 } 79 80 return cardView; 81 } 82 83 /** 84 * 灵魂 85 */ 86 //- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset 87 88 //- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view 89 //{ 90 // UIView *cardView = view; 91 // 92 // if ( !cardView ) 93 // { 94 // cardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.cardSize.width, self.cardSize.height)]; 95 // 96 // ... 97 // ... 98 // 99 // //添加一个lbl 100 // UILabel *lbl = [[UILabel alloc] initWithFrame:cardView.bounds]; 101 // lbl.text = [@(index) stringValue]; 102 // [cardView addSubview:lbl]; 103 // lbl.font = [UIFont boldSystemFontOfSize:200]; 104 // lbl.textAlignment = NSTextAlignmentCenter; 105 // } 106 // 107 // return cardView; 108 //} 109 110 - (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform 111 { 112 NSLog(@"%f",offset); 113 114 // return transform; 115 // return CATransform3DTranslate(transform, offset * self.cardSize.width, 0, 0); 116 CGFloat scale = [self scaleByOffset:offset]; 117 CGFloat translation = [self translationByOffset:offset]; 118 119 // return CATransform3DScale(CATransform3DTranslate(transform, translation * self.cardSize.width, 0, 0), scale, scale, 1.0f); 120 return CATransform3DScale(CATransform3DTranslate(transform, translation * self.cardSize.width, 0, offset), scale, scale, 1.0f); 121 } 122 - (void)carouselDidScroll:(iCarousel *)carousel 123 { 124 for ( UIView *view in carousel.visibleItemViews) 125 { 126 CGFloat offset = [carousel offsetForItemAtIndex:[carousel indexOfItemView:view]]; 127 128 if ( offset < -3.0 ) 129 { 130 view.alpha = 0.0f; 131 } 132 else if ( offset < -2.0f) 133 { 134 view.alpha = offset + 3.0f; 135 } 136 else 137 { 138 view.alpha = 1.0f; 139 } 140 } 141 } 142 //形变是线性的就ok了 143 - (CGFloat)scaleByOffset:(CGFloat)offset 144 { 145 return offset*0.04f + 1.0f; 146 } 147 //位移通过得到的公式来计算 148 - (CGFloat)translationByOffset:(CGFloat)offset 149 { 150 CGFloat z = 5.0f/4.0f; 151 CGFloat n = 5.0f/8.0f; 152 153 //z/n是临界值 >=这个值时 我们就把itemView放到比较远的地方不让他显示在屏幕上就可以了 154 if ( offset >= z/n ) 155 { 156 return 2.0f; 157 } 158 159 return 1/(z-n*offset)-1/z; 160 } 161 162 @end
最后你会发现奇怪的现象,就是非常简单的实现了多任务
时间: 2024-10-06 00:58:42