1. 程式人生 > >iOS 訂單選擇型別的實現

iOS 訂單選擇型別的實現

通常我們在做購物車方面的APP時,我們會遇到類似於淘寶訂單的選擇樣式,例如下圖


這樣頁面可能會有很多,若果我們要全部去寫就會很麻煩,所以我們最後要寫公用的類,哪裡需要我們就呼叫一下即可。

廢話不多說上程式碼。

首先,我們要建立一個UIView,並重寫它的初始化方法。顯示 . h檔案

#import <UIKit/UIKit.h>

@protocol getClickButtonNumberDelegate <NSObject>

-(void)getClickButtonNumber:(NSInteger)num;

@end

@interface QWSelectorView :

UIView

@property (assign,nonatomic,readwrite)id <getClickButtonNumberDelegate>delegate;

/*

 * 1.選擇按鈕顯示內容

 * 2.獲取按鈕數量

 */

@propertyNSArray *selectNumber;

/*

 * 1.選擇按鈕顏色

 */

@property (strong,nonatomic) UIColor *buttonColor;

/*

 * 1.下劃線顏色

 */

@property (strong,nonatomic) UIColor *LineColor;

/*

 * 1.篩選UIview的高度

 */

@propertyCGFloat height;

@property (strong,nonatomic) UILabel *LineLabel;

#pragma mark 重寫UIView初始化方法

-(instancetype)initWithFrame:(CGRect)frame arr:(NSArray*)arr buttonColor:(UIColor*)buttonColor lineColor:(UIColor*)lineColor;

. m 檔案

#import "QWSelectorView.h"

#define SCREEN_WIDTH  [[UIScreen mainScreen] bounds].size.width

#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

@implementation QWSelectorView

#pragma mark 重寫UIView

-(instancetype)initWithFrame:(CGRect)frame arr:(NSArray*)arr buttonColor:(UIColor*)buttonColor lineColor:(UIColor*)lineColor{

self = [superinitWithFrame:frame];

if (self) {

self.frame =CGRectMake(0, frame.origin.y, frame.size.width, frame.size.height);

_height       = frame.size.height;

_buttonColor  = buttonColor;

_LineColor    = lineColor;

_selectNumber = arr;

        [selfcreatSelectButtonByArr];

        [selfcreatLineLabelByArr];

    }

returnself;

}

#pragma mark 建立按鈕通過陣列的數量

-(void) creatSelectButtonByArr{

if ( _selectNumber.count >0 ) {

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

UIButton * button = [[UIButtonalloc]initWithFrame:CGRectMake( i * (SCREEN_WIDTH / _selectNumber.count),0, (SCREEN_WIDTH /_selectNumber.count),_height - 1)];

            [selfaddSubview:button];

            button.titleLabel.font = [UIFontsystemFontOfSize:15];

            [button setTitle:_selectNumber[i]forState:UIControlStateNormal];

if (i ==0) {

                [button setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

            }else{

                [button setTitleColor:_buttonColorforState:UIControlStateNormal];

            }

            button.tag = i+1;

            [button addTarget:selfaction:@selector(onClick:)forControlEvents:UIControlEventTouchUpInside];

            [selfaddSubview:button];

        }

    }

}

#pragma mark 建立按鈕通過陣列的數量

-(void) creatLineLabelByArr{

if (_selectNumber.count >0) {

_LineLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,_height - 1,SCREEN_WIDTH / _selectNumber.count,1)];

_LineLabel.backgroundColor  =_LineColor;

        [selfaddSubview:_LineLabel];

    }

}

#pragma mark  點選後改變按鈕的顏色

-(void)onClick:(UIButton*)btn{

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

UIButton * button = [selfviewWithTag: i +1 ];

if (button.tag == btn.tag) {

            [btn    setTitleColor:[UIColorredColor]   forState:UIControlStateNormal];

        }else{

            [button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        }

    }

    [selfchanageLinePosition:btn.tag];

    [selfgetButtonNumber:btn.tag];

}

#pragma mark 下劃線跟著按鈕帶著動畫移動到相應的位置

-(void)chanageLinePosition:(NSInteger)num{

    [UILabelbeginAnimations:nilcontext:nil];

CGRect rect =_LineLabel.frame;

    rect.origin.x =SCREEN_WIDTH / _selectNumber.count * ( num -1);

    rect.size.width =SCREEN_WIDTH / _selectNumber.count;

_LineLabel.frame=rect;

    [UILabelsetAnimationDuration:1.0];

    [UILabelcommitAnimations];

}

#pragma mark 通過協議的方法獲取點選的是第幾個按鈕

-(void)getButtonNumber:(NSInteger)num{

if ([_delegaterespondsToSelector:@selector(getClickButtonNumber:)]) {

        [_delegategetClickButtonNumber:num];

    }

}


這樣我們只要,在Controller裡面,直接建立view即可,並可以隨時改變篩選條件的個數,以及按鈕的顏色,以及下劃線的顏色

在Controller中製藥寫入下面的程式碼並遵守協議即可

NSArray *arr =@[@"全部訂單",@"待付款",@"進行中",@"已完成",@"待收貨"];

QWSelectorView *sv = [[QWSelectorViewalloc]initWithFrame:CGRectMake(0,64, SCREEN_WIDTH,0.12 * SCREEN_WIDTH)arr:arr buttonColor:[UIColorblackColor] lineColor:[UIColorredColor]];

    sv.delegate =self;

    [self.viewaddSubview:sv];

然後,呼叫協議的方法獲取,點選的第幾個按鈕

-(void)getClickButtonNumber:(NSInteger)num{

UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"提示"

message:[NSStringstringWithFormat:@"點選第 == %lu ==個按鈕",num]

delegate:nil

cancelButtonTitle:@"確定"

otherButtonTitles:nil,nil];

    [alertView show];

NSLog(@"你點選的是第 =============  %lu  ============個按鈕",num);

}

下面在附上Demo的連結:http://download.csdn.net/download/wangqinglei0307/9950613