iOS简易版的 仿popviewcontroller 功能

//ZFYPopView.h

#import <UIKit/UIKit.h>

@interface ZFYPopView : UIView

 

typedef void(^dismissWithOperation)();

 

@property (nonatomic, strong) dismissWithOperation dismissOperation;
@property (nonatomic, strong) UIView *backView;
//初始化方法
//传入参数:模型数组,弹出原点,宽度,高度(每个cell的高度)
- (instancetype)initWithOrigin:(CGPoint)origin
                         width:(CGFloat)width
                        height:(CGFloat)height;

//弹出
- (void)pop;
//消失
- (void)dismiss;

 

@end

 

 

 

//ZFYPopView.m

#import "ZFYPopView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define CellLineEdgeInsets UIEdgeInsetsMake(0, 10, 0, 10)
#define LeftToView 10.f
#define TopToView 10.f

@interface ZFYPopView ()

@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat width;

 

@end

 

 

 

@implementation ZFYPopView

- (instancetype)initWithOrigin:(CGPoint)origin
                         width:(CGFloat)width
                        height:(CGFloat)height
{
    
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]) {
        //背景色为clearColor
        
        self.backgroundColor = [UIColor clearColor];
        self.origin = origin;
        self.height = height;
        self.width  = width;
        
        self.backView = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y, width, height)];
        _backView.backgroundColor = [UIColor grayColor];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = 10.0f;
        

        
        [self addSubview:self.backView];
        
        
    }
    return self;
}

 

//画出三角
- (void)drawRect:(CGRect)rect {
    //拿到当前视图准备好的画板
    CGContextRef context = UIGraphicsGetCurrentContext();
    //利用path进行绘制三角形
    CGContextBeginPath(context);//标记
    //这里设置箭头出现的位置
    
    //三角行三个点
    
    CGFloat startX = self.origin.x;
    CGFloat startY = self.origin.y+20;
    //第一个点
    CGContextMoveToPoint(context, startX, startY);//设置起点
    
    //下一个点
    CGContextAddLineToPoint(context, startX - 10, startY +10);
    
    //第三个点
    CGContextAddLineToPoint(context, startX, startY+20);
    
    
    CGContextClosePath(context);//路径结束标志,不写默认封闭
    
    [self.backView.backgroundColor setFill]; //设置填充色
    
    
    [self.backView.backgroundColor setStroke];
    
    CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path
    
    
}

- (void)pop {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    //动画效果弹出
    self.alpha = 0;
    CGRect frame = self.backView.frame;
    self.backView.frame = CGRectMake(self.origin.x, self.origin.y, 0, 0);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.backView.frame = frame;
 
        
    }];
}

- (void)dismiss {
    //动画效果淡出
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
        self.backView.frame = CGRectMake(self.origin.x, self.origin.y, 0, 0);
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
            if (self.dismissOperation) {
                self.dismissOperation();
            }
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (![touch.view isEqual:self.backView]) {
        [self dismiss];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

 

//调用方式

- (void)click:(UIButton *)sender {
    CGFloat x = sender.frame.origin.x+sender.frame.size.width+15;
    CGFloat y = sender.frame.origin.y;
    
    _popView = [[ZBPopView alloc]initWithOrigin:CGPointMake(x, y) width:kScreenWidth/4 height:kScreenHeight/5];
    

    _popView.dismissOperation = ^() {
        _popView = nil;
    };
    
    [_popView pop];
}

 

转载于:https://my.oschina.net/iceTear/blog/679798

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值