网易新闻主页头部

- (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];
    }];
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值