首页 > 其他 > 详细

IOS实现顶部显示信息

时间:2014-01-16 21:29:07      阅读:371      评论:0      收藏:0      [点我收藏+]

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

    实现如下:

    

bubuko.com,布布扣
    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

    

IOS实现顶部显示信息

原文:http://www.cnblogs.com/tyrant2012/p/3521064.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!