KWMSearchBrandView.m 3.67 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
//
//  KWMSearchBrandView.m
//  iCemarose
//
//  Created by 陈荣科 on 16/8/30.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMSearchBrandView.h"

@implementation KWMSearchBrandView{
    BOOL isSearching;
}

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

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

- (void)initContentView{
    [[NSBundle mainBundle] loadNibNamed:@"KWMSearchBrandView" owner:self options:nil];
    [self addSubview:_vContent];
    self.vContent.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.brandLeft.constant = (UI_SCREEN_WIDTH - 71)*0.5;
    self.searchBtnRight.constant = 10;
    self.tfBrand.returnKeyType = UIReturnKeySearch;
}

houweibin committed
38 39 40 41 42 43 44
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.vContent.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}


houweibin committed
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
- (void)startAnimate{
    if (isSearching) {//搜索状态
        self.brandLeft.constant = 22 + 10 + 71 - UI_SCREEN_WIDTH;
        self.searchBtnRight.constant = UI_SCREEN_WIDTH - 22 - 30 + 15;
        self.tfWidth.constant = UI_SCREEN_WIDTH - (30+10+22)-(30+6) - 10;
        self.btnBackLeft.constant = -UI_SCREEN_WIDTH;
    }else{//不是搜索状态
        self.brandLeft.constant = (UI_SCREEN_WIDTH - 71)*0.5;
        self.searchBtnRight.constant = 10;
        self.btnBackLeft.constant = 0;
    }
    [UIImageView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
        if (isSearching) {
            self.tfBrand.text = nil;
            [self.tfBrand becomeFirstResponder];
        }
        self.btnCancel.hidden = !isSearching;
        self.tfBrand.hidden = !isSearching;
    }];
}

- (IBAction)onClickSearchBtn:(id)sender {
    NSLog(@"点击了搜索按钮");
    if(!isSearching){
        isSearching = YES;
        [self startAnimate];
    }
}
- (IBAction)onClickACancelBtn:(id)sender {
    NSLog(@"点击了取消按钮");
    self.btnCancel.hidden = YES;
    isSearching = NO;
    [self.tfBrand resignFirstResponder];
    [self startAnimate];
    if ([self.delegate respondsToSelector:@selector(kwm_cancelSearch)]) {
        [self.delegate kwm_cancelSearch];
    }
}

//点击背景取消动画
- (void)CancelSearch{
    self.btnCancel.hidden = YES;
    isSearching = NO;
    [self.tfBrand resignFirstResponder];
    [self startAnimate];
}

- (void)onClickBackBtn:(id)sender {
    NSLog(@"点击返回按钮");
    if ([self.delegate respondsToSelector:@selector(kwm_GoBackLastController)]) {
        [self.delegate kwm_GoBackLastController];
    }
}

- (IBAction)tfValueChanged:(id)sender {

    if ([self.delegate respondsToSelector:@selector(kwm_tfValueChange:)]) {
        [self.delegate kwm_tfValueChange:_tfBrand.text];
    }
}

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

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

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

@end