IOS中的Android吐司代码

本文介绍了一个使用Objective-C在iOS应用中实现自定义对话框的方法。通过创建一个UIView子类SystemDialog,可以轻松地展示带有自定义文本的对话框,并实现淡入淡出效果。该实现考虑了屏幕适配问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#import <UIKit/UIKit.h>
#import "CustomDialog.h"
@interface SystemDialog :UIView{
    
}
- (id)initWithText:(NSString *)text;
- (void) show;
@end


#import "SystemDialog.h"
#import "AppDelegate.h"
@implementation SystemDialog

- (id)initWithText:(NSString *)text
{
    self = [super init];
    if (self) {
       
        self.backgroundColor=[UIColor blackColor];
        self.alpha=0.0;
        self.layer.cornerRadius = 6;
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 0;
        self.layer.borderColor =[[UIColor clearColor]CGColor];
        
        CGSize size=[text sizeWithFont:[UIFont boldSystemFontOfSize:16.0] constrainedToSize:CGSizeMake(200, 100) lineBreakMode:UILineBreakModeWordWrap];
        UILabel * titleLable=[[UILabel alloc] initWithFrame:CGRectMake(8,8,size.width,size.height)];
        titleLable.lineBreakMode = UILineBreakModeCharacterWrap;
        titleLable.numberOfLines = 0;
        titleLable.textColor=[UIColor whiteColor];
        titleLable.textAlignment=UITextAlignmentLeft;
        titleLable.backgroundColor=[UIColor clearColor];
        titleLable.font=[UIFont boldSystemFontOfSize:16.0];
        titleLable.text=text;
        [self addSubview:titleLable];

//        self.frame=CGRectMake((320-kControlWidth(titleLable)-16)/2,460-216-60,kControlWidth(titleLable)+16, kControlRbY(titleLable)+8);
        self.frame = CGRectMake((ScreenWidth - titleLable.width -16)/2, (ScreenHeight-64-216)+5, (titleLable.width + 16), (titleLable.height + 16));
        [titleLable release];
        
        [((AppDelegate*)[UIApplication sharedApplication].delegate).window addSubview:self];
    }
    return self;
}


- (void) show {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    self.alpha=0.8;
    [self performSelector:@selector(hidden:) withObject:nil afterDelay:2.0];
    [UIView commitAnimations];
    
    
}

- (void) hidden:(id)sender{
    if ([self superview]) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0f];
        self.alpha=0.0;
        [UIView commitAnimations];
    }
    
}



- (void)dealloc
{
    [super dealloc];
}

@end




转载于:https://my.oschina.net/u/936286/blog/129151

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值