(real IOS) similar to the Android toast control

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The 1 Principle

 Create a custom control, the text displayed in the control, and set up an animation, after three seconds, the control of alpha is 0, the animation is finished, control from the ViewControl.


2 create PopView

The 2.1 part 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 mode:

[self.view addSubview:_popView];
    [_popView setText:@"Synthesis of recovery"];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Stephen at December 01, 2013 - 5:48 AM