(Ios practice) Implement toast controls similar to android

Source: Internet
Author: User

1. Implementation Principle

Create a custom control that displays text in the control and sets an animation. After three seconds, the alpha of the control is 0. After the animation is complete, the control is removed from the ViewControl.

 

2. Create a 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. Call method:

[Self. view addSubview: _ popView]; [_ popView setText: @ "merging and recovery"];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.