子类控件-快捷代码

2019-07-10  本文已影响0人  守护地中海的花
- (UIButton *)saveButton
{
    if (!_saveButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];
        [button setTitle:@"保存" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightBold];
        button.frame = CGRectMake(15*ADAPTER_WIDTH, HEIGHT - 44 *ADAPTER_WIDTH - kBottomSafeHeight - 27*ADAPTER_WIDTH, WIDTH - 30*ADAPTER_WIDTH, 44*ADAPTER_WIDTH);
        button.layer.cornerRadius = 5*ADAPTER_WIDTH;
        button.layer.masksToBounds = YES;
        //button.backgroundColor = kAZHMainColor;
        button.backgroundColor = [UIColor lightGrayColor];
        _saveButton = button;
    }
    return _saveButton;
}
UIView *searchBGView = [[UIView alloc]initWithFrame:CGRectMake(12*ADAPTER_WIDTH, kStatusBarHeight + 9, WIDTH - 24*ADAPTER_WIDTH, 26)];
[self addSubview:searchBGView];
searchBGView.backgroundColor = rgba(246, 246, 246, 1);
searchBGView.layer.cornerRadius = 13;
searchBGView.layer.masksToBounds = YES;
searchBGView.userInteractionEnabled = YES;
[searchBGView bk_whenTapped:^{
    [sharedKeyWindow showWarning:@"功能升级中..."];
}];

UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchBGView addSubview:searchButton];
//searchButton.frame = CGRectMake(22, 5.5, 15, 15);
searchButton.frame = CGRectMake(0, 0, 15 + 35, 15 + 11);
[searchButton setImage:[UIImage getPNGimageInBundleWithName:@"search_gray"] forState:UIControlStateNormal];
searchButton.imageEdgeInsets = UIEdgeInsetsMake(5.5, 22, 5.5, 13);
[searchButton bk_addEventHandler:^(id sender) {
    NSLog(@"搜索");
} forControlEvents:UIControlEventTouchUpInside];

UILabel *searchLab = [[UILabel alloc]init];
[searchBGView addSubview:searchLab];
searchLab.frame = CGRectMake(searchButton.right, 0, searchBGView.width - searchButton.right - 5, searchBGView.height);
searchLab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
searchLab.textColor = [UIColor lightGrayColor];
searchLab.text = @"搜索关键字";

UIView *searchBGView = [[UIView alloc]initWithFrame:CGRectMake(49, kStatusBarHeight + 9, WIDTH - 49 - 27, 26)];
    [self.customNavi addSubview:searchBGView];
    searchBGView.backgroundColor = rgba(246, 246, 246, 1);
    searchBGView.layer.cornerRadius = 13;
    searchBGView.layer.masksToBounds = YES;
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBGView addSubview:searchButton];
    //searchButton.frame = CGRectMake(22, 5.5, 15, 15);
    searchButton.frame = CGRectMake(0, 0, 15 + 35, 15 + 11);
    [searchButton setImage:[UIImage getPNGimageInBundleWithName:@"search_gray"] forState:UIControlStateNormal];
    searchButton.imageEdgeInsets = UIEdgeInsetsMake(5.5, 22, 5.5, 13);
    [searchButton bk_addEventHandler:^(id sender) {
        NSLog(@"搜索");
    } forControlEvents:UIControlEventTouchUpInside];
    
    UITextField *searchTF = [[UITextField alloc]initWithFrame:CGRectMake(searchButton.right, 0, searchBGView.width - searchButton.right - 5, searchBGView.height)];
    [searchBGView addSubview:searchTF];
    searchTF.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
    searchTF.placeholder = @"输入城市名进行搜索";
    searchTF.delegate = self;
    [searchTF addTarget:self action:@selector(addObserveSearchTF:) forControlEvents:UIControlEventEditingChanged];
    searchTF.returnKeyType = UIReturnKeySearch;
    self.searchTF = searchTF;

//搜索相关
@property(nonatomic,assign)NSTimeInterval lastTimeInterval;
@property(nonatomic,assign)BOOL quickStatus;
@property(nonatomic,strong)NSString *lastContent;
@property(nonatomic,assign)BOOL timeInterval;//时间间隔
@property(nonatomic,strong)UITextField *searchTF;

