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

1。首先cell里面

@protocol personal_Center_CellDelegate <NSObject>

- (void)cellDontLikeClick:(UITableViewCell *)cell rectofBtn:(CGRect)rect;
@optional
@end
@property (nonatomic, weak) id<personal_Center_CellDelegate> 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 <UIKit/UIKit.h>

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,142评论 1 32
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 早上起来第一件事就是看看我还能再躺几分钟。 走出家门就像被鞭策般莫名的越走越快,每次走到一个院里时总能看到几个大爷...
    空灵异阅读 138评论 0 0
  • 论开会的无聊性…
    隔壁老王是中醫阅读 796评论 0 0
  • 微信普及,社交电商风行,我们会发现自己朋友圈里做微商的人越来越多了。 我不排斥微商,能够利用微信这个平台做生意,并...
    木子桃心说阅读 1,375评论 1 2