wag*_*shi 10 uitouch uialertview ios uitapgesturerecognizer
我想在一个点击之外的任何地方解雇UIAlertView.我想在没有任何按钮的情况下显示UIAlertView.
我这里有标准的UIAlertView代码,但如果有可能,我需要输入如何解除它.
有了UITouch吗?使用UITapGestureRecognizer?
谢谢.
编辑:
在viewDidLoad中
alertview初始化,名称为"alert"
if (alert)
{
emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
emptyview.backgroundColor = [UIColor clearColor];
[self.view addSubview:emptyview];
[emptyview addSubview:alert];
NSLog (@"emptyview is there!");
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[emptyview addGestureRecognizer:singleTap];
[singleTap release];
}
Run Code Online (Sandbox Code Playgroud)
但是这个空视图根本没有响应,它没有响应handleSingleTap选择器,我重写了一下:
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
[alert dismissWithClickedButtonIndex:0 animated:YES];
[emptyview removeFromSuperview];
}
Run Code Online (Sandbox Code Playgroud)
我需要这个空视图在显示警报时处于警戒状态然后我可以通过一次点击来解除警报.
我试过了:
if (alert)
{
emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
emptyview.backgroundColor = [UIColor clearColor];
[self.view addSubview:emptyview];
[emptyview addSubview:alert];
NSLog (@"emptyview is there!");
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[alert addGestureRecognizer:singleTap];
[singleTap release];
}
Run Code Online (Sandbox Code Playgroud)
当然,警报确实响应了handleSingleTap功能.我对emptyview有什么不对?
第二次编辑:
在这种情况下我想要实现的是在选择一个单词后显示一个小视图和解释,在Kindle应用程序中有相似的功能,如果你有的话.也许我应该创建一个UIView而不是UIAlertView?但Kindle应用程序中的小视图是如此美观,它下面有阴影,它怎么可能?
NJo*_*nes 10
听起来你本质上是想在iOS上重新创建一个"Toast".好消息,有人已经这样做了.看到这个项目.
编辑:不想使用iToast.我喜欢你的风格,更少的代码.这是我想出来的.这似乎是显而易见的,因为其他人已经说过,克服模态性质的唯一方法UIAlertView是添加一个超视图来处理触摸事件.但是您不必每次都手动执行此操作,请考虑子类化UIAlertView.尝试这样的事情:
编辑: @wagashi,感谢接受我的回答,并感谢有关setFrame:调整大小的好地方.你的代码确实做了一个像吐司一样的小警报,但是当我尝试它时,我发现如果消息很长,那么视图似乎就会崩溃.所以我已经修改setFrame:为简单地通过大约一个按钮的大小减小警报的大小,并保持在屏幕的中心.因此,该课程准确地回答了问题标题"iOS如何在任何地方轻松解雇UIAlertView?"
NoButtonAlertView.h
#import <UIKit/UIKit.h>
@interface _NoButtonAlertViewCover : UIView
@property (nonatomic,assign) UIAlertView *delegate;
@end
@interface NoButtonAlertView : UIAlertView
-(id)initWithTitle:(NSString *)title message:(NSString *)message;
@end
Run Code Online (Sandbox Code Playgroud)
NoButtonAlertView.m
#import "NoButtonAlertView.h"
@implementation _NoButtonAlertViewCover
@synthesize delegate = _delegate;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self removeFromSuperview];
[_delegate dismissWithClickedButtonIndex:0 animated:YES];
}
@end
@implementation NoButtonAlertView
-(void)show{
[super show];
_NoButtonAlertViewCover *cover = [[_NoButtonAlertViewCover alloc] initWithFrame:[UIScreen mainScreen].bounds];
cover.userInteractionEnabled = YES;
cover.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.01];
cover.delegate = self;
[self.superview addSubview:cover];
}
-(id)initWithTitle:(NSString *)title message:(NSString *)message{
if ((self = [super initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil])){
}
return self;
}
- (void)setFrame:(CGRect)rect {
// Called multiple times, 4 of those times count, so to reduce height by 40
rect.size.height -= 10;
self.center = self.superview.center;
[super setFrame:rect];
}
@end
Run Code Online (Sandbox Code Playgroud)
使用这个简单的UIAlertView子类及其UIView子类作为封面,您可以像使用标准一样简单地使用它UIAlertView.像这样:
NoButtonAlertView *alert = [[NoButtonAlertView alloc] initWithTitle:@"Hello" message:@"I am the very model of a modern major general; I'm information, vegitable, animal, and mineral."];
[alert show];
Run Code Online (Sandbox Code Playgroud)
将产量:

| 归档时间: |
|
| 查看次数: |
12626 次 |
| 最近记录: |