zoukankan      html  css  js  c++  java
  • 【转载】iOS 自定义UIActionSheet

    【转自】http://o0o0o0o.iteye.com/blog/1749756

    一:模态视图

    UIActionSheet、UIAlertView、GKPeerPickerController、UIAPopover、GKPanel等都是ios系统自带

    的模态视图。

    模态视图的一个重要的特性就是在显示模态视图的时候可以阻断其他视图的事件响应。

    该特性在有些时候对我们是非常有用的。

    那么任何自己实现一个模态视图呢?

    一种方式就是自己实现一个UIViewController,然后作为一个modalViewController视图显示。这种方式固然可以

    ,但是需要的工作量可就不少了,例如显示动画,动态布局等等。

    这里我要说的另一种方法,就是实现这些系统模态视图的子视图,对系统视图进行定制,以满足我们自己的需求。可以完美的使用系统模态

    视图已经提供的动画、自动布局等等行为。

    二:系统模态视图的显示方式

    系统模态视图的显示和上面提到到的自定义模态视图的显示方式有很大的差别。

    UIActionSheet、UIAlertView、GKPeerPickerController、UIAPopover、GKPanel 都是UIView的子类,

    如果直接在读取视图之上显示这些UIActionSheet等等的视图,是无法阻断事件的。因此系统自带的这些模态

    视图先被添加到一个UIWindow的视图,再把给window视图作为application的keyWindow显示。也就是在显示

    模态视图的时候,application的多window的。

    三:移除系统模态

    调用dismiss系列的方法移除模态视图

    四:模态视图的实现基础

    所有的模态视图都基于UIViewControll提供的两个方法:

    – presentModalViewController:animated:

    – dismissModalViewControllerAnimated:

    UIActionSheet、UIAlertView的实现都是对这两个方法的实现和在实现。

    五:向模态视图添加其他UIView的方式:

    一:动态添加UIView

    (1):通过[UIApplication sharedApplication].keyWindow 取得当前的makeKeyAndVisible window,该window的

    大小是这个屏幕的大小,和该window包含的 UIActionSheet等视图不是一个级别。UIActionSheet 仅仅是该window的

    一个子视图而已。

    [[UIApplication sharedApplication].keyWindow addSubview:self.progressHUD];

    (2):根据读取模态视图的window属性,取得当前的makeKeyAndVisible window

        [self.actionSheet.window addSubview:self.progressHUD]

    注意:以上两种方式一定要在模态视图显示出来之后再添加其他的UIview,否则添加的UIView可能无法显示。

    二:静态添加UIView

    把需要添加的UIView视图直接在模态视图的drawRect方法中添加就可以了。这些在显示模态视图时候,添加的

    UIView也会被显示处理。

    UIActionSheet 视图的自定义

    Cpp代码  收藏代码
    1. //  
    2. //  UserDetailVoiceCustomActionSheet.h  
    3. //  BaiHe  
    4. //  
    5. //  Created by xu on 12-12-15.  
    6. //  Copyright (c) 2012年 itotemstudio. All rights reserved.  
    7. //  
    8.   
    9. #import <UIKit/UIKit.h>  
    10.   
    11. @protocol BHCustomActionSheetDelegate <NSObject>  
    12.   
    13.  @optional  
    14.     - (void)actionSheet:(UIActionSheet *)actionSheet clickedOtherButtonAtIndex:(NSInteger)buttonIndex;  
    15.     - (void)actionSheetCancel:(UIActionSheet *)actionSheet;  
    16.     - (void)actionSheetDestructive:(UIActionSheet *)actionSheet;  
    17.   
    18. @end  
    19.   
    20.   
    21. @interface BHCustomActionSheet : UIActionSheet  
    22. {  
    23.     UIButton                        *_destructiveButton;  
    24.     id<BHCustomActionSheetDelegate> _customActionDelegate;  
    25. }  
    26.   
    27. @property (nonatomic, retain) UIButton *destructiveButton;  
    28. @property (nonatomic, assign) id<BHCustomActionSheetDelegate> customActionDelegate;  
    29.   
    30. @end  
    Cpp代码  收藏代码
    1. //  
    2. //  UserDetailVoiceCustomActionSheet.m  
    3. //  BaiHe  
    4. //  
    5. //  Created by xu on 12-12-15.  
    6. //  Copyright (c) 2012年 itotemstudio. All rights reserved.  
    7. //  
    8.   
    9. #import "BHCustomActionSheet.h"  
    10. #import "UIImageExt.h"  
    11.   
    12.   
    13. @interface BHCustomActionSheet ()  
    14.   
    15. - (void)clickedOtherButton:(id)sender;  
    16. - (void)clickCanncelButton;  
    17. - (void)clickDestructiveButton;  
    18.   
    19. @end  
    20.   
    21. @implementation BHCustomActionSheet  
    22.   
    23. @synthesize destructiveButton    = _destructiveButton;  
    24. @synthesize customActionDelegate = _customActionDelegate;  
    25.   
    26.   
    27. - (void)dealloc  
    28. {  
    29.     _customActionDelegate = nil;  
    30.     RELEASE_SAFELY(_destructiveButton);  
    31.       
    32.     [super dealloc];  
    33. }  
    34.   
    35. //去除背景色  
    36. - (void)drawRect:(CGRect)rect  
    37. {  
    38.     UIImageView *bgView = [[[UIImageView alloc] initWithFrame:self.bounds] autorelease];  
    39.     bgView.image = [[UIImage imageNamed:@"bg"] resizeUsingCapInsets:UIEdgeInsetsMake(180.f, 5.f, 4.f, 4.f)];  
    40.     [self addSubview:bgView];  
    41.       
    42.     UILabel *titleLabel = nil;  
    43.     NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:10];  
    44.     for (UIView *view in self.subviews) {  
    45.         if ([view isKindOfClass:[UIControl class]]) {  
    46.             [self bringSubviewToFront:view];  
    47.             [buttons addObject:view];  
    48.             [view removeFromSuperview];  
    49.         }  
    50.         if ([view isKindOfClass:[UILabel class]]) {  
    51.             titleLabel = (UILabel *)view;  
    52.         }  
    53.     }  
    54.       
    55.       
    56. //    if (titleLabel) {  
    57.         CGRect hilghtBgImageFrame = CGRectMake(0.0f, 0.0f, CGRectGetWidth(rect), CGRectGetMaxY(titleLabel.frame) == 0.f?60.f:CGRectGetMaxY(titleLabel.frame) + 5.f);  
    58.         UIImageView *hilghtBgImageView = [[[UIImageView alloc] initWithFrame:hilghtBgImageFrame] autorelease];  
    59.         [self addSubview:hilghtBgImageView];  
    60.         [self bringSubviewToFront:titleLabel];  
    61.         hilghtBgImageView.image = [[UIImage imageNamed:@"bg-highlight"] resizeUsingCapInsets:UIEdgeInsetsMake(31.f, 5.f, 42.f, 4.f)];  
    62.           
    63. //    }  
    64.       
    65.     NSMutableIndexSet *delSet = [NSMutableIndexSet indexSet];  
    66.       
    67.     if (self.destructiveButtonIndex >= 0) {  
    68.         NSString *destructiveButtonTitle = [self buttonTitleAtIndex:self.destructiveButtonIndex];  
    69.         UIButton *customDestructiveBut = [UIButton buttonWithType:UIButtonTypeCustom];  
    70.         self.destructiveButton = customDestructiveBut;  
    71.           
    72.         [customDestructiveBut setTitleColor:[UIColor whiteColor]  
    73.                                    forState:UIControlStateNormal];  
    74.         [customDestructiveBut setTitleShadowColor:[UIColor whiteColor]  
    75.                                    forState:UIControlStateNormal];  
    76.         customDestructiveBut.titleLabel.font = [UIFont boldSystemFontOfSize:18.f];  
    77.           
    78.         [customDestructiveBut setBackgroundImage:[[UIImage imageNamed:@"but_Destructive"] resizeUsingCapInsets:UIEdgeInsetsMake(15.f, 50., 20.f, 60.f)]  
    79.                                         forState:UIControlStateNormal];  
    80.           
    81.         [customDestructiveBut addTarget:self  
    82.                                  action:@selector(clickDestructiveButton)  
    83.                        forControlEvents:UIControlEventTouchUpInside];  
    84.         customDestructiveBut.frame = ((UIControl *)[buttons objectAtIndex:self.destructiveButtonIndex]).frame;  
    85.         [customDestructiveBut setTitle:destructiveButtonTitle forState:UIControlStateNormal];  
    86.         [self addSubview:customDestructiveBut];  
    87.         [delSet addIndex:self.destructiveButtonIndex];  
    88.     }  
    89.       
    90.     if (self.cancelButtonIndex >= 0) {  
    91.         NSString *cancelButtonTitle = [self buttonTitleAtIndex:self.cancelButtonIndex];  
    92.         UIButton *customCancelBut = [UIButton buttonWithType:UIButtonTypeCustom];  
    93.         [customCancelBut setTitleColor:[UIColor grayColor]  
    94.                               forState:UIControlStateNormal];  
    95.         [customCancelBut setTitleShadowColor:[UIColor grayColor]  
    96.                                     forState:UIControlStateNormal];  
    97.         customCancelBut.titleLabel.font = [UIFont boldSystemFontOfSize:18.f];  
    98.         [customCancelBut setBackgroundImage:[[UIImage imageNamed:@"but_Cancel"] resizeUsingCapInsets:UIEdgeInsetsMake(15.f, 50., 20.f, 60.f)]  
    99.                                    forState:UIControlStateNormal];  
    100.   
    101.           
    102.         [customCancelBut addTarget:self  
    103.                             action:@selector(clickCanncelButton)  
    104.                   forControlEvents:UIControlEventTouchUpInside];  
    105.         customCancelBut.frame = ((UIControl *)[buttons objectAtIndex:self.cancelButtonIndex]).frame;  
    106.         [customCancelBut setTitle:cancelButtonTitle forState:UIControlStateNormal];  
    107.         [self addSubview:customCancelBut];  
    108.         [delSet addIndex:self.cancelButtonIndex];  
    109.     }  
    110.       
    111.     [buttons removeObjectsAtIndexes:delSet];  
    112.       
    113.     int index = 0;  
    114.     for (UIControl *control in buttons) {  
    115.         NSString *otherButtonTitle = [self buttonTitleAtIndex:index];  
    116.         UIButton *customOtherBut = [UIButton buttonWithType:UIButtonTypeCustom];  
    117.         [customOtherBut setTitleColor:[UIColor grayColor]  
    118.                              forState:UIControlStateNormal];  
    119.         [customOtherBut setTitleShadowColor:[UIColor grayColor]  
    120.                                    forState:UIControlStateNormal];  
    121.         customOtherBut.titleLabel.font = [UIFont boldSystemFontOfSize:18.f];  
    122.         [customOtherBut setBackgroundImage:[[UIImage imageNamed:@"but_Cancel"] resizeUsingCapInsets:UIEdgeInsetsMake(15.f, 50., 20.f, 60.f)]  
    123.                                    forState:UIControlStateNormal];  
    124.         customOtherBut.tag = index;  
    125.         [customOtherBut addTarget:self  
    126.                            action:@selector(clickedOtherButton:)  
    127.                  forControlEvents:UIControlEventTouchUpInside];  
    128.         customOtherBut.frame = ((UIControl *)[buttons objectAtIndex:index]).frame;  
    129.         [customOtherBut setTitle:otherButtonTitle forState:UIControlStateNormal];  
    130.         [self addSubview:customOtherBut];  
    131.         index ++;  
    132.     }  
    133.       
    134.     [buttons removeAllObjects];  
    135. }  
    136.   
    137. #pragma mark - Private Method  
    138.   
    139. - (void)clickedOtherButton:(id)sender  
    140. {  
    141.     NSInteger index = ((UIControl *)sender).tag;  
    142.     if (self.customActionDelegate  
    143.         && [self.customActionDelegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {  
    144.         [self.customActionDelegate actionSheet:self clickedOtherButtonAtIndex:index];  
    145.     }  
    146.       
    147.     [self clickCanncelButton];  
    148. }  
    149.   
    150. - (void)clickCanncelButton  
    151. {  
    152.     if (self.customActionDelegate  
    153.         && [self.customActionDelegate respondsToSelector:@selector(actionSheetCancel:)]) {  
    154.         [self.customActionDelegate actionSheetCancel:self];  
    155.     }  
    156.       
    157.     [UIApplication sharedApplication].statusBarHidden = NO;  
    158. }  
    159.   
    160. - (void)clickDestructiveButton  
    161. {  
    162.     if (self.customActionDelegate  
    163.         && [self.customActionDelegate respondsToSelector:@selector(actionSheetDestructive:)]) {  
    164.         [self.customActionDelegate actionSheetDestructive:self];  
    165.     }  
    166. }  
    167.   
    168.   
    169. @end  

    重新drawRect方法,可以去除UIActionSheet的默认样式,然后通过addSubView方法添加定制的视图。

  • 相关阅读:
    September 29th 2017 Week 39th Friday
    September 28th 2017 Week 39th Thursday
    September 27th 2017 Week 39th Wednesday
    September 26th 2017 Week 39th Tuesday
    September 25th 2017 Week 39th Monday
    September 24th 2017 Week 39th Sunday
    angular2 学习笔记 ( Form 表单 )
    angular2 学习笔记 ( Component 组件)
    angular2 学习笔记 ( Http 请求)
    angular2 学习笔记 ( Router 路由 )
  • 原文地址:https://www.cnblogs.com/wilma/p/2841647.html
Copyright © 2011-2022 走看看