标签:
#import <UIKit/UIKit.h>
@protocol CustomSelectViewDelegate <NSObject>
-(void)selectedButtonTag:(NSInteger)tag;
@end
@interface CustomSelectView : UIView
//红线
@property (nonatomic, strong) UILabel *redLabel;
//数据源
@property (nonatomic, strong) NSMutableArray *tableArray;
@property (nonatomic, strong) NSString *selectedString;
//中间变量按钮
@property (strong,nonatomic) UIButton *tempBtn;
@property (nonatomic, assign) CGFloat oldViewWidth;
//滚动视图
@property (nonatomic, strong) UIScrollView *myScroll;
@property (nonatomic, assign) id<CustomSelectViewDelegate>myDelegate;
- (id)initWithFrame:(CGRect)frame dataArray:(NSArray *)array;
-(void)updateView;
-(void)touchDownButtonClicked:(id)sender;
#import "CustomSelectView.h"
@implementation CustomSelectView
@synthesize redLabel,tableArray,tempBtn,myScroll;
- (id)initWithFrame:(CGRect)frame dataArray:(NSArray *)array
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor whiteColor];
CGFloat curW = frame.size.width;
CGFloat curH = frame.size.height;
CGFloat bw = curW/7;
CGFloat bh = 32;
CGFloat bx = 0;
CGFloat by = 6;
self.myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, curW, curH)];
self.myScroll.showsHorizontalScrollIndicator = NO;
self.myScroll.showsVerticalScrollIndicator = NO;
// self.myScroll.backgroundColor = [UIColor yellowColor];
[self addSubview:self.myScroll];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(curW, 12,1 , curH-20)];
lineView.backgroundColor = [UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1];
[self addSubview:lineView];
}
return self;
}
-(void)updateView
{
for (UIView *vw in self.myScroll.subviews)
{
[vw removeFromSuperview];
}
CGFloat curW = self.frame.size.width;
CGFloat curH = self.frame.size.height;
CGFloat bw = curW/7;
CGFloat bh = 32;
CGFloat bx = 0;
CGFloat by = 6;
for (int i = 0; i < self.tableArray.count; i++)
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(bx, by, bw, bh)];
button.tag = 100+i;
button.titleLabel.font = [UIFont systemFontOfSize:14.0];
IndexLeiMuObject *leiMuObj = [tableArray objectAtIndex:i];
NSString *titleString = leiMuObj.sourceName;
[button setTitle:titleString forState:UIControlStateNormal];
// [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
[self.myScroll addSubview:button];
[button addTarget:self action:@selector(touchDownButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
bx += bw;
if([self.selectedString isEqualToString:titleString])
{
tempBtn = button;
button.titleLabel.font = [UIFont systemFontOfSize:14.0];
[button setTitleColor:[UIColor colorWithRed:250.0/255.0 green:9.0/255.0 blue:58.0/255.0 alpha:1.0] forState:UIControlStateNormal];
}
else
{
button.titleLabel.font = [UIFont systemFontOfSize:14.0];
[button setTitleColor:[UIColor colorWithRed:90/255.0 green:90/255.0 blue:90/255.0 alpha:1] forState:UIControlStateNormal];
}
[self.myScroll addSubview:button];
}
self.redLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, curH-3, bw-10, 2)];
self.redLabel.backgroundColor = [UIColor colorWithRed:250/255.0 green:9/255.0 blue:58/255.0 alpha:1];
[self.myScroll addSubview:self.redLabel];
self.myScroll.contentSize = CGSizeMake(bx,0);
}
- (void)touchDownButtonClicked:(id)sender
{
[UIView animateWithDuration:0.2 animations:^{
tempBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
[tempBtn setTitleColor:[UIColor colorWithRed:90/255.0 green:90/255.0 blue:90/255.0 alpha:1] forState:UIControlStateNormal];
UIButton *btn = sender;
// if(daPaiFanArray)
// {
// NSDictionary *diationary = [daPaiFanArray objectAtIndex:btn.tag-100];
// [self.topScrollviewDelegate selectIndexRow:btn.tag-100];
// NSString *sourceName = [diationary objectForKey:@"browse_value"];
// self.selectedString = sourceName;
// btn.titleLabel.font = [UIFont fontWithName:@"YouYuan" size:18];
// [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// selectOldButton = btn;
//
// if(isNormalType)
// {
// return ;
// }
// if(btn.frame.origin.x+btn.frame.size.width/2-self.oldViewWidth/2>=0 && btn.frame.origin.x+btn.frame.size.width/2+self.oldViewWidth/2<=self.contentSize.width)
// {
// [self setContentOffset:CGPointMake(btn.frame.origin.x+btn.frame.size.width/2-self.oldViewWidth/2, btn.frame.origin.y) animated:YES];
// }
// else if(btn.frame.origin.x+btn.frame.size.width/2-self.oldViewWidth/2<0)
// {
// [self setContentOffset:CGPointMake(0, btn.frame.origin.y) animated:YES];
// }
// else if(btn.frame.origin.x+btn.frame.size.width/2+self.oldViewWidth/2>self.contentSize.width)
// {
// [self setContentOffset:CGPointMake(self.contentSize.width-self.frame.size.width, btn.frame.origin.y) animated:YES];
// }
// }
// else
// {
IndexLeiMuObject *leiMuObj = [tableArray objectAtIndex:btn.tag-100];
[self.myDelegate selectedButtonTag:btn.tag];
self.selectedString = leiMuObj.sourceName;
btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
[btn setTitleColor:[UIColor colorWithRed:250/255.0 green:9/255.0 blue:58/255.0 alpha:1] forState:UIControlStateNormal];
self.redLabel.center = btn.center;
CGRect redLabelFrame = self.redLabel.frame;
redLabelFrame.origin.y = btn.frame.origin.y+btn.frame.size.height-1;
self.redLabel.frame = redLabelFrame;
tempBtn = btn;
// if(isNormalType)
// {
// return ;
// }
if(btn.frame.origin.x+btn.frame.size.width/2-self.oldViewWidth/2>=0 && btn.frame.origin.x+btn.frame.size.width/2+self.oldViewWidth/2<=self.myScroll.contentSize.width)
{
[self.myScroll setContentOffset:CGPointMake(btn.frame.origin.x+btn.frame.size.width/2-self.oldViewWidth/2, 0) animated:YES];
}
else if(btn.frame.origin.x+btn.frame.size.width/2-self.oldViewWidth/2<0)
{
[self.myScroll setContentOffset:CGPointMake(0, 0) animated:YES];
}
else if(btn.frame.origin.x+btn.frame.size.width/2+self.oldViewWidth/2>self.myScroll.contentSize.width)
{
[self.myScroll setContentOffset:CGPointMake(self.myScroll.contentSize.width-self.frame.size.width, 0) animated:YES];
}
// }
}];
}
@end
标签:
原文地址:http://www.cnblogs.com/camillezlh/p/4691412.html