- (void)addObserveSearchTF:(UITextField *)tf
{
    UITextRange *range = tf.markedTextRange;
    if (range == nil)
    {
        //如果输入框框输入过快 则需要判断
        /*
         1.默认第一次肯定加载的,记录lastTimeInterval 并且quickStatus 记录为NO
         2.如果上一次加载的记录和当前的currentTimeInterval之间大于1.0 则继续加载一次 记录lastTimeInterval 并且quickStatus 记录为NO
         2.1 如果上一次加载的记录和当前的currentTimeInterval之间小于于1.0 不记录lastTimeInterval 并且quickStatus 记录为YES
         3. 如果是quickStatus 为YES 并且lastContent 和当前tf内容一致 则加载一次 (不重复加载)
         */
        NSTimeInterval currentTimeInterval = [NSString getCurrentTimeStamp];
        if ((currentTimeInterval - self.lastTimeInterval) > self.timeInterval) {
            self.quickStatus = NO;
            self.lastTimeInterval = currentTimeInterval;
            //搜索
            NSLog(@"%@",self.searchTF.text);
            self.searchResultVC.keyword = self.searchTF.text;
        } else {
            self.quickStatus = YES;
            //self.lastTimeInterval = currentTimeInterval;
            [self performSelector:@selector(performHandleMethod) withObject:nil afterDelay:self.timeInterval];
        }
    }
}
- (void)performHandleMethod
{
    NSTimeInterval currentTimeInterval = [NSString getCurrentTimeStamp];
    if ((currentTimeInterval - self.lastTimeInterval) > self.timeInterval && self.quickStatus) {
        if (![self.searchTF.text isEqualToString:self.lastContent]) {
            self.lastContent = self.searchTF.text;
            //搜索
            NSLog(@"%@",self.searchTF.text);
            self.searchResultVC.keyword = self.searchTF.text;
        }
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"textFieldShouldBeginEditing");
    self.searchResultVC.view.hidden = NO;
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"textFieldShouldEndEditing");
    //self.searchResultVC.view.hidden = YES;
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%@",self.searchTF.text);
    self.searchResultVC.keyword = self.searchTF.text;
    return YES;
}
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
//对fame添加监听
[vc.view addObserver:self forKeyPath:@"hidden" options:options context:nil];

