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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容