@property (assign, nonatomic)int page;
@property (assign, nonatomic)int pageCount;
@property (strong, nonatomic) ScrollView *scrollView;
@end
@implementation MainViewController
// 构建广告滚动视图
- (void)createScrollView {
self.scrollView = [[AdvertisingScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 175)];
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.scrollView.imageNameArray = [NSArray arrayWithObjects:@"picture1.png",@"picture2.png",@"picture3.png",@"picture4.png", nil];
self.scrollView.PageControlShowStyle = UIPageControlShowStyleCenter;
self.scrollView.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
self.scrollView.pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
[self.view addSubview:self.scrollView];
}
- (id)init
{
self = [super init];
if (self) {
//[self setup];
}
return self;
}
//加载新数据
-(void)LoadNewMessage
{
}
//向上重新加载数据
-(void)UpReloadData
{//当页面小于总页数 就加入数据 否则等于原始数据
[self.collectionView addHeaderWithCallback:^{
if (self.page < self.pageCount) {
self.page ++;
} else {
self.page = self.pageCount;
}
[self LoadNewMessage];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 结束刷新
[self.collectionView headerEndRefreshing];
});
}];
}
//向下重新加载数据
-(void)DownReloadData
{
[self.collectionView addFooterWithCallback:^{
if (self.page < self.pageCount) {
self.page ++;
} else {
self.page = self.pageCount;
}
[self LoadNewMessage];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 结束刷新
[self.collectionView footerEndRefreshing];
});
}];
}
- (void)dealloc
{
NSLog(@"MJCollectionViewController--dealloc---");
}
- (void)viewDidLoad {
/* x = 0; // 打印所有字体名
y= 0;
NSArray *familyNames = [UIFont familyNames];
for( NSString *familyName in familyNames ){
x++;
NSLog( @"===%d=Family: %s \n",x, [familyName UTF8String] );
NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
for( NSString *fontName in fontNames ){
y++;
// printf( "-%d---\tFont: [[%s]] \n]", y,[fontName UTF8String] );
}
}*/
// MicrosoftYaHei
[super viewDidLoad];
self.page = 0;
self.pageCount = 0;
[self createScrollView];
selectedIdx = [NSMutableDictionary new];
//判断定位是否开启
if ([CLLocationManager locationServicesEnabled] &&
([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized
|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) {
NSLog(@"定位功能以及开启");
}
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
NSLog(@"定位功能不可用,提示用户或忽略");
}
NavigationView *navigationView = [NavigationView new];
[self.view addSubview:navigationView];
int navigationHeight = navigationView.frame.size.height;
self.navigationController.navigationBarHidden=YES;// 隐藏导航栏
//[UIColor colorWithRed:0/152.0f green:122/148.0f blue:255/142.0f alpha:0.5];
//self.view.backgroundColor = [UIColor whiteColor];
//首页滚动推荐图
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, navigationHeight,self.view.frame.size.width, self.view.frame.size.height - navigationHeight - 49) collectionViewLayout:flowLayout];
flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 175);
flowLayout.minimumInteritemSpacing = 0;
//间距
flowLayout.minimumLineSpacing = 4;
flowLayout.sectionInset = UIEdgeInsetsMake(4, 4, 0, 4);
self.automaticallyAdjustsScrollViewInsets = NO;//顶部不预留空白区域
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[ProjectCollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
[self.collectionView setAllowsMultipleSelection:YES];
[self.view addSubview:self.collectionView];
[self.collectionView addSubview:self.scrollView];
EntityInformation *entity = [EntityInformation new];
aryInformation = [entity infomationCategoryArray];
[self.collectionView reloadData];
// 2.集成刷新控件
[self UpReloadData];
[self DownReloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return aryInformation.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
int cellWidth;
int cellHight = (self.view.frame.size.width - 16)/3;
switch (indexPath.row % 3) {
case 0: case 1:
cellWidth = (self.view.frame.size.width - 16)/3;
break;
case 2:
cellWidth = (self.view.frame.size.width - 16)/3 + 1;
break;
default:
break;
}
return CGSizeMake(cellWidth,cellHight);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ProjectCollectionViewCell *cell=(ProjectCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];//@"cellEmpty"
cell.row = indexPath.row % 3;
[cell setInformationCell:aryInformation[indexPath.row]];
return cell;
}
//选中
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[selectedIdx setValue:@"1" forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn =[defaults boolForKey:@"switchOn"];
if (!switchOn) {
InformationClassificationViewController *icvc=[InformationClassificationViewController new];
[self presentViewController:icvc animated:YES completion:nil];
} else {
SecondViewController *svc = [SecondViewController new];
[self presentViewController:svc animated:YES completion:nil];
}
}
//取消选择
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
[selectedIdx removeObjectForKey:[NSString stringWithFormat:@"%ld", (long)indexPath.row]];
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
}