簡體   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