- (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];tSize = 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)]; = [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)]; = [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]; = [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]; = [itemDataArray objectAtIndex:currentIndex];}];
}
本文固定链接: /?p=3815 | 掌中天际
本文发布于:2024-02-02 12:59:46,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170684998743962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |