首先要说一下这篇文章要说的主要的功能是一个指向UIBarButtonItem的UIPopoverController,UIPopoverController 的View 中有一个UILabel 和一个带图片的UIButton,点击按钮UIPopoverController 消失,如果不点击按钮那么UIPopoverController就会在15秒之内淡化逐渐淡化消失,好了废话不多说了,直接上代码,首先要创建一个继承自UIVIewController 的类,我的类名字叫:OfflinePromptController,小面试h 文件,没什么好解释的。。
因为都不是什么难懂的知识点上面都做了注释,废话我就不多说了哈,,下面就是我们运行的效果图:
#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,这样我们的渐变效果就实现了,有不懂得或者改善欢迎留言噢。。