zoukankan      html  css  js  c++  java
  • 第十九篇、坐标转换,弹出下拉选择框

    .h

    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    
    
    typedef NS_ENUM(NSInteger,Type) {
        cityType,
        recommentType,
        classifyType
    };
    /*
     
      使用方式
     NSArray *array = [NSArray arrayWithObjects:@"1222",@"1222",@"1222",@"1222",@"1222", nil];
     _selectedTypeView = [[SelectedTypeView alloc ]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) button:but dataArray:array];
     */
    
    
    @protocol SelectedTypeViewDelegate <NSObject>
    
    @optional
    - (void) seletedTypeFinish:(NSString *)typeString index:(NSInteger)index type:(Type)type;
    
    @end
    
    @interface SelectedTypeView : UIView
    
    - (id)initWithFrame:(CGRect)frame button:(UIButton *)typeButton dataArray:(NSArray *)arry type:(Type) type;
    
    @property (nonatomic,weak) id<SelectedTypeViewDelegate>delegate;
    
    @end

    .m

    #import "SelectedTypeView.h"
    #import "MainCityModel.h"
    #import "MainCitySportModel.h"
    #import "MainOpenCityModel.h"
    
    @interface SelectedTypeView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
    {
        CGRect _rect;
    }
    
    @property (strong,nonatomic) UITableView *contentTableView;
    @property (strong, nonatomic) UIView *backgroundView;
    @property (strong,nonatomic) UIButton *button;
    @property (strong,nonatomic) NSArray *dataArray;
    @property (assign, nonatomic) Type type;
    
    @end
    
    @implementation SelectedTypeView
    
    
    // 懒加载
    -(UITableView *)contentTableView
    {
        if (! _contentTableView) {
            UITableView *tableView = [[UITableView alloc]init];
            tableView.delegate = self;
            tableView.dataSource = self;
            tableView.rowHeight = 44;
            _contentTableView = tableView;
        }
        return _contentTableView;
    }
    
    - (id)initWithFrame:(CGRect)frame button:(UIButton *)typeButton dataArray:(NSArray *)arry type:(Type) type
    {
        self = [super initWithFrame:frame];
        if (self) {
            _rect = frame;
            _button = typeButton;
            _dataArray = [NSArray arrayWithArray:arry];
            self.type = type;
            [self setupUI];
        }
        return self;
    }
    
    // 初始化UI
    - (void) setupUI
    {
        UIWindow *window = [[UIApplication sharedApplication] delegate].window;
        UIView *backgroundView = [[UIView alloc]initWithFrame:_rect];
        backgroundView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0];
        [window addSubview:backgroundView];
        _backgroundView = backgroundView;
        
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(diss)];
        tap.delegate = self;
        [backgroundView addGestureRecognizer:tap];
        
        // 转换坐标
        CGRect buttonRect = [window convertRect:_button.frame fromView:_button.superview];
        CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
        CGFloat tableViewY = buttonRect.origin.y + buttonRect.size.height;
        
    
        
         self.contentTableView.transform = CGAffineTransformMakeScale(1.0, 0);
        
        // 设置描点
        _contentTableView.layer.anchorPoint = CGPointMake(0.5, 0);
    //    _contentTableView.layer.position = CGPointMake(0, 0);
        
        [UIView animateWithDuration:0.3 animations:^{
            
            // 进行还原
            _contentTableView.transform = CGAffineTransformIdentity;
        }];
        self.contentTableView.frame = CGRectMake(0, tableViewY, [UIScreen mainScreen].bounds.size.width, (_dataArray.count * 44.0 + tableViewY) > screenHeight ? (screenHeight - tableViewY) : _dataArray.count * 44.0 );
        
        [backgroundView addSubview:_contentTableView];
    }
    
    #pragma mark------UITableViewDatasource Methods
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.dataArray.count;
    }
    
    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"ID";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        switch (_type) {
            case cityType:
            {
                if (0 == indexPath.row) {
                    cell.textLabel.text = _dataArray[indexPath.row];
                }else{
                    MainOpenCityListListModel *list=_dataArray[indexPath.row];
                    cell.textLabel.text=list.shortname;
                }
    
            }
                break;
            case recommentType:
            {
                cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
            }
                break;
            case classifyType:
            {
                if (0 == indexPath.row) {
                    cell.textLabel.text = _dataArray[indexPath.row];
                }else{
                    MainCitySportListModel *list = _dataArray[indexPath.row];
                    cell.textLabel.text=list.name;
                }
            }
                break;
                
            default:
                break;
        }
        
    
        
        return cell;
    }
    
    #pragma mark------UITableViewDelegate Methods
    - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        [self diss];
        
        if ([_delegate respondsToSelector:@selector(seletedTypeFinish:index: type:)]) {
            [_delegate seletedTypeFinish:[_dataArray objectAtIndex:indexPath.row] index:indexPath.row type:_type];
        }
        
    }
    
    #pragma mark----private Methods
    // 去掉当前View
    - (void) diss
    {
        _backgroundView.alpha = 1.0;
        [UIView animateWithDuration:0.3 animations:^{
            _contentTableView.transform = CGAffineTransformMakeScale(1.0, 0.000001);
            //_backgroundView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [_backgroundView removeFromSuperview];
        }];
        
    
    }
    
    - (void) removeCurrentView
    {
        
    }
    
    #pragma mark-----gestureRecognizerDelegate
    
    /**
     * @brief 手势的代理方法
     */
    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        NSLog(@"class %@",touch.view);
        
        if (touch.view == _backgroundView )
        {
            return YES;
        }
        return NO;
    }
    
    @end
  • 相关阅读:
    SGU 499 Greatest Greatest Common Divisor
    pku 3468 A Simple Problem with Integers
    pku2226 Muddy Fields
    pku3041 Asteroids
    java基础string操作
    PowerDesigner(7)转载
    java基础2
    PowerDesigner(6)转载
    java基础3
    java基础(1)
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5818224.html
Copyright © 2011-2022 走看看