關于項目首頁的一些整理

1.關于布局方式

首頁本想采用collectionView加瀑布流方式,可是后來由于中間有輪播,要8個section,放棄了這種寫法。
那就自定義cell吧,可是每一組cell 都不同,所以反復創建了多種cell。在此期間也出現了一些問題。比如一個cell中有一個大item和4個小Item組成,開始想大的占兩個cell高度,剩下的排在大item后面,可是卻排在了下面,應該是flowLayout的問題,如果重寫,那么每個都要寫,又給否定了。
然后想著那就用for-Loop來創建吧,可是給寫在setter 方法里面了,導致每次滑動都會創建,使得圖冊無限疊加,耗費內存。后來寫在init方法中避免了疊加。

2.關于cell的item和出現分割線問題

第3個section有8個Item,想著那就用collection的優勢來創建吧,卻沒想到被幾條線擊碎了。Item之間的線沒有出頭,那就畫吧,可是一會顯示,一會不顯示,甚是折騰

在自定義CollectionCell的時候,添加一下代碼
UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.top.mas_equalTo(self.mas_top);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];

剩下的控件添加到view上就可以了

后來即將要上線,索性寫了個view上面放了8個imageView和label,還有要求的間隔線。雖然問題解決了,但是仔細想想,好像是完全放棄了collection的優勢,有些遺憾。此處還得繼續尋找更加合適的方案

3.關于collection刷新指定section或者row的問題

在iOS7 中reloadItemsAtIndexPaths 這個方法竟然會崩潰,找了好多資料,都沒有找到解決方法,只能 全部刷新了,看起來沒有多大的視覺變化,可是內存會有增加

Talk is cheap,show me the code:

// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:2];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:2];
[UIView performWithoutAnimation:^{//這個animation可以消除刷新的動畫
    if (IOS_VERSION < 8.0) {
        [_collectionView reloadData];
    }else{
        [_collectionView reloadItemsAtIndexPaths:@[indexPath]];
    }
}];

4.那么問題來了

一個cell中有8個imageView,用delegate看來是不行了,只能給每個imageView添加了手勢,又導致 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{}這個方法沒有使用到。

5.關于漸變式導航欄,不想多說,看code吧

-(void)viewWillAppear:(BOOL)animated{
    [superviewWillAppear:animated];
     if (self.navigationController) {
        [self.navigationControllersetNavigationBarHidden:YES animated:animated];
    }
}
-(void)viewDidDisappear:(BOOL)animated{    
    [superviewDidDisappear:animated];  
    if (self.navigationController) {
        [self.navigationControllersetNavigationBarHidden:NOanimated:NO];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y < 0) {
        [selfshowView:_naviViewhidden:YES];
        [selfsetStatusBarStyle:UIStatusBarStyleDefault];   
    }else if(scrollView.contentOffset.y > 50) {
        [selfshowView:_naviViewhidden:NO];
        CGFloat alpha = MIN(1, 1 - ((50 + 64 - scrollView.contentOffset.y) / 64));
        [_naviViewsetBackgroundColor:[[UIColorwhiteColor] colorWithAlphaComponent:alpha]];
    }else{
        [selfshowView:_naviViewhidden:NO];
        [_naviViewsetBackgroundColor:[[UIColorwhiteColor] colorWithAlphaComponent:0.0f]];
    }
}

-(void)showView:(UIView *)view hidden:(BOOL)hidden{      
        CATransition *animation = [CATransitionanimation];        
        animation.type = kCATransitionFade;    
        animation.duration = 0.4;    
        [view.layeraddAnimation:animation forKey:nil];    
        view.hidden = hidden;
}
//根據數組隨機生成一個長度為3內容不重復的數組
-(NSArray*)createRandomArray:(NSMutableArray*)mutableArray{
    
    if (mutableArray.count < 4) {
        return mutableArray;
    }else{
        NSMutableArray *startArray = [NSMutableArray array];
        NSMutableArray *resultArray=[[NSMutableArray alloc] initWithCapacity:0];
        for (NSInteger i = 0; i < mutableArray.count; i++) {
            [startArray addObject:[NSString stringWithFormat:@"%ld",i]];
        }
        for (int i = 0; i<3; i++) {
            int t = arc4random()%startArray.count;
            resultArray[i] = startArray[t];
            startArray[t] = [startArray lastObject];
            [startArray removeLastObject];
        }
        NSInteger i = [resultArray[0] integerValue];
        NSInteger j = [resultArray[1] integerValue];
        NSInteger k = [resultArray[2] integerValue];
        
        return @[mutableArray[i], mutableArray[j], mutableArray[k]];
    }
}

小結:此次雖然完成改版,但是還是有許多可優化更改的地方,仙路漫漫,道友仍需努力!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容