| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- //
- // SegmentView.m
- // live
- //
- // Created by hysd on 15/8/19.
- // Copyright (c) 2015年 kenneth. All rights reserved.
- //
- #import "SegmentView.h"
- @interface SegmentView()
- {
- int _itemCount;
- }
- @property(nonatomic, strong) UIView *indicatorView;
- @end
- @implementation SegmentView
- - (id)initWithFrame:(CGRect)frame andItems:(NSArray*)items andSize:(NSInteger)size border:(BOOL)border isrankingRist:(BOOL)isrankingRist
- {
- self = [super initWithFrame:frame];
- if(self)
- {
- _itemCount = (int)items.count;
- self.segmentControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
- for(int index = 0; index < items.count; index++)
- {
- [self.segmentControl insertSegmentWithTitle:items[index] atIndex:index animated:NO];
- }
- self.segmentControl.selectedSegmentIndex = 1;
- self.segmentControl.tintColor = kWhiteColor;
- self.segmentControl.backgroundColor = kClearColor;
- [self.segmentControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
-
- _indicatorView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/(4*items.count), frame.size.height-6, kRealValue(20), 3)];
- _indicatorView.backgroundColor = kWhiteColor;
- _indicatorView.layer.cornerRadius = 3 / 2;
- _indicatorView.layer.masksToBounds = YES;
- _indicatorView.hidden = YES;
- if (isrankingRist)
- {
- NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:size + 2], NSForegroundColorAttributeName: kWhiteColor};
- [self.segmentControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
- NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:size], NSForegroundColorAttributeName: kWhiteColor};
- [self.segmentControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
- _indicatorView.hidden = NO;
- }
- else
- {
- NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:size], NSForegroundColorAttributeName: kWhiteColor};
- [self.segmentControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
- NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName: kWhiteColor};
- [self.segmentControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
-
- }
-
-
-
- UIColor *Stock_Red = kClearColor;
-
- if (@available(iOS 13.0, *)) {
- _segmentControl.selectedSegmentTintColor = Stock_Red;
- } else {
- _segmentControl.tintColor = Stock_Red;
- }
- // 以下代码可以代替上面:
- [_segmentControl setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
- [_segmentControl setBackgroundImage:[UIImage imageWithColor:kClearColor] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
-
-
- // if (!isrankingRist) {
- // [_segmentControl setImage:[UIImage imageNamed:@"du_voice_background"] forSegmentAtIndex:1];
- //// setBackgroundImage:[UIImage imageNamed:@"du_voice_background"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
- // }
-
- //去掉中间的分割线
- [_segmentControl setDividerImage:[UIImage imageWithColor:[UIColor clearColor]] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
-
- [self addSubview:self.segmentControl];
- if(border)
- {
- UIView* sep = [[UIView alloc] initWithFrame:CGRectMake(0, self.segmentControl.frame.size.height-1, self.segmentControl.frame.size.width, 1)];
- sep.backgroundColor = kWhiteColor;
- [self.segmentControl addSubview:sep];
- }
- [self.segmentControl addSubview:_indicatorView];
- }
- return self;
- }
- - (void)setIsBlack:(bool)isBlack{
- NSDictionary* selectedTextAttributes = @{ NSForegroundColorAttributeName: [UIColor colorWithHexString:@"887BFF"]};
- [self.segmentControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
- NSDictionary* unselectedTextAttributes = @{ NSForegroundColorAttributeName: kBlackColor};
- [self.segmentControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
- }
- - (void)segmentChanged:(UISegmentedControl *)sender
- {
-
- // self.underLine.frame.origin.x = CGFloat(index) * self.view.frame.size.width/5 + self.view.frame.size.width/10 - 8.5
- // }
- NSLog(@"%@",sender);
- NSLog(@"superviewFrame%@",sender.superview);
- NSLog(@"%@",self);
-
-
- [UIView animateWithDuration:0.2f animations:^{
-
- NSInteger newX = self.segmentControl.selectedSegmentIndex
- *sender.width / 2 + _indicatorView.frame.size.width/2 + 16;
-
- _indicatorView.left = newX;
- // _indicatorView.centerX = self.segmentControl.selectedSegmentIndex * sender.width + 15 + sender.width * self.segmentControl.selectedSegmentIndex;
-
-
-
- }];
-
- if(self.delegate)
- {
- [self.delegate segmentView:self selectIndex:self.segmentControl.selectedSegmentIndex];
- }
- }
- - (void)setSelectIndex:(NSInteger)index
- {
- self.segmentControl.selectedSegmentIndex = index;
- // NSInteger newX = _segmentControl.selectedSegmentIndex * _indicatorView.frame.size.width;
- // NSInteger newX = self.segmentControl.selectedSegmentIndex *_indicatorView.frame.size.width*2+_indicatorView.frame.size.width/2;
-
- NSInteger newX = self.segmentControl.selectedSegmentIndex * self.segmentControl.width / 2 + _indicatorView.frame.size.width/2 + 16;
- [UIView animateWithDuration:0.2f animations:^{
-
-
- _indicatorView.left = newX;
- }];
- }
- - (NSInteger)getSelectIndex
- {
- return self.segmentControl.selectedSegmentIndex;
- }
- - (void)changeIndex
- {
- self.segmentControl.selectedSegmentIndex = 1;
- [self segmentChanged:self.segmentControl];
- }
- @end
|