- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
itemDataArray = [[NSMutableArray alloc] init];
for (int i = 0 ; i< MAX_ITEM;i++) {
NSString *itemStr = [NSString stringWithFormat:@"第%d个",i];
[itemDataArray addObject:itemStr];
}
currentIndex = 0;
itemScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
itemScrollView.backgroundColor = [UIColor redColor];
[itemScrollView setShowsHorizontalScrollIndicator:NO];
[itemScrollView setBounces:NO];
itemScrollView.delegate = self;
itemScrollView.tag = 11;
[self.view addSubview:itemScrollView];
itemScrollView.contentSize = CGSizeMake(ITEM_WIDTH*[itemDataArray count], 40);
for (int i = 0; i< [itemDataArray count]; i++) {
UILabel *labtitle = [[UILabel alloc] initWithFrame:CGRectMake(i*ITEM_WIDTH, 5, ITEM_WIDTH-4, 30)];
labtitle.text = [itemDataArray objectAtIndex:i];
labtitle.font = [UIFont systemFontOfSize:14];
labtitle.backgroundColor = [UIColor clearColor];
[labtitle setTextAlignment:NSTextAlignmentCenter];
[itemScrollView addSubview:labtitle];
UIButton *bt = [[UIButton alloc] initWithFrame:CGRectMake(i*ITEM_WIDTH, 5, ITEM_WIDTH-4, 30)];
bt.tag = 1000+i;
[bt addTarget:self action:@selector(itemPressed:) forControlEvents:UIControlEventTouchUpInside];
[itemScrollView addSubview:bt];
}
itemBottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 38, ITEM_WIDTH, 2)];
itemBottomLabel.backgroundColor = [UIColor blackColor];
[itemScrollView addSubview:itemBottomLabel];
UILabel *labname = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 100, 40)];
labname.text = [itemDataArray objectAtIndex:0];
labname.tag = 100;
[self.view addSubview:labname];
UISwipeGestureRecognizer *panSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @selector(leftPanSwipe:)];
UISwipeGestureRecognizer *panSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @selector(rightPanSwipe:)];
[self.view addGestureRecognizer: panSwipeLeft];
[self.view addGestureRecognizer: panSwipeRight];
panSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
panSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
}
//右划
-(void)rightPanSwipe:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(@"right");
if (currentIndex > 0) {
currentIndex--;
int itag = currentIndex;
[UIView animateWithDuration:0.2 animations:^{
itemBottomLabel.frame = CGRectMake(itag*ITEM_WIDTH, 38, ITEM_WIDTH, 2);
} completion:^(BOOL finished) {
UILabel *lab = (UILabel *)[self.view viewWithTag:100];
lab.text = [itemDataArray objectAtIndex:currentIndex];
}];
UIScrollView *s = (UIScrollView *)[self.view viewWithTag:11];
if (s.contentOffset.x > currentIndex * ITEM_WIDTH) {
[s setContentOffset:CGPointMake(ITEM_WIDTH*currentIndex, 0) animated:YES];
}
}
}
//获取类型
-(void)itemPressed:(UIButton *)sender
{
int index = [sender tag];
int itag = index - 1000;
currentIndex = itag;
[UIView animateWithDuration:0.2 animations:^{
itemBottomLabel.frame = CGRectMake(itag*ITEM_WIDTH, 38, ITEM_WIDTH, 2);
} completion:^(BOOL finished) {
UILabel *lab = (UILabel *)[self.view viewWithTag:100];
lab.text = [itemDataArray objectAtIndex:currentIndex];
}];
}