?
一:模態(tài)視圖
UIActionSheet、UIAlertView、GKPeerPickerController、UIAPopover、GKPanel等都是ios系統(tǒng)自帶
的模態(tài)視圖。
模態(tài)視圖的一個(gè)重要的特性就是在顯示模態(tài)視圖的時(shí)候可以阻斷其他視圖的事件響應(yīng)。
該特性在有些時(shí)候?qū)ξ覀兪欠浅S杏玫摹?
?
那么任何自己實(shí)現(xiàn)一個(gè)模態(tài)視圖呢?
?
一種方式就是自己實(shí)現(xiàn)一個(gè)UIViewController,然后作為一個(gè)modalViewController視圖顯示。這種方式固然可以
,但是需要的工作量可就不少了,例如顯示動(dòng)畫,動(dòng)態(tài)布局等等。
這里我要說的另一種方法,就是實(shí)現(xiàn)這些系統(tǒng)模態(tài)視圖的子視圖,對(duì)系統(tǒng)視圖進(jìn)行定制,以滿足我們自己的需求。可以完美的使用系統(tǒng)模態(tài)
視圖已經(jīng)提供的動(dòng)畫、自動(dòng)布局等等行為。
?
二:系統(tǒng)模態(tài)視圖的顯示方式
系統(tǒng)模態(tài)視圖的顯示和上面提到到的自定義模態(tài)視圖的顯示方式有很大的差別。
?
UIActionSheet、UIAlertView、GKPeerPickerController、UIAPopover、GKPanel 都是UIView的子類,
如果直接在讀取視圖之上顯示這些UIActionSheet等等的視圖,是無法阻斷事件的。因此系統(tǒng)自帶的這些模態(tài)
視圖先被添加到一個(gè)UIWindow的視圖,再把給window視圖作為application的keyWindow顯示。也就是在顯示
模態(tài)視圖的時(shí)候,application的多window的。
?
三:移除系統(tǒng)模態(tài)
調(diào)用dismiss系列的方法移除模態(tài)視圖
?
四:模態(tài)視圖的實(shí)現(xiàn)基礎(chǔ)
所有的模態(tài)視圖都基于UIViewControll提供的兩個(gè)方法:
?
– presentModalViewController:animated:
– dismissModalViewControllerAnimated:
?
UIActionSheet、UIAlertView的實(shí)現(xiàn)都是對(duì)這兩個(gè)方法的實(shí)現(xiàn)和在實(shí)現(xiàn)。
?
五:向模態(tài)視圖添加其他UIView的方式:
?
一:動(dòng)態(tài)添加UIView
(1):通過[UIApplication sharedApplication].keyWindow 取得當(dāng)前的makeKeyAndVisible window,該window的
大小是這個(gè)屏幕的大小,和該window包含的 UIActionSheet等視圖不是一個(gè)級(jí)別。UIActionSheet 僅僅是該window的
一個(gè)子視圖而已。
[[UIApplication sharedApplication].keyWindow addSubview:self.progressHUD];
?
(2):根據(jù)讀取模態(tài)視圖的window屬性,取得當(dāng)前的makeKeyAndVisible window
?
? ? [self.actionSheet.window addSubview:self.progressHUD]
?
注意:以上兩種方式一定要在模態(tài)視圖顯示出來之后再添加其他的UIview,否則添加的UIView可能無法顯示。
?
二:靜態(tài)添加UIView
把需要添加的UIView視圖直接在模態(tài)視圖的drawRect方法中添加就可以了。這些在顯示模態(tài)視圖時(shí)候,添加的
UIView也會(huì)被顯示處理。
?
?
UIActionSheet 視圖的自定義
?
?
//
// UserDetailVoiceCustomActionSheet.h
// BaiHe
//
// Created by xu on 12-12-15.
// Copyright (c) 2012年 itotemstudio. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol BHCustomActionSheetDelegate <NSObject>
@optional
- (void)actionSheet:(UIActionSheet *)actionSheet clickedOtherButtonAtIndex:(NSInteger)buttonIndex;
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;
- (void)actionSheetDestructive:(UIActionSheet *)actionSheet;
@end
@interface BHCustomActionSheet : UIActionSheet
{
UIButton *_destructiveButton;
id<BHCustomActionSheetDelegate> _customActionDelegate;
}
@property (nonatomic, retain) UIButton *destructiveButton;
@property (nonatomic, assign) id<BHCustomActionSheetDelegate> customActionDelegate;
@end
?
?
?
//
// UserDetailVoiceCustomActionSheet.m
// BaiHe
//
// Created by xu on 12-12-15.
// Copyright (c) 2012年 itotemstudio. All rights reserved.
//
#import "BHCustomActionSheet.h"
#import "UIImageExt.h"
@interface BHCustomActionSheet ()
- (void)clickedOtherButton:(id)sender;
- (void)clickCanncelButton;
- (void)clickDestructiveButton;
@end
@implementation BHCustomActionSheet
@synthesize destructiveButton = _destructiveButton;
@synthesize customActionDelegate = _customActionDelegate;
- (void)dealloc
{
_customActionDelegate = nil;
RELEASE_SAFELY(_destructiveButton);
[super dealloc];
}
//去除背景色
- (void)drawRect:(CGRect)rect
{
UIImageView *bgView = [[[UIImageView alloc] initWithFrame:self.bounds] autorelease];
bgView.image = [[UIImage imageNamed:@"bg"] resizeUsingCapInsets:UIEdgeInsetsMake(180.f, 5.f, 4.f, 4.f)];
[self addSubview:bgView];
UILabel *titleLabel = nil;
NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:10];
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[UIControl class]]) {
[self bringSubviewToFront:view];
[buttons addObject:view];
[view removeFromSuperview];
}
if ([view isKindOfClass:[UILabel class]]) {
titleLabel = (UILabel *)view;
}
}
// if (titleLabel) {
CGRect hilghtBgImageFrame = CGRectMake(0.0f, 0.0f, CGRectGetWidth(rect), CGRectGetMaxY(titleLabel.frame) == 0.f?60.f:CGRectGetMaxY(titleLabel.frame) + 5.f);
UIImageView *hilghtBgImageView = [[[UIImageView alloc] initWithFrame:hilghtBgImageFrame] autorelease];
[self addSubview:hilghtBgImageView];
[self bringSubviewToFront:titleLabel];
hilghtBgImageView.image = [[UIImage imageNamed:@"bg-highlight"] resizeUsingCapInsets:UIEdgeInsetsMake(31.f, 5.f, 42.f, 4.f)];
// }
NSMutableIndexSet *delSet = [NSMutableIndexSet indexSet];
if (self.destructiveButtonIndex >= 0) {
NSString *destructiveButtonTitle = [self buttonTitleAtIndex:self.destructiveButtonIndex];
UIButton *customDestructiveBut = [UIButton buttonWithType:UIButtonTypeCustom];
self.destructiveButton = customDestructiveBut;
[customDestructiveBut setTitleColor:[UIColor whiteColor]
forState:UIControlStateNormal];
[customDestructiveBut setTitleShadowColor:[UIColor whiteColor]
forState:UIControlStateNormal];
customDestructiveBut.titleLabel.font = [UIFont boldSystemFontOfSize:18.f];
[customDestructiveBut setBackgroundImage:[[UIImage imageNamed:@"but_Destructive"] resizeUsingCapInsets:UIEdgeInsetsMake(15.f, 50., 20.f, 60.f)]
forState:UIControlStateNormal];
[customDestructiveBut addTarget:self
action:@selector(clickDestructiveButton)
forControlEvents:UIControlEventTouchUpInside];
customDestructiveBut.frame = ((UIControl *)[buttons objectAtIndex:self.destructiveButtonIndex]).frame;
[customDestructiveBut setTitle:destructiveButtonTitle forState:UIControlStateNormal];
[self addSubview:customDestructiveBut];
[delSet addIndex:self.destructiveButtonIndex];
}
if (self.cancelButtonIndex >= 0) {
NSString *cancelButtonTitle = [self buttonTitleAtIndex:self.cancelButtonIndex];
UIButton *customCancelBut = [UIButton buttonWithType:UIButtonTypeCustom];
[customCancelBut setTitleColor:[UIColor grayColor]
forState:UIControlStateNormal];
[customCancelBut setTitleShadowColor:[UIColor grayColor]
forState:UIControlStateNormal];
customCancelBut.titleLabel.font = [UIFont boldSystemFontOfSize:18.f];
[customCancelBut setBackgroundImage:[[UIImage imageNamed:@"but_Cancel"] resizeUsingCapInsets:UIEdgeInsetsMake(15.f, 50., 20.f, 60.f)]
forState:UIControlStateNormal];
[customCancelBut addTarget:self
action:@selector(clickCanncelButton)
forControlEvents:UIControlEventTouchUpInside];
customCancelBut.frame = ((UIControl *)[buttons objectAtIndex:self.cancelButtonIndex]).frame;
[customCancelBut setTitle:cancelButtonTitle forState:UIControlStateNormal];
[self addSubview:customCancelBut];
[delSet addIndex:self.cancelButtonIndex];
}
[buttons removeObjectsAtIndexes:delSet];
int index = 0;
for (UIControl *control in buttons) {
NSString *otherButtonTitle = [self buttonTitleAtIndex:index];
UIButton *customOtherBut = [UIButton buttonWithType:UIButtonTypeCustom];
[customOtherBut setTitleColor:[UIColor grayColor]
forState:UIControlStateNormal];
[customOtherBut setTitleShadowColor:[UIColor grayColor]
forState:UIControlStateNormal];
customOtherBut.titleLabel.font = [UIFont boldSystemFontOfSize:18.f];
[customOtherBut setBackgroundImage:[[UIImage imageNamed:@"but_Cancel"] resizeUsingCapInsets:UIEdgeInsetsMake(15.f, 50., 20.f, 60.f)]
forState:UIControlStateNormal];
customOtherBut.tag = index;
[customOtherBut addTarget:self
action:@selector(clickedOtherButton:)
forControlEvents:UIControlEventTouchUpInside];
customOtherBut.frame = ((UIControl *)[buttons objectAtIndex:index]).frame;
[customOtherBut setTitle:otherButtonTitle forState:UIControlStateNormal];
[self addSubview:customOtherBut];
index ++;
}
[buttons removeAllObjects];
}
#pragma mark - Private Method
- (void)clickedOtherButton:(id)sender
{
NSInteger index = ((UIControl *)sender).tag;
if (self.customActionDelegate
&& [self.customActionDelegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
[self.customActionDelegate actionSheet:self clickedOtherButtonAtIndex:index];
}
[self clickCanncelButton];
}
- (void)clickCanncelButton
{
if (self.customActionDelegate
&& [self.customActionDelegate respondsToSelector:@selector(actionSheetCancel:)]) {
[self.customActionDelegate actionSheetCancel:self];
}
[UIApplication sharedApplication].statusBarHidden = NO;
}
- (void)clickDestructiveButton
{
if (self.customActionDelegate
&& [self.customActionDelegate respondsToSelector:@selector(actionSheetDestructive:)]) {
[self.customActionDelegate actionSheetDestructive:self];
}
}
@end
?
?
重新drawRect方法,可以去除UIActionSheet的默認(rèn)樣式,然后通過addSubView方法添加定制的視圖。
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

