0

因此,这会显示一个略微透明的图像,右上角的“X”按钮会关闭图像。由于某种原因,它不起作用!关于如何关闭图像的任何想法?

检查按钮,也许我没有正确创建选择器......

#define OVERLAY_TAG 997
-(void)showTutorial
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *overlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    overlay.backgroundColor = [UIColor clearColor];
    overlay.userInteractionEnabled = YES;
    [keyWindow addSubview:overlay];
    UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                     action:@selector(dismissTutorial)];
    CGFloat border = 10;
    CGRect frame = overlay.bounds;

    // 20 is the status bar height (sorry for using the number)
    frame = CGRectMake(border, border + 20, frame.size.width - border * 2, frame.size.height - border * 2 - 20);

    // the black view in the example is probably a scroll view
    UIView *blackView = [[UIView alloc] initWithFrame:frame];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.alpha = 0.7;
    [overlay addSubview:blackView];

    // add all the subviews for your tutorial
    /*UIImage* image = [UIImage imageNamed:@"slide_image_3.png"];
    UIImageView* info = [[UIImageView alloc] initWithImage:image];
    info.frame = CGRectMake(0, 0, 200, 150);
    [blackView addSubview:info];*/

    UIImage* image4 = [UIImage imageNamed:@"close_img.png"];
    dismissTut = [[UIButton alloc] initWithFrame:CGRectMake(250, 18, 26, 26)];
    [dismissTut setBackgroundImage:image4 forState:UIControlStateNormal];
    [dismissTut addTarget:self action:@selector(dismissTutorial)
        forControlEvents:UIControlEventTouchUpInside];
    [dismissTut setShowsTouchWhenHighlighted:YES];

    [blackView addSubview:dismissTut];

    // make it appear with an animation
    [UIView animateWithDuration:0.3
                     animations:^{blackView.alpha = 0.6;}
                     completion:^(BOOL finished){[overlay addGestureRecognizer:tapRecognizer];}];
}

-(void)dismissTutorial
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *overlay = [keyWindow viewWithTag:OVERLAY_TAG];
    [UIView animateWithDuration:0.3
                     animations:^{
                         overlay.alpha = 0.0;
                     }
                     completion:^(BOOL finished){
                         [overlay removeFromSuperview];
                     }];
}
4

1 回答 1

0

在视图中添加标签:overlay.tag = OVERLAY_TAG;

-(void)showTutorial
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *overlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    overlay.tag = OVERLAY_TAG;
.....
}
于 2013-09-16T19:11:15.543 回答