日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

简单的自定义弹框

發布時間:2025/5/22 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单的自定义弹框 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  作為初學者,很多人都是用的系統自帶的彈框,非常的簡單,完全不能滿足用戶的交互,所以這里,我們需要自定義一個彈框,把輸入框、圖片、按鈕等添加到彈框里面。為了避免重復冗余的代碼,參考了別人的代碼,自己做了一個自定義彈框,可以在項目中使用到。給大家一個思路。  

  這是代碼的接口定義,只需要調用一行代碼就可以彈出一個自定義的視圖啦。還會添加一些動畫效果,讓彈框彈出跟消失更美觀。

+ (void)showPromptBoxWithCustomView:(UIView *)customView;+ (void)promptBoxHiden;// 不會消失,需要手動點擊 + (void)showPromptBoxWithImage:(UIImage *)image text:(NSString *)text;// 在幾秒有消失 + (void)showPromptBoxWithText:(NSString *)text;

?

  動畫效果跟顯示框效果也是必須有的

/*** 顯隱提示框*/ - (void)promptBoxHidenWithView:(SQPromptBox *)view speed:(CGFloat)speed {[UIView animateWithDuration:speed animations:^{view.coverView.alpha = 0.0f;} completion:^(BOOL finished) {if (finished) {[view.coverView removeFromSuperview];}}]; }/*** 動畫效果*/ - (void)presentWithDuration:(CGFloat)duration speed:(CGFloat)speed {[UIView animateWithDuration:speed animations:^{self.alpha = 1.0f;self.coverView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5f];} completion:^(BOOL finished) {if (finished) {if (!self.customView) {[self performBlock:^{[self promptBoxHidenWithView:self speed:speed];} afterDelay:duration];}}}]; }

  

  還有一步很重要,需要通過字數來計算label的高度,如果沒有這一步完成的效果就沒有那么好看了。

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize {NSDictionary *attrs = @{NSFontAttributeName : font};return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; }

?

  這是一個布局的方法,需要計算好,不然就不能自適應不能尺寸的iPhone了

- (void)layoutSubviews {if (!self.customView) {CGSize textSize = [self sizeWithText:self.textLabel.text font:self.textLabel.font maxSize:CGSizeMake(kWidth - 2 * kMagin, MAXFLOAT)];if (self.imageView.image) {self.imageView.frame = CGRectMake((self.frame.size.width - self.image.size.width) / 2.0f, kMagin, self.image.size.width, self.image.size.height);self.textLabel.frame = CGRectMake((kWidth - textSize.width) / 2.0f, CGRectGetMaxY(self.imageView.frame) + kMagin, textSize.width, textSize.height);}else {CGFloat height = textSize.height < kHeight ? kHeight : textSize.height;self.textLabel.frame = CGRectMake((kWidth - textSize.width) / 2.0f, kMagin, textSize.width, height);}CGRect rect = [self typeFrame];rect.size.height = CGRectGetMaxY(self.textLabel.frame) + kMagin;self.frame = rect;} }

?

  

?

  這是把彈框和蒙版加載當前顯示控制器的view上,通過下面的方法來遍歷,能獲取到控制器

/*** 得到currentViewController*/ - (UIViewController *)getCurrenViewController {UIViewController *currenVIewController = nil;// 返回一個app的實例,keyWindow(只讀)UIWindow *window = [UIApplication sharedApplication].keyWindow;if (window.windowLevel != UIWindowLevelNormal) {NSArray *windows = [[UIApplication sharedApplication] windows];for (UIWindow *tempWindow in windows) {if (tempWindow.windowLevel == UIWindowLevelNormal) {window = tempWindow;break;}}}UIView *frontView = [[window subviews] objectAtIndex:0];// 接受下一個響應者id nextResponder = [frontView nextResponder];if ([nextResponder isKindOfClass:[UIViewController class]]) {currenVIewController = nextResponder;} else {currenVIewController = window.rootViewController;}return currenVIewController; }

?

  因為代碼比較多,就沒有一一展示出來,就提供一個小小的思路,供大家參考。

  最后就是上面代碼的網站
? ? ? https://github.com/empty-sq/-.git

轉載于:https://www.cnblogs.com/shensq/p/5203445.html

總結

以上是生活随笔為你收集整理的简单的自定义弹框的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。