简体   繁体   English

关闭自定义警报视图iOS

[英]Dismiss custom alert view iOS

I'm setting up a subclass of UIView to roll my own UIAlertView style view. 我正在设置UIView的子类来滚动我自己的UIAlertView样式视图。 I've got everything set up properly with displaying the view, but I'm at a bit of a loss as to how to dismiss the view properly. 我已经通过显示视图正确设置了所有内容,但我对如何正确解除视图感到有点失落。 Specifically, when a user taps on the button in the view, it needs to animate out of the main view. 具体地,当用户点击视图中的按钮时,它需要在主视图之外进行动画制作。 This is the code for the view itself: 这是视图本身的代码:

+ (void)showCustomAlertWithTitle:(NSString *)titleString andMessage:(NSString *)messageString inView:(UIView *)view andButton1Title: (NSString *)button1Title andButton2Title: (NSString *)button2Title
{
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];

CGRect windowFrame = window.frame;

[view setAlpha:0.5f];

UIColor *buttonColor = [UIColor colorWithRed:0/255.0f green:130/255.0f blue:216/255.0f alpha:1];
UIColor *titleColor = [UIColor colorWithRed:0/255.0f green:153/255.0f blue:102/255.0f alpha:1];

// Shade
UILabel *shadeWindow = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, windowFrame.size.width, windowFrame.size.height)];
shadeWindow.backgroundColor = [UIColor blackColor];
shadeWindow.alpha = 0.50f;

// Define size and origin of alert box
float alertBoxHeight = 225;
float alertBoxWidth = 200;
float alertBoxXorigin = windowFrame.size.width / 2 - (alertBoxWidth / 2);
float alertBoxYorigin = windowFrame.size.height / 2 - (alertBoxHeight / 2);

// Initialize background
UIView *alertBackground = [[UIView alloc] initWithFrame:CGRectMake(alertBoxXorigin, alertBoxYorigin, alertBoxWidth, alertBoxHeight)];
alertBackground.layer.cornerRadius = 5.0f;
[alertBackground.layer setMasksToBounds:YES];
alertBackground.layer.backgroundColor = [UIColor whiteColor].CGColor;

// Title Label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertBoxWidth, 40)];
titleLabel.text = titleString;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = titleColor;
titleLabel.layer.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.05f].CGColor;
[titleLabel setFont:[UIFont fontWithName:@"AvenirNext-Bold" size:20.0]];

// Title Divider
UILabel *titleDivider = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, 1.0)];
titleDivider.backgroundColor = [UIColor grayColor];
titleDivider.alpha = 0.5f;

// Alert Message Text
UITextView *alertMessage = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, alertBoxHeight - 90)];
alertMessage.text = messageString;
alertMessage.layer.backgroundColor = [UIColor whiteColor].CGColor;
[alertMessage setFont:[UIFont fontWithName:@"Avenir" size:15.0]];
alertMessage.textAlignment = NSTextAlignmentCenter;
alertMessage.textColor = [UIColor grayColor];
[alertMessage setEditable:NO];

// Button 1
UIButton *button1 = [[UIButton alloc] init];
UIButton *button2 = [[UIButton alloc] init];

[button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

if (button2Title == nil)
{
    button1.frame = CGRectMake(10, alertBoxHeight - 50, alertBoxWidth - 20, 40);
}
else
{
    button1.frame = CGRectMake(10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
    button2.frame = CGRectMake(alertBoxWidth / 2 + 10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
}

button1.layer.backgroundColor = buttonColor.CGColor;
[button1 setTitle:button1Title forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont fontWithName:@"AvenirNext-Bold" size:15.0f]];
button1.layer.cornerRadius = 2.5f;
[button1.layer setMasksToBounds:YES];

// Button 2
button2.layer.backgroundColor = buttonColor.CGColor;
[button2 setTitle:button2Title forState:UIControlStateNormal];
[button2.titleLabel setFont:[UIFont fontWithName:@"AvenirNext-Bold" size:15.0f]];
button2.layer.cornerRadius = 2.5f;
[button2.layer setMasksToBounds:YES];

// Bounce Implementation
alertBackground.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
 {
     alertBackground.transform = CGAffineTransformIdentity;
 }
                 completion:^(BOOL finished)
 {
     // do something once the animation finishes, put it here
 }];

[window addSubview:view];
[window addSubview:alertBackground];

[view addSubview:shadeWindow];
[view bringSubviewToFront:alertBackground];

[alertBackground addSubview:button1];
[alertBackground addSubview:titleLabel];
[alertBackground addSubview:titleDivider];
[alertBackground addSubview:alertMessage];
[alertBackground addSubview:button2];
[alertBackground bringSubviewToFront:titleDivider];

 /*   [[[customAlerts sharedInstance] subViewArray] addObject:alertBackground];
[[[customAlerts sharedInstance] subViewArray] addObject:view];
[[[customAlerts sharedInstance] subViewArray] addObject:window];*/
}

When button1 is tapped, for instance, I need to have it animate the view out of the superView and remove it from the stack. 例如,当点击button1 ,我需要让它从superView为视图设置动画并将其从堆栈中移除。 I'm not sure how to handle this. 我不知道如何处理这个问题。 Does anyone have any ideas? 有没有人有任何想法?

To permanently remove a view, I generally use the UIView instance method removeFromSuperview . 要永久删除视图,我通常使用UIView实例方法removeFromSuperview followed by a line setting the relevant variable to nil : 然后是一行将相关变量设置为nil

[myAlertView removeFromSuperview];
myAlertView = nil;  

In your case then, I think you need to move the view off the screen by animating the its bounds property, then use the above couple of lines to remove any references to it. 在你的情况下,我认为你需要通过设置其bounds属性的动画来移动屏幕,然后使用上面的几行来删除对它的任何引用。

Just found the answer here: 刚刚找到答案:

Dismiss view controller from @selector without creating seperate method 从@selector中关闭视图控制器而不创建单独的方法

Had to download some custom classes but it worked: 不得不下载一些自定义类,但它工作:

    [button1 addEventHandler:^(id sender, UIEvent *event)
{
    [UIView animateWithDuration:0.25f animations:^{
        [alertBackground setAlpha:0.0f];
        [shadeWindow setAlpha:0.0f];
        [window setAlpha:0.0f];
    }];
} forControlEvent:UIControlEventTouchUpInside];

Custom classes can be found here: 可在此处找到自定义类:

https://github.com/ZeR0-Wu/JTTargetActionBlock https://github.com/ZeR0-Wu/JTTargetActionBlock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM