我正在xcode中创建应用程序。我需要在UIView中显示广告,然后隐藏它。例如。我需要将UIView显示为15秒并将其隐藏30秒。做这个的最好方式是什么? 2个NSTimers会做这个工作吗?请帮忙。
答案 0 :(得分: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;
}
希望有所帮助
答案 1 :(得分:1)
accepted answer的Swift 3版本:
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
}
答案 2 :(得分:0)
此方法对您有所帮助。
[NSTimer scheduledTimerWithTimeInterval:15.0f
target:self selector:@selector(yourMethodToShowHide:) userInfo:nil repeats:YES];
}
你可以玩重复或不重复的选项。 设置调用hide / show方法的时间间隔。
答案 3 :(得分:0)
您也可以延迟使用UIView动画。
[UIView animateWithDuration:0.2
delay:_cuToast.duration
options:UIViewAnimationOptionCurveLinear
animations:^{
[_cuToast.view setAlpha:0];
} completion:^(BOOL finished) {
[_cuToast.view removeFromSuperview];
}];
我已经使用UIView动画实现了一个像Android一样的toast to up message。