//
//  RouteTaskView.m
//  gaotingshan
//
//  Created by Mac Book DQ02 on 16/4/8.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//

#import "RouteTaskView.h"

@implementation RouteTaskView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        WS(ws);
        self.innerView = [[RouteTaskInnerView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        ((RouteTaskInnerView *)self.innerView).startingField.delegate = self;
        ((RouteTaskInnerView *)self.innerView).endingField.delegate = self;
        
        self.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.5];
        [self addSubview:self.innerView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        tap.numberOfTapsRequired =1;
        tap.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:tap];
        [tap addTarget:self action:@selector(tapIconView:)];
        [self.innerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(ws.mas_centerX);
            make.centerY.mas_equalTo(ws.mas_centerY);
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(150);
        }];
    }
    return self;
}

-(void)tapIconView:(UITapGestureRecognizer *)gesture
{
    if (CGRectContainsPoint(self.innerView.frame, [gesture locationInView:self])) {
    //    NSLog(@"点在子视图了");
    }else
    {
     //   NSLog(@"点在父视图了");
        [self.innerView removeFromSuperview];
        [self removeFromSuperview];
        if ([self.delegate respondsToSelector:@selector(tapFatherViewSetButtonSelectedNo)]) {
            [self.delegate tapFatherViewSetButtonSelectedNo];
        }
    }
}


//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    _newFrame = self.innerView.frame;
    self.innerView.frame = CGRectMake(self.innerView.frame.origin.x, self.innerView.frame.origin.y-100, self.innerView.frame.size.width, self.innerView.frame.size.height);
    
    [UIView commitAnimations];
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.innerView.frame = _newFrame;
}



@end
