iOS中popwind简单例子

首先看下实现效果图:

话不多说直接看代码,首先是头文件:

//
//
//  Created by yuzhuo on 2017/12/15.
//

#import <UIKit/UIKit.h>

@protocol YZPopwindowViewDelegate <NSObject>

@optional
- (void)popViewClick;
@end


@interface YZPopwindowView : UIView

@property (weak, nonatomic) id<YZPopwindowViewDelegate>delegate;

-(instancetype)initWithFrame:(CGRect)frame contentStr:(NSString *)contentStr absoluteX:(CGFloat)x absoluteY:(CGFloat)y;
- (void)show;
- (void)disMiss;
- (BOOL)getShowing;

@end

然后是.m文件:

//
//
//  Created by yuzhuo on 2017/12/15.
//

#import "YZPopwindowView.h"

@interface YZPopwindowView ()

/** bg */
@property (nonatomic,strong) UIView *bg;

@property (nonatomic,strong) UIImageView *bottomTriggleView;

@property (nonatomic,strong) UIButton * btn;

@property (nonatomic,strong) UILabel *contentLabel;

/** UITapGestureRecognizer */
@property (nonatomic,strong) UITapGestureRecognizer *tapGesture;

/** showing */
@property (nonatomic,assign) BOOL showing;

@end

@implementation YZPopwindowView

-(instancetype)initWithFrame:(CGRect)frame contentStr:(NSString *)contentStr absoluteX:(CGFloat)x absoluteY:(CGFloat)y;
{
    self =[super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    if (self) {
        self.backgroundColor =[UIColor clearColor];
        self.bg = [[UIView alloc]initWithFrame:CGRectMake(frame.origin.x, y - frame.size.height-3, frame.size.width, frame.size.height - 7.5)];
        self.bg.backgroundColor =[UIColor nvColorWith333];
        self.bg.layer.cornerRadius = 4;
        [self initSubViews:frame contentStr:contentStr absoluteX:x];
        [self addSubview:self.bg];
        self.tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeAlert:)];
    }
    
    return self;
    
}

- (void)initSubViews:(CGRect)frame contentStr:(NSString *)contentStr absoluteX:(CGFloat)x{
    self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 13, frame.size.width - 30, 32)];
    self.contentLabel.textColor = [UIColor whiteColor];
    self.contentLabel.text = contentStr;
    self.contentLabel.font = [UIFont systemFontOfSize:12.f];
    self.contentLabel.numberOfLines = 2;
    self.contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.bg addSubview:self.contentLabel];
    self.btn =[UIButton buttonWithType:UIButtonTypeSystem];
    self.btn.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    [self.btn addTarget:self action:@selector(Clicks:) forControlEvents:UIControlEventTouchUpInside];
    [self.bg addSubview:self.btn];
    x = x > (ScreenWidth - 35)?(ScreenWidth - 35): x;
    self.bottomTriggleView = [[UIImageView alloc]initWithFrame:CGRectMake(x-1.5, self.bg.bottom - 1, 15, 7.5)];
    UIImage *img = [self getImageByPath:CGRectMake(0, 0, 45, 22.5)];
    self.bottomTriggleView.image = img;
    [self addSubview:self.bottomTriggleView];
}

- (void)show
{
    UIWindow * keyWindow =[UIApplication sharedApplication ].keyWindow;
    [keyWindow addSubview:self];
    if (![self.superview.gestureRecognizers containsObject:self.tapGesture]) {
        [self.superview addGestureRecognizer:self.tapGesture];
        self.superview.userInteractionEnabled = YES;
    }
    _showing = YES;
}

- (BOOL)getShowing {
    return _showing;
}

- (void)disMiss
{
    _showing = NO;
    [self.superview removeGestureRecognizer:self.tapGesture];
    [self removeFromSuperview];
}
- (void)Clicks:(id)sender
{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(popViewClick)]) {
        [self disMiss];
        [_delegate popViewClick];
    }
}

- (void)closeAlert:(UITapGestureRecognizer *) gesture{
    [self disMiss];
}

-(UIImage *)getImageByPath:(CGRect)rect {
    // Drawing code
    UIGraphicsBeginImageContext(rect.size);
    //定义画图的path
    UIBezierPath *path = [[UIBezierPath alloc] init];
    
    //path移动到开始画图的位置
    [path moveToPoint:CGPointMake(rect.origin.x, rect.origin.y)];
    //从开始位置画一条直线到(rect.origin.x + rect.size.width, rect.origin.y)
    [path addLineToPoint:CGPointMake(rect.origin.x + rect.size.width, rect.origin.y)];
    //再从rect.origin.x + rect.size.width, rect.origin.y))画一条线到(rect.origin.x + rect.size.width/2, rect.origin.y + rect.size.height)
    [path addLineToPoint:CGPointMake(rect.origin.x + rect.size.width/2, rect.origin.y + rect.size.height)];
    
    [path addLineToPoint:CGPointMake(rect.origin.x, rect.origin.y)];
    //关闭path
    [path closePath];
    
    //三角形内填充颜色
    [[UIColor nvColorWith333] setFill];
    
    [path fill];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}


@end

 

如何使用:

- (void)showAlertView:(NSString*)content responseView:(UIView *)respView {
    CGFloat width = 0.f;
    CGFloat leftMargin = 0.f;
    UILabel *widthLabel = [UILabel new];
    widthLabel.text = content;
    widthLabel.font = [UIFont systemFontOfSize:12.f];
    [widthLabel sizeToFit];
    if (widthLabel.width > SCREEN_WIDTH - 64){
        width = SCREEN_WIDTH - 32;
    }else {
        width = widthLabel.width + 32;
    }
    if (respView.tag == 3){
        leftMargin = SCREEN_WIDTH/2 - width/2;
        
    }else {
        leftMargin = SCREEN_WIDTH - width - 16;
    }
    
    _popView = [[YZPopwindowView alloc]initWithFrame:CGRectMake(leftMargin, 100, width, 66) contentStr:content absoluteX:respView.centerX absoluteY:respView.screenViewY];
    _popView.delegate = self;
    [_popView show];
}

//需要实现在协议
- (void)popViewClick {
    [_popView disMiss];
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值