我有一个观点,希望用户能够快速切换。我尝试过使用 UITapGestureRecognizer,但它并没有像我想要的那样很好地拾取水龙头。我还应该尝试其他方法吗?
谢谢!
最佳答案
您可以创建自己的 UIView 派生类,并将其作为您正在使用的任何 View 的 subview 插入,以直接获取“触摸”输入。我将它用作屏幕上的覆盖层,以便我可以触发屏幕更改、动画、“我已经看完了”输入机制等。
此方法使用UIView 的touchesEnded 覆盖(参见代码),而不是UITapGestureRecognizer。这是“老派”objective-C,不使用 ARC。
这是头文件“TapView.h”
#import <UIKit/UIKit.h>
@interface TapView : UIView
{
SEL touchedCallback;
id touchedCallbackTarget;
UILabel* label;
}
@property(nonatomic,assign) SEL touchedCallback;
@property(nonatomic,retain) id touchedCallbackTarget;
@property(nonatomic,retain) UILabel* label;
-(void)respondToTap:(SEL)withCallback andTarget:(id)target;
@end
您创建该类并调用 [tapView respondToTap....] 以使用您的目标对其进行初始化。像这样的东西(在 viewDidLoad 中):
- (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;
}
此代码用于允许用户在我为 iPhone 编写的名为“六件事”( you can find it here ) 的应用程序中“重置”“Thing Tank”(想想鱼缸)。
这是选择器函数(它不带参数):
-(void)tapNavBar
{
[[NSNotificationCenter defaultCenter] postNotificationName:[Constants NOTIFICATION_RESTART_BACKGROUND] object:nil];
}
这是实现,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
这有帮助吗?
关于ios - 让 UIView 响应快速、快速的单击的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723957/