[self addSegement];
UICollectionViewFlowLayout *picture_V = [[UICollectionViewFlowLayout alloc] init];
//最小行间距
picture_V.minimumLineSpacing = 0 ;
//滚动方向
picture_V.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//单位大小
picture_V.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height - 64 );
//与屏幕四边的间距
picture_V.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0 , 35 * ScreenHeigth , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 60) collectionViewLayout:picture_V];
self.collectionV.dataSource = self;
self.collectionV.delegate = self;
// self.collectionV.contentOffset = CGPointMake(375, 200);
self.collectionV.pagingEnabled = YES;
//设置点击状态栏不会顶部
self.collectionV.scrollsToTop = NO;
self.collectionV.showsHorizontalScrollIndicator = YES;
self.collectionV.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.collectionV];
[self.collectionV registerClass:[NEWSCollectionViewCell class] forCellWithReuseIdentifier:@"news"];
[self.collectionV registerClass:[HOTCollectionViewCell class] forCellWithReuseIdentifier:@"hot"];
[self.collectionV registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:@"category"];
//滑条
self.scrollView = [[UIView alloc] initWithFrame:CGRectMake(75 * ScreenWidth, 33 * ScreenHeigth, 75 * ScreenWidth, 2 * ScreenHeigth)];
self.scrollView.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:134 / 255.0 blue:12 / 255.0 alpha:1.0];
[self.view addSubview:self.scrollView];
//添加喜欢分类
- (void)addSegement
{
UIView view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width , 35 ScreenHeigth )];
view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:view];
self.segmentedC = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"动态", @"热门", @"分类", nil]];
self.segmentedC.frame = CGRectMake(75 * ScreenWidth, 0, [UIScreen mainScreen].bounds.size.width - 75 * ScreenWidth * 2 , 35 * ScreenHeigth );
self.segmentedC.selectedSegmentIndex = 0;
pragma 将segmentedControl去掉边框
self.segmentedC.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16 ], NSForegroundColorAttributeName: [UIColor colorWithRed:240 / 255.0 green:134 / 255.0 blue:12 / 255.0 alpha:1.0]};
[self.segmentedC setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
//设置文字属性
NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16 ],NSForegroundColorAttributeName: [UIColor lightGrayColor]};
[self.segmentedC setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
[self.segmentedC addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
[view addSubview:self.segmentedC];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 3 ;
}
// 点击segment偏移屏幕
- (void)segmentedAction:(UISegmentedControl *)sender
{
self.collectionV.contentOffset = CGPointMake(self.collectionV.frame.size.width * sender.selectedSegmentIndex, 0);
[self.collectionV reloadData];
//动画滑块 点击改变滑块位置
[UIView animateWithDuration:0.2 animations:^{
self.scrollView.frame = CGRectMake(75 *ScreenWidth + (75 *ScreenWidth) * sender.selectedSegmentIndex, 33 * ScreenHeigth, 75 * ScreenWidth, 2 * ScreenHeigth);
}];
}
// 滑动屏幕改变segment
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.segmentedC.selectedSegmentIndex = self.collectionV.contentOffset.x / self.collectionV.frame.size.width;
//动画滑块 点击改变滑块位置
[UIView animateWithDuration:0.2 animations:^{
self.scrollView.frame = CGRectMake(75 * ScreenWidth + (75 * ScreenWidth) * self.collectionV.contentOffset.x / self.collectionV.frame.size.width, 33 * ScreenHeigth, 75 * ScreenWidth, 2 * ScreenHeigth);
}];
}