IOS Tips Toast

Source: Internet
Author: User
Tags uikit

IOS often encounter such situations, such as network connection errors, need to pop up a text prompt, and then automatically close

such as login success prompt login complete, later close



The implementation is as follows, we create a singleton, subsequent use only need to copy the code on the line

Create a class naming Myalertcenter control cue information reality and shutdown

At the same time, create inner class Myalert inherit uiview realistic hint content


The specific code is as follows

. h file


#import <UIKit/UIKit.h> @interface myalert:uiview-(id) init;-(void) Setmessagetext: (NSString *) message;@ End@interface myalertcenter:nsobject{        myalert *myalertview;//alertview    BOOL active;//is currently in use        }+ ( Myalertcenter *) defaultcenter;//single-generation alert controller-(void) Postalertwithmessage: (nsstring*) message;//popup text @end


. m file

#import "MyAlertCenter.h" @implementation myalertcenter+ (Myalertcenter *) defaultcenter{static Myalertcenter *default    Center;    if (!defaultcenter) {defaultcenter=[[myalertcenter alloc]init]; } return defaultcenter;} -(ID) init{if (! (        Self=[super Init]) return nil;    Myalertview = [[Myalert alloc] init];    Myalertview.alpha = 0.0f;        active = NO;        [[UIApplication Sharedapplication].keywindow Addsubview:myalertview]; return self;}    -(void) Postalertwithmessage: (nsstring*) message{//Determine if the current is in use if (!active) {[Self showalerts:message];    }}-(void) Showalerts: (NSString *) str {//start using, set current to use status active = YES;    Myalertview.alpha = 0;    [[UIApplication Sharedapplication].keywindow Addsubview:myalertview];    [Myalertview SETMESSAGETEXT:STR];        Myalertview.center = [UIApplication sharedapplication].keywindow.center;    Set animation [UIView Beginanimations:nil Context:nil];    [UIView setanimationduration:0.2]; [UIView SetAnimationdelegate:self];    [UIView setanimationdidstopselector: @selector (ANIMATIONSTEP2)];    Myalertview.alpha = 0.8; [UIView commitanimations];}    -(void) animationstep2{[UIView Beginanimations:nil Context:nil];    [UIView setanimationdelay:1.0];    [UIView setanimationdelegate:self];    [UIView setanimationdidstopselector: @selector (ANIMATIONSTEP3)];    Myalertview.alpha = 0; [UIView commitanimations];}    -(void) animationstep3{[Myalertview Removefromsuperview];    Active=no; } @end @implementation Myalertcgrect messagerect;    NSString *text;-(ID) init{self=[super initwithframe:cgrectmake (0, 0, 100, 10)];    if (self) {messagerect =cgrectinset (self.bounds, 10, 10); } return self;}    -(void) Setmessagetext: (NSString *) message{text=message;        Nsdictionary *attribute = @{nsfontattributename: [Uifont systemfontofsize:14]}; Cgsize S=[text boundingrectwithsize:cgsizemake (0) options:nsstringdrawingtruncateslastvisibleline | NsstringdrawinguSeslinefragmentorigin |        Nsstringdrawingusesfontleading Attributes:attribute context:nil].size;        Self.bounds = CGRectMake (0, 0, s.width+40, s.height+15+15);    Messagerect.size = s;    MessageRect.size.height + = 5;    messagerect.origin.x = 20;        MESSAGERECT.ORIGIN.Y = 15;    [Self setneedslayout]; [Self setneedsdisplay];} -(void) DrawRect: (cgrect) rect{nsdictionary* attrs [Email protected]{nsforegroundcolorattributename:[uicolor                           Whitecolor], Nsfontattributename:[uifont fontwithname:@ "Americantypewriter" size:18]    }; [Text Drawinrect:messagerect withattributes:attrs]; Limits the text to a rectangular boundary, preventing the rectangle from stretching;} @end


Well, the Myalertcenter class implementation is complete.


If you need to use it, we can call it directly.

    [[Myalertcenter Defaultcenter] postalertwithmessage:@ "landing success"];

It's easier.


If we need to adjust the actual time of the cue content

can find

-(void) animationstep2{    [UIView beginanimations:nil context:nil];    [UIView setanimationdelay:1.0]; Display time    [UIView setanimationdelegate:self];    [UIView setanimationdidstopselector: @selector (ANIMATIONSTEP3)];    Myalertview.alpha = 0;    [UIView commitanimations];}

Second line of code adjustment


If you want to adjust where the hint appears (the default reality in the example is the middle of the screen)

can find


    Myalertview.center = [UIApplication sharedapplication].keywindow.center; Set Location

Re-assign Myalertview.center to the value



Apple Development Group: 414319235 Welcome to join the Welcome discussion question




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Tips Toast

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.