1. 程式人生 > >自定義的彈出浮動框-自動消失

自定義的彈出浮動框-自動消失

//
//  FadePromptView.m
//
//  Created by wuyj on 5/27/13.
//  Copyright (c) 2013 wuyj. All rights reserved.
//

#import "FadePromptView.h"
#import "NSString+Utility.h"


#define screenHeight [UIScreen mainScreen].bounds.size.height
#define screenWidth [UIScreen mainScreen].bounds.size.width


@interface FadePromptView
()
@property(nonatomic,strong,readonly)NSTimer *fadeOutTimer; @property(nonatomic,strong)UILabel *promptLabel; @property(nonatomic,copy)finishPrompt finishBlock; @end @implementation FadePromptView - (void)setFadeOutTimer:(NSTimer *)newTimer { if(_fadeOutTimer){ //因為不是一個重複性的NSTimer所以不需要invalidate
//[fadeOutTimer invalidate]; _fadeOutTimer =nil; } if(newTimer) _fadeOutTimer = newTimer; } - (instancetype)initWithFrame:(CGRect)frame { self = [superinitWithFrame:frame]; if (self) { // Initialization code [selfsetBackgroundColor:[UIColorcolorWithWhite:0.0
alpha:0.7]]; [selfsetClipsToBounds:YES]; UILabel* prompt = [[UILabelalloc]initWithFrame:CGRectZero]; [prompt setBackgroundColor:[UIColorclearColor]]; [prompt setTextColor:[UIColorwhiteColor]]; [prompt setFont:[UIFontsystemFontOfSize:16]]; [prompt setNumberOfLines:0]; [prompt setLineBreakMode:NSLineBreakByWordWrapping]; [selfaddSubview:prompt]; self.promptLabel = prompt; } returnself; } +(void)showPromptStatus:(NSString*)status duration:(NSTimeInterval)seconds finishBlock:(finishPrompt)finish { FadePromptView *promptView = [[FadePromptViewalloc]initWithFrame:CGRectZero]; [[[UIApplicationsharedApplication]keyWindow]addSubview:promptView]; promptView.finishBlock = [finishcopy]; [promptView show:statusduration:secondspositionY:screenHeight -100]; } +(void)showPromptStatus:(NSString*)status duration:(NSTimeInterval)seconds positionY:(CGFloat)y finishBlock:(finishPrompt)finish { FadePromptView *promptView = [[FadePromptViewalloc]initWithFrame:CGRectZero]; [[[UIApplicationsharedApplication]keyWindow]addSubview:promptView]; promptView.finishBlock = [finishcopy]; [promptView show:statusduration:secondspositionY:y]; } - (void)show:(NSString*)status duration:(NSTimeInterval)seconds positionY:(CGFloat)y { __blockCGFloat yy = y; dispatch_async(dispatch_get_main_queue(), ^{ CGSize size = [statussizeWithFontCompatible:self.promptLabel.fontconstrainedToSize:CGSizeMake(kFadePromptViewMaxWidth -30, CGFLOAT_MAX)lineBreakMode:self.promptLabel.lineBreakMode]; CGFloat w = size.width +30; CGFloat h = size.height +16; CGFloat x = (screenWidth - w )/2.0; yy = yy - h; self.promptLabel.text = status; CGRect rect = CGRectMake(x , yy, w, h); self.frame = rect; self.promptLabel.frame =CGRectMake(15,8, size.width, size.height); self.alpha =0.0; [UIViewanimateWithDuration:0.3animations:^{ self.alpha =1.0; } completion:^(BOOL finished) { [selfdismiss:seconds]; }]; }); } -(void)dismiss:(NSTimeInterval)seconds{ self.fadeOutTimer = [NSTimerscheduledTimerWithTimeInterval:secondstarget:selfselector:@selector(dismiss)userInfo:nilrepeats:NO]; } -(void)dismiss { dispatch_async(dispatch_get_main_queue(), ^{ [UIViewanimateWithDuration:0.3animations:^{ self.alpha =0.0; } completion:^(BOOL finished) { [selfremoveFromSuperview]; if (_finishBlock) { _finishBlock(); } }]; }); } @end

用法也很簡單,就是提供一個block來處理提示之後的操作,適用於那些提示之後需要做進一步操作的場景!