[_searchResultVC.view removeObserver:self forKeyPath:@"hidden"];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"hidden"]) {
        UIView *view = (UIView *)object;
    }
}
- (void)longGesture
{
    WK(weakSelf)
    UIAlertController *longAlertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存到相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImage *image = [weakSelf.topBGView snapshotImage];
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [longAlertVC addAction:saveAction];
    [longAlertVC addAction:cancelAction];
    BaseNavigationVC *naviVC = [Factory backCurrentNavigationVC];
    [naviVC.topViewController presentViewController:longAlertVC animated:YES completion:nil];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error)
    {
        [sharedKeyWindow showWarning:@"保存失败"];
    }
    else
    {
        [sharedKeyWindow showWarning:@"保存成功"];
    }
}
控制器用view.superView 因为superview 会自动调用viewDidLoad
addSubview或者view.frame 控制器都会走viewDidLoad
.viewLoaded 则不会
[self removeAllSubviews];
    
    //item左右距离 item之间距离 item的行与行距离 item最大的宽度 item的高度
    CGFloat leftRightMargin = 12*ADAPTER_WIDTH,itemMargin = 13*ADAPTER_WIDTH,lineMargin = 16*ADAPTER_WIDTH,maxWidth = floor(WIDTH-24*ADAPTER_WIDTH),itemHeight = 23*ADAPTER_WIDTH;
    //item的orinX orinY
    CGFloat orinX = leftRightMargin,orinY = 15*ADAPTER_WIDTH;
    //item的内边距
    CGFloat itemPadding = 17*ADAPTER_WIDTH;
    //布局完成剩余宽度
    CGFloat leftWidth = WIDTH;
    //是否是最左边的
    BOOL leftMost = YES;
    //一共有多少行
    NSInteger rowNum = 0;//暂时用不到
    //第一 leftRightMargin+自己+itemMargin
    //第二 itemMargin+自己+itemMargin
    //如果 剩余的leftWidth 不足于放下 第一/第二种 则下一行布局
    for (NSInteger index = 0; index < tagModel.info.count; index ++)
    {
        CircleTagInfoModel *infoModel = tagModel.info[index];
        //lab
        UILabel *itemLab = [[UILabel alloc]init];
        [self addSubview:itemLab];
        itemLab.text = infoModel.name;
        itemLab.textColor = kColor86;
        itemLab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        itemLab.textAlignment = NSTextAlignmentCenter;
        itemLab.backgroundColor = [UIColor whiteColor];
        itemLab.layer.cornerRadius = 11.5*ADAPTER_WIDTH;
        itemLab.layer.masksToBounds = YES;
        itemLab.layer.borderColor = rgba(212, 212, 212, 1).CGColor;
        itemLab.layer.borderWidth = 1;
        //手势
        itemLab.tag = index;
        itemLab.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTagsLab:)];
        [itemLab addGestureRecognizer:tap];
        //先排除最大的Width 保证一行可以布局的下
        CGFloat itemWidth = [infoModel.name sizeWithFont:[UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular]].width+2*itemPadding;
        if (itemWidth > maxWidth)
        {
            itemWidth = maxWidth;
        }
        //布局
        //0.先计算剩余的是否够自己存放 + 判断是否是第一个leftMost + 如果itemMargin大于leftRightMargin(存在 剩余的长度可以存放leftRightMargin+自己+itemMargin 也就是说这个item是一行中最后一个排放)
        CGFloat itemTotalWidth = 0.0f;
        //一行中 最左侧item(其中这里只会执行一次 因为第二次没有预计算leftMost 如果预先计算 则可以网络请求或者布局for循环之前先计算)
        if (leftMost)
        {
            itemTotalWidth = leftRightMargin + itemWidth + itemMargin;
            //如果剩余不足布局一个 (其实这里还是存在itemMargin大于leftRightMargin情况)
            if (leftWidth < itemTotalWidth)
            {
                //NSLog(@"1");
                //直接设置为最大的吧
                itemTotalWidth = maxWidth;
                itemLab.frame = CGRectMake(orinX, orinY, itemWidth, itemHeight);
                //其他属性
                leftMost = YES;
                rowNum = rowNum + 1;
                orinX = leftRightMargin;
                orinY = (itemHeight + lineMargin) + orinY;
                leftWidth = WIDTH;
            }
            //可以布局
            else
            {
                //NSLog(@"2");
                itemLab.frame = CGRectMake(orinX, orinY, itemWidth, itemHeight);
                //其他属性
                leftMost = NO;
                rowNum = rowNum;
                orinX = itemLab.right+itemMargin;
                orinY = orinY;
                leftWidth = leftWidth - (leftRightMargin + itemWidth);
            }
        }
        //一行中 非左侧item
        else
        {
            itemTotalWidth = itemMargin + itemWidth + itemMargin;
            //如果剩余不足布局一个
            if (leftWidth < itemTotalWidth)
            {
                //如果还存在一种情况就是 布局行内最后一个
                itemTotalWidth = itemMargin + itemWidth + leftRightMargin;
                if (leftWidth > itemTotalWidth)
                {
                    //换行 还在这一行
                    //NSLog(@"3");
                    itemLab.frame = CGRectMake(orinX, orinY, itemWidth, itemHeight);
                    //其他属性
                    leftMost = YES;
                    rowNum = rowNum + 1;
                    orinX = leftRightMargin;
                    orinY = (itemHeight + lineMargin) + orinY;
                    leftWidth = WIDTH;
                }
                //真心放不下了
                else
                {
                    //换行 重启一行 这是leftMost了
                    //NSLog(@"4");
                    //其他属性
                    leftMost = YES;
                    rowNum = rowNum + 1;
                    orinX = leftRightMargin;
                    orinY = (itemHeight + lineMargin) + orinY;
                    leftWidth = WIDTH;
                    
                    itemLab.frame = CGRectMake(orinX, orinY, itemWidth, itemHeight);
                    
                    //其他属性
                    leftMost = NO;
                    rowNum = rowNum;
                    orinX = itemLab.right+itemMargin;
                    orinY = orinY;
                    leftWidth = leftWidth - (leftRightMargin + itemWidth);
                }
            }
            //可以布局
            else
            {
                //NSLog(@"5");
                itemLab.frame = CGRectMake(orinX, orinY, itemWidth, itemHeight);
                //其他属性
                leftMost = NO;
                rowNum = rowNum;
                orinX = itemLab.right+itemMargin;
                orinY = orinY;
                leftWidth = leftWidth - (itemMargin + itemWidth);
            }
        }
    }
    
    //orinY+itemHeight+lineMargin
上一篇下一篇

猜你喜欢

热点阅读