iOS简易版的 仿popviewcontroller 功能
博客专区 > 冰泪_ 的博客 > 博客详情
iOS简易版的 仿popviewcontroller 功能
冰泪_ 发表于2年前
iOS简易版的 仿popviewcontroller 功能
  • 发表于 2年前
  • 阅读 394
  • 收藏 0
  • 点赞 2
  • 评论 0
摘要: 仿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];
}

 

共有 人打赏支持
粉丝 14
博文 58
码字总数 41278
×
冰泪_
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: