zoukankan      html  css  js  c++  java
  • 完美兼容iOS7自定义的AlertView

    在适配iOS7时发现,7以下的自定义的AlertView不起作用,查了下文档,原来从iOS7开始,AlertView不能随便写了,所以就找到个替代AlertView的方法

    #import <UIKit/UIKit.h>
    
    @interface CustomIOS7AlertView : UIView
    
    @property (nonatomic, retain) UIView *parentView;    
    
    @property (nonatomic, retain) UIView *dialogView;    
    
    @property (nonatomic, retain) UIView *containerView; 
    
    @property (nonatomic, retain) UIView *buttonView;    
    
    
    @property (nonatomic, assign) id delegate;
    @property (nonatomic, retain) NSMutableArray *buttonTitles;
    
    - (id)initWithParentView: (UIView *)_parentView;
    - (void)show;
    - (void)close;
    - (void)setButtonTitles: (NSMutableArray *)buttonTitles;
    #import "CustomIOS7AlertView.h"
    
    @implementation CustomIOS7AlertView
    
    @synthesize parentView, containerView, dialogView, buttonView;
    @synthesize delegate;
    @synthesize buttonTitles;
    
    CGFloat static defaultButtonHeight = 50;
    CGFloat static defaultButtonSpacerHeight = 1;
    CGFloat static cornerRadius = 7;
    
    CGFloat buttonHeight = 0;
    CGFloat buttonSpacerHeight = 0;
    
    - (id)initWithParentView: (UIView *)_parentView
    {
        self = [super initWithFrame:_parentView.frame];
        if (self) {
            parentView = _parentView;
            delegate = self;
            buttonTitles = [NSMutableArray arrayWithObject:@"Close"];
        }
        return self;
    }
    
    // Create the dialog view, and animate opening the dialog
    - (void)show
    {
        dialogView = [self createContainerView];
    
        dialogView.layer.opacity = 0.5f;
        dialogView.layer.transform = CATransform3DMakeScale(1.3f, 1.3f, 1.0);
    
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    
        [self addSubview:dialogView];
        [parentView addSubview:self];
    
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4f];
                             dialogView.layer.opacity = 1.0f;
                             dialogView.layer.transform = CATransform3DMakeScale(1, 1, 1);
                         }
                         completion:NULL
         ];
    }
    
    - (void)setDelegate: (id)_delegate
    {
        delegate = _delegate;
    }
    
    // Button has touched
    - (IBAction)customIOS7dialogButtonTouchUpInside:(id)sender
    {
        [delegate customIOS7dialogButtonTouchUpInside:self clickedButtonAtIndex:[sender tag]];
        [self close];
    }
    
    // Default button behaviour
    - (void)customIOS7dialogButtonTouchUpInside: (CustomIOS7AlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        NSLog(@"Button Clicked! %d, %d", buttonIndex, [alertView tag]);
        [self close];
    }
    
    // Dialog close animation then cleaning and removing the view from the parent
    - (void)close
    {
        dialogView.layer.transform = CATransform3DMakeScale(1, 1, 1);
        dialogView.layer.opacity = 1.0f;
    
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f];
                             dialogView.layer.transform = CATransform3DMakeScale(0.6f, 0.6f, 1.0);
                             dialogView.layer.opacity = 0.0f;
                         }
                         completion:^(BOOL finished) {
                             for (UIView *v in [self subviews]) {
                                 [v removeFromSuperview];
                             }
                             [self removeFromSuperview];
                         }
         ];
    }
    
    - (void)setSubView: (UIView *)subView
    {
        containerView = subView;
    }
    
    // Creates the container view here: create the dialog, then add the custom content and buttons
    - (UIView *)createContainerView
    {
        if ([buttonTitles count] > 0) {
            buttonHeight = defaultButtonHeight;
            buttonSpacerHeight = defaultButtonSpacerHeight;
        } else {
            buttonHeight = 0;
            buttonSpacerHeight = 0;
        }
    
        if (containerView == NULL) {
            containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
        }
    
        CGFloat dialogWidth = containerView.frame.size.width;
        CGFloat dialogHeight = containerView.frame.size.height + buttonHeight + buttonSpacerHeight;
    
        CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
        CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    
        UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
        if (UIDeviceOrientationIsLandscape(deviceOrientation)) {
            CGFloat tmp = screenWidth;
            screenWidth = screenHeight;
            screenHeight = tmp;
        }
    
        // For the black background
        [self setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    
        // This is the dialog's container; we attach the custom content and the buttons to this one
        UIView *dialogContainer = [[UIView alloc] initWithFrame:CGRectMake((screenWidth - dialogWidth) / 2, (screenHeight - dialogHeight) / 2, dialogWidth, dialogHeight)];
    
    
        // First, we style the dialog to match the iOS7 UIAlertView >>>
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = dialogContainer.bounds;
        gradient.colors = [NSArray arrayWithObjects:
                           (id)[[UIColor colorWithRed:218.0/255.0 green:218.0/255.0 blue:218.0/255.0 alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:218.0/255.0 green:218.0/255.0 blue:218.0/255.0 alpha:1.0f] CGColor],
                           nil];
        gradient.cornerRadius = cornerRadius;
        [dialogContainer.layer insertSublayer:gradient atIndex:0];
    
        dialogContainer.layer.cornerRadius = cornerRadius;
        dialogContainer.layer.borderColor = [[UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f] CGColor];
        dialogContainer.layer.borderWidth = 1;
        dialogContainer.layer.shadowRadius = cornerRadius + 5;
        dialogContainer.layer.shadowOpacity = 0.1f;
        dialogContainer.layer.shadowOffset = CGSizeMake(0 - (cornerRadius+5)/2, 0 - (cornerRadius+5)/2);
        // ^^^
    
        // There is a line above the button
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight, dialogContainer.bounds.size.width, buttonSpacerHeight)];
        lineView.backgroundColor = [UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f];
        [dialogContainer addSubview:lineView];
        // ^^^
    
        // Add the custom container if there is any
        [dialogContainer addSubview:containerView];
    
        // Add the buttons too
        [self addButtonsToView:dialogContainer];
    
        return dialogContainer;
    }
    
    - (void)addButtonsToView: (UIView *)container
    {
        CGFloat buttonWidth = container.bounds.size.width / [buttonTitles count];
    
        for (int i=0; i<[buttonTitles count]; i++) {
    
            UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
            [closeButton setFrame:CGRectMake(i * buttonWidth, container.bounds.size.height - buttonHeight, buttonWidth, buttonHeight)];
    
            [closeButton addTarget:self action:@selector(customIOS7dialogButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
            [closeButton setTag:i];
    
            [closeButton setTitle:[buttonTitles objectAtIndex:i] forState:UIControlStateNormal];
            [closeButton setTitleColor:[UIColor colorWithRed:0.0f green:0.5f blue:1.0f alpha:1.0f] forState:UIControlStateNormal];
            [closeButton setTitleColor:[UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:0.5f] forState:UIControlStateHighlighted];
            [closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f]];
            [closeButton.layer setCornerRadius:cornerRadius];
    
            [container addSubview:closeButton];
        }
    
    }
    
    @end

    用法:

    CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] initWithParentView:self.view];
    
        // Add some custom content to the alert view
        [alertView setContainerView:[self createDemoView]];
    
        [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"Close1", @"Close2", @"Close3", nil]];
        [alertView setDelegate:self];
    
        // And launch the dialog
        [alertView show];
    - (UIView *)createDemoView
    {
        UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 200)];
    
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];
        [imageView setImage:[UIImage imageNamed:@"demo"]];
        [demoView addSubview:imageView];
    
        return demoView;
    }

    其他:完整的Demo下载

  • 相关阅读:
    T4文本模板转换过程
    ARKit从入门到精通(6)-ARSession介绍
    ARKit从入门到精通(5)-ARScnView介绍
    ARKit从入门到精通(4)-ARKit全框架API大全
    ARKit从入门到精通(3)-ARKit自定义实现
    ARKit从入门到精通(2)-ARKit工作原理及流程介绍
    ARKit从入门到精通(1)-ARKit初体验
    ios系统中各种设置项的url链接
    iOS 检测网络状态 自动判断 认为提示网络改变
    ios 根据scrollview滑动的偏移计算滑动到第几页算法(不同需求不同计算)
  • 原文地址:https://www.cnblogs.com/kw-ios/p/3482477.html
Copyright © 2011-2022 走看看