Ios 获取UIView以响应快速单次点击的最佳方法是什么?
我有一个视图,我希望用户能够快速切换。我尝试过使用UITapGestureRecognitizer,但它并没有像我所希望的那样很好地拾取水龙头。我还有其他的方法可以尝试吗Ios 获取UIView以响应快速单次点击的最佳方法是什么?,ios,objective-c,uigesturerecognizer,uitapgesturerecognizer,Ios,Objective C,Uigesturerecognizer,Uitapgesturerecognizer,我有一个视图,我希望用户能够快速切换。我尝试过使用UITapGestureRecognitizer,但它并没有像我所希望的那样很好地拾取水龙头。我还有其他的方法可以尝试吗 谢谢 您可以创建自己的UIView派生类,并将其作为用于直接获取“触摸”输入的任何视图的子视图插入。我使用它作为屏幕上的覆盖,这样我就可以触发屏幕变化、动画、一个“我已经看完这个”的输入机制,等等 这种方法对UIView使用touchesEnded覆盖(请参见代码),而不是UIApgestureRecognizer。这是“老派
谢谢 您可以创建自己的UIView派生类,并将其作为用于直接获取“触摸”输入的任何视图的子视图插入。我使用它作为屏幕上的覆盖,这样我就可以触发屏幕变化、动画、一个“我已经看完这个”的输入机制,等等 这种方法对UIView使用touchesEnded覆盖(请参见代码),而不是UIApgestureRecognizer。这是“老派”目标C,不使用ARC 这是头文件“TapView.h” 这段代码用于允许用户在我为iPhone编写的名为Six Things()的应用程序中“重置”一个“Thing Tank”(智囊团) 以下是选择器函数(它不接受任何参数): 下面是实现,TapView.m
#import "TapView.h"
@implementation TapView
@synthesize touchedCallback;
@synthesize touchedCallbackTarget;
@synthesize label;
-(TapView*)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.touchedCallback = nil;
self.userInteractionEnabled = YES;
UILabel *lbl = [[UILabel alloc] init];
lbl.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont boldSystemFontOfSize:16];
lbl.text = nil;
lbl.adjustsFontSizeToFitWidth = YES;
self.label = lbl;
[self addSubview:lbl];
[lbl release];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
}
*/
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(self.touchedCallback != nil && self.touchedCallback != nil)
[self.touchedCallbackTarget performSelector:self.touchedCallback];
}
-(void)respondToTap:(SEL)withCallback andTarget:(id)target
{
self.touchedCallback = withCallback;
self.touchedCallbackTarget = target;
}
- (void)dealloc
{
self.label = nil;
[super dealloc];
}
@end
这有帮助吗?或者你可以选择UIResponder方法(触摸方法),比如触摸开始。。。
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* navView = [ViewUtilities SetNavigationTitle:@"Thing Tank" andSubtitle:@"Tap Here To Empty Tank" forView:self];
CGRect frame = navView.frame;
CGRect tapFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
TapView* tapView = [[[TapView alloc] initWithFrame:tapFrame] autorelease];
[navView addSubview:tapView];
[tapView respondToTap:@selector(tapNavBar) andTarget:self];
self._tapView = tapView;
}
-(void)tapNavBar
{
[[NSNotificationCenter defaultCenter] postNotificationName:[Constants NOTIFICATION_RESTART_BACKGROUND] object:nil];
}
#import "TapView.h"
@implementation TapView
@synthesize touchedCallback;
@synthesize touchedCallbackTarget;
@synthesize label;
-(TapView*)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.touchedCallback = nil;
self.userInteractionEnabled = YES;
UILabel *lbl = [[UILabel alloc] init];
lbl.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont boldSystemFontOfSize:16];
lbl.text = nil;
lbl.adjustsFontSizeToFitWidth = YES;
self.label = lbl;
[self addSubview:lbl];
[lbl release];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
}
*/
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(self.touchedCallback != nil && self.touchedCallback != nil)
[self.touchedCallbackTarget performSelector:self.touchedCallback];
}
-(void)respondToTap:(SEL)withCallback andTarget:(id)target
{
self.touchedCallback = withCallback;
self.touchedCallbackTarget = target;
}
- (void)dealloc
{
self.label = nil;
[super dealloc];
}
@end