-1

I'm making an application in xcode. I need to show an ad in a UIView for while and hide it after that. For eg. I need to show the UIView for like 15 seconds and hide it for like 30 seconds. What is the best way to do this? Will 2 NSTimers do the job? Please help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
avrospirit
  • 173
  • 4
  • 15

4 Answers4

1
ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self performSelector:@selector(POPUpshow:) withObject:self afterDelay:0.5];
    }

-(void)POPUpshow:(id)sender
{
    PopUpview=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
    [PopUpview setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:PopUpview];
    PopUpview.hidden=NO;

    [self performSelector:@selector(popupHide) withObject:self afterDelay:1];

}

-(void)popupHide{
    PopUpview.hidden=YES;
}

hope it helps

Iphonenew
  • 299
  • 2
  • 11
1

Swift 3 version of accepted answer :

override func viewDidLoad() {
    super.viewDidLoad()
    self.performSelector(#selector(self.POPUpshow), withObject: self, afterDelay: 0.5)
}

func popUpshow(_ sender: Any) {
    PopUpview = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(50), width: CGFloat(320), height: CGFloat(430)))
    PopUpview.backgroundColor = UIColor.yellow
    self.view.addSubview(PopUpview)
    PopUpview.isHidden = false
    self.performSelector(#selector(self.popupHide), withObject: self, afterDelay: 1)
}

func popupHide() {
    PopUpview.isHidden = true
}
Community
  • 1
  • 1
Aung Thu Win
  • 31
  • 1
  • 6
0

This method will be helpful for you.

[NSTimer scheduledTimerWithTimeInterval:15.0f 
target:self selector:@selector(yourMethodToShowHide:) userInfo:nil repeats:YES];
}

YOu can play with options like Repeat or not. Set time interval when to call hide/show method.

Shahab Qureshi
  • 952
  • 1
  • 7
  • 19
0

You can also use UIView animation with delay.

[UIView animateWithDuration:0.2 
                      delay:_cuToast.duration 
                    options:UIViewAnimationOptionCurveLinear 
                 animations:^{

                     [_cuToast.view setAlpha:0];

                 } completion:^(BOOL finished) {

                     [_cuToast.view removeFromSuperview];

 }];

I have implemented an android's toast like message with UIView animations.

Ryan
  • 4,799
  • 1
  • 29
  • 56