让 UIView 响应快速、快速的单击的最佳方法是啥?

Posted

技术标签:

【中文标题】让 UIView 响应快速、快速的单击的最佳方法是啥?【英文标题】:What is the best way to get a UIView to respond to fast, rapid single taps?让 UIView 响应快速、快速的单击的最佳方法是什么? 【发布时间】:2013-12-21 21:56:03 【问题描述】:

我希望用户能够快速切换。我曾尝试使用 UITapGestureRecognizer,但它并没有像我想要的那样很好地拾取水龙头。还有其他我应该尝试的方法吗?

谢谢!

【问题讨论】:

或者你也可以选择 UIResponder 方法(触摸方法),就像触摸开始... 【参考方案1】:

您可以创建自己的 UIView 派生类,并将其作为您用于直接获取“触摸”输入的任何视图的子视图插入。我将它用作屏幕上的叠加层,以便我可以触发屏幕变化、动画、“我看完了”输入机制等。

此方法使用 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 编写的名为 Six Things (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

这有帮助吗?

【讨论】:

以上是关于让 UIView 响应快速、快速的单击的最佳方法是啥?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用自动布局在情节提要上使网格并排 uiview 响应

如何根据不同设备类型快速地实现网页界面的响应式布局,只需一句代码立马实现。

界面视图。如何快速找到根 SuperView?

在自定义 UIView 类中从 UIView 快速创建 IBoutlet [重复]

UIButton 对快速点击没有响应

如何在快速按下按钮时添加独立的 UIView?