我正在尝试制作一个幻灯片通知栏,以便当应用收到通知并且应用当前处于打开状态且位于前台时。我使用了自定义UIView
(实际上它是UIButton
)和动画。出于某种原因,虽然我点击它时没有注册新闻。它不会调用选择器方法。代码如下:
-(void)appOpenPush {
//get string
NSLog(@"got a push while app was open");
AppDelegate *appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *pushString = appDelegate.pushString;
//play sound
AudioServicesPlaySystemSound(MessageSound);
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
//note I am actually using a button here
UIButton *pushView =[[UIButton alloc] initWithFrame:CGRectMake(0, -75, [[UIScreen mainScreen] bounds].size.width,75)];
pushView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
[pushView addTarget:self action:@selector(pushSlidePress) forControlEvents:UIControlEventTouchUpInside];
//set image
UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon120.png"]];
logo.frame=CGRectMake(5, 20, 35, 35);
logo.layer.cornerRadius=8;
logo.clipsToBounds=YES;
[pushView addSubview:logo];
UILabel *pushLabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 34, 0, 0)];
pushLabel.text=pushString;
pushLabel.font=[UIFont systemFontOfSize:13];
[pushLabel sizeToFit];
pushLabel.textColor=[UIColor whiteColor];
[pushView addSubview:pushLabel];
//animate
UIWindow* currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:pushView];
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
pushView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width,75);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.4 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
pushView.frame = CGRectMake(0, -75, [[UIScreen mainScreen] bounds].size.width,75);
} completion:^(BOOL finished) {
[pushView removeFromSuperview];
}];
}];
}
有人能给我一个指针,说明为什么这不起作用?是因为我要将UIButton
添加到主窗口吗?任何提示都会非常有用。谢谢!
答案 0 :(得分:0)
尝试此操作以避免徽标和pushLabel接收触摸事件:
logo.userInteractionEnabled = NO;
pushLabel.userInteractionEnabled = NO;
答案 1 :(得分:0)
答案 2 :(得分:0)
事实证明,如果按钮移动,你不能简单地检测按下。我测试了这个只是添加框架设置而没有它动画并且它注册了按下,当动画打开时它没有。似乎UIButton
没有内置跟踪。我确实想出了一个快速解决方法。我刚制作了一个与整个动画区域相同大小的按钮(即滑出栏的大小),并将其添加到动画帧顶部的currentWindow。所以它只是动画顶部的一个清晰的隐形按钮,但它本身并没有移动。即:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,[[UIScreen mainScreen]bounds].size.width , 75)];
[button addTarget:self action:@selector(pushSlidePress) forControlEvents:UIControlEventTouchUpInside];
[currentWindow addSubview:button];
[UIView animateWithDuration:0.4 delay:0.0 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
pushView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width,75);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.4 delay:2.0 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
pushView.frame = CGRectMake(0, -75, [[UIScreen mainScreen] bounds].size.width,75);
} completion:^(BOOL finished) {
[pushView removeFromSuperview];
[button removeFromSuperview];
}];
}];
显然,您正在覆盖屏幕上可能阻挡其他内容的区域,但这取决于您的设置。动画很快,我只有一个按钮,通知栏会阻止,而且它太快注意到了。希望这对其他试图达到类似功能的人有所帮助。谢谢你的帮助。