|
//
// CollectionController.m
// shelf_test
//
// Created by Scar on 14-8-26.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "CollectionController.h"
@interface collectCell:UICollectionViewCell {
}
@property(nonatomic, strong)UILabel *textLabel;
@property(nonatomic, strong)UILabel *assistLabel;
@property(nonatomic, strong)NSDictionary *dataDic;
@property(nonatomic, strong)UIView *selectFrontView;
@end
@implementation collectCell
@synthesize dataDic = _dataDic;
@synthesize selectFrontView;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.selectFrontView = [[UIView alloc]initWithFrame:CGRectZero];
self.selectFrontView.backgroundColor = [UIColor blackColor];
self.selectFrontView.alpha = 0.2;
self.selectFrontView.hidden = YES;
[self addSubview:self.selectFrontView];
[self addSubview:self.textLabel];
[self addSubview:self.assistLabel];
}
return self;
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.selectFrontView.hidden = NO;
}else {
self.selectFrontView.hidden = YES;
}
}
- (void)setDataDic:(NSDictionary *)dic {
_dataDic = dic;
self.backgroundColor = dic[@"color"] ? [UIColor colorWithHexString:dic[@"color"]]:[UIColor clearColor];
self.textLabel.text = dic[@"name"] ? dic[@"name"]:@"";
self.assistLabel.text = dic[@"enname"] ? dic[@"enname"]:@"";
CGFloat height = [self.textLabel sizeThatFits:CGSizeMake(35, CGFLOAT_MAX)].height;
if (self.textLabel.text.length < 4) {
self.textLabel.frame = CGRectMake(0, CGRectGetHeight(self.bounds)*8/15-height, CGRectGetWidth(self.bounds), height);
}else {
self.textLabel.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-70/4, CGRectGetHeight(self.bounds)*2/3-height+3, 70/2, height);
}
CGFloat assHeight = [self.assistLabel sizeThatFits:CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX)].height;
self.assistLabel.frame = CGRectMake(0, CGRectGetMaxY(self.textLabel.frame), CGRectGetWidth(self.bounds), assHeight);
self.selectFrontView.frame = self.bounds;
}
- (void)layoutSubviews {
[super layoutSubviews];
}
- (UILabel*)textLabel {
if (!_textLabel) {
_textLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.textAlignment = NSTextAlignmentCenter;
_textLabel.numberOfLines = 0;
_textLabel.lineBreakMode = NSLineBreakByWordWrapping;
_textLabel.textColor = [UIColor colorWithHexString:@"0xffffff"];
}
_textLabel.font = appFont(TEXT_THREE_LEVELSIZE, NO);
return _textLabel;
}
- (UILabel*)assistLabel {
if (!_assistLabel) {
_assistLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_assistLabel.backgroundColor = [UIColor clearColor];
_assistLabel.textAlignment = NSTextAlignmentCenter;
_assistLabel.textColor = [UIColor colorWithHexString:@"0xffffff"];
}
//英文小字号,待讨论
_assistLabel.font = appFont(7, NO);
return _assistLabel;
}
@end
@interface CollectionController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate> {
NSInteger isInLoop;
BOOL dataRefreshSign;
UILabel *textLabel;
CGFloat cardPadding;
CGFloat cardWidthAndHeight;
}
@property(nonatomic, strong)UICollectionView *collectView;
//@property(nonatomic, strong)NSTimer *scrollTimer;
@property(nonatomic, strong)NSDictionary *sourceDic; //初始数据字典
@end
@implementation CollectionController
@synthesize collectView;
//@synthesize scrollTimer;
@synthesize dataList = _dataList;
@synthesize sourceDic;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dataRefreshSign = NO;
if (IS_IPHONE_6P) {
cardPadding = 88/3;
cardWidthAndHeight = (rect_screen.size.width-5*cardPadding)/4;
}else if (IS_IPHONE_6) {
cardPadding = 15;
cardWidthAndHeight = (rect_screen.size.width-5*cardPadding)/4;
}else {
cardPadding = 15;
cardWidthAndHeight = (rect_screen.size.width-5*cardPadding)/4;
}
// Do any additional setup after loading the view.
self.view.clipsToBounds = NO;
self.view.backgroundColor = [UIColor clearColor];
CGSize itemSize = CGSizeMake(cardWidthAndHeight, cardWidthAndHeight);
CGFloat space = cardPadding;
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumLineSpacing = cardPadding;
layout.minimumInteritemSpacing = space;
layout.itemSize = itemSize;
collectView = [[UICollectionView alloc]initWithFrame:self.view.bounds
collectionViewLayout:layout];
collectView.delegate = self;
collectView.dataSource = self;
collectView.clipsToBounds = NO;
collectView.delaysContentTouches = NO;
[collectView registerClass:[collectCell class] forCellWithReuseIdentifier:@"CollectionViewIdentifier"];
collectView.allowsSelection = YES;
collectView.scrollEnabled = NO;
collectView.backgroundColor = [UIColor clearColor];
[self.view addSubview:collectView];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
[self.collectView addGestureRecognizer:longPress];
textLabel = [UILabel new];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.text = @"长按拖动调整顺序";
textLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO);
textLabel.textColor = [UIColor colorWithHexString:TextLightGray];
textLabel.numberOfLines = 0;
textLabel.lineBreakMode = NSLineBreakByCharWrapping;
[self.view addSubview:textLabel];
CGFloat offset = cardWidthAndHeight/2-22;
/**
* bug:5786(【倒退】频道导航页:“长按拖动调整顺序”未居中)
*/
[textLabel makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view.right).offset(-cardPadding-offset);
make.bottom.equalTo(self.view.bottom).offset(-cardPadding);
make.size.mas_equalTo(CGSizeMake(44, 30));
}];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.dataList && self.dataList.count > 0) {
self.sourceDic = self.dataList[[TPUserDefault instance].currentPageIndex];
/**
* bug:5069( 频道导航页,手动拖动频道排序后,应立即切换排序)
*/
}
}
//#pragma mark - collction View delegate and datasource
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
needGestureOfSideController(NO);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
needGestureOfSideController(YES);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.dataList && self.dataList.count > 0) {
return self.dataList.count;
}else return 0;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellId = @"CollectionViewIdentifier";
collectCell *cell = (collectCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
if (!cell) {
}
// [cell sizeToFit];
if (self.dataList && self.dataList.count>0) {
NSDictionary *channelDic = self.dataList[indexPath.row];
// if (channelDic[@"color"] && [channelDic[@"color"] isKindOfClass:[NSString class]]) {
// NSString *colorString = channelDic[@"color"];
// cell.backgroundColor = [UIColor colorWithHexString:colorString];
//
// }
cell.dataDic = channelDic;
cell.alpha = 1.0f;
cell.hidden = NO;
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if ([self.dataList[indexPath.row][@"nodeId"] isEqualToString:@"-3"]) {
[MobClick event:@"79"];
}
[[NSNotificationCenter defaultCenter] postNotificationName:SELECTCHANNEL object:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
// collectCell *cell = (collectCell*)[collectionView cellForItemAtIndexPath:indexPath];
//
// [UIView animateWithDuration:0.3 animations:^{
// cell.selectFrontView.hidden = NO;
// } completion:^(BOOL finished) {
// }];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(cardWidthAndHeight, cardWidthAndHeight);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets top = {cardPadding,cardPadding,0,cardPadding};
return top;
}
#pragma mark - long press gesture event
- (void)longPressGestureRecognized:(id)sender {
needGestureOfSideController(NO);
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;
CGPoint location = [longPress locationInView:self.view];
CGPoint collectLoaction = location;
collectLoaction.x = collectLoaction.x + self.collectView.contentOffset.x;
collectLoaction.y = collectLoaction.y-0;
NSIndexPath *indexPath = [self.collectView indexPathForItemAtPoint:collectLoaction];
static UIView *snapshot = nil;
static NSIndexPath *sourceIndexPath = nil;
static UIColor *firstColor;
static UICollectionViewCell* firstCell;
switch (state) {
case UIGestureRecognizerStateBegan: {
if (indexPath) {
if (indexPath.row == 0) {
break;
}
sourceIndexPath = indexPath;
UICollectionViewCell *cell = [self.collectView cellForItemAtIndexPath:indexPath];
firstCell = [self.collectView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
firstColor = firstCell.backgroundColor;
// Take a snapshot of the selected row using helper method.
snapshot = [self customSnapshoFromView:cell];
__block CGPoint center = cell.center;
center.x = center.x - self.collectView.contentOffset.x;
center.y = center.y+0;
snapshot.center = center;
snapshot.alpha = 0.0;
[self.view addSubview:snapshot];
[UIView animateWithDuration:0.35 animations:^{
firstCell.backgroundColor = [UIColor colorWithHexString:BUTTONDISABLEBACK];
center.y = location.y;
center.x = location.x;
snapshot.center = center;
snapshot.transform = CGAffineTransformMakeScale(1.3, 1.3);
snapshot.alpha = 0.98;
cell.hidden = YES;
// cell.backgroundColor = [UIColor blackColor];
cell.alpha = 0.0f;
} completion:nil];
}
break;
}
case UIGestureRecognizerStateChanged: {
// isInLoop = 0;
// [self.scrollTimer invalidate];
if (!snapshot) {
return;
/**
* bug:5273(频道导航页,按住“长按拖动调整顺序”,会把其他频道都擦掉)
*/
}
CGPoint center = snapshot.center;
center.y = location.y;
center.x = location.x;
snapshot.center = center;
UICollectionViewCell *cell = [self.collectView cellForItemAtIndexPath:indexPath];
UICollectionViewCell *sCell = [self.collectView cellForItemAtIndexPath:sourceIndexPath];
cell.alpha = 0.0f;
sCell.alpha = 0.0f;
// //...scroll view when snap shot is in the edge
// if (collectLoaction.x - self.collectView.contentOffset.x + 50 > self.collectView.frame.size.width) {
// isInLoop = 1;
// self.scrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(scrollAction:) userInfo:nil repeats:YES];
// [[NSRunLoop currentRunLoop] addTimer:self.scrollTimer forMode:NSRunLoopCommonModes];
// }else if (collectLoaction.x - self.collectView.contentOffset.x - 50 < 0) {
// isInLoop = 2;
// self.scrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(scrollAction:) userInfo:nil repeats:YES];
// [[NSRunLoop currentRunLoop] addTimer:self.scrollTimer forMode:NSRunLoopCommonModes];
// }
if (indexPath.row == 0) {
cell.alpha = 1.0f;
sCell.alpha = 1.0f;
break;
}
//... animation about cell
if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
if (sourceIndexPath.row != 0) {
//...update data source
NSObject *obj = self.dataList[sourceIndexPath.row];
NSMutableArray *tempList = [NSMutableArray arrayWithArray:self.dataList];
if (indexPath.row == self.dataList.count - 1) {
[tempList removeObject:obj];
[tempList addObject:obj];
}else {
[tempList removeObject:obj];
[tempList insertObject:obj atIndex:indexPath.row];
}
dataRefreshSign = YES;
self.dataList = tempList;
//...change cell location
[self.collectView moveItemAtIndexPath:sourceIndexPath toIndexPath:indexPath];
sourceIndexPath = indexPath;
sCell.backgroundColor = [UIColor clearColor];
}
}
// center.y = location.y;
// center.x = location.x;
// snapshot.center = center;
break;
}
default: {
if (!snapshot) {
return;
}
// clean up.
UICollectionViewCell *cell = [self.collectView cellForItemAtIndexPath:sourceIndexPath];
NSDictionary *dic = self.dataList[sourceIndexPath.row];
UICollectionViewCell *curentCell = [self.collectView cellForItemAtIndexPath:indexPath];
// testCell *currentCell = (testCell*)[self.collectView cellForItemAtIndexPath:indexPath];
[UIView animateWithDuration:0.35 animations:^{
firstCell.backgroundColor = firstColor;
__block CGPoint snapShotCenter = cell.center;
snapShotCenter.x = cell.center.x - self.collectView.contentOffset.x;
snapShotCenter.y = snapShotCenter.y +0;
snapshot.center = snapShotCenter;
snapshot.transform = CGAffineTransformIdentity;
snapshot.alpha = 0.0;
cell.backgroundColor = [UIColor colorWithHexString:dic[@"color"]];
cell.hidden = NO;
cell.alpha = 1.0f;
curentCell.hidden = NO;
curentCell.alpha = 1.0f;
//
// isInLoop = 0;
// [self.scrollTimer invalidate];
} completion:^(BOOL finished) {
[MobClick event:@"48"];
[snapshot removeFromSuperview];
snapshot = nil;
[self.collectView reloadData];
[self addDataListToMemory];
needGestureOfSideController(YES);
}];
sourceIndexPath = nil;
break;
}
}
}
#pragma mark - make snapshot method
- (UIView *)customSnapshoFromView:(UIView *)inputView { //The method that 'snapshotViewAfterScreenUpdates' has bugs in iphone 6 and 6+
UIView *snapshot = UIView.new;
// if ([inputView respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)]) { //ios 7 snapshot method
// snapshot = [inputView snapshotViewAfterScreenUpdates:YES];
// snapshot.layer.masksToBounds = NO;
// snapshot.layer.cornerRadius = 0.0;
//// snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
//// snapshot.layer.shadowRadius = 5.0;
//// snapshot.layer.shadowOpacity = 0.4;
// }else { //...make snapshot avilibile under ios7
// snapshot = [[UIView alloc]initWithFrame:inputView.frame];
// UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, YES, 1);
// [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
// UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// UIImageView *imageView = [[UIImageView alloc]initWithImage:viewImage];
// [snapshot addSubview:imageView];
// }
snapshot = [[UIView alloc]initWithFrame:inputView.frame];
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, inputView.opaque, 7.0);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc]initWithImage:viewImage];
[snapshot addSubview:imageView];
return snapshot;
}
//- (void)scrollAction:(NSTimer*)timer {
// if (isInLoop == 0) {
// [timer invalidate];
// }else if (isInLoop == 1) {
// if (self.collectView.contentOffset.x > 90*3) {
// isInLoop = 0;
// [timer invalidate];
// [self.collectView setContentOffset:CGPointMake(90*3.5,0) animated:YES];
// return;
// }
// [self.collectView setContentOffset:CGPointMake(self.collectView.contentOffset.x+90,0) animated:YES];
// }else {
// if (self.collectView.contentOffset.x < 65-25) {
// isInLoop = 0;
// [timer invalidate];
// [self.collectView setContentOffset:CGPointMake(0,0) animated:YES];
// return;
// }
// [self.collectView setContentOffset:CGPointMake(self.collectView.contentOffset.x-70,0) animated:YES];
// }
//}
- (void)reloadData {
[self.collectView reloadData];
}
- (void)setDataList:(NSMutableArray *)list {
_dataList = list;
textLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO);
if (!dataRefreshSign) {
[self.collectView reloadData];
self.sourceDic = list[[TPUserDefault instance].currentPageIndex];
if (self.collectView.contentOffset.x == 0) {
[self.collectView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:[TPUserDefault instance].currentPageIndex inSection:0]
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:NO];
}
}
dataRefreshSign = NO;
}
//将数据源数据加入全局存储
- (void)addDataListToMemory {
if (self.dataList.count > 0) {
NSMutableArray *tempList = [NSMutableArray arrayWithArray:[TPUserDefault instance].channldataList];
[self.dataList enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
if ([TPUserDefault instance].channldataList.count != 7) {
[tempList addObject:dic];
}else {
[tempList replaceObjectAtIndex:idx withObject:dic];
}
//重置首页当前页面
if (self.sourceDic) {
if ([self.sourceDic[@"name"] isEqualToString:dic[@"name"]]) {
[TPUserDefault instance].currentPageIndex = idx;
}
}
}];
[TPUserDefault instance].channldataList = tempList;
self.dataList = tempList;
}
}
- (void)dealloc {
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|