iOS自定义带有动画效果AlertView

最近比较闲,提前为下个项目中需要用得到功能造个轮子.alertView在项目中应该经常用的到,然而用自带的系统控件多少缺乏点儿激情.于是打算给alertView加上一点点儿动画,(这里的动画用到的是faceBook的POP)提高用户体验.话不多说.看效果图先:

第一个是弹出提示信息后自动消失的alertView


messageAlert.png

第二个是弹出带有button的alertView


NormalAlert.png

接下来大体说一下思路:

首先创建这两个不同形式alert的基


在.h中:
AbstractBasicAlertView基类里面定义了所有需要用到的控件的属性title subTitle message buttonsTitle contentView autoHiden delayAutoHidenDuration 以及方法:-(void)show; -(void)hide -(void)setView:(UIView *)view withKey:(NSString *)key;-(UIView *)viewWithKey:(NSString *)key;
以及协议方法:协议方法会在viewController中使用到的时候说明他们的意思

- (void)alertView:(AbstractBasicAlertView *)alertView data:(id)data atIndex:(NSInteger)index;

- (void)alertViewWillHide:(AbstractBasicAlertView *)alertView;

- (void)alertViewDidHide:(AbstractBasicAlertView *)alertView;```
在.m中:
声明一个字典:```NSMapTable```这是一个弱引用的字典,也可以替换成NSMutableDictionary.
  • (instancetype)init {

    if (self = [super init]) {

      self.delayAutoHidenDuration = 2.f;
      self.autoHiden              = NO;
      self.mapTable               = [NSMapTable strongToWeakObjectsMapTable];
    

    }

    return self;
    }
    在初始化方法中,设置自动隐藏延迟2秒,自动隐藏为NO,初始化mapTable


这两个方法是将view根据key放入字典中存起来,在viewController中会有用到

  • (void)setView:(UIView *)view withKey:(NSString *)key {

    [self.mapTable setObject:view forKey:key];
    }

  • (UIView *)viewWithKey:(NSString *)key {

    return [self.mapTable objectForKey:key];
    }


#####MessageAlertView
基于```AbstractBasicAlertView```创建提示信息的alertView.
复写父类方法:
  • (void)setContentView:(UIView *)contentView {

    self.frame = contentView.bounds;
    [super setContentView:contentView];
    }

  • (void)show {

    if (self.contentView) {
    [self.contentView addSubview:self];
    //当show的时候创建背景view和MessageView
    [self createBlackView];
    [self createMessageView];

    //如果设置了自动隐藏,执行定时器的方法
      if (self.autoHiden) {
          [self performSelector:@selector(hide) withObject:nil afterDelay:self.delayAutoHidenDuration];
      }
    

    }
    }

  • (void)hide {

    if (self.contentView) {
    [self removeViews];
    }
    }


  • (void)createMessageView {

      //创建信息label
    

    NSString *text = self.message;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 170, 0)];
    textLabel.text = text;
    textLabel.font = [UIFont systemFontOfSize:15.f];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.numberOfLines = 0;
    [textLabel sizeToFit];

      //创建信息窗体view
    

    self.messageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 190, textLabel.height + 20)];
    self.messageView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.75f];
    self.messageView.center = self.contentView.middlePoint;
    textLabel.center = self.messageView.middlePoint;
    self.messageView.alpha = 0.f;
    [self.messageView addSubview:textLabel];
    [self addSubview:self.messageView];

      //执行动画
    

    POPBasicAnimation *alpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    alpha.toValue = @(1.f);
    alpha.duration = 0.3f;
    [self.messageView pop_addAnimation:alpha forKey:nil];

    POPSpringAnimation *scale = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scale.fromValue = [NSValue valueWithCGSize:CGSizeMake(1.75f, 1.75f)];
    scale.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    scale.dynamicsTension = 1000;
    scale.dynamicsMass = 1.3;
    scale.dynamicsFriction = 10.3;
    scale.springSpeed = 20;
    scale.springBounciness = 15.64;
    [self.messageView.layer pop_addAnimation:scale forKey:nil];
    }


  • (void)removeViews {

    [UIView animateWithDuration:0.2f animations:^{

      self.blackView.alpha = 0.f;
      self.messageView.alpha = 0.f;
      self.messageView.transform = CGAffineTransformMakeScale(0.75f, 0.75f);
    

    } completion:^(BOOL finished) {

      [self removeFromSuperview];
    

    }];
    }


#####ButtonsAlertView
其实大部分是模仿alertView构建相似的一个视图

初始化:

  • (instancetype)init {

    if (self) {
    self = [super init];

      self.firstButton = [[UIButton alloc] initWithFrame:CGRectZero];
      self.secondButton = [[UIButton alloc] initWithFrame:CGRectZero];
      
      self.firstButton.userInteractionEnabled = NO;
      self.firstButton.tag = 0;
      self.firstButton.titleLabel.font = [UIFont systemFontOfSize:16];
      [self.firstButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      [self.firstButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
      [self.firstButton addTarget:self action:@selector(messageButtonsEvent:) forControlEvents:UIControlEventTouchUpInside];
      
      self.secondButton.userInteractionEnabled = NO;
      self.secondButton.tag = 0;
      self.secondButton.titleLabel.font = [UIFont systemFontOfSize:16];
      [self.secondButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      [self.secondButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
      [self.secondButton addTarget:self action:@selector(messageButtonsEvent:) forControlEvents:UIControlEventTouchUpInside];
      
          //Store View
      [self setView:self.firstButton withKey:@"firstButton"];
      [self setView:self.secondButton withKey:@"secondButton"];
    

    }
    return self;
    }

重写父类方法:
  • (void)setContentView:(UIView *)contentView {

    [super setContentView: contentView];
    self.frame = contentView.bounds;

}

  • (void)show {

    if (self.contentView) {

      [self.contentView addSubview:self];
      
      [self createBlackView];
      [self createMessageView];
    

    }
    }

  • (void)hide {

    if (self.contentView) {
    [self removeViews];
    }
    }

创建视图:
  • (void)createBlackView {

    self.blackView = [[UIView alloc] initWithFrame:self.contentView.bounds];
    self.blackView.backgroundColor = [UIColor blackColor];
    self.blackView.alpha = 0;
    [self addSubview:self.blackView];

    [UIView animateWithDuration:0.3f animations:^{
    self.blackView.alpha = 0.25f;
    }];
    }

  • (void)createMessageView {

      //创建信息label
    

    NSString *text = self.message;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 210, 0)];
    textLabel.text = text;
    textLabel.font = [UIFont systemFontOfSize:15];
    textLabel.textColor = [UIColor blackColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.numberOfLines = 0;
    [textLabel sizeToFit];

      //创建信息窗体view
    

    self.messageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, textLabel.height + 100)];
    self.messageView.backgroundColor = [UIColor whiteColor];
    self.messageView.layer.cornerRadius = 10.f;
    self.messageView.center = self.contentView.middlePoint;
    textLabel.center = CGPointMake(self.messageView.middleX, 0);
    textLabel.top = 30;
    self.messageView.alpha = 0.f;
    [self.messageView addSubview:textLabel];
    [self addSubview:self.messageView];

      //处理按钮
    

    NSArray *buttonsInfo = self.buttonsTitle;

      //如果有一个按钮
    

    if (buttonsInfo.count == 1) {
    [self.messageView addSubview:[[self class] lineViewWithFrame:CGRectMake(0, self.messageView.height - 40, self.messageView.width, 0.5f) color:[UIColor lightGrayColor]]];
    self.firstButton.frame = CGRectMake(0, self.messageView.height - 40, self.messageView.width, 40);
    self.firstButton.userInteractionEnabled = NO;
    self.firstButton.tag = 0;
    self.firstButton.titleLabel.font = [UIFont systemFontOfSize:16.f];
    [self.firstButton setTitle:self.buttonsTitle[0] forState:UIControlStateNormal];
    [self.firstButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.firstButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [self.firstButton addTarget:self action:@selector(messageButtonsEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.messageView addSubview:self.firstButton];
    }

      //如果有2个按钮
    

    if (buttonsInfo.count == 2) {

      [self.messageView addSubview:[[self class] lineViewWithFrame:CGRectMake(0, self.messageView.height - 40, self.messageView.width, 0.5f) color:[UIColor lightGrayColor]]];
      [self.messageView addSubview:[[self class] lineViewWithFrame:CGRectMake(self.messageView.width / 2.f, self.messageView.height - 40, 0.5f, 40.f) color:[UIColor lightGrayColor]]];
      
      self.firstButton.frame = CGRectMake(0, self.messageView.height - 40, self.messageView.width / 2.f, 40);
      [self.firstButton setTitle:self.buttonsTitle[0] forState:UIControlStateNormal];
      [self.messageView addSubview:self.firstButton];
      
      self.secondButton.frame = CGRectMake(self.messageView.width / 2.f, self.messageView.height - 40, self.messageView.width / 2.f, 40);
      [self.secondButton setTitle:self.buttonsTitle[1] forState:UIControlStateNormal];
      [self.messageView addSubview:self.secondButton];
    

    }

      //执行动画
    

    POPBasicAnimation *alpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    alpha.toValue = @(1.f);
    alpha.duration = 0.3f;
    [self.messageView pop_addAnimation:alpha forKey:nil];

    POPSpringAnimation *scale = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scale.fromValue = [NSValue valueWithCGSize:CGSizeMake(1.75f, 1.75f)];
    scale.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    scale.dynamicsTension = 1000;
    scale.dynamicsMass = 1.3;
    scale.dynamicsFriction = 10.3;
    scale.springSpeed = 20;
    scale.springBounciness = 15.64;
    scale.delegate = self;
    [self.messageView.layer pop_addAnimation:scale forKey:nil];
    }

  • (void)removeViews {

    [UIView animateWithDuration:0.2f animations:^{

      self.blackView.alpha = 0.f;
      self.messageView.alpha = 0.f;
      self.messageView.transform = CGAffineTransformMakeScale(0.75f, 0.75f);
    

    } completion:^(BOOL finished) {
    [self removeFromSuperview];
    }];
    }

按钮点击后将代理方法传递出去
  • (void)messageButtonsEvent:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(alertView:data:atIndex:)]) {
    [self.delegate alertView:self data:nil atIndex:button.tag];
    }
    }
#####ViewController中,定义两种alertView的样式
  • (void)btnClick:(UIButton *)btn {

    if (btn.tag == 100) {
    //MessageButton

      AbstractBasicAlertView *alertView = [[MessageAlertView alloc] init];
      alertView.message = @"Test content Test content Test content Test content Test content Test content Test content ";
      alertView.contentView = self.view;
      alertView.autoHiden = YES;
      alertView.delayAutoHidenDuration = 5.f;
      [alertView show];
    

    } else {
    //NormalButton
    AbstractBasicAlertView *showView = [[ButtonsAlertView alloc] init];
    showView.delegate = self;
    showView.contentView = self.view;
    showView.buttonsTitle = @[@"OK",@"Cancel"];
    showView.message = @"Test message show";
    UIButton *button = (UIButton *)[showView viewWithKey:@"secondButton"];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [showView show];
    }

}

执行代理方法,可以在这个店里方法中做一些其他的操作,不过我们alertView的大致作用就是一个提示作用了.
  • (void)alertView:(AbstractBasicAlertView *)alertView data:(id)data atIndex:(NSInteger)index {

    [alertView hide];

}


demo地址:https://github.com/irembeu/AnimationAlertView.git
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,492评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,048评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,927评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,293评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,309评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,024评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,638评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,546评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,073评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,188评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,321评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,998评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,678评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,186评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,303评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,663评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,330评论 2 358

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,154评论 30 470
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,492评论 1 14
  • 充满音符的天空中 有喜有悲 每一片净土都似乎冻结 让人有一丝忧郁 眩晕 闭上眼睛 绿洲 海洋 白帆 仿佛散土化泥 ...
    哈尔滨王海军阅读 180评论 0 0
  • 神句组: 1. It is tempting to believe that the security probl...
    Echooos阅读 370评论 0 0
  • —4— 我读初中的那三年是中国球市最火爆的三年。四川全兴队在全国刮起了一阵“黄色旋风”。马明宇、魏群、邹侑...
    欢喜随笔阅读 451评论 0 2