KWMSearchBar.m 4.57 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
//
//  KWMSearchBar.m
//  iCemarose
//
//  Created by HouWeiBin on 16/8/24.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMSearchBar.h"
#import "KWMShopCartData.h"

@implementation KWMSearchBar

- (id)init{
    if (self=[super init]){
        [self addView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self addView];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self addView];
}

-(void) addView{
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                  owner:self
                                options:nil];
    
    self.vView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self addSubview:self.vView];
    [self.vCount setUserInteractionEnabled:NO];
    [self.lbCount setUserInteractionEnabled:NO];
    _lbCount.text = @"0";
    self.tfSearch.delegate = self;
    self.tfSearch.returnKeyType = UIReturnKeySearch;
    [self initView];
}

- (void)setCount:(NSInteger)count{
    _count = count;
    if (count > 0) {
        _lbCount.text = [NSString stringWithFormat:@"%ld",(long)count];
        if (count > 9) {
            _lbCount.font = [UIFont systemFontOfSize:8];
        }
    }else{
        _lbCount.text = @"0";
    }
}

-(void)initView{
    [self resumeView];
}

- (void)resumeView{
    //初始化是不在搜索狀態
    //self.logMarginLeft.constant = 20;
    //本來是20,改成10是因為返回按鈕的位置在那了。。
    self.logMarginLeft.constant = 10;
    //搜索按鈕到logo的距離是 屏幕寬度,減去logo的寬度(135+20left),view本身寬度(22),購物車按鈕的寬度(22+30left+20right)
    self.searchToLog.constant = UI_SCREEN_WIDTH - (135+20) - 22 -(22+30+20);
    //搜索欄的寬度是 屏幕寬度,減去搜索按鈕寬度(22+20left+),取消按鈕寬度(22+18right),注意,這個寬度是不會改變的
    self.textFieldWidth.constant = UI_SCREEN_WIDTH - (22+20) - (22+18);
    [self.vView layoutIfNeeded];
    self.vCancel.hidden = YES;
    self.vShopCar.hidden = NO;
    self.tfSearch.hidden = YES;
    self.tfSearch.text = nil;
    self.isSearching= NO;
}

-(void)startAnimation{
    if(self.isSearching){
        //如果正在搜索狀態,該距離為 购物车按钮的宽度(22+30left+20right) + 搜索按钮宽度(22+20left) - 屏幕寬度
        self.logMarginLeft.constant = (22+30+20) + (22 + 20) - UI_SCREEN_WIDTH + 15;
    }else{
        self.logMarginLeft.constant = 10;
    }
    [UIImageView animateWithDuration:0.3 animations:^{
        [self.vView layoutIfNeeded];
        self.vCancel.hidden = !self.isSearching;
        self.tfSearch.hidden = !self.isSearching;
        self.vShopCar.hidden = self.isSearching;
        
        if (self.isSearching) {
            [self.tfSearch becomeFirstResponder];
        }
    }];
};

-(void)onClickStartSearch:(id)sender{
    if(!self.isSearching){
        self.isSearching = YES;
        
        [self startAnimation];
        if([self.delegate respondsToSelector:@selector(kwm_startSearch)]){
            [self.delegate kwm_startSearch];
        }
    }
}

-(void)onClickCancel:(id)sender{
    self.isSearching = NO;
    self.tfSearch.text = nil;
    [self.tfSearch resignFirstResponder];
    [self startAnimation];
    if ([self.delegate respondsToSelector:@selector(kwm_onCancel)]) {
        [self.delegate kwm_onCancel];
    }
}

-(void)onClickShopCar:(id)sender{
    if([self.delegate respondsToSelector:@selector(kwm_onClickShopCar)]){
        [self.delegate kwm_onClickShopCar];
    }
}

//点击背景停止搜索
- (void)stopSearch{
    self.isSearching = NO;
    [self.tfSearch resignFirstResponder];
    [self startAnimation];
}


#pragma mark - UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(kwm_tfSearchBeginEditing)]) {
        [self.delegate kwm_tfSearchBeginEditing];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([textField.text isEqualToString:@""] && [string isEqualToString:@" "]) {
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if ([self.delegate respondsToSelector:@selector(kwm_tfSearchFinished:)]) {
        [self.delegate kwm_tfSearchFinished:textField.text];
    }
    return YES;
}

@end