在iOS程序设计中,往往需要暂时阻断用户操作界面。比如,用户点击了某个按钮后,需要执行一些操作,在这些操作的执行过程中,我们不想让用户操作界面,以避免发生冲突,所以就需暂时阻断用户操作界面。下面是实现这种功能的方法:
//******************UserInterfaceBlock.h****************************
#import <Foundation/Foundation.h>
@interface UserInterfaceBlock : NSObject{
}
@property (nonatomic, retain) UIColor *blockColor;
@property (nonatomic, retain) UIView *blockView;
+ (id)sharedBlock;
//blcok和unblock方法必须成对的使用,即调用了一次blcok方法,则必需相应的调用unblcok一次。
- (void)block;
- (void)unblock;
//确保在调用了unblock(如果已实用了blcok方法)之后再调用该方法
- (void)resetBlock;
@end
//**************************************************************
//*********************UserInterfaceBlock.m*************************
#import "UserInterfaceBlock.h"
static UIView *block;
@implementation UserInterfaceBlock
@synthesize blockColor;
@synthesize blockView;
+ (id)sharedBlock{
if (block == nil) {
block = [[super alloc] init];
}
return block;
}
- (void)block{
if (blockView == nil) {
self.blockColor = [UIColor colorWithWhite:0.1 alpha:0.5];
blockView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 748, 1024)];
}
blockView.backgroundColor = blockColor;
UIWindow *winBlocked = [[UIApplication sharedApplication] keyWindow];
[winBlocked addSubview:blockView];
blockView.hidden = NO;
}
- (void)unblock{
if (blockView != nil && [blockView superview]) {
blockView.hidden = YES;
}
}
- (void)resetBlock{
self.blockView = nil;
}
- (void)dealloc{
[blockColor release];
[blockView release];
[super dealloc];
}
@end
//**************************************************************