简体   繁体   English

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

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

I would like to implement a temporary alert into to a uitableviewcontroller to inform a user that their data has been saved. 我想对uitableviewcontroller实施临时警报,以通知用户其数据已保存。

I could do this with a uiAlertView - but for aesthetic reasons it would be preferable to implement something similar to the round cornered fade in/out alert view used in IOS7 for tasks such as showing volume control - as in this pic - 我可以使用uiAlertView做到这一点-但出于美学原因,最好实现类似于IOS7中用于显示音量控制等任务的圆角淡入/淡出警报视图的功能-如上图所示-

在此处输入图片说明

Is this a IOS7 class? 这是IOS7类吗? I cant find any info on it (probably because I don't know what its called!), or would I need to extend the uiAlertView to replicate this functionality? 我找不到有关它的任何信息(可能是因为我不知道它叫什么!),还是需要扩展uiAlertView来复制此功能?

No, this is not a native iOS 7 subclass but there are plenty implementations of it in the wild. 不,这不是本地的iOS 7子类,但是在野外有很多实现。 One example would be DTAlertView or CXAlertView or SDCAlertView . 一个示例是DTAlertViewCXAlertViewSDCAlertView Check them out. 去看一下。

If you just want the volume-thingy type behavior, don't search too far. 如果只想让音量变大,请不要搜索得太远。 It's easily home-rolled with a full screen sized image view that's mostly transparent, and has in it's center a rounded-corner message rectangle, translucent if you like. 它可以轻松滚动到全屏大小的图像视图,该视图几乎是透明的,并且在中心具有一个圆角的消息矩形,如果您愿意,可以将其透明。 Then build a convenience method to show/hide it with animation... 然后建立一个方便的方法来显示/隐藏动画...

- (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];
            });
        }     
    }];
}

All you need other than this is to build the image view lazily... 除此之外,您所需要做的就是懒惰地构建图像视图。

- (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;
}

Realize it's not exactly what you need, but this was pretty easy to paste together. 意识到这并不是您真正需要的,但这很容易粘贴在一起。 It's the volume thing on a screen sized transparent background... 这是屏幕大小的透明背景上的音量...

在此处输入图片说明

This is a quick implementation of a subview that will do what you want 这是子视图的快速实现,可以完成您想要的操作

#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

If you want more complex functionality, you can implement public properties and a protocols 如果您想要更复杂的功能,则可以实现公共属性和协议

If you want to check it working look at it at GitHub... https://github.com/eharo2/BellAlert 如果您想检查它的运行状况,请在GitHub ... https://github.com/eharo2/BellAlert

for aesthetic reasons it would be preferable to implement something similar to the round cornered fade in/out alert view used in IOS7 出于美学原因,最好实现类似于IOS7中使用的圆角淡入/淡出警报视图的方法

Rounding the corners of any view is incredibly easy: 圆角化任何视图都非常容易:

someView.layer.cornerRadius = 5.0;

Making a view fade in and out is also trivial using Core Animation . 使用Core Animation使视图淡入和淡出也很容易。 For example, if you want to fade in someView , set its alpha property to 0 and then animate setting it to 1: 例如,如果要在someView淡入淡出,请将其alpha属性设置为0,然后设置其动画设置为1:

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

With those tools at your disposal, you'll find it very easy to roll your own version of Apple's display. 使用这些工具,您会发现滚动自己版本的Apple显示屏非常容易。

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

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