zoukankan      html  css  js  c++  java
  • 仿网易顶部滑动选择分类视图

    #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

    方式二 --------------------------------

    #import <UIKit/UIKit.h>

    #import "SheJiaoTopObject.h"

    @protocol CustomHomeSelectViewDelegate <NSObject>

    //选中类目

    - (void)CustomHomeSelectViewDelegateTitleView:(NSInteger)row;

    //选中更多事件

    - (void)CustomHomeSelectViewDelegateMoreButton;

    @end

    @interface CustomHomeSelectView : UIView

    {

        UIButton *oldButton;

    }

    @property(nonatomic, weak) id<CustomHomeSelectViewDelegate> delegate;

    //红线

    @property(nonatomic, strong) UIView *redLineView;

    //下拉按钮

    @property(nonatomic, strong) UIButton *xialaButton;

    //记录数据

    @property(nonatomic, strong) NSMutableArray *dataArray;

    //添加滚动试图

    @property (nonatomic, strong) UIScrollView *myScroll;

    @property (nonatomic, assign) NSInteger dataCount;

    @property (nonatomic, assign) CGFloat oldViewWidth;

    - (id)initWithFrame:(CGRect)frame titleArray:(NSMutableArray *)array;

    //修改选中的项

    - (void)updateSelectedButton:(IndexLeiMuObject *)topObject row:(NSInteger)row;

    //更新数据

    -(void)updateView;

    @end

    #import "CustomHomeSelectView.h"

    #define HomeButtonSelectedBGColor [UIColor colorWithRed:241.0/255.0 green:242.0/255.0 blue:244.0/255.0 alpha:1.0]

    #define HomeBoderBGColor [UIColor colorWithRed:220.0/255.0 green:221.0/255.0 blue:222.0/255.0 alpha:1.0]

    #define HomeButtonTag 2000

    #define HomeTitleTag 3000

    #define HomeButtonTitleBGColor [UIColor colorWithRed:49.0/255.0 green:50.0/255.0 blue:51.0/255.0 alpha:1.0]

    #define HomeButtonTitleSelectedBGColor [UIColor colorWithRed:247.0/255.0 green:64.0/255.0 blue:88.0/255.0 alpha:1.0]

    @implementation CustomHomeSelectView

    @synthesize delegate,redLineView,xialaButton,dataArray,myScroll;

    - (id)initWithFrame:(CGRect)frame titleArray:(NSMutableArray *)array

    {

        self = [super initWithFrame:frame];

        if(self)

        {

    //        self.dataArray = array;

            //初始化数据

            [self setBackgroundColor:[UIColor whiteColor]];

            

            self.myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width-(frame.size.height-2), frame.size.height)];

    //        self.myScroll.backgroundColor = [UIColor yellowColor];

            self.myScroll.showsHorizontalScrollIndicator = NO;

            self.myScroll.showsVerticalScrollIndicator = NO;

            [self addSubview:self.myScroll];

            

            

    //        UIButton *hotButton;

    //        //计算宽度

    //        CGFloat bw = (frame.size.width-25-(array.count-1)*1)/array.count;

    //        CGFloat bh = frame.size.height-2;

    //        CGFloat bx = 0;

    //        for (int i=0; i<array.count; i++) {

    //            SheJiaoTopObject *topObject = [array objectAtIndex:i];

    //            bx = i *bw;

    //            if(i>0)

    //            {

    //                bx = i*bw + i*1;

    //            }

    //            CGRect rect = CGRectMake(bx, 1, bw, bh);

    //            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    //            button.tag = HomeButtonTag+i;

    //            button.frame = rect;

    //            [button setTitleColor:HomeButtonTitleBGColor forState:UIControlStateNormal];

    //            [button setTitle:topObject.titleName forState:UIControlStateNormal];

    //            [button.titleLabel setFont:[UIFont systemFontOfSize:14]];

    //            [button addTarget:self action:@selector(clickedHomeButton:) forControlEvents:UIControlEventTouchUpInside];

    //            [self.myScroll addSubview:button];

    //            if(i == 0)

    //            {

    //                [button setTitleColor:HomeButtonTitleSelectedBGColor forState:UIControlStateNormal];

    //                oldButton = button;

    //            }

    //            if([topObject.titleName isEqualToString:@"一元抢"])

    //            {

    //                hotButton = button;

    //            }

    //            if(i+1 == array.count) //最后一个添加下拉按钮

    //            {

    //                CGFloat xialaW = bh;

    //                self.xialaButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-xialaW, 1, xialaW, bh)];

    //                CGSize size = self.xialaButton.frame.size;

    //                CGFloat imageH = 10;

    //                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((size.width-imageH)/2-5, (size.height-imageH)/2, imageH, imageH)];

    //                [imageView setImage:[UIImage imageNamed:@"bbbaoNew_xialade.png"]];

    //                [self.xialaButton addSubview:imageView];

    //                [self.xialaButton addTarget:self action:@selector(clickedMoreButton) forControlEvents:UIControlEventTouchUpInside];

    //                [self addSubview:self.xialaButton];

    //

    //            }

    //        }

    //        

    //        self.myScroll.contentSize = CGSizeMake(bx,0);

    //        //添加红线

    //        UITextView *textView = [[UITextView alloc] initWithFrame:oldButton.bounds];

    //        textView.text = oldButton.titleLabel.text;

    //        CGFloat redLineW = [NSString getFormatUILabelWidth:textView]+8;

    //        CGFloat redLineX = oldButton.frame.origin.x+(oldButton.frame.size.width-redLineW)/2;

    //        self.redLineView = [[UIView alloc] initWithFrame:CGRectMake(redLineX, bh-1, redLineW, 2)];

    //        [self.redLineView setBackgroundColor:[UIColor redColor]];

    //        [self.myScroll addSubview:self.redLineView];

    //        

    //        //添加热度图片

    //        textView = [[UITextView alloc] initWithFrame:oldButton.bounds];

    //        textView.text = hotButton.titleLabel.text;

    //        redLineW = [NSString getFormatUILabelWidth:textView]+8;

    //        CGFloat hotX = hotButton.frame.size.width/2+redLineW/2;

    //        UIImageView *hotImageView = [[UIImageView alloc] initWithFrame:CGRectMake(hotX, 0, 20, 18)];

    //        [hotImageView setImage:[UIImage imageNamed:@"bbbaoNew_hot.png"]];

    //        hotImageView.userInteractionEnabled = NO;

    //        [hotButton addSubview:hotImageView];

        }

        return self;

    }

    //修改选中的项

    - (void)updateSelectedButton:(IndexLeiMuObject *)topObject row:(NSInteger)row

    {

        if(oldButton)

        {

            [oldButton setTitleColor:HomeButtonTitleBGColor forState:UIControlStateNormal];

        }

        //改变选中的button名字

        

        

         UIButton *button =  (UIButton *)[self.myScroll viewWithTag:HomeButtonTag+row];

        

    //    if(topObject.jiluNumber == 4)

    //    {

    //        [button setTitle:topObject.titleName forState:UIControlStateNormal];

    //    }

        [button setTitleColor:HomeButtonTitleSelectedBGColor forState:UIControlStateNormal];

        oldButton = button;

        //改变红色线条

        UITextView *textView = [[UITextView alloc] initWithFrame:button.bounds];

        textView.text = button.titleLabel.text;

        CGFloat redLineW = [NSString getFormatUILabelWidth:textView]+8;

        CGRect redRect = self.redLineView.frame;

        redRect.origin.x = button.frame.origin.x+(button.frame.size.width-redLineW)/2;

        redRect.size.width = redLineW;

        self.redLineView.frame = redRect;

        

        if(button.frame.origin.x+button.frame.size.width/2-self.oldViewWidth/2>=0 && button.frame.origin.x+button.frame.size.width/2+self.oldViewWidth/2<=self.myScroll.contentSize.width)

        {

            [self.myScroll setContentOffset:CGPointMake(button.frame.origin.x+button.frame.size.width/2-self.oldViewWidth/2, 0) animated:YES];

        }

        else if(button.frame.origin.x+button.frame.size.width/2-self.oldViewWidth/2<0)

        {

            [self.myScroll setContentOffset:CGPointMake(0, 0) animated:YES];

        }

        else if(button.frame.origin.x+button.frame.size.width/2+self.oldViewWidth/2>self.myScroll.contentSize.width)

        {

            [self.myScroll setContentOffset:CGPointMake(self.myScroll.contentSize.width-self.frame.size.width+30, 0) animated:YES];

        }

    }

    //点击按钮事件

    - (void)clickedHomeButton:(id)sender

    {

        UIButton *button = sender;

        //改变它的样式

        NSInteger row = button.tag - HomeButtonTag;

        IndexLeiMuObject *leiMuObj = [self.dataArray objectAtIndex:row];

        [self updateSelectedButton:leiMuObj row:row];

       

        oldButton = button;

        

        [self.delegate CustomHomeSelectViewDelegateTitleView:row];

    }

    //点击更多类目

    - (void)clickedMoreButton

    {

        [self.delegate CustomHomeSelectViewDelegateMoreButton];

    }

    //---------

    -(void)updateView

    {

        for (UIView *vw in self.myScroll.subviews)

        {

            [vw removeFromSuperview];

        }

        

        CGFloat curW = self.frame.size.width;

        CGFloat curH = self.frame.size.height;

        

        UIButton *hotButton;

        //计算宽度

    //    CGFloat bw = (self.frame.size.width-25-(self.dataArray.count-1)*1)/self.dataArray.count;

        CGFloat bw = 50;

        CGFloat bh = curH-2;

        CGFloat bx = 10;

        NSInteger count = self.dataArray.count;

        NSString *titleName;

        for (int i=0; i < self.dataArray.count; i++)

        {

            IndexLeiMuObject *topObject = [self.dataArray objectAtIndex:i];

            titleName = topObject.sourceName;

            

            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

            //添加红线

            UITextView *textView = [[UITextView alloc] initWithFrame:button.bounds];

            textView.text = titleName;

            bw = [NSString getFormatUILabelWidth:textView]+8;

            CGRect rect = CGRectMake(bx, 1, bw, bh);

            button.tag = HomeButtonTag+i;

            button.frame = rect;

            [button setTitleColor:HomeButtonTitleBGColor forState:UIControlStateNormal];

            [button setTitle:titleName forState:UIControlStateNormal];

            [button.titleLabel setFont:[UIFont systemFontOfSize:14]];

            [button addTarget:self action:@selector(clickedHomeButton:) forControlEvents:UIControlEventTouchUpInside];

            [self.myScroll addSubview:button];

            if(i == 0)

            {

                [button setTitleColor:HomeButtonTitleSelectedBGColor forState:UIControlStateNormal];

                oldButton = button;

            }

            if(i+1 == self.dataArray.count) //最后一个添加下拉按钮

            {

                CGFloat xialaW = bh;

                self.xialaButton = [[UIButton alloc] initWithFrame:CGRectMake(curW-xialaW, 1, xialaW, bh)];

                CGSize size = self.xialaButton.frame.size;

                CGFloat imageH = 10;

                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((size.width-imageH)/2-5, (size.height-imageH)/2, imageH, imageH)];

                [imageView setImage:[UIImage imageNamed:@"bbbaoNew_xialade.png"]];

                [self.xialaButton addSubview:imageView];

                [self.xialaButton addTarget:self action:@selector(clickedMoreButton) forControlEvents:UIControlEventTouchUpInside];

                [self addSubview:self.xialaButton];

                

            }

            bx += bw+10;

        }

        

        self.myScroll.contentSize = CGSizeMake(bx,0);

        //添加红线

        UITextView *textView = [[UITextView alloc] initWithFrame:oldButton.bounds];

        textView.text = oldButton.titleLabel.text;

        CGFloat redLineW = [NSString getFormatUILabelWidth:textView]+8;

        CGFloat redLineX = oldButton.frame.origin.x+(oldButton.frame.size.width-redLineW)/2;

        self.redLineView = [[UIView alloc] initWithFrame:CGRectMake(redLineX, bh-1, redLineW, 2)];

        [self.redLineView setBackgroundColor:[UIColor redColor]];

        [self.myScroll addSubview:self.redLineView];

        

    }

    @end

  • 相关阅读:
    (转)Android获取手机信息
    ListView中RadioButton实现单项选择
    ListView 实现分组
    解决PopupWindow遮住输入法
    Oracle多行记录合并处理
    ubuntu install express
    图片以BLOB存储在后台数据库中,Android客户端要进行读取显示
    将图片以Blob格式存入数据库,再通过Servlet显示到界面
    httpcomponents-client-4.3.6 HttpPost的简单使用
    设置Ubuntu Mysql可以远程链接
  • 原文地址:https://www.cnblogs.com/camillezlh/p/4691412.html
Copyright © 2011-2022 走看看