UIPopoverController 指向 UIBarButtonItem 并缓慢消失
2015-10-19 20:14
537 查看
首先要说一下这篇文章要说的主要的功能是一个指向UIBarButtonItem的UIPopoverController,UIPopoverController 的View 中有一个UILabel 和一个带图片的UIButton,点击按钮UIPopoverController 消失,如果不点击按钮那么UIPopoverController就会在15秒之内淡化逐渐淡化消失,好了废话不多说了,直接上代码,首先要创建一个继承自UIVIewController 的类,我的类名字叫:OfflinePromptController,小面试h
文件,没什么好解释的。。
下面是OfflinePromptController 的m文件,
再下面就是初始化UIBarButtonItem,以及调用我们写好的UIViewController
因为都不是什么难懂的知识点上面都做了注释,废话我就不多说了哈,,下面就是我们运行的效果图:
![](http://img.blog.csdn.net/20151019201010239?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20151019201029673?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20151019201056547?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
由上面的图片我们可以看到我们自定义 的View正在逐渐淡化,最终会完全消失,至于有点的小点是一个图片按钮,点击之后响应clearMessage,来关闭我们的UIPopoverController,这样我们的渐变效果就实现了,有不懂得或者改善欢迎留言噢。。
文件,没什么好解释的。。
#import <UIKit/UIKit.h> #define kFontColor_RGB0 [UIColor blackColor] @interface OfflinePromptController : UIViewController @property (nonatomic,strong)UILabel *message; @property (nonatomic,strong)UIButton *closeMessage; @property (nonatomic,strong)UIPopoverController *popover; -(void)setData:(NSString *)str; @end
下面是OfflinePromptController 的m文件,
// OfflinePromptController.m // Meetings // // Created by avepoint on 15/10/19. // Copyright © 2015年 GearPart. All rights reserved. // #import "OfflinePromptController.h" @implementation OfflinePromptController - (instancetype)init{ self =[super init]; if(self){ self.closeMessage = [[UIButton alloc]init]; self.closeMessage = [UIButton buttonWithType:UIButtonTypeCustom]; self.closeMessage.translatesAutoresizingMaskIntoConstraints = NO; self.closeMessage.titleLabel.font = TheHelveticaNeueFont(17); [self.closeMessage setTitleColor:kFontColor_RGB0 forState:UIControlStateNormal]; [self.view addSubview:self.closeMessage]; UILabel *message = [[UILabel alloc]init]; message.translatesAutoresizingMaskIntoConstraints = NO; message.font = TheHelveticaNeueFont(17); message.textColor = kFontColor_RGB0; [self.view addSubview:message]; //计算实际frame大小,并将label的frame变成实际大小 NSString *str = @"Some features dosen't work cause by the network connection.Please check your network settings."; CGSize sizeReal = [self sizeWithText:str font:[UIFont systemFontOfSize:17] maxSize:CGSizeMake(300, 300)]; [message setFrame:CGRectMake(10,10, sizeReal.width, sizeReal.height)]; [message setText:str]; message.numberOfLines = 0; message.lineBreakMode=NSLineBreakByWordWrapping; [self.closeMessage setImage:[UIImage imageNamed:@"wait_finish"] forState:UIControlStateNormal]; NSDictionary *views = NSDictionaryOfVariableBindings(_closeMessage,message); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[message(==400)]-10-[_closeMessage]-10-|" options:nil metrics:nil views:views]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[message]|" options:nil metrics:nil views:views]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_closeMessage]|" options:nil metrics:nil views:views]]; } return self; } /** * 计算文字尺寸 * * @param text 需要计算尺寸的文字 * @param font 文字的字体 * @param maxSize 文字的最大尺寸 */ - (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; } -(void)setData:(NSString *)str{ [self.message setText:str]; } @end上面涉及到的知识点重要点得无非就是自动布局,以及根据字体以及字符串计算控件的大小,根据所限定的宽度高度计算出行数,上面都做了注释,不明白的地方可以留言。。
再下面就是初始化UIBarButtonItem,以及调用我们写好的UIViewController
UIBarButtonItem *OfflineImage = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"upload_failed_white"] style:UIBarButtonItemStylePlain target:self action:@selector(showOfflineImage:)]; self.navigationItem.rightBarButtonItems = @[ newItem, otherBtn, LocationNotification, CancleLocationNotification,OfflineImage ];
/** * Offline 时候显示的图标以及提示语 */ - (void)showOfflineImage:(UIBarButtonItem *)item{ t = 0; opc = [[OfflinePromptController alloc]init]; [opc.closeMessage addTarget:self action:@selector(clearMessage) forControlEvents:UIControlEventTouchUpInside]; popover = [[UIPopoverController alloc]initWithContentViewController:opc]; popover.popoverContentSize = CGSizeMake(500, 80); [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; __block NSTimer *timer; timer = [NSTimer scheduledTimerWithTimeInterval: 0.0 target: self selector: @selector(handleTimer:) userInfo: nil repeats: YES]; }
/** * 关闭提示信息 */ - (void)clearMessage{ [popover dismissPopoverAnimated:YES]; NSLog(@"GGle"); } /** * 根据时间变化改变透明度最后关闭popover 和 timmer * * @param timmer timmer */ - (void)handleTimer:(NSTimer*)timmer{ t++; opc.view.alpha = 1.0-t/15.0; if(t==15){ [timmer invalidate]; [self clearMessage]; } NSLog(@"+++++++++++++++%d",t); }
因为都不是什么难懂的知识点上面都做了注释,废话我就不多说了哈,,下面就是我们运行的效果图:
由上面的图片我们可以看到我们自定义 的View正在逐渐淡化,最终会完全消失,至于有点的小点是一个图片按钮,点击之后响应clearMessage,来关闭我们的UIPopoverController,这样我们的渐变效果就实现了,有不懂得或者改善欢迎留言噢。。
相关文章推荐
- UI21_豆瓣数据库
- java关键字之break与continue的区别(简单明了的小例子)
- UI20_数据库
- IOS开发之----让UILabel 垂直方向顶端对齐的代码
- UI19_豆瓣收藏
- iOS UINavigationBar颜色和文字渲染
- UI19_单例
- UIViewController各个方法的加载顺序
- leetcode Unique Paths
- Android Studio 运行时出现 finished with non-zero exit value 2 错误分析
- 解决 Agreeing to the Xcode/iOS license requires admin privileges
- 检查内核反馈uevent消息,并提取出USB插入事件
- Boolean.parseBoolean("true") 和 Boolean.getBoolean("true");的区别及用法
- 手势和UI控件之间冲突的解决方法(比如UITapGesture和UIButton冲突,UIPanGesture和UISlider冲突)
- mvc5 @RenderSection("scripts", required: false) 什么意思
- UILabel顶端对齐
- blueZ应用编程
- 一步一步来做Android UI自动化,UiAutomator实例
- iOS UITabBar的隐藏和显示
- UI18_数据持久化