iOS Category分类长按控件弹出View

填写描述能让测试小哥们更省力

添加描述长按label、button控件能显示popview


#import "UILabel+test.h"

#import

#import "GNSTestPopView.h"

//@interface UILabel()

//@property (assign,nonatomic) CGPoint touchPoint;

//@end

staticconstchar*pointFlag ="pointFlag";

staticconstchar*descFlag ="descFlag";

@implementation UILabel (test)

- (NSString*)desc{

    return objc_getAssociatedObject(self, &descFlag);

}

- (void)setDesc:(NSString*)desc {

    objc_setAssociatedObject(self,&descFlag,desc ,OBJC_ASSOCIATION_COPY_NONATOMIC);


    //新增长按手势

    UILongPressGestureRecognizer* longPressGr = [[UILongPressGestureRecognizer  alloc]initWithTarget:self action:@selector(longPressToDo:)];

    longPressGr.minimumPressDuration=0.8;

    [selfaddGestureRecognizer:longPressGr];

    self.userInteractionEnabled = YES;


}

- (CGPoint )touchPoint {

    NSValue *value = objc_getAssociatedObject(self, &pointFlag);

      if(value) {

          CGPointtouchPoint;

          [valuegetValue:&touchPoint];

          returntouchPoint;

      }else{

          returnCGPointZero;

      }


}

- (void)setTouchPoint:(CGPoint)touchPoint {

    NSValue*value = [NSValuevalue:&touchPointwithObjCType:@encode(CGPoint)];

    objc_setAssociatedObject(self,&pointFlag, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event {

    //获取点击的point

      NSSet*allTouch = [eventallTouches];

      UITouch*touch = [allTouchanyObject];

      CGPoint point = [touch locationInView:[UIApplication sharedApplication].windows.firstObject];

      self.touchPoint= point;

}

-(void)longPressToDo:(UILongPressGestureRecognizer*)gesture

{

    if(gesture.state==UIGestureRecognizerStateBegan)

    {

        [[GNSTestPopView shareManager] showWithTextStrWithTextStr:self.desc WithTouchPoint:self.touchPoint];

    }

    if(gesture.state == UIGestureRecognizerStateEnded||gesture.state == UIGestureRecognizerStateCancelled) {

        [[GNSTestPopView shareManager] dismiss];

        return;

    }

}



#import "GNSTestPopView.h"

#define SCREENWIDTHX ([[UIScreen mainScreen] bounds].size.width)

#define SCREENHEIGHTX ([[UIScreen mainScreen] bounds].size.height)

@interface GNSTestPopView()

@property (strong, nonatomic) UILabel *textLabel;

@end

@implementation GNSTestPopView

+ (GNSTestPopView *)shareManager {

    staticGNSTestPopView* operation =nil;

    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        operation = [[selfalloc]init];

    });

    returnoperation;

}

- (instancetype)init {


    self= [superinit];

    if(self) {

        self.textLabel= [[UILabelalloc]initWithFrame:CGRectMake(16,16,200-32,0)];

        self.textLabel.textColor= [UIColorblackColor];

        self.textLabel.font= [UIFontsystemFontOfSize:17];

        [selfaddSubview:self.textLabel];

        self.textLabel.numberOfLines=0;

        self.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245./255.0 blue:245.0/255.0 alpha:0.9];

    }

    return self;

}

- (void)showWithTextStrWithTextStr:(NSString *)str WithTouchPoint:(CGPoint)point {

    CGPointfinalPoint;

    if(SCREENWIDTHX- point.x>=200){

        finalPoint = point;

    }elseif(point.x>=200) {

        finalPoint =CGPointMake(point.x-200, point.y);

    }else{

        finalPoint =CGPointMake(SCREENWIDTHX-200, point.y);

    }


    self.textLabel.text= str;

    CGSizesize = [self.textLabelsizeThatFits:CGSizeMake(200-32,MAXFLOAT)];

    self.textLabel.frame=CGRectMake(16,16,200-32, size.height);

    self.frame=CGRectMake(finalPoint.x, finalPoint.y,200, size.height+32);

    [[UIApplication sharedApplication].windows.firstObject addSubview:self];


}

- (void)dismiss {

    [self removeFromSuperview];

}

github地址https://github.com/GNStark/TestPopViewDemo/tree/main

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

推荐阅读更多精彩内容