视图控制器 支持滑动点击

[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);

}];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值