IOS7 Style Popup-supports block callbacks

Source: Internet
Author: User

has not updated the blog for a long time, today wrote a popup box, mainly used in the SDK

Now posted out to share under:

#import <Foundation/Foundation.h> @interface sytipview:nsobject+ (Sytipview *) shareinstance;-(void) Showsuccesstipwithtext: (NSString *) tiptext;-(void) Showerrortipwithtext: (NSString *) tiptext;-(void) StartWithText :(NSString *) tiptext;-(void) Dismisssuccesswithtext: (NSString *) TipText block: (void (^) ()) action;-(void) Dismisserrorwithtext: (NSString *) TipText block: (void (^) ()) Action; @end

#import "SYTipView.h" #define Keywindow [uiapplication sharedapplication].keywindow#define getkeywindow Keywindow? Keywindow: [[uiapplication sharedapplication].windows objectatindex:0] #define Tipview_width 100#define TIPIMAGEVIEW_ WIDTH 30static Sytipview *sytipview = nil; @interface Sytipview () <UITextViewDelegate> @property (nonatomic, strong ) Uiactivityindicatorview *activityindicatorview; @property (nonatomic, strong) Uitextview *tiptextview; @property ( Nonatomic, Strong) Uiviewcontroller *tipviewcontroller, @property (nonatomic, strong) UIView *tipbgview; @property ( Nonatomic, Strong) Uiimageview *rightimageview, @property (nonatomic, strong) Uiimageview *wrongimageview; @property (    Nonatomic, assign) BOOL isdisplaying;    Limit cannot repeat point @end@implementation sytipview+ (Sytipview *) shareinstance{static dispatch_once_t Onecetoken;    Dispatch_once (&onecetoken, ^{sytipview = [[[Self class] alloc] init];    }); return Sytipview;}    -(ID) init{self = [super init]; if (sElf) {UIWindow *keywindow = Getkeywindow;            if (_tipviewcontroller = = nil) {_tipviewcontroller = [[Uiviewcontroller alloc] init];            [_tipviewcontroller.view setframe:cgrectmake (0, 0, Tipview_width, tipview_width)]; _tipviewcontroller.view.center = Cgpointmake (KeyWindow.rootViewController.view.bounds.size.width * 0.5f,            KeyWindow.rootViewController.view.bounds.size.height * 0.5f); _tipviewcontroller.view.autoresizingmask = Uiviewautoresizingflexibleleftmargin | Uiviewautoresizingflexiblerightmargin | Uiviewautoresizingflexibletopmargin |        Uiviewautoresizingflexiblebottommargin;  } if (_activityindicatorview = = nil) {_activityindicatorview = [[Uiactivityindicatorview alloc]            Initwithframe:cgrectmake (0, 0, Tipview_width, tipview_width)];            _activityindicatorview.activityindicatorviewstyle = Uiactivityindicatorviewstylegray; _activityindicatorview.backgroundcolor = [Uicolor grAycolor];            _activityindicatorview.alpha = 0.5;        _activityindicatorview.layer.cornerradius = 10; } if (_tipbgview = = nil) {_tipbgview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, tipview_w            Idth, Tipview_width)];            _tipbgview.layer.cornerradius = 10;            _tipbgview.backgroundcolor = [Uicolor Graycolor];        _tipbgview.alpha = 0.5; } if (_rightimageview = = nil) {NSString *bundlepath = [[NSBundle mainbundle] pathforresource:@ "            Sytipview "oftype:@" bundle "];            NSBundle *bundle = [NSBundle Bundlewithpath:bundlepath];            NSString *imagepath = [Bundle pathforresource:@ "Images/sy_arrow" oftype:@ "PNG"]; _rightimageview = [[Uiimageview alloc] initwithimage:[uiimage imagewithcontentsoffile:imagepath]];//NSString * Const MJREFRESHBUNDLENAME = @ "Sytipview.bundle";//NSString *path = [Mjrefreshbundlename stringbyappendingpathc omponent:@ "ImagEs/sy_arrow.png "];//UIImage *a = [UIImage imagenamed:path];//_rightimageview = [[Uiimageview alloc            ] INITWITHIMAGE:A];            [_rightimageview setframe:cgrectmake (0, 0, Tipimageview_width, tipimageview_width)]; _rightimageview.center = Cgpointmake (_TIPVIEWCONTROLLER.VIEW.BOUNDS.SIZE.WIDTH/2, _        TIPVIEWCONTROLLER.VIEW.BOUNDS.SIZE.HEIGHT-64); } if (_wrongimageview== nil) {nsstring *bundlepath = [[NSBundle mainbundle] pathforresource:@ "S            Ytipview "oftype:@" bundle "];            NSBundle *bundle = [NSBundle Bundlewithpath:bundlepath];            NSString *imagepath = [Bundle pathforresource:@ "Images/sy_wrong" oftype:@ "PNG"];            _wrongimageview = [[Uiimageview alloc] initwithimage:[uiimage Imagewithcontentsoffile:imagepath]];            [_wrongimageview setframe:cgrectmake (0, 0, Tipimageview_width, tipimageview_width)]; _wrongimageview.center = Cgpointmake (_tipviewcontroller.view.bounds.size.width/2, _tipviewcontroller.view.bounds.size.height-64); }} return self;}  #pragma mark-construct tiptextview-(void) Createtiptextview: (NSString *) tiptext{//text hint _tiptextview = [[Uitextview alloc]        Initwithframe:cgrectmake (0, 0, Tipview_width, tipview_width)];    Make text vertical centered _tiptextview.delegate = self;    [_tiptextview addobserver:self forkeypath:@ "contentsize" options: (nskeyvalueobservingoptionnew) Context:nil];    _tiptextview.textalignment = Nstextalignmentcenter;    _tiptextview.font = [Uifont boldsystemfontofsize:14];    _tiptextview.backgroundcolor = [Uicolor Clearcolor];    _tiptextview.textcolor = [Uicolor Whitecolor];    _tiptextview.userinteractionenabled = NO; _tiptextview.text = TipText;} #pragma mark-////-(void) Showtipwithtext: (NSString *) tiptext//{//UIWindow *keywindow = getkeywindow;////_ti PViewController.view.center = Cgpointmake (KeyWindow.rootViewController.view.bounds.size.width * 0.5f, KeyWindow.rootViewController.view.bounds.size.heiGht * 0.5f);//[_tipviewcontroller.view addsubview:_activityindicatorview];////[self createtiptextview:tiptext] ;//[_tipviewcontroller.view addsubview:_tiptextview];////[KeyWindow.rootViewController.view Addsubview:_tipvie wcontroller.view];////[_activityindicatorview startanimating];////[Self Performselector: @selector (dismissTipVi EW) Withobject:nil afterdelay:1];//}//#pragma mark-Displays the correct information prompt////-(void) dismisstipview//{//[_activityindicatorview s topanimating];//[_activityindicatorview removefromsuperview];//[_tiptextview removefromsuperview];//[_tipViewC Ontroller.view removefromsuperview];//} #pragma mark-Displays the correct information hint-(void) Showsuccesstipwithtext: (NSString *) tiptext{if (    _isdisplaying) {return;    } _isdisplaying = YES;        UIWindow *keywindow = Getkeywindow; _tipviewcontroller.view.center = Cgpointmake (KeyWindow.rootViewController.view.bounds.size.width * 0.5f, KeyWindow.rootViewController.view.bounds.size.height * 0.5f);    [_tipbgview Addsubview:_rightimageview];    [Self createtiptextview:tiptext];    [_tipbgview Addsubview:_tiptextview];        [_tipviewcontroller.view Addsubview:_tipbgview];        [KeyWindow.rootViewController.view Addsubview:_tipviewcontroller.view]; [Self performselector: @selector (Removesuccesstipview) Withobject:nil afterdelay:1];}    -(void) removesuccesstipview{[_rightimageview Removefromsuperview];    [_tiptextview Removefromsuperview];    _tiptextview = nil;    [_tipbgview Removefromsuperview];        [_tipviewcontroller.view Removefromsuperview]; _isdisplaying = NO;}    #pragma mark-Show error message hint-(void) Showerrortipwithtext: (NSString *) tiptext{if (_isdisplaying) {return;    } _isdisplaying = YES;        UIWindow *keywindow = Getkeywindow; _tipviewcontroller.view.center = Cgpointmake (KeyWindow.rootViewController.view.bounds.size.width * 0.5f,        KeyWindow.rootViewController.view.bounds.size.height * 0.5f); [_tipbgview Addsubview:_wrongimageview];    [Self createtiptextview:tiptext];    [_tipbgview Addsubview:_tiptextview];        [_tipviewcontroller.view Addsubview:_tipbgview];        [KeyWindow.rootViewController.view Addsubview:_tipviewcontroller.view]; [Self performselector: @selector (Removeerrortipview) Withobject:nil afterdelay:1];}    -(void) removeerrortipview{[_wrongimageview Removefromsuperview];    [_tiptextview Removefromsuperview];    _tiptextview = nil;    [_tipbgview Removefromsuperview];        [_tipviewcontroller.view Removefromsuperview]; _isdisplaying = NO;} #pragma mark-Start prompt-(void) Startwithtext: (NSString *) TipText;    {if (_isdisplaying) {return;    } _isdisplaying = YES;        UIWindow *keywindow = Getkeywindow; _tipviewcontroller.view.center = Cgpointmake (KeyWindow.rootViewController.view.bounds.size.width * 0.5f,    KeyWindow.rootViewController.view.bounds.size.height * 0.5f);    [_tipviewcontroller.view Addsubview:_activityindicatorview];    [Self createtiptextview:tiptext]; [_TIPVIewcontroller.view Addsubview:_tiptextview];    [KeyWindow.rootViewController.view Addsubview:_tipviewcontroller.view]; [_activityindicatorview startanimating];} #pragma mark-Close the successful prompt with callback-(void) Dismisssuccesswithtext: (NSString *) TipText block: (void (^) ()) action{[_    Activityindicatorview stopanimating];        [_activityindicatorview Removefromsuperview];        [_tipbgview Addsubview:_rightimageview];    [_tiptextview Removefromsuperview];    _tiptextview = nil;    [Self createtiptextview:tiptext];        [_tipbgview Addsubview:_tiptextview];    [_tipviewcontroller.view Addsubview:_tipbgview]; [Self performselector: @selector (removesuccesstipviewaction:) withobject:action afterdelay:1];}    -(void) Removesuccesstipviewaction: (void (^) ()) action{[_tiptextview Removefromsuperview];    _tiptextview = nil;    [_rightimageview Removefromsuperview];    [_tipbgview Removefromsuperview];    [_tipviewcontroller.view Removefromsuperview];        Action (); _isdisplaying = NO;} #pragma mArk-closed failure prompt with callback-(void) Dismisserrorwithtext: (NSString *) TipText block: (void (^) ()) action{[_activityindicatorview    Stopanimating];        [_activityindicatorview Removefromsuperview];        [_tipbgview Addsubview:_wrongimageview];    [_tiptextview Removefromsuperview];    _tiptextview = nil;    [Self createtiptextview:tiptext];        [_tipbgview Addsubview:_tiptextview];        [_tipviewcontroller.view Addsubview:_tipbgview]; [Self performselector: @selector (removeerrortipviewaction:) withobject:action afterdelay:1];}    -(void) Removeerrortipviewaction: (void (^) ()) action{[_tiptextview Removefromsuperview];    _tiptextview = nil;    [_wrongimageview Removefromsuperview];    [_tipbgview Removefromsuperview];    [_tipviewcontroller.view Removefromsuperview];        Action (); _isdisplaying = NO;} -(void) Dismisstipviewaction: (void (^) ()) action//{//[_activityindicatorview stopanimating];//[_ Activityindicatorview removefromsuperview];//[_tiptextview Removefromsuperview];[_tipviewcontroller.view removefromsuperview];//Action ();//}//-(void) Dismisswithtext: (NSString *) TipText block: (void (^) ()) action//{//_tiptextview.text = tiptext;////[Self Performselector: @selector (dismisstipviewaction:) w Ithobject:action afterdelay:1];//} #pragma mark-text vertically centered or bottom center aligned kvc-(void) Observevalueforkeypath: (NSString *)    KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{uitextview *temptextview = object; Vertical Center alignment//CGFloat Topcorrect = (Temptextview.bounds.size.height-temptextview.contentsize.height * tempTe    Xtview.zoomscale)/2; Topcorrect = Topcorrect < 0.0?    0.0:topcorrect;        Temptextview.contentoffset = (cgpoint) {. x = 0,. y =-topcorrect}; Bottom vertical alignment CGFloat topcorrect = ([Temptextview bounds].size.height-[Temptextview Contentsize].heigh    T);    Topcorrect = (Topcorrect < 0.0 0.0:topcorrect); Temptextview.contentoffset = (cgpoint) {. x = 0,.y =-topcorrect}; [Temptextview removeobserver:self forkeypath:@ "Contentsize" Context:nil];} @end

IOS7 Style Popup-supports block callbacks

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.