Как показать тост сообщение в приложении Mac, используя цель c? - PullRequest
1 голос
/ 30 апреля 2019

Я хочу показать тостовое сообщение в приложении Mac, которое я пробовал ниже блока кода, который не работает для меня, поэтому есть ли другой способ, чтобы я мог представить тостовое сообщение, как в Android.

@interface ToastView : NSView

@property (strong, nonatomic) NSString *text;

+ (void)showToastInParentView: (NSView *)parentView withText:(NSString *)text withDuaration:(float)duration;

@end

иначе с этим кодом я делаю ошибку, если да, помогите мне разобраться в проблеме. Если будет полезен другой вариант представления тостового сообщения без сторонних библиотек.

#import "ToastView.h"
float const ToastHeight = 50.0f;
float const ToastGap = 10.0f;
@implementation ToastView
{
    NSTextField *_textLabel;

}

-(NSTextField *)textLabel
{
    if (!_textLabel) {
        [_textLabel setWantsLayer:YES];
        _textLabel = [[NSTextField alloc] initWithFrame:CGRectMake(5.0, 5.0, self.frame.size.width - 10.0, self.frame.size.height - 10.0)];
        _textLabel.backgroundColor = [NSColor clearColor];
        _textLabel.alignment = NSTextAlignmentCenter;
        _textLabel.textColor = [NSColor whiteColor];
        //        _textLabel.numberOfLines = 2;
        _textLabel.font = [NSFont systemFontOfSize:13.0];
        _textLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [self addSubview:_textLabel];

    }
    return _textLabel;
}

- (void)setText:(NSString *)text
{
    _text = text;
    self.textLabel.stringValue = text;
}

+ (void)showToastInParentView: (NSView *)parentView withText:(NSString *)text withDuaration:(float)duration;
{

    //Count toast views are already showing on parent. Made to show several toasts one above another
    int toastsAlreadyInParent = 0;
    for (NSView *subView in [parentView subviews]) {
        if ([subView isKindOfClass:[ToastView class]])
        {
            toastsAlreadyInParent++;
        }
    }

    CGRect parentFrame = parentView.frame;

    float yOrigin = parentFrame.size.height - (70.0 + ToastHeight * toastsAlreadyInParent + ToastGap * toastsAlreadyInParent);

    CGRect selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, ToastHeight);
    ToastView *toast = [[ToastView alloc] initWithFrame:selfFrame];
    [toast setWantsLayer:YES];
    [toast.textLabel setWantsLayer:YES];
    toast.layer.backgroundColor = [NSColor darkGrayColor].CGColor;
    //    toast.layer.opacity = 0.0f;
    toast.alphaValue = 0.0f;
    toast.layer.cornerRadius = 4.0;
    toast.text = text; 
    [parentView addSubview:toast];

    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
        context.duration =3;
        toast.alphaValue = 0.9f;
        toast.animator.textLabel.alphaValue = 0.9f;
        toast.textLabel.alphaValue = 0.9f;
        toast.animator.alphaValue = 0.9f;


    }completionHandler:^{
        toast.hidden = YES;
        toast.alphaValue = 1;

    }];


    [toast performSelector:@selector(hideSelf) withObject:nil afterDelay:duration];

}



- (void)hideSelf
{ 
    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
        context.duration =3;
        self.alphaValue = 0.0f;
        self.textLabel.alphaValue = 0.0f;

    }completionHandler:^{
        [self removeFromSuperview];
    }];

}

@end

Любые предложения будут оценены по достоинству ..

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...