(ios實戰)實作類別似於android 的toast控制項

來源:互聯網
上載者:User

1實現原理

 建立一個自訂控制項,控制項中顯示文本,同時設定一個動畫,三秒鐘後,控制項的alpha為0,動畫完成後,控制項移出掉ViewControl

 

2 建立PopView

2.1 PopView.h 部分

@interface PopView : UIView{    UILabel         *_textLabel;    int             _queueCount;}- (void) setText:(NSString *) text;@end

2.2 PopView.m

#import "PopView.h"#import <QuartzCore/QuartzCore.h>@implementation PopView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0.75f];        self.layer.cornerRadius = 5.0f;        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 10)];        _textLabel.numberOfLines = 0;        _textLabel.font = [UIFont systemFontOfSize:17];        _textLabel.textColor = [UIColor whiteColor];        _textLabel.textAlignment = NSTextAlignmentCenter;        _textLabel.backgroundColor = [UIColor clearColor];        _textLabel.textAlignment = NSTextAlignmentCenter;        [self addSubview:_textLabel];        _queueCount = 0;    }    return self;}- (void) setText:(NSString *) text{    _textLabel.frame = CGRectMake(0, 0, 100, 10);    _queueCount ++;    self.alpha = 1.0f;    _textLabel.text = text;    [_textLabel sizeToFit];    CGRect frame = CGRectMake(5, 0, _textLabel.frame.size.width, _textLabel.frame.size.height);    _textLabel.frame = frame;   frame =  CGRectMake(self.frame.origin.x, self.frame.origin.y, _textLabel.frame.size.width+10, _textLabel.frame.size.height+10);    self.frame = frame;    [UIView animateWithDuration:3.0                          delay:0.0                        options:UIViewAnimationOptionCurveEaseIn                     animations:^{                         self.alpha = 0;                     }                     completion:^(BOOL finished){                         if (_queueCount == 1) {                             [self removeFromSuperview];                         }                        _queueCount--;                     }     ];    }@end

3 調用方式:

[self.view addSubview:_popView];    [_popView setText:@"合成恢複"];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.