zoukankan      html  css  js  c++  java
  • IOS实现顶部显示信息

        做公司项目的时候有个需求,需要从顶端划出一个消息,显示一段时候后自动消失。

        实现如下:

        

        CGRect rect = [self.view bounds];
        CGFloat viewHeight = 50.0f;
        UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, -viewHeight, rect.size.width, viewHeight)];
        CGContextRef bitmapContext = CGBitmapContextCreate(NULL, rect.size.width, viewHeight, 8, 4 * rect.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);
        CGFloat colors[] =
        {
            255.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0, 1.0,
            255.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0, 1.0,
            255.0 / 255.0,  0.0 / 255.0, 0.0 / 255.0, 1.0,
        };//这里可以设置渐变色。
        CGGradientRef gradient = CGGradientCreateWithColorComponents
        (CGColorSpaceCreateDeviceRGB(), colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
        CGContextDrawLinearGradient(bitmapContext, gradient, CGPointMake(0.0f, 0.0f), CGPointMake(rect.size.width, rect.size.height), kCGGradientDrawsBeforeStartLocation);
        CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
        UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
        CGImageRelease(cgImage);
        CGContextRelease(bitmapContext);
        
        [alertView setBackgroundColor:[UIColor colorWithPatternImage:uiImage]];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 20.0f, rect.size.width, 24.0f)];
        [label setFont:[UIFont systemFontOfSize:20.0f]];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextAlignment:NSTextAlignmentCenter];
        label.textColor = [UIColor blackColor];
        label.text = @"网络出错!";
        [alertView addSubview:label];
        [self.view addSubview:alertView];
        //动画效果,从顶部出现,5秒后消失
        [UIView animateWithDuration:0.5f animations:^{
            [alertView setFrame:CGRectMake(0.0f, 0.0f, rect.size.width, viewHeight)];
        } completion:^(BOOL finished){
            if(finished){
                [UIView animateWithDuration:0.5f delay:5.0f options:UIViewAnimationOptionTransitionNone animations:^{
                    [alertView setFrame:CGRectMake(0.0f, -viewHeight, rect.size.width, viewHeight)];
                } completion:^(BOOL finished){
                    [alertView removeFromSuperview];
                }];
            }
        }];
    View Code

        

  • 相关阅读:
    大话位运算
    Docker部署jar包
    linux系统备份mysql数据库
    关于Centos7 firewalld防火墙开放端口后仍不能访问ftp和nginx的问题解决
    MySql查询当天、本周、本月、本季度、本年的数据
    c#模拟线性回归
    监控服务器配置(五)-----Redis_exporter安装配置
    监控服务器配置(二)-----Grafana安装配置
    监控服务器配置(三)-----Node_exporter安装配置
    js网页唤起支付宝进行支付
  • 原文地址:https://www.cnblogs.com/tyrant2012/p/3521064.html
Copyright © 2011-2022 走看看