如何在不同的时间和延迟逐个显示警报

时间:2017-05-15 11:04:31

标签: objective-c xcode multithreading grand-central-dispatch objective-c-blocks

我必须显示具有不同时间延迟的不同类型的警报,逐个显示警报。一个警报显示并停留一段时间然后下一个警报显示延迟....一个一个显示这是我的代码

for ( int j = 0; j  < [[CMAppData appData].arrayDistanceAlert count]; j++)
{

        CMBeaconsDisplayList * displayData = [[CMAppData appData].arrayDistanceAlert objectAtIndex:j];


        if ([displayData.type isEqualToString:@"1"])
        {
            if ([displayData.type isEqualToString:@"1"] &&[displayData.displayForTime isEqualToString:@"0"]) {


                [self performSelector:@selector(dismissAlertView:) withObject:displayData afterDelay:[displayData.displayForTime intValue]*60];
            }

            else{

                double delayInSeconds =[displayData.displayForTime intValue] *10 ;

                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

                    NSLog(@"%f",delayInSeconds);

                    UIAlertView * alert =[[UIAlertView alloc] initWithTitle:displayData.title message:displayData.content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"pause", nil];
                    alert.alertViewStyle =UIAlertViewStyleDefault;
                    [alert show];
                    [self performSelector:@selector(dismissUIAlertViewType:) withObject:alert afterDelay:15];

                });

                               }

        }

        else {

            if ([displayData.type isEqualToString:@"2"]&&[displayData.displayForTime isEqualToString:@"0"]) {

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayData.title message:displayData.content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
                [alertView show];
            }
            else{
                double delayInSeconds =[displayData.displayForTime intValue]*10;

                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

                    CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
                    UIView *customView  = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width-20, [UIScreen mainScreen].bounds.size.height-100)];
                    customView.backgroundColor = [UIColor blueColor];
                    [alertView setContainerView:customView];

                    UIScrollView  *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,[UIScreen mainScreen].bounds.size.width-20 ,[UIScreen mainScreen].bounds.size.height-100)];
                    MyScrollVw.delegate= self;
                    [MyScrollVw setShowsHorizontalScrollIndicator:NO];
                    [MyScrollVw setShowsVerticalScrollIndicator:YES];
                    MyScrollVw.scrollEnabled= YES;
                    MyScrollVw.userInteractionEnabled= YES;
                    MyScrollVw.contentSize= CGSizeMake([UIScreen mainScreen].bounds.size.width-20 ,1150);//(width,height)
                    [customView addSubview:MyScrollVw];


                    UILabel * lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, customView.frame.size.width, 30.0)];
                    lblTitle.text = displayData.title;
                    lblTitle.font = [UIFont fontWithName:@"Arial" size:15.0];
                    lblTitle.numberOfLines = 1;
                    lblTitle.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
                    lblTitle.adjustsFontSizeToFitWidth = YES;
                    lblTitle.minimumScaleFactor = 10.0f/12.0f;
                    lblTitle.clipsToBounds = YES;
                    lblTitle.backgroundColor = [UIColor clearColor];
                    lblTitle.textColor = [UIColor whiteColor];
                    lblTitle.textAlignment = NSTextAlignmentCenter;
                    [MyScrollVw addSubview:lblTitle];

                    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, lblTitle.frame.origin.y+lblTitle.frame.size.height, customView.frame.size.width, 250)];
                    displayData.image = [displayData.image stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
                    if(displayData.image && ![displayData.image isKindOfClass:[NSNull class]])
                    {
                    [imageView sd_setImageWithURL:[NSURL URLWithString:displayData.image] placeholderImage:nil options:/* DISABLES CODE */ (0) == 0 ? SDWebImageRefreshCached : 0];
                    }
                    if ([displayData.image length]==0)
                    {
                        [imageView setImage:[UIImage imageNamed:@""]];
                    }
                    [MyScrollVw addSubview:imageView];


                    NSString *videoString = displayData.video;
                    NSString *lastWord = [[videoString componentsSeparatedByString:@"="] lastObject];
                    self.playerView = [[YTPlayerView alloc]init];
                    self.playerView.frame = CGRectMake(0.0, imageView.frame.origin.y+imageView.frame.size.height, customView.frame.size.width, 250.0);
                    [self.playerView loadWithVideoId:lastWord playerVars:@{@"playsinline":@1}];
                    [MyScrollVw addSubview:self.playerView];



                    UIButton * btnPlayAudio = [[UIButton alloc] initWithFrame:CGRectMake(10.0,self.playerView .frame.origin.y+self.playerView .frame.size.height, customView.frame.size.width-20, 30)];

                    [ btnPlayAudio addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
                    btnPlayAudio.backgroundColor = [UIColor redColor];
                    [btnPlayAudio setTitle:@"Play Audio" forState:UIControlStateNormal];
                    [btnPlayAudio setAccessibilityValue:displayData.audio];

                    [MyScrollVw addSubview:btnPlayAudio];

                    UILabel * lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(10.0,btnPlayAudio .frame.origin.y + btnPlayAudio .frame.size.height, customView.frame.size.width-20, 100)];
                    lblDescription.text = displayData.content;
                    lblDescription.font = [UIFont fontWithName:@"Arial" size:15.0];
                    lblDescription.numberOfLines = 0;
                    [lblDescription sizeToFit];
                    lblDescription.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
                    lblDescription.adjustsFontSizeToFitWidth = YES;
                    lblDescription.minimumScaleFactor = 10.0f/12.0f;
                    lblDescription.clipsToBounds = YES;
                    lblDescription.backgroundColor = [UIColor clearColor];
                    lblDescription.textColor = [UIColor whiteColor];
                    lblDescription.textAlignment = NSTextAlignmentCenter;
                    [MyScrollVw addSubview:lblDescription];
                    [alertView show];

                    [self performSelector:@selector(dismissImageAlertView:) withObject:alertView afterDelay:15];

                });

                delayInSeconds = delayInSeconds * 10;

            }

        }


    }

1 个答案:

答案 0 :(得分:-1)

setTimeout:允许在一段时间后运行一次函数。

completionHandler