美文网首页恩美第二个APP项目控件类
iOS 获取某个点然后下拉弹出列表

iOS 获取某个点然后下拉弹出列表

作者: 雪_晟 | 来源:发表于2017-07-16 17:56 被阅读49次
    1.png

    如图,如果要在点击按钮的时候出现一个弹框。


    gif.gif

    我们需要做的就是,获取这个点 在 window上的位置。 把弹框加到window上,当然 背景色为白色,效果就像弹框出现在当前页面。

    关乎 坐标系转换,参考文章:坐标转换

    
        LxButton *button = [LxButton LXButtonWithTitle:@"请选择车牌号" titleFont:Font(14) Image:nil backgroundImage:nil backgroundColor:[UIColor blueColor] titleColor:[UIColor blackColor] frame:CGRectMake(20, 100, 100, 40)];
        [self.tempView addSubview:button];
    
        //获取window 
        UIWindow *window =[UIApplication sharedApplication].windows.lastObject;
        NSLog(@"%@",[UIApplication sharedApplication].windows);
    //把 当前点 转换到window上
      CGPoint  point = [window convertPoint:CGPointMake(button.frame.origin.x, button.frame.origin.y+ button.frame.size.height) fromView:self.tempView];
    
      
        self.dropView =[[LXDropView alloc]initWithTitleArray:@[@"国家经济见附件啊",@"噶个人;人孔盖看看",@"噶人看过卡"] orignPoint:point listWidth:Device_Width/2 selectResult:^(NSInteger index) {
            
        }];
    
        NCWS(weakSelf);
        [button addClickBlock:^(UIButton *button) {
            [weakSelf.dropView show];
        }];
    
    

    代码如下:

    
    #import <UIKit/UIKit.h>
    
    @interface LXDropView : UIView
    
    
    -(instancetype)initWithTitleArray:(NSArray *)titleA orignPoint:(CGPoint)orginPoint listWidth:(CGFloat)listWidth selectResult:(void (^)(NSInteger index))selectResult;
    -(void)show;
    @end
    
    
    #import "LXDropView.h"
    #import "LXDropListCell.h"
    #define ROWHEIGHT 44
    @interface LXDropView()<UITableViewDelegate,UITableViewDataSource>
    @property(nonatomic,assign)CGPoint orignPoint;
    @property(nonatomic,assign)CGFloat listWidth;
    @property(nonatomic,copy)void (^selectResult)(NSInteger index);
    @property(nonatomic,strong)NSArray *titleA;
    @property(nonatomic,strong)UITableView *listView;
    @end
    @implementation LXDropView
    -(instancetype)initWithTitleArray:(NSArray *)titleA orignPoint:(CGPoint)orginPoint listWidth:(CGFloat)listWidth selectResult:(void (^)(NSInteger))selectResult{
        
        self = [super init];
        
        if (self) {
            
            self.orignPoint = orginPoint;
            
            //超出屏幕宽度的判断
            if (listWidth +  self.orignPoint.x >= [UIScreen mainScreen].bounds.size.width) {
                listWidth = [UIScreen mainScreen].bounds.size.width;
            }
            self.listWidth = listWidth;
            self.selectResult = selectResult;
            self.titleA = titleA;
            self.frame = [UIScreen mainScreen].bounds;
            self.backgroundColor =[UIColor clearColor];
            [self setUp];
            [self.listView reloadData];
        }
        
        return self;
    }
    -(void)setUp{
        
        [self addSubview:self.listView];
        
    }
    
    
    -(void)show{
        
        UIWindow *keyWindow = [UIApplication sharedApplication].windows.lastObject;
        
        [keyWindow addSubview:self];
        
      
        [self creatAnimaiton];
    }
    
    -(void)creatAnimaiton{
        self.listView.alpha = 0.f;
        self.listView.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
        [UIView animateWithDuration:0.2f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.listView.transform = CGAffineTransformMakeScale(1.05f, 1.05f);
            self.listView.alpha = 1.f;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.08f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
                self.listView.transform = CGAffineTransformIdentity;
            } completion:nil];
        }];
    
    }
    -(void)dismiss{
        [UIView animateWithDuration:0.3f animations:^{
             self.listView.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
             self.listView.alpha = 0.f;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.titleA.count;
        
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        LXDropListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        if (!cell) {
            cell =[[LXDropListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.contentLabel.text = self.titleA[indexPath.row];
        return cell;
    }
    -(UITableView *)listView{
        if (!_listView) {
            _listView =[[UITableView alloc]initWithFrame:CGRectMake(self.orignPoint.x, self.orignPoint.y, self.listWidth, self.titleA.count * ROWHEIGHT) style:UITableViewStylePlain];
            _listView.delegate = self;
            _listView.dataSource = self;
            _listView.showsVerticalScrollIndicator = NO;
            _listView.showsHorizontalScrollIndicator = NO;
            _listView.rowHeight = ROWHEIGHT;
            [_listView registerNib:[UINib nibWithNibName:@"LXDropListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
            _listView.layer.cornerRadius =3;
            _listView.layer.masksToBounds = YES;
        }
        return _listView;
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        
        if (self.selectResult) {
            self.selectResult(indexPath.row);
            [self dismiss];
        }
    }
    //-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //    UITouch *touch =[touches anyObject];
    //    
    //    CGPoint point = [touch locationInView:self];
    //    point = [self.listView.layer convertPoint:point fromLayer:self.layer];
    //    if ([self.listView.layer containsPoint:point]  ) {
    //        
    //    }else{
    ////        [self dismiss];
    //        [self removeFromSuperview];
    //        
    //    }
    //    
    //}
    @end
    
    

    demo地址:漫漫的demo

    相关文章

      网友评论

        本文标题:iOS 获取某个点然后下拉弹出列表

        本文链接:https://www.haomeiwen.com/subject/wqfxkxtx.html