// // 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)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) addView{ [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; [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)layoutSubviews { [super layoutSubviews]; self.vView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); } - (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