点击cell 右上角的按钮 弹出带尖的弹窗

1。首先cell里面

@protocol personal_Center_CellDelegate 

- (void)cellDontLikeClick:(UITableViewCell *)cell rectofBtn:(CGRect)rect;
@optional
@end
@property (nonatomic, weak) id delegate;
 [_toprightClickButton addTarget:self action:@selector(toprightClickButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//把自己 和按钮的 frame 传出 
- (void)toprightClickButtonClick:(UIButton *)btn
{
    if (_delegate && [_delegate respondsToSelector:@selector(cellDontLikeClick:rectofBtn:)]) {
        [_delegate cellDontLikeClick:self rectofBtn:btn.frame];
    }
}

2 VC 里面

- (void)cellDontLikeClick:(UITableViewCell *)cell rectofBtn:(CGRect)rect
{
    NSIndexPath *indexpatch = [_tableView indexPathForCell:cell];
    CGRect rectInTableView = [_tableView rectForRowAtIndexPath:indexpatch];
    CGRect rectInSuperview = [_tableView convertRect:rectInTableView toView:self.view];

    //这里是cell 的位置
    NSLog(@"%f,%f", rectInSuperview.origin.x, rectInSuperview.origin.y);

    CGRect realrect = CGRectMake(rectInSuperview.origin.x + rect.origin.x, rectInSuperview.origin.y + rect.origin.y, rect.size.width, rect.size.height);
    //这里是btn 的位置
    NSLog(@"%f,%f", rect.origin.x, rect.origin.y);
    NSLog(@"%f,%f", realrect.origin.x, realrect.origin.y);
    [self alertviewMake:realrect cell:cell];
}
- (void)alertviewMake:(CGRect)realrect cell:(UITableViewCell *)realcell
{
    NSIndexPath *indexpatch = [_tableView indexPathForCell:realcell];

    NSLog(@"indexpatch.row == %ld", indexpatch.row);

    DD_Media_AlertView *view = [[DD_Media_AlertView alloc] init];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:view];
}

3.封装的 DD_Media_AlertView

#import 

NS_ASSUME_NONNULL_BEGIN
typedef void (^topClickButtonBlock)(NSString *buttontitle);
typedef void (^bottomClickButtonBlock)(NSString *buttontitle);


@interface DD_Media_AlertView : UIView
@property (nonatomic, strong) UIButton *bgView; //灰色背景

@property (nonatomic, strong) UIView *contentView;         //按钮bg
@property (nonatomic, strong) UIButton *topClickButton;    //上边按钮
@property (nonatomic, strong) UIButton *bottomClickButton; //下边按钮
@property (nonatomic, strong) UIImageView *tipimageView;   //小尖

@property (nonatomic, copy) topClickButtonBlock topblock;
@property (nonatomic, copy) bottomClickButtonBlock bottomblock;

- (void)setToptitle:(NSString *)toptitle BottomTitle:(NSString *)bottomtitle;
- (void)setRect:(CGRect)rect;

@end

NS_ASSUME_NONNULL_END
#define WIDTH_SCALE (SCREEN_WIDTH / 750)
#import "DD_Media_AlertView.h"


@implementation DD_Media_AlertView

- (instancetype)init
{
    if ([super init]) {
        [self makeUI];
    }
    return self;
}
- (void)makeUI
{
    [self addSubview:self.bgView];
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.topClickButton];
    [self.contentView addSubview:self.bottomClickButton];
    [self addSubview:self.tipimageView];
}
- (void)setToptitle:(NSString *)toptitle BottomTitle:(NSString *)bottomtitle
{
    [self.topClickButton setTitle:toptitle forState:UIControlStateNormal];
    [self.bottomClickButton setTitle:bottomtitle forState:UIControlStateNormal];
}
- (void)setRect:(CGRect)rect
{
    float Hofviewbottom = rect.origin.y + 176 * WIDTH_SCALE + 26 + CuteStatusAndNavigationHeight;
//判断是不是超出屏幕 要是超出就 向上显示 
    if (Hofviewbottom < SCREEN_HEIGHT - CuteStatusAndNavigationHeight) {
        self.contentView.frame = CGRectMake(SCREEN_WIDTH - 280 * WIDTH_SCALE, rect.origin.y + rect.size.height + 25 + CuteStatusAndNavigationHeight, 260 * WIDTH_SCALE, 176 * WIDTH_SCALE);
        self.tipimageView.frame = CGRectMake(SCREEN_WIDTH - 40, self.contentView.top - 6, 13.5, 6.5);
    } else {
        self.contentView.frame = CGRectMake(SCREEN_WIDTH - 280 * WIDTH_SCALE, rect.origin.y - (rect.size.height) + CuteStatusAndNavigationHeight - (176 * WIDTH_SCALE), 260 * WIDTH_SCALE, 176 * WIDTH_SCALE);
        self.tipimageView.frame = CGRectMake(SCREEN_WIDTH - 40, self.contentView.bottom - 1, 13.5, 6.5);
        self.tipimageView.transform = CGAffineTransformMakeScale(1.0, -1.0);
    }
}
- (void)bgViewclick:(UIButton *)btn
{
    [self removeFromSuperview];
}
- (UIButton *)bottomClickButton
{
    if (!_bottomClickButton) {
        _bottomClickButton = [[UIButton alloc] init];
        [_bottomClickButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [_bottomClickButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#F8F8F8"]] forState:UIControlStateHighlighted];
        [_bottomClickButton setTitleColor:[UIColor colorWithHexString:@"#4E4E4E"] forState:UIControlStateNormal];
        _bottomClickButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_bottomClickButton addTarget:self action:@selector(bottomClickButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _bottomClickButton.frame = CGRectMake(0, 88 * WIDTH_SCALE, 260 * WIDTH_SCALE, 88 * WIDTH_SCALE);
    }
    return _bottomClickButton;
}
- (void)bottomClickButtonClick:(UIButton *)btn
{
    if (self.bottomblock) {
        self.bottomblock(btn.titleLabel.text);
    }
}
- (UIButton *)topClickButton
{
    if (!_topClickButton) {
        _topClickButton = [[UIButton alloc] init];
        [_topClickButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [_topClickButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#F8F8F8"]] forState:UIControlStateHighlighted];
        [_topClickButton setTitleColor:[UIColor colorWithHexString:@"#4E4E4E"] forState:UIControlStateNormal];
        _topClickButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_topClickButton addTarget:self action:@selector(topClickButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _topClickButton.frame = CGRectMake(0, 0, 260 * WIDTH_SCALE, 88 * WIDTH_SCALE);
    }
    return _topClickButton;
}
- (void)topClickButtonClick:(UIButton *)btn
{
    if (self.topblock) {
        self.topblock(btn.titleLabel.text);
    }
}
- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        [_contentView setBackgroundColor:[UIColor whiteColor]];
        _contentView.layer.masksToBounds = YES;
        _contentView.layer.cornerRadius = 3;
    }
    return _contentView;
}
- (UIButton *)bgView
{
    if (!_bgView) {
        _bgView = [[UIButton alloc] init];
        _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.1;
        [_bgView addTarget:self action:@selector(bgViewclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bgView;
}
- (UIImageView *)tipimageView
{
    if (!_tipimageView) {
        _tipimageView = [[UIImageView alloc] init];
        _tipimageView.image = [UIImage imageNamed:@"media_alert_tip"];
    }
    return _tipimageView;
}
@end

效果 :


image.png
image.png

你可能感兴趣的:(点击cell 右上角的按钮 弹出带尖的弹窗)