简单的自定义弹框
作為初學者,很多人都是用的系統自帶的彈框,非常的簡單,完全不能滿足用戶的交互,所以這里,我們需要自定義一個彈框,把輸入框、圖片、按鈕等添加到彈框里面。為了避免重復冗余的代碼,參考了別人的代碼,自己做了一個自定義彈框,可以在項目中使用到。給大家一個思路。
這是代碼的接口定義,只需要調用一行代碼就可以彈出一個自定義的視圖啦。還會添加一些動畫效果,讓彈框彈出跟消失更美觀。
+ (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
總結
- 上一篇: SQL语句将某字段查询出以逗号隔开
- 下一篇: 过程作为黑箱抽象——《计算机程序的构造和