繁体   English   中英

iOS-圆角临时警报-如何实施?

[英]IOS - round cornered temporary alert - how to implement?

我想对uitableviewcontroller实施临时警报,以通知用户其数据已保存。

我可以使用uiAlertView做到这一点-但出于美学原因,最好实现类似于IOS7中用于显示音量控制等任务的圆角淡入/淡出警报视图的功能-如上图所示-

在此处输入图片说明

这是IOS7类吗? 我找不到有关它的任何信息(可能是因为我不知道它叫什么!),还是需要扩展uiAlertView来复制此功能?

不,这不是本地的iOS 7子类,但是在野外有很多实现。 一个示例是DTAlertViewCXAlertViewSDCAlertView 去看一下。

如果只想让音量变大,请不要搜索得太远。 它可以轻松滚动到全屏大小的图像视图,该视图几乎是透明的,并且在中心具有一个圆角的消息矩形,如果您愿意,可以将其透明。 然后建立一个方便的方法来显示/隐藏动画...

- (void)setAlertHidden:(BOOL)hidden animated:(BOOL)animated {

    UIImageView *alertImageView = [self alertImageView];
    BOOL currentlyHidden = alertImageView.alpha == 0.0;
    if (currentlyHidden == hidden) return;

    NSTimeInterval alpha = (hidden)? 0.0 : 1.0;
    NSTimeInterval duration = (animated)? 0.3 : 0.0;

    [UIView animateWithDuration:duration animations:^{
        alertImageView.alpha = alpha;
    } completion:^(BOOL finished) {
        // if we showed the alert, hide it after 3 seconds
        // you can make this duration a parameter
        if (!hidden) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                [self setAlertHidden:YES animated:YES];
            });
        }     
    }];
}

除此之外,您所需要做的就是懒惰地构建图像视图。

- (UIImageView *)alertImageView {
    UIImageView *alertImageView = (UIImageView *)[self.view viewWithTag:999];
    if (!alertImageView) {
        alertImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        alertImageView.image = [UIImage imageNamed:@"fullscreen-volume-looking-thing.png"];
        alertImageView.tag = 999;
        [self.view addSubview:alertImageView];
    }
    return alertImageView;
}

意识到这并不是您真正需要的,但这很容易粘贴在一起。 这是屏幕大小的透明背景上的音量...

在此处输入图片说明

这是子视图的快速实现,可以完成您想要的操作

#import "BellAlert.h"

@implementation BellAlert

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        UIImageView *bell = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bell.png"]];
        bell.frame = CGRectMake(0, 0, 50, 50);
        bell.center = self.center;
        [self addSubview:bell];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.1].CGColor);

    CGContextMoveToPoint(context, 5, 0);
    CGContextAddLineToPoint(context, 95, 0);
    CGContextAddArcToPoint(context, 100, 0, 100, 5, 10);
    CGContextAddLineToPoint(context, 100, 95);
    CGContextAddArcToPoint(context, 100, 100, 95, 100, 10);
    CGContextAddLineToPoint(context, 5, 100);
    CGContextAddArcToPoint(context, 0, 100, 0, 95, 10);
    CGContextAddLineToPoint(context, 0, 5);
    CGContextAddArcToPoint(context, 0, 0, 5, 0, 10);

    CGContextFillPath(context);
}

@end

如果您想要更复杂的功能,则可以实现公共属性和协议

如果您想检查它的运行状况,请在GitHub ... https://github.com/eharo2/BellAlert

出于美学原因,最好实现类似于IOS7中使用的圆角淡入/淡出警报视图的方法

圆角化任何视图都非常容易:

someView.layer.cornerRadius = 5.0;

使用Core Animation使视图淡入和淡出也很容易。 例如,如果要在someView淡入淡出,请将其alpha属性设置为0,然后设置其动画设置为1:

someView.alpha = 0.0;
[UIView animateWithDuration:1.0 animations:^{
    someView.alpha = 1.0;
}];

使用这些工具,您会发现滚动自己版本的Apple显示屏非常容